Backup files with tar

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 anddatabase.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

http://www.geekvenue.net/chucktips/jason/chuck/994016279/index_html

Leave a comment