Backing Up and Restoring Files
By now, you know how to perform a number of systems-administration tasks, from automatically starting servers to scheduling periodic jobs. The next few sections introduce you to another systems-administration task-backing up and restoring files. You learn about backup strategies, backup media, and how to back up and restore files by using the tape archiver (tar) program that comes with Red Hat Linux. Also, you learn how to perform incremental and automatic backups on tapes.
Selecting a Backup Strategy and Media
Your Linux system's hard disk contains everything needed to keep the system running, as well as other files, such as documents and databases you need to keep your business running. You need to back up these files so that you can recover quickly and bring the system back to normal in case the hard disk crashes. Typically, you have to follow a strict regimen of regular backups because you can never tell when the hard disk might fail or the file system might get corrupted. To implement such a regimen, you need to decide which files you want to back up, how often, and what backup storage media to use. This is what I mean by selecting a backup strategy and backup media.Your choice of backup strategy and backup media depends on your assessment of the risk of business disruption due to hard disk failure. Depending on how you use your Linux system, a disk failure may or may not have much impact on you.For example, if you use your Linux system as a learning tool (to learn about Linux or programming), all you may need are backup copies of some system files required to configure Linux. In this case, your backup strategy can be to save important system- configuration files on one or more floppies every time you change any system configuration.On the other hand, if you use your Linux system as an office server that provides shared file storage for many users, the risk of business disruption due to disk failure is much higher. In this case, you have to back up all the files every week and back up any new or changed files every day. You should perform these backups in an automated manner (this is where you can use the job-scheduling features from earlier in this chapter). Also, you probably need a backup storage medium that can store large amounts (multiple gigabytes) of data on a single tape. In other words, for high-risk situations, your backup strategy is more elaborate and requires additional equipment (such as a tape drive).Your choice of backup media depends on the amount of data you have to back up. For a small amount of data, such as system-configuration files, you can use floppy disks as the backup media. If your PC has a Zip drive, you can use Zip disks as backup media; these are good for backing up a single-user directory. To back up servers, you should use a tape drive, typically a 4-mm or 8-mm tape drive that connects to a SCSI controller. Such tape drives can store several gigabytes of data per tape, and you can use them to back up an entire file system on a single tape.When backing up files to these backup media, you have to refer to the backup device by name. Table 20-6 lists device names for some common backup devices.
Backup Device | Linux Device Name |
---|---|
Floppy disk | /dev/fd0 |
IDE Zip drive | /dev/hdc4 or /dev/hdd4 |
IDE floppy tape (Ftape) drive | /dev/qft0 |
SCSI Zip drive | /dev/sda (assuming it's the first SCSI drive; otherwise, the device name depends on the SCSI ID) |
SCSI tape drive | /dev/st0 or /dev/nst0 (the n prefix means that the tape is not rewound after files are copied to the tape) |
Taking Stock of Commercial Backup Utilities for Linux
This chapter describes how to back up and restore files using the tape archiver (tar) program that comes with Red Hat Linux. Although you can manage backups with tar, a number of commercial backup utilities come with GUIs and other features to simplify backups. Here are some well-known backup utilities for Red Hat Linux:
BRU-Backup and Restore Utility from The TOLIS Group, Inc. (http://www.tolisgroup.com/ )
LONE-TAR-Tape-backup software package from Lone Star Software Corporation (http://www.cactus.com/ )
Arkeia-Backup and recovery software for heterogeneous networks from Knox Software (http://www.knox-software.com/ )
CTAR-Backup and recovery software for UNIX systems from UniTrends Software Corporation (http://www.unitrends.com/productsl )
BrightStor ARCserve Backup for Linux-Data protection technology for Linux systems from Computer Associates (http://www3.ca.com/Solutions/Product. asp?ID=3370 )
Using the Tape Archiver-tar
You can use the tar command to archive files to a device such as a floppy disk or tape. The tar program creates an archive file that can contain other directories and files and (optionally) compress the archive for efficient storage. The archive is then written to a specified device or another file. In fact, many software packages are distributed in the form of a compressed tar file. The command syntax of the tar program is as follows:
tar options destination source
Here, options are usually specified by a sequence of single letters, with each letter specifying what tar should do. destination is the device name of the backup device. And source is a list of file or directory names denoting the files to back up. Optionally, you can add a hyphen prefix for the options.
Backing Up and Restoring a Single-Volume Archive
For example, suppose you want to back up the contents of the /etc/X11 directory on a floppy disk. Log in as root , place a disk in the floppy drive, and type the following command:
tar zcvf /dev/fd0 /etc/X11
The tar program displays a list of filenames as each file is copied to the compressed tar archive on the floppy disk. In this case, the options are zcvf , the destination is /dev/fd0 (the floppy disk), and the source is the /etc/X11 directory (which implies all its subdirectories and their contents). You can use a similar tar command to back up files to a tape-simply replace /dev/fd0 with the tape device-such as /dev/st0 for a SCSI tape drive.tar options.
Option | Definition |
---|---|
c | Creates a new archive |
f | Specifies the name of the archive file or device on the next field in the command line |
M | Specifies a multivolume archive (the next section describes multivolume archives) |
t | Lists the contents of the archive |
v | Displays verbose messages |
x | Extracts files from the archive |
z | Compresses the tar archive using gzip |
To view the contents of the tar archive you create on the floppy disk, type the following command:
tar ztf /dev/fd0
You should see a list of the filenames (each begins with /etc/X11 ) indicating what's in the backup. In this tar command, the t option lists the contents of the tar archive.To learn how to extract the files from a tar backup, try the following steps while logged in as root :
Change the directory to /tmp by typing this command:cd /tmp
This is where you extract the files from the tar backup.
Type the following command:tar zxvf /dev/fd0
This tar command uses the x option to extract the files from the archive stored on /dev/fd0 (the floppy disk).
If you check the contents of the /tmp directory, notice that the tar command creates an etc/X11 directory tree in /tmp and restores all the files from the tar archive into that directory. The tar command strips off the leading / from the filenames in the archive and restores the files in the current directory. If you want to restore the /etc/X11 directory from the archive on the floppy, use this command:
tar zxvf /dev/fd0 /
The / at the end of the command denotes the directory where you want to restore the backup files.As you can see, the tar command enables you to create, view, and restore an archive. You can store the archive in a file or in any device you specify with a device name.
Backing Up and Restoring a Multivolume Archive
Sometimes the capacity of a single storage medium is less than the total storage space needed to store the archive. In this case, you can use the M option for a multivolume archive-meaning the archive can span multiple tapes or floppies. Note, however, that you cannot create a compressed, multivolume archive. You have to drop the z option. To see how multivolume archives work, log in as root , place one disk in the floppy drive, and type the following tar command:
tar cvfM /dev/fd0 /usr/share/doc/ghostscript*
Note the M option in the option letters; it tells tar to create a multivolume archive. The tar command prompts you for a second floppy when the first one is filled. Take out the first floppy, and insert another floppy when you see the following prompt:
Prepare volume #2 for `/dev/fd0' and hit return:
When you press Enter, the tar program continues with the second floppy. In this example, you need only two floppies to store the archive; for larger archives, the tar program continues to prompt you for floppies if more floppies are needed.To restore from this multivolume archive, type cd /tmp to change the directory to /tmp . Then type:
tar xvfM /dev/fd0
The tar program prompts you to feed the floppies as necessary.
Insider Insight | Use the du -s command to determine the amount of storage you need to archive a directory. For example, here's how you can get the total size of the /etc directory in kilobytes: du -s /etc |
The resulting output shows that the /etc directory requires at least 14,532K of storage space to back up. If you plan to back up on multiple high-density floppies, you need about 14,532/1,440 = 11 floppies.
Backing Up on Tapes
Although backing up on tapes is as simple as using the right device name in the tar command, you do need to know some nuances of the tape device to use it well. When you use tar to back up to the device named /dev/st0 (the first SCSI tape drive), the tape device automatically rewinds the tape after the tar program finishes copying the archive to the tape. The /dev/st0 device is called a rewinding tape device because it rewinds tapes by default.
If your tape can hold several gigabytes of data, you may want to write several tar archives-one after another-to the same tape (otherwise, much of the tape may be empty). To do this, you do not want the tape device to rewind the tape after the tar program finishes. To help you with this, there are several Linux tape devices that are nonrewinding. The nonrewinding SCSI tape device is called /dev/nst0 . Use this device name if you want to write one archive after another on a tape.After each archive, the nonrewinding tape device writes an end-of-file (EOF) marker to separate one archive from the next. You can use the mt command to control the tape-you can move from one marker to the next or rewind the tape. For example, after you finish writing several archives to a tape using the /dev/nst0 device name, you can rewind the tape with the following command:
mt -f /dev/nst0 rewind
After rewinding the tape, you can use the following command to extract files from the first archive to the current disk directory:
tar xvf /dev/nst0
After that, you must move past the EOF marker to the next archive. To do this, use the following mt command:
mt -f /dev/nst0 fsf 1
This positions the tape at the beginning of the next archive. You can now use the tar xvf command again to read this archive.
Note | If you save multiple archives on a tape, you have to keep track of the archives yourself. |
Performing Incremental Backups
Suppose that you back up your system's hard disk on a tape by using tar. Because such a full backup can take quite some time, you do not want to repeat this task every night. (Besides, only a small number of files may have changed during the day.) You can use the find command to list those files that have changed in the past 24 hours:
find / -mtime -1 -type f -print
This command prints a list of files that have changed within the last day. The -mtime -1 option means that you want the files that were last modified less than one day ago. You can now combine this find command with the tar command to back up only those files that have changed within the last day:
tar cvf /dev/st0 `find / -mtime -1 -type f -print`
When you place a command between single back quotes, the shell executes that command and places the output at that point in the command line. The net result is that the tar program saves only the changed files in the archive. Thus, you get an incremental backup that includes files that have changed since the previous day.
Earlier in this chapter, you learn to use crontab to set up recurring jobs (called cron jobs). The Linux system performs these tasks at regular intervals. Backing up your system is a good use of the crontab facility. Suppose your backup strategy is as follows:
Every Sunday at 1:15 a.m. , your system backs up the entire disk on the tape.
Monday through Saturday, your system performs an incremental backup at 3:10 a.m. by saving only those files that have changed during the past 24 hours.To set up this automated backup schedule, log in as root and type the following lines in a file named backups (this example assumes that you use a SCSI tape drive):15 1 * * 0 tar zcvf /dev/st0 /
10 3 * * 1-6 tar zcvf /dev/st0 `find / -mtime -1 -type f -print`
Next, submit this job schedule by using the following crontab command:crontab backups
Now, you should be set for an automated backup. All you need to do is place a new tape in the tape drive everyday. You should also give each tape an appropriate label.