BSD DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to BSD Subscribe to Newsletters

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...
cZ\333\300\021\207\335v\333J\235\212\335H\335<\270\377\203\025\323}\333\220\016
\215\335h*d\335?\320\223\333\225\335\206\333\224\335\234\020\007\334\324]m\312D
s.\017\214\256\374\251H\320\016\252\031\332YE\316\304\360\301\003\242\362\301\2
35\327\241\260\261\030\377\t3\256S\320H\t\327\270\204\302\246\335\030\207/\242(
\251
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 Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com