BSD DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Backing up Files with Tar
Pages: 1, 2, 3, 4

Also, when listing the contents of an archive, you can include the v, or verbose, switch. Here is an example of the difference in the output, first without the v switch:



tar tzf backup.tar.gz | tail -2
www/mod_tsunami/pkg-plist
www/Makefile

Then with the v switch:

tar tzvf backup.tar.gz | tail -2
-rw-r--r-- test/wheel      116 May 11 09:53 2002 www/mod_tsunami/pkg-plist
-rw-r--r-- test/wheel     9713 May 11 09:53 2002 www/Makefile

Notice that long listing of the files in the backup and compare that to a long listing of the backup file itself:

ls -l backup.tar.gz
-rw-r--r--  1 root  wheel  15098016 May 11 17:31 backup.tar.gz

The backup was created by the superuser at 17:31, yet the files in the backup still belong to the test user and those files were created at 9:53 (when I set up the test directory). This is what I was talking about in the last article when I said that archivers preserve the permissions and ownership of the files that are backed up.

It is also possible to tell tar to backup multiple directories:

tar cvzf partial.tar.gz www/apache2 www/chimera www/zope

The above command will create (c) a gzipped (z) file (f) named partial.tar.gz by archiving the contents of the directories apache2, chimera, and zope. Remember, tell tar which file you want to send the backup to first; everything after that name will be what tar will back up for you.

If you are still saving up for a tape device and want to do a poor man's backup using floppies, this command will backup everthing in your current directory:

tar cvMf /dev/fd0 .

Don't forget to put a floppy disc in your floppy drive, and make sure you are in the directory you want to backup. Note that instead of using the f switch to give the name of a backup, I used it to specify the name of my floppy device (/dev/fd0).

Why did I also include the M, or multi-volume, switch? Since I was backing up a directory with 28M of data, I knew it wouldn't fit all one floppy. Since I used the M switch, when tar had filled up the first floppy, it displayed this message:

Prepare volume #2 for /dev/fd0 and hit return:

It is a good idea to always include the M switch when backing up to floppies, just in case the data won't fit on one floppy. Also, if you want to save on the number of floppies you'll need, include either of the z or the Z switches so that the data will also be compressed as it is backed up.

An interesting note about backing up to floppies: you don't mount the floppy first, and once it contains the backup, you won't be able to mount the floppy. Yet the tar utility understands the data on the floppy, so this command will work:

tar tvf /dev/fd0

You should run this command after you create your backup, so you can verify the files in the backup.

However, if I try this command:

mount /dev/fd0 /floppy

I'll receive this error:

mount: /dev/fd0 on /floppy: incorrect super block

One last thing about creating archives with tar: tar was designed to back up everything in the specified directory. This means that every single file and subdirectory that exists beneath the specified directory will be backed up. It is possible to specify which files you don't want backed up using the X switch.

Let's say I want to backup everything in the www directory except for the apache2 and zope subdirectories. In order to use the X switch, I have to create a file containing the names of the files I wish to exclude. I've found that if you try to create this file using a text editor, it doesn't always work. However, If you create the file using echo, it does. So I'll make a file called exclude:

echo apache2 > exclude
echo zope >> exclude

Here, I used the echo command to redirect (>) the word apache2 to a new file called exclude. I then asked it to append (>>) the word zope to that same file. If I had forgotten to use two >'s, I would have overwritten the word apache2 with the word zope.

Now that I have a file to use with the X switch, I can make that backup:

tar cvfX backup.tar exclude www

This is the first backup I've demonstrated where the order of the switches is important. I need to tell tar that the f switch belongs with the word backup.tar and the X switch belongs with the word exclude. So if I decide to place the f switch before the X switch, I need to have the word backup.tar before the word exclude. This command will also work as the right switch is still associated with the right word:

tar cvXf exclude backup.tar www

But this command would not work the way I want it to:

tar cvfX exclude backup.tar www
tar: can't open backup.tar : No such file or directory

Here you'll note that the X switch told tar to look for a file called backup.tar to tell it which files to exclude, which isn't what I meant to tell tar.

Let's return to the command that did work. To test that it didn't back up the file called apache2, I used grep to sort through tar's listing:

tar tf backup.tar | grep apache2

Since I just received my prompt back, I know my exclude file worked. It is interesting to note that since apache2 was really a subdirectory of www, all of the files in the apache2 subdirectory were also excluded from the backup. I then tested to see if the zope subdirectory was also excluded in the backup:

tar tf backup.tar | grep zope
www/zope-zpt/
www/zope-zpt/Makefile
www/zope-zpt/distinfo
www/zope-zpt/pkg-comment
<output snipped>

This time I got some information back, as there were other subdirectories that started with the term "zope," but the subdirectory that was just called zope was excluded from the backup.

Pages: 1, 2, 3, 4

Next Pagearrow





Sponsored by: