Using Backup and Restore Commands
The commands you use depend in part on how you’re backing up your data. Generic backups commonly use the tar or cpio commands. Alternatively, you might dump and restore data to and from a tape drive. Backups to local CDs are associated with the mkisofs, cdrecord, and dvdrecord commands. Some variations are required to back up and restore data through the network to remote locations.
Generic Backup Commands
Let’s look at the two generic Linux commands for backing up a group of files. The tar command was originally developed to archive files and directories to tape drives; the cpio command also copies files and directories to and from an archive. With the right options, these commands can be used to back up files to most media.
Note | You can also use the dd command to dump the contents of a directory directly to a device—for example, a floppy drive device such as /dev/fd0 or a tape drive device such as /dev/st0. For more information on dd, see Chapter 03. |
Archiving by tar
You examined the tar command for the first time in Chapter 10. It’s simple to use. The format is easily compressed and downloadable. This command is the main alternative to the RPM system for packaging programs and applications. With the right options, it’s functionally similar to the .zip file system associated with Microsoft Windows.
The tar command is designed to copy a series of files into a single large file. If you want to back up the files in mj’s home directory, you might run the following command:
# tar cvzf mjbackup.tar.gz /home/mj
This command creates (c) a backup, listing every filename in the archive (v = verbose) in compressed format (z = zip) in the file (f) named mjbackup.tar.gz. Files in subdirectories of /home/mj are also saved to this archive. You can then save this archived file to a backup area such as a network share or a tape drive.
Note | Compressed tar archives often include the .tar.gz, .tgz, or .tar.bz2 extensions. The first two extensions are both tar archives compressed with the gzip command. The last extension, based on the bzip2 "Burrows-Wheeler block sorting compression algorithm," is slightly more efficient at data compression. |
You can just as easily unarchive files with the following command:
# tar tkvzf mjbackup.tar.gz
This command lists (t) the files in your archive. When it restores, it does not overwrite your current files (k = keep old files). In verbose (v) mode, you see everything that happens. If you stored files in a zipped format, you need to restore from the zipped (z) format. Also, it is restoring from the backup file named mjbackup.tar.gz.
You can review some of the available tar switches in Table 14.2. Note that the first switch in the tar command should start with a c, t, or an x.
Option | Function |
---|---|
c | Creates an archive. |
d | Compares files between an archive and a current directory. |
f | Uses the following filename for the archive. |
j | Compresses in bzip2 format to or from an archive. |
k | Does not overwrite existing files. |
r | Adds files to the end of an archive. |
t | Lists files in a current archive. |
v | Verbose; lists all files going in or coming out of an archive. |
z | Zip. Compresses files to or from an archive in regular gzip format. |
Note | The tar command is path dependent. If you save the files in a directory using the absolute path (with a leading forward slash, such as /home/mj), you can restore the files to that directory from any location on that computer. Alternatively, if you use the relative path (without a leading forward slash, such as home/mj), files may not be restored to their original locations; it depends on the present working directory. |
You can use a number of tar commands to create and extract archives. Some typical commands include the following. Read them over using the descriptions in Table 14.2.
# tar xzvf download.tar.gz
# tar czvf backup.tar.gz /somedirectory
Note | The tar command is similar to ps in that single-letter command options do not require a leading dash. |
Archiving by cpio
The cpio command can help you archive a class of files, because unlike tar, it works with standard input and output. This use is suggested by its name (cpio = copy + input/output).
As with tar, it’s fairly easy to archive known directories (along with the files in their subdirectories). For example, if you want to back up the files in mj’s home directory, you run the following command:
# find /home/mj | cpio -o > mjarch.cpio
But there is a disadvantage; cpio takes from standard input and archives to standard output. Note how the standard input, all files in the /home/mj directory, are piped to the cpio command. Since this works with classes of files, you can use wildcards to set up a group of files as standard input as well. For example, the following command creates an archive from the .tif files in the current directory:
# find *.tif | cpio -o > mjtifs.cpio
Remember, the find command is flexible; the following command creates an archive from all the .tif files on your system:
# find / -name ‘*.tif’ | cpio -o > mjtifs.cpio
It’s easy to restore the files from a .cpio archive. The following command restores the files in the mjarch.cpio:
# cpio -i < mjarch.cpio
As with tar, the way cpio restores files saved from a directory depends on whether you used the absolute or relative path.
One of the advantages of cpio is the ability to send files directly to external sources. For example, the following commands send and restore the files from mj’s home directory to a SCSI tape drive:
# find /home/mj | cpio -o > /dev/st0
# cpio -i < /dev/st0
A number of options are available for the cpio command. Some of the important options are shown in Table 14.3.
Option | Function |
---|---|
-A | Appends to an existing archive. Closely associated with -F. |
-F | Specifies archive filename. Can substitute for redirection arrow (>). |
-i | Extracts from an archive file or device. |
-o | Copies to an archive file or device. |
-u | Replaces all files, even if they are newer. |
-v | Verbose mode. |
Tape dump and restore
The dump and restore commands make it easy to implement incremental and or differential backups. dump allows you to take the contents of a directory, and restore allows you to interactively return backed-up files to their original locations.
Although these commands are most commonly associated with tape drives, they work with other media as well. The examples shown in this section are based on using these commands to back up a home directory to a floppy disk.
Archiving by dump
The dump command has three basic levels of options. More are shown in the Appendix. You can set up a series of commands that starts with a full backup of a home directory, followed by differential backups. For example, if you want to back up the home directory of mao with dump to the /dev/nst0 tape drive, you run the following commands:
# dump 0f /dev/nst0 /home/mao
# dump 1f /dev/nst0 /home/mao
# dump 2f /dev/nst0 /home/mao
# dump 3f /dev/nst0 /home/mao
# dump 4f /dev/nst0 /home/mao
# dump 5f /dev/nst0 /home/mao
The first command, with the 0f option, sets up a full backup of the /home/mao directory. The commands that follow, when run in sequence, set up differential backups that save only those files that were changed since the previous backup.
Tip | To speed the backup, you may be able to use the biggest block size allowed by your backup system (for instance, a tape drive). For example, the command dump 0f /dev/nst0 /home/mao -b 2048 uses a block size of 2048 bytes. You may want to experiment with larger block sizes to reduce backup time. But remember, you should also verify the results of your experiment with the appropriate restore command. |
Alternatively, you could start with a full backup, followed by incremental backups with a sequence of commands such as:
# dump 0f /dev/nst0 /home/mao
# dump 8f /dev/nst0 /home/mao
# dump 7f /dev/nst0 /home/mao
# dump 6f /dev/nst0 /home/mao
# dump 5f /dev/nst0 /home/mao
# dump 4f /dev/nst0 /home/mao
If you’re backing up an entire filesystem, you’ll want to use the u option, which stores the history in /etc/dumpdates. For example, the following command backs up the entire root (/) directory filesystem:
# dump 0uf /dev/nst0 /
Take a look at the workings of a dump command on the files in the /home/mao directory in Figure 14.1.

Figure 14.1: dump output
There are a number of options available for the dump command. Table 14.4 shows some of the important options.
Option | Function |
---|---|
0-9 | Dump level. 0 = full backup. Differential backups use dump with increasing numbers (e.g., 1, 2, 3…). Incremental backups use dump with decreasing numbers (e.g., 8, 7, 6…). |
A | Archives a table of contents for the backup. |
f | Writes the backup to a file or device. |
j level | Writes with compression; you need to specify a compression level such as 2 or 4. |
T date | Uses the specified date instead of what is shown in /etc/dumpdates. |
u | Updates /etc/dumpdates after a successful backup. |
Recovering with restore
There are two ways to restore from a backup created with the dump command: interactively or directly. In either case, you can restore an entire backup, or just the files that you need.
You can view a listing of files that were backed up with the dump command. As shown in Figure 14.2, the following command lists the files from the backup of mao’s home directory:
# restore -tf /dev/fd0

Figure 14.2: Files on a backup
Alternatively, you can use restore mode to search through a current backup. As shown in Figure 14.3, the -i option brings you into interactive mode, where you can use some very basic Linux navigational commands. As of this writing, restore interactive mode doesn’t allow you to use options such as -l for the ls command.

Figure 14.3: An interactive restore
A number of options are available for the restore command. Table 14.5 lists some of the important ones.
Option | Function |
---|---|
-C | Compares a backup with current files. |
-f | Specifies a file. |
-i | Allows interactive recovery from a backup; several commands are available in restore mode. |
-r | Rebuilds the data to a freshly formatted partition. |
-t | Lists the filenames in the backup. |
Backup Commands for CDs/DVDs
Before you can start recording data, you need to check whether Red Hat Linux recognizes your hardware. This is normally a simple exercise. Then you can create files suitable for CDs or DVDs, and then record them with the appropriate commands.
Checking Hardware
Before you can start backing up data to your writeable CD or DVD drive, you need to make sure it’s actually working. Assuming Red Hat Linux has automatically detected the right drive, you should see the appropriate setting for it when you issue one of these commands:
# cdrecord --scanbus
# dvdrecord --scanbus
Linux uses SCSI drives for recording. But that’s probably not a big deal if all you have is an IDE drive; in most cases, Red Hat Linux automatically configures SCSI emulation by default. In other words, it makes your IDE CD or DVD writer look like a SCSI drive.
From either of these commands, you should see output associated with an scsibus, similar to the following. Though this component is listed as a CD-ROM, it works as it should as a CD-RW drive:
0,0,0 0) ‘LG ‘ ‘CD-RW CED-8083B ‘ ‘1.09’ Removable CD-ROM
However, if you get a “No such file or directory” message, there’s a problem. Red Hat may be having a bit of trouble adapting your system to SCSI emulation. It’s not hard to do yourself. First, add the following line to /etc/modules.conf, which ignores IDE settings:
options ide-cd ignore=hdb
If your writeable CD or DVD drive is on a different IDE device such as /dev/hdc, substitute accordingly. You can check for your device with the dmesg | grep hd command. Add the SCSI emulation module with the following command, and you should be on your way:
# modprobe -a ide-scsi
To make it work, you’ll need to link the appropriate SCSI device file, normally /dev/scd0, to /dev/cdrom, in /etc/fstab.
For example, you might change the following line in /etc/fstab from the first version to the second:
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,kudzu,rw 0 0
/dev/scd0 /mnt/cdrom iso9660 noauto,owner,kudzu,rw 0 0
Alternatively, you can link the device file directly with the following command:
# ln -s /dev/scd0 /dev/cdrom
If you get a “File exists” error, try the ls -l /dev/cdrom command. Linux may have already created the proper link for you.
Making an Image
The next step is to make an image file. Whether you’re recording to a CD or a DVD, you can create the image file with the mkisofs command. As an example, assume you want to back up all the files and directories under /home. You might use the following command, where -r includes Rock Ridge extensions (which supports Unix-based filesystems), -J includes the Joliet filesystem (which makes files readable under Microsoft operating systems), -T preserves long filenames, and -o stands for output:
# mkisofs -J -r -T -o newcd.iso /home
This may create a very big file; if you’re creating an image for a DVD, the file could easily be several gigabytes in size. It’s a good idea to check the integrity of this file. One way to do this is to mount the image file as if it were a CD or DVD. For example, for CDs, the following command mounts your newly created newcd.iso image on /mnt/cdrom:
# mount -t iso9660 -o loop newcd.iso /mnt/cdrom
Alternatively, for DVDs, the following command lists the files in the appropriate image file:
# isoinfo -i newdvd.iso -l
Note | Commands for recording data on DVDs are still under development; thus the commands shown in this chapter are subject to change. For the latest information, see the official website of the dvdrtools project at www.nongnu.org/dvdrtools. |
Burning the Image
Now we’re ready to copy the image to a blank writeable CD. The cdrecord command can help. For the items cited in the previous section, you’d use this command:
# cdrecord -v speed=2 dev=0,0,0 newcd.iso
The -v option allows you to see what happens as Linux copies the image onto your CD. If there is a problem, these messages can also help you diagnose the cause. Chapter 04. As you can see in the figure, a substantial number of useful messages are available when you run this command.
Alternatively, the command to copy the image to a blank writeable DVD is similar:
# dvdrecord -v speed=1 -dao dev=0,1,0 newdvd.iso
This records the newdvd.iso image, verbosely (-v), at first speed (speed = 1), in Disk at Once (-dao) mode, where data is written in a single operation.

Figure 14.4: The cdrecord process
Note | If you’re in GNOME and insert a blank writable CD, Nautilus automatically opens a burn:/// window, where you can copy the files and folders that you want written to that CD. It includes a Write To CD button and easy to understand prompts. |