13.4 Using tar for Backups and Restores

13.4 Using tar for Backups and Restores

In Section 1.18, you learned the basics of tar. As it turns out, you already know nearly all you need in order to do manual backups. If you want a comprehensive list of tar options, you can look at the (gigantic) GNU info documents. However, the good news is that you don't need to know very many options, and after reading this section, you should only need tar --help every now and then as a refresher.

13.4.1 Creating Archives

With processors as fast as they are now, and disks getting ever larger, it doesn't make much sense to omit compression for any archive. Recall that you can create a compressed archive of a directory with one command:

tar zcvf archive directory

A large part of making a backup is figuring out where to put the archive. There are a few choices here:

  • For tape drives, see Section 13.6.2 for how to write the archive to the tape device.

  • For backing up to a spare disk, you can write the archive as a file on the disk's filesystem.

  • If you need to pipe the output to standard output (to feed it to another command, such as cdwrite), use - as the archive name.

Here are some options to consider when running GNU tar:

  • --exclude file Exclude file from the archive.

  • --exclude-from file Doesn't include in the archive any of the files listed in file.

  • --one-file-system Archives files from inside a single filesystem. This is handy for breaking up backups by partitions.

  • --absolute-paths Doesn't strip the leading / from an absolute pathname. This has its uses, but only if you know what you're doing.

  • --listed-incremental file Uses file as the incremental state file (see Section 13.4.2).

One tricky part about adding these options to a traditional tar command is that you must insert a - in front of the regular one-character options. Therefore, if you want to create an archive of the misc directory, but exclude misc/scratch, you would have to run the following with -zcvf rather than with zcvf, as you saw in the previous example. Here's how it would look:

tar --exclude misc/scratch -zcvf archive misc


Remember that tar strips the leading / from an absolute pathname (unless you specify the --absolute-paths parameter). Therefore, to exclude an item, you must match the path that goes into the archive, not necessarily the one on the system. Let's say that you wanted to create an archive of / without /usr. You must remove the / by yourself:

tar --exclude usr -zcvf archive /

13.4.2 Incremental Backups with tar

To enable incremental backups with GNU tar, you need to create an incremental state file that contains information about each newly archived file. Here's how to do it:

  1. Run the following command to make a full backup in full_archive (state_file is the name of the state file; you must choose your own name):

    tar --listed-incremental state_file -zcvf full_archive file1 ...
  2. Make a copy of the original state file. For example:

    cp state_file state_file_incr
  3. When it is time to make an incremental backup, run the same tar command as earlier, but with the state file copy that you created in step 2, state_file_incr:

    tar --listed-incremental state_file_incr -zcvf incremental_archive file1 ...

    This should produce a much smaller archive than the full backup, because tar compares the time(s) listed in state_file_incr (remember, it's a copy of the original state_file) against the modification times of the current files on the disk.

  4. Repeat steps 2 and 3 for subsequent incremental backups.

  5. To do another full backup, delete or rename state_file and start at step 1.

You need to make a copy of the original state file before each incremental backup because GNU tar updates the file specified with --listed-incremental after its run. If you were to perform backups with the same state file over and over again, the incremental backups would only record the changes since the last incremental backup, not since the last full backup. Although this would yield a complete set of changes, it would also make it nearly impossible to find a lost file inside a set of incremental backups. Furthermore, to do a complete restore, you would need to use all of your incremental backups.


Remember that incremental backups are only good when accompanied by the previous full backup. Make sure that you don't overwrite your full backup with an incremental backup.

13.4.3 Extracting Archives

Recall from Section 1.18.1 that you can extract files from a compressed archive as follows:

tar zxvf archive

By default, tar extracts all files in the archive. However, you can limit the extraction to specific files and directories by adding their names to the end of the command line (you may need to run tar ztvf to get a list of filenames):

tar zxvf archive file1 file2 ...

When extracting system files and home directories as root (for example, from a restore or when moving the things around), it is critical that you use the p option to preserve the original permissions:

tar zxvpf archive

Otherwise, tar creates files using the current permissions mask (umask). This can be annoying at the very least, and could cause your system to malfunction at the worst.


Before extracting an archive, it's very important to check your current working directory against any files that the extraction process might create so that you don't accidentally clobber newer versions of files with older ones from the archive. When doing a partial restore, it's usually a good idea to change to an empty temporary directory first.