BSD DevCenter
oreilly.comSafari Books Online.Conferences.


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

I'll start simple, by telling tar to create (c) a backup of my current directory (.) to a file I'll call backup.tar. Since this is not the default backup location, I'll use the f switch to indicate the name of the file I'd like the backup sent to:

tar cf backup.tar .

When I ran this command, my prompt disappeared for a moment and I heard my hard drive churning away. When my prompt reappeared, I had a new file in my home directory named backup.tar. If you don't want to just wait in silent anticipation, use the v switch and tar will tell you what it is doing while it is doing it. I'll remove that backup and try again with the v switch:

rm backup.tar
tar cvf backup.tar .

You'll understand the difference when you try this for yourself. Now, let's see what type of file tar created:

file backup.tar
backup.tar: GNU tar archive

This is not an ASCII text file, so I won't be able to view its contents with a pager or an editor. However, tar understands this file and I can ask it to read it for me using the t switch:

tar t backup.tar
tar: can't open /dev/sa0 : Device not configured

Oops, I forgot that tar expects to read that SCSI tape device unless I tell it to look somewhere else. I'll try again, this time including the f switch:

tar tf backup.tar

This time, a whole bunch of files and directories fly by very quickly; it looks like I've successfully made a backup. If I wanted to verify the file list, I'd send the output to a pager so I could read it one page at a time:

tar tf backup.tar |more
Learning the Unix Operating System

Related Reading

Learning the Unix Operating System
A Concise Guide for the New User
By Jerry Peek, Grace Todino-Gonguet, John Strang

It is also possible to create a compressed backup by including either of the z or Z switches when using tar. Let's take a look at the size of that backup we just created:

ls -l backup.tar
-rw-r--r--  1 root  wheel  25722880 May 11 16:41 backup.tar

I'll now remove that backup, tell tar to create a compressed backup using the gzip utility, then view the difference in size and type:

rm backup.tar
tar cvzf backup.tar.gz .

ls backup.tar.gz
-rw-r--r--  1 root  wheel  5899840 May 11 16:45 backup.tar.gz

file backup.tar.gz
backup.tar.gz: gzip compressed data, deflated, last modified: Sat May 11 
16:45:47 2002, os: Unix

And I'll repeat the above, except this time tell tar to compress using the compress utility instead:

rm backup.tar.gz
tar cvZf backup.tar.Z

ls backup.tar.Z
-rw-r--r--  1 root  wheel  9444468 May 11 16:50 backup.tar.Z

file backup.tar.Z
backup.tar.Z:   compress'd data 16 bits

To list the files in a compressed archive, don't forget to include the z (or Z) switch. For example, if I try this:

tar tf backup.tar.Z

I'll get this strange error:

tar: Hmm, this doesn't look like a tar archive.
tar: Skipping to next file header...
tar: Skipping to next file header...
tar: only read 3188 bytes from archive backup.tar.Z

Since this file was created with the Z switch, I have to remember to include the Z switch whenever I work with this file.

tar tZf backup.tar.Z

The above command will give me the listing of the contents. You'll note that when I created my backups, I gave the archives I created with the z switch the extension of tar.gz and the files I created with the Z switch the extension of tar.Z. I can call my archive whatever I want; I just used that convention to remind me that I'm dealing with a tar archive file and what type of compression I used when I created that file. It is always a good idea to use the file utility on an archive to verify whether or not it has been compressed, and if so, whether it was compressed with the z or the Z switch.

Pages: 1, 2, 3, 4

Next Pagearrow

Sponsored by: