Using tars Features

Because it's the lowest-common-denominator backup tool and because it's used in nonbackup tasks, tar deserves a bit more attention. The basic syntax for this command is tar command [qualified...]] [file-or-dir[...]]

This syntax is deceptively simple, because tar supports a very large number of commands and qualifiers, the most important of which are summarized in Tables 17.2 and 17.3, respectively. (Consult the tar man page for a more complete list.) Many qualifiers take options themselves, such as filenames. To use tar, you combine precisely one command with any number of qualifiers. The Ule-or-dir that you specify is the file or directory that is to be backed up or restored.

Table 17.2: tar Commands






Creates an archive



Appends tar files to an archive



Appends non-tar files to an archive



Appends files that are newer than those in an archive

—diff or -compare


Compares an archive to files on disk



Lists archive contents

-extract or-get


Extracts files from an archive

Table 17.3: tar Qualifiers




-directory dir


Changes to directory dir before performing operations

-file [hostile


Uses file called file on computer called host as the archive file

-listed-incremental file


Performs incremental backup or restore, using file as a list of previously archived files


Backs up or restores only one filesystem (partition)



Creates or extracts a multitape archive

-tape-length N


Changes tapes after N kilobytes



Preserves all protection information



Retains the leading / on filenames



Lists all files read or extracted; when used with -list, displays file sizes, ownership, and time stamps



Verifies the archive after writing it

-exclude file


Excludes file from the archive

-exclude-from file


Excludes files listed in file from the archive

-gzip or-ungzip


Processes archive through gzip



Processes archive through bzip2

As an example of tar in action, consider a backup of the root directory (/), /home and /usr. These directories are backed up to a SCSI tape drive (/dev/stO) with the following command:

# tar -create -verbose -one-file-system —file /dev/stO I /home I usr This command can be abbreviated as follows:

This document was created by an unregistered ChmMagic, please go to to regist* # tar cvlf /dev/stO I /home Iusr

Note Some non-GNU versions of tar require a dash 0 prior to abbreviated commands.

Linux's versions of tar don't require this feature, though. Warning Using -one-file-system and backing up each partition explicitly is generally a good idea. If you try to simply back up the root partition and all mounted partitions, your backup will include the contents of/proc, any removable media that happen to be mounted, and perhaps other undesirable directories. The extra data will consume space on your backup media and may cause problems when you restore the data. Alternatively, you can explicitly exclude such directories using -exclude or-exclude-from.

Using tar to Back Up to a Removable Disk

Although cp may be the obvious choice for backing up to a removable disk, tar has certain advantages in this role. For one thing, tar can perform incremental backups, as described shortly in "Incremental Backups: Minimizing Backup Resources." How can you use tar to back up to another disk, though? There are three ways. First, you can archive data to a tar file (aka a tarball) on an ordinary filesystem. Second, you can use a partition or even a raw disk device as the tar destination, much as you can use a tape device as the destination. Third, you can create a pipe to copy files through tar. For instance, suppose you've mounted a backup disk at /mnt/backup, and you want to archive the contents of/home to this new location. You could type the following command to achieve this effect:

This command creates an archive of /home on stdout (-) and pipes it to a pair of commands. The first of these commands changes to the target directory and the second extracts the archive from stdin into the new directory. This example produces results that are very similar to those of the much simpler cp -a command, but you can add more tar options—say, to perform incremental backups—which can make tar worthwhile for this task.

Team LIB


0 0

Post a comment