Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Tape Archive: tar

The simplest of backup utilities is tar. Its functionality is fairly simple: It creates a file that archives other files. It is common in UNIX to distribute programs in a tar file. (tar files are analogous to zip files found in the PC world.) Modern versions of tar can not only archive but also compress data, making the tar file smaller. Note that compressing tar files on tape is not recommended because tar is then unable to recover from tape errors.

The tar program has many options. I would recommend that you read its man page to see what it has to offer (see Chapter 15, “Help Please,” for information on accessing man pages). In the following section, you’ll learn the least you need to know to make use of tar.

Making a tar File

The format of the tar command follows this pattern:

     tar options destination source

To back up my home directory in /home/alberto to the file /tmp/bkup.tar, I would type the following:

     tar cvf archiveName pathsToArchive

     tar cvf /tmp/bkup.tar /home/alberto

In tar, the arguments are typically given without a dash. The order of the options (cvf) and the arguments that follow are important. In our case, the cvf options tell tar to

  Create an archive (c option)
  Be verbose and list all it does (v option)
  Put the archive on a file or device (f option)

The f option expects the next argument to be the filename or device where the tar file will be created. In our case, it is a regular file in the /tmp directory called bkup.tar: /tmp/bkup.tar. This file can be replaced by the name of your backup device where you want the file written. (I’ll examine this later in this section.)

The f command will back up all the files in my home directory into a file that I called bkup.tar. One important thing about tar is that if I give it a path to back up, that path is preserved. So when I ”untar” the files (we’ll see this later, in the section “Extracting Files from the tar Archive”), it will create a home/alberto directory if it doesn’t already exist. Note that the leading forward slash (/) is removed.

All paths and files specified after the file argument are diligently copied. In my case, I only provided /home/alberto, but I could as easily have typed this:

     tar cvf /tmp/bkup.tar /home/alberto  /var/spool/mail/alberto

This command would have backed up my home directory and my mailbox into the same archive.

Extracting Files from a tar Archive

There’s no “untar” command. To extract files, you use the tar command and provide different options. To extract the files in the tar file I created in the preceding section, I would type this:

     tar xvf archive destination_or_name_of_file_to_extract
     tar xvf /tmp/bkup.tar /

The first argument contains the options to the tar command. In this case, the xvt options tell tar to

  Extract files (x option)
  Be verbose and list what it does (v option)
  Read from the file archive provided (f option)

The second argument specifies the name of the archive (/tmp/bkup.tar).

The final argument (/) is optional and specifies the path where I want to put the files I extract from my archive. If I don’t provide this option, the archive is extracted wherever my shell happens to be. Remember that when I created the file, I specified /home/alberto. When tar created the archive, it removed the leading / so that the archive could be extracted into any directory. By choice, I am extracting the archive back into /, which will put all my files into /home/alberto, effectively replacing any files that have the same name.

If the path you provide is neither absolute nor relative, it is taken to mean a particular file in the archive to extract, and tar will try to extract this file only. The only caveat is that you need to know the path to the file in the archive. If I know that I want to restore /home/alberto/nsmail, I could type this:

     tar xvf bkup.tar home/alberto/nsmail

Tar would only extract the path home/alberto/nsmail, putting it into the current directory (remember that tar, by default, strips the leading slash [/] on any path you provide).


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.