geekvenue.net

Welcome to Chucktips Hardware HELP WITH DEAD OS Installing FreeBSD Miscellaneous
 faq
 search
 add article
 cool-stuff
 how-to
 main
 tips


HOWTO: Backup files with tar
Backup and Restore Posted by Jason on Sunday July 01, @12:37PM
from the It's-Sticky-Fun-Fun-Fun dept.
TAR is the Unix Tape ARchive utility. It can be used to either store data on a streaming tape device like a DAT drive, or store files in what is commonly called a tarball file- somewhat like a pkzip file, only compression is optional.


[The basics]

In these examples, I will use the following file structure: a top level directory called DIR1 containing the files picture.jpg, document.doc and database.db.

  DIR1/

  DIR1/picture.jpg
  DIR1/document.doc
  DIR1/database.db


[Creating a tarball]

If we were in the directory DIR1 and wanted to backup all the files to a tarball called backup.tar, we could issue this command:

  $ tar cvf backup.tar .

  ./
  picture.jpg
  doucment.doc
  database.db
  tar: backup.tar is the archive; not dumped


Note:
c=create (an archive)
v=verbose (just because)
f=filename (the name of our tarball)
.=current directory (what's going to be backed up)

Also worth mentioning is that by default tar is recursive- meaning it will back up all files and subdirectories recursively unless you otherwise specify with the n flag (non-recursive)


[Displaying the Contents of a Tarball]

The current directory will now contain a file called backup.tar. To display the contents of the tarball file, we could issue this command:

  $ tar tvf backup.tar

  drwxr-xr-x root/gci 0 Jun 29 10:10 ./
  -rw-r--r-- root/gci 1 Jun 29 10:10 picture.jpg
  -rw-r--r-- root/gci 1 Jun 29 10:10 document.doc
  -rw-r--r-- root/gci 1 Jun 29 10:10 databse.db


Note:
t=table of contents (list)
v=verbose (display all info)
f=filename (backup.tar)


[Extracting Data from a Tarball]

To extract the entire contents of the tarball to the current directory, we can type:

  $ tar xvf backup.tar

  ./
  picture.jpg
  doucment.doc
  database.db


Note:
x=extract
v=verbose
f=filename (backup.tar)

To extract only the picture.jpg file from the archive, type the following command:

  $ tar xvf backup.tar picture.jpg

Alternatively, you can use wild cards in either the creation or extraction of a tarball. To extract all jpg files from our archive, we can use a command like this:

  $ tar xvf backup.tar *.jpg


[Using Compression]

If you would also like to add compression to your tarballs, you can combine the gzip utility with tar on the command line by adding the z switch to the command. Usually when this is done, we change the suffix of our tarball filename from .tar to either .tgz or .tar.gz. This will let whoever sees the file know that it is a gzipped tarball.

  $ tar zcvf tarball.tgz .

Note:
z=gzip compression
c=create
v=verbose
f=filename (backup.tgz)
.=current directory (what to backup)


[Permissions with tar]

If you would like to preserve the permissions of the files you backup, use the p option with the tar command. This will save the uid, gid as well as the specific permission attributes of the files (read, write, execute etc.)

  $ tar pzcvf tarball.tgz .

You should also use the p option with the tar extraction command:

  $ tar pxvf tarball.tgz .


[Using tar with a Tape Drive]

I use tar in conjunction with my Seagate DAT drive. If you issue any of the previous commands in this HOW-TO without the f option and a tarball filename, you will find that tar will default to writing the files to the device /dev/rsa0 (raw sequential access device 0). Basically this is your first SCSI tape drive. If you have more than one tape drive you would like to use, you can issue the f option and the device name (rsa0, rsa1 etc.) of your specific tape drive instead of a filename.

Examples:
Backing up the current directory to Tape:

  $ tar cv . <- default tape drive (rsa0)

  $ tar cvf /dev/rsa1 . <- second tape drive (rsa1)

Restore files from Tape:

  $ tar xv .
  $ tar xvf /dev/rsa1

So, there are the basics of tar. If you would like more information on the tar command, try the man pages.

man tar


<  |  >

 

Related Links
  • Articles on Backup and Restore
  • Also by Jason
  • Contact author

    Lilliput Mini USB Computer Monitor

    If you have ever wanted to learn about Cisco Routers, check out my new book "Cisco Routers for the Small Business" - it's Cisco CLI for the Regular Guy!

  • Sponsors

    The Fine Print: The following comments are owned by whoever posted them.
    ( Add a Reply )

    Over 10 comments listed. Printing out index only.
    Re: HOWTO: Backup files with tar
    by Maciej Król on Wednesday June 26, @03:30PM
    Do you know if there are any restrcitions about size of single Tarball on FreeBSD filesystem (UFS?) ? I have to backup (to other HDD) data which will take after compression about 4 Gb,so there will be one big file,I experienced some problems about file size on Linux with kernel 2.2.x.,how is it with FBSD ?

    --
    Greetings

    MK
    [ Add a Reply to this ]
    Re: HOWTO: Backup files with tar
    by Maciej Król on Wednesday June 26, @03:31PM
    Do you know if there are any restrcitions about size of single Tarball on FreeBSD filesystem (UFS?) ? I have to backup (to other HDD) data which will take after compression about 4 Gb,so there will be one big file,I experienced some problems about file size on Linux with kernel 2.2.x.,how is it with FBSD ?

    --
    Greetings

    MK
    [ Add a Reply to this ]
    HOWTO: Backup files with tar
    by Chua Sk on Sunday October 27, @05:29PM
    How to use "tar" command to backup an entire system from its root directory and subsequently retrieve it into /temp directory.
    [ Add a Reply to this ]
    Re: HOWTO: Backup files with tar
    by meister_sd on Monday November 18, @04:00AM
    Very nice!!! Thanks for the help. I'm new to Linux and this is a very well written piece on the basics of this command.
    [ Add a Reply to this ]
    Re: HOWTO: Backup files with tar
    by Squeeb on Monday June 09, @07:33PM
    I have a DDS-1 Tape drive.. I can only shove 2 gig of stuff on to the tapes.. The tapes say 4GB *compressed* HOw do I turn on compression? .. does it mean tar --compress ? i have a HP SureStore 2000 or 5000 .. im not sure which ..
    [ Add a Reply to this ]
    Re: HOWTO: Backup files with tar
    by Shawn on Wednesday July 16, @12:15PM
    Is it possible to exclude directories while 'taring' I would like to tar my entire /home directory with many user directories and exclude one of those directories. I have been using tar and ssh together to back up a 40G /home partition onto a separate remote machine with a removable harddrive of 20G. Yesterday the gzipped tarball finally made it over the 20G mark. ther is one directory /home/unwanted_dir that is 8G that I don't need to back up. Shawn
    [ Add a Reply to this ]
    Tar problems with jpgs
    by Tom Haddon on Thursday October 23, @06:38AM

    Hi,

    Does anyone know how to help me out with a rather bizarre problem? I have a cron job that's tar-ing and gzipping a directory (for a website), and it runs fine if I run it from the command line as root, but if I run it from cron (as root - in the /etc/cron.daily/ folder), it consistently fails on certain jpgs. The error message when unzipping and untar-ing the file to check for errors is "unexpected end of file" or something similar.

    Here's the command:

    tar -zcvpf /var/backup/site.tgz --directory /var/www/html .

    Any help appreciated!!

    Thanks, Tom


    [ Add a Reply to this ]
    Re: HOWTO: Backup files with tar
    by Fredrik on Monday December 15, @08:18AM
    Thanks. Been into Linux quite some time now, but haven't really thought of learning 'tar' the real way. It's a good utility. :-)
    [ Add a Reply to this ]

     
    The Fine Print: The following comments are owned by whoever posted them.
    ( Article Reply )

    "You never know how many friends you have until you own a Condo on the beach." -- Jason's Postulate

    Powered by Zope  Powered by Apache  Squishdot Powered
    All trademarks and copyrights on this page are owned by their respective companies. Comments are owned by the Poster. The Rest ©2001 Jason Neumann.
    [ main | post article | search ]