-->
Previous Table of Contents Next


Using tar for Backups

The tar (tape archiver) program is usually the command used to save files and directories to an archive medium and recover them later. The tar command works by creating an archive file, which is a single large entity that holds many files within it (much like PKZIP does in DOS, for example). The tar command only works with archives it creates.

The format of the command is a little awkward and takes some getting used to, but fortunately, there are only a few more variations that users will need. The format of the tar command is:


tar switch modifiers files

The files section of the command indicates which files or directories you want to archive or restore. Usually this is a full file system such as /usr or in the case of recovery, a single file such as /usr/tparker/big_file.

The switch controls how tar reads or writes to the backup media. Only one switch at a time can be used with tar. The valid switches are:

  c create a new archive media
  r write to end of existing archive
  t lists names of files in an archives
  u files added if not modified or archived already
  x extract from the archive

A number of modifiers can be added to the tar command to control the archive and how tar uses it. Valid modifiers include:

  A suppress absolute filenames
  b provide a blocking factor (1-20)
  e prevent splitting files across volumes
  f specifies the archive media device name
  F specifies the name of a file for tar arguments
  k gives size of archive volume (in kilobytes)
  l displays error messages if links unresolved
  m does not restore modification times
  n indicates the archive is not a tape
  p extracts files with their original permissions
  v verbose output (lists files on the console)
  w displays archive action and waits for user confirmation

The tar command uses absolute pathnames for most actions, unless the “A” switch is specified.

A few examples will help explain the tar command and the use of tar switches. If you are using a tape drive called /dev/tape and the entire file system to be archived totals to less than the tape’s capacity, you can create the tape archive with the command:


tar cf /dev/tape /

The “f” option is used to specify the device name, in this case /dev/tape. The entire root file system is archived in a new archive file (indicated by the “c”). Any existing contents on the tape are automatically overwritten when the new archive is created (it does not ask if you are sure you want to delete the existing contents of the tape, so make sure you are overwriting material you don’t need). If the “v” option is included in the command, tar echoes the filenames and their sizes to the console as they are archived.

Now, if you need to restore the entire file system from the tape used in the above example, issue the command:


tar xf /dev/tape

This restores all files on the tape because no specific directory is indicated for recovery. The default, when no file or directory is specified, is the entire tape archive. On the other hand, if you want to restore a single file from the tape, the command is:


tar xf /dev/tape /usr/tparker/big_file

which restores only the file /usr/tparker/big_file.

Sometimes you may want to obtain a list of all files on a tape archive. You can do this with the command:


tar tvf /dev/tape

This uses the “v” option to display the results from tar. If the list is long, you may want to redirect the command to a file.

Now that you’ve seen the basic use of tar, we should look at some practical examples. Most tapes require a blocking factor when creating an archive, but with tar, specifying a blocking factor is not necessary when reading a tape because tar can figure it out automatically. The blocking factor tells tar how much data to write in a chunk on the tape. When archiving to a tape, the blocking factor is specified with the “b” modifier. For example, the command:


tar cvfb /dev/tape 20 /usr

creates a new archive on /dev/tape with a blocking factor of 20 and contains all the files in /usr. Most tapes can use a blocking factor of 20, and this can be assumed a default value unless your tape drive specifically won’t work with this value. The only other time blocking factors are changed is for floppy disks and other hard disk volumes. Note that the arguments following the modifiers are in the same order as the modifier. The “f” precedes the “b” modifier so the arguments have the device before the blocking factor. The arguments must be in the same order as the modifiers, which can sometimes cause a little confusion.

Another common problem is that a tape may not be large enough to hold the entire archive, in which case more than one tape is needed. To tell tar the size of each tape, you need the “k” option. It uses an argument that specifies the capacity in kilobytes. For example, the command:


tar cvbfk 20 /dev/tape 122880 /usr

tells tar to use a blocking factor of “20” for the device /dev/tape. The tape capacity is 122,880 kilobytes (approx. 120MB). Again, note the order of arguments and the modifiers that match.

Floppy disks create another problem with tar because the blocking factor is usually different. When floppy disks are used, archives usually require more than one disk and the “k” option is used to specify the archive volumes capacity. For example, to back up the /usr/tparker directory to 1.2MB disks, the command is:


tar cnfk /dev/fd0 1200 /usr/tparker

where /dev/fd0 is the device name of the floppy drive and 1200 is the size of the disk in kilobytes. The “n” modifier tells tar that this is not a tape, and tar will run a little more efficiently than if the modifier had been left off.

Summary

This chapter looks at the basics of backups. You should maintain a backup log and make regular backups to protect your work. Although tar is a little awkward to use, once you’ve tried it a few times it becomes almost second nature.

A number of scripts are beginning to appear that automate the backup process or give you a menu-driven interface to the backup system. These are not in general distribution, but you may want to check FTP and BBS sites for a utility that will simplify backups for you. From here, you can go to these chapters for more information. To learn about:

Automating the backup routines on your system, see Chapter 46.
Setting up your Linux system to serve the Internet, see Chapter 47, “Setting up an Internet Site.”
Applications you can run under Linux, see Chapter 62, “Adabas-D and other Databases” through Chapter 64, “Lone Star Software’s Lone-Tar.”


Previous Table of Contents Next