geekvenue.net

Welcome to Chucktips HELP WITH DEAD OS Hardware 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, @11:37AM
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

Add Reply

Name
Email
Notify Notify me via email of responses to this message
Title
Comment
(Check those URLs! Don't forget the http://!)
Encoding
If none of the above mean anything to you, select 'Plain'!
Attachment
(You can attach a file to your reply which can then be retrieved by other readers.
Try to keep the file sizes below 500Kb in order to conserve network and server resources.)
Allowed HTML <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <HR> <STRONG> <BLOCKQUOTE> <DIV .*> <DIV> <P .*>
Important Stuff:
  • Note: Fields with bold titles are required.
  • Please try to keep posts on topic.
  • Try to reply to other people comments instead of starting new threads,
  • Read other people's messages before posting your own to avoid simply duplicating what has already been said.
  • Use a clear subject that describes what your message is about.
  • Please do not post offtopic, inflammatory, inappropriate, illegal, or offensive comments. Repeat offenders will be sanctioned.
  • "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 ]