Backing Up and Restoring Your Data


By the time you have used Linux for a few months, you will have built a collection of important files of various kindsimportant spreadsheets, documents in progress, web content, or any number of other types of files. What an annoyanceor worseit would be to lose them!

Modern hard drives are remarkably reliable, especially when compared to the storage devices used by early computers. However, for every handful of users who have never lost an important file due to circumstance or hardware failure, there is at least one user who has seen hours and hours of work lost in the void. Users who suffer data loss inevitably regret not having made backups of their data.

A number of common types of backup media are in widespread use today, but they all fit into one of three major categories:

  • Hot-pluggable drives, like USB or IEEE1394 (FireWire) hard drives or storage keys. These devices have a fixed amount of storage and can be accessed like a hard drive or floppy drive. Back up files by copying them to the external drive or key and then store it in a safe place.

  • Tape drives, such as 8mm or 4mm DAT drives or DLT cartridge streamers. These devices typically have very large storage capacities, which must be accessed in a single, extended read or write (stream) operation, rather than a file at a time.

  • Removable disk drives, such as Zip, Jaz, Orb, DVD-RAM, magneto-optical, or LS-120. These devices act like a cross between a floppy drive and a hard drive: They are removable and easily (randomly) accessible like floppy drives but hold much more data than can fit on a single floppy disk. If you have many gigabytes of data, though, watch out: Making a backup copy of every file on a full 80 gigabyte hard drive can take more 100 CD-R disks or more than 300 Zip disks.

  • CD-R and DVD-R drives, which can be written to only once (or in some cases, several times) and which must also be written in a single, extended write operation using special software tools. If you need to use a CD-R or DVD-R drive in Linux, see the manual pages for mkisofs and cdrecord, or refer to Chapter 15, "Playing and Recording Sound and Music" for details on burning files to CDs or DVDs using the file manager.

If your Linux computer is acting as a personal workstation or small server, most of the files you want to save will be documentsOpenOffice.org files, World Wide Web content, LaTeX source files, or similar things that you have invested your time and effort in creating. These types of files don't take up much space and are relatively easy to back up simply by copying them to your backup medium.

If you need to back up a larger amount of data or a large number of files, a tool specially designed for backing up large amounts of data, like the tar command, can be extremely helpful.

Both of these techniques are discussed in the sections that follow.

Backing Up to Disk or Removable Media

In Chapter 15, "Playing and Recording Sound and Music," you learned how to copy files onto CD-ROM discs. USB keys and Zip, Jaz, or other removable magnetic storage devices are also ideal backup media for small-to-medium amounts of data. They are more convenient than CD-ROMs because they can be accessed in the same way as the rest of your Linux file system, though they generally hold slightly less.

Backing up files or entire directory trees to removable disk devices from your desktop is simple. Just insert the disk into its drive or the USB key into your USB port and, if the Fedora Core 4 desktop supports it, an icon will appear for the device, as shown in Figure 35.1.

Figure 35.1. Icons for compatible removable storage devices appear automatically when the device or disk is in serted. Double-click to open a file manager window.


Double-click a removable device's desktop icon to open a file manager window into which you can copy important filesfrom your desktop, your home directory, or anywhere elsefor safe-keeping.

To access devices that aren't automatically shown on your desktop you'll need to use the mount and cp commands. Access the device and store backup files by following these three steps:

1.

Use the mount command to mount the removable disk device so that you can copy files to it.

2.

Use the cp command to copy individual files or the cp command with the -R option to copy entire directory trees to the removable disk, or use the file manager to copy files and directories you want to back up.

3.

Unmount the removable disk and file it away for safekeeping.

If You Find Yourself Lost in This Section


The information in this section refers to material covered in earlier chapters.

The mount commandand how to use it with common SCSI and IDE deviceswas discussed in Chapter 28, "Command-Line System Administration."

The cp and mkdir commands were discussed in several places in Chapter 6, "Working with Files in the Shell."

The process of copying files and directories using the file manager was discussed in several places in Chapter 5, "Working with Files on the Desktop."

Additional information can also be found in the mount, cp, and mkdir manual pages.


To illustrate, let's step through a sample backup session at the command line. For this example, assume that you are using a common removable slide-in, slide-out hard disk drive, connected as the master device on your secondary IDE channel. The disk you're using is formatted as a single Windows volume, meaning that it has just one partition of the type that Windows creates by default.

Your first order of business is to mount the disk. First, make a mountpoint where your files will appear by using the mkdir command. By convention, mountpoints for removable devices reside in the /mnt directory:

 [you@workstation20 you]# mkdir /mnt/removable [you@workstation20 you]# 

Now, mount the disk so that you can copy files to it. Because it was formatted for Windows, the correct file system type to supply to the mount command is vfat (consult the manual page for mount for more details on various filesystem types):

 [you@workstation20 you]# mount -t vfat /dev/hdc1 /mnt/removable [you@workstation20 you]# 

Now that your disk has been mounted, you can begin to copy files to it using the cp command. Let's assume that you want to copy the file /var/www/mypage.html and the entire contents of your home directory to the disk. You can use the cp command with the -R argument to copy entire directory trees. To do this, simply supply a list of files and directory trees you want to copy as arguments, listing the destination directory as your final argument. If you want to see the files listed as they are copied, use the -v (verbose) argument:

 [root@workstation20 you]# cp -R -v /var/www/mypage.html /home/you /mnt/removable '/var/www/mypage.html' -> '/mnt/removable/mypage.html' '/home/you/myfile.txt' -> '/mnt/removable/you/myfile.txt' '/home/you/document-1.tex' -> '/mnt/removable/you/document-1.tex' '/home/you/document-2.tex' -> '/mnt/removable/you/document-2.tex' '/home/you/old/budget-jan.sxw' -> '/mnt/removable/you/old/budget-jan.sxw' '/home/you/old/budget-feb.sxw' -> '/mnt/removable/you/old/budget-feb.sxw' [root@workstation20 you]# 

You can see by the output of the cp command that your files have been copied to the /mnt/removable directory, which is the place where your removable disk is mounted. Now that your files have been backed up, unmount your removable disk:

 [root@workstation20 you]# umount /mnt/removable [root@workstation20 you]# 

The next time you shut down, remove the slide-out hard disk and file it away for safekeeping, or until the next time you want to save files offline. Your files have now been backed up.

Creating Magnetic Tape Backups with tar

Backups to disk devices are relatively easy to perform. However, depending on the format of your disksthey are generally formatted for Windowsthere is a good chance that any permissions and ownership information connected with your files will not be preserved when you back them up with the cp command.

Furthermore, the cp command can't be used to copy files to magnetic tape devices, which often used for backing up larger amounts of data or backing up data on frequent basis, simply because magnetic tape is available in sizes that far exceed anything single removable disks can offer and because magnetic tape is generally less expensive, past a certain size threshold, than other types of storage.

The aforementioned problems can be solved by using the tar (Tape ARchive) command, which can communicate with magnetic tape drives and can also be used to save the permissions of files you back up on Windows-formatted media. The tar command must be used entirely from the command line; it has no graphical equivalent. To create a backup with the tar command, call tar in the following format:

 tar -c -v -f dest /path1 [/path2 ...] 

The required -c option tells tar that you want to create a backup. The optional -v option causes tar to display the name of each file as it is backed up.

Replace dest with the device or file that should hold the backed-up data. Table 35.1 lists some common magnetic tape devices; dest can also simply be the name of a file, however, as is the case if you are backing up to a mounted removable disk device.

Table 35.1. Common Magnetic Tape Device Names

Device

Description

/dev/st0

First SCSI magnetic tape device

/dev/st1

Second SCSI magnetic tape device

/dev/ht0

First IDE magnetic tape device

/dev/ht1

Second IDE magnetic tape device


Replace /path1 with the directory tree that tar should back up, the optional /path2 with the second directory tree tar should back up, and so on.

Backing Up with tar Examples

Given what you've just learned about using the tar command, let's step through some examples for purposes of illustration.

To back up the /home directory tree (which backs up all user home directories) to the first SCSI tape device, listing each file as it is being processed, you use the following command:

 tar -c -v -f /dev/st0 /home 

To back up the files in /var/www/html and /var/www/cgi-bin to the first IDE tape device, listing each file as it is being processed, you use the following command:

 tar -c -v -f /dev/ht0 /var/www/html /var/www/cgi-bin 

To back up the /home, /var/www, and /var/ftp directory trees to a backup file called backup-oct52004.tar, which is to be stored on a removable disk mounted on /mnt/opticaldiskwithout bothering to list the files as they are storedyou use the following command:

 tar -c -f /mnt/opticaldisk/backup-oct52004.tar /home /var/www /var/ftp 

Because tar is a relatively quick command that does little processing of your files, you will find that the speed of your backups is limited only by the speed of your storage device or streaming tape device.

Creating a List of Your Backup Files

If you want to create a list or index of the files you're backing up, use the -v option and then save tar's output to a file, like this:

 tar -c -v -f /dev/st0 /home > filelist.txt 


Compressing Critical Backup Data

If you have read the tar manual page and found the compress (-z) option, you might be tempted to use it for your tape backups.

Don't.

When you use the tar compress option, tar doesn't compress files individually; it compresses the entire stream of files as they are written out. This is great, unless your backup media develops an error.

If tar finds a read error when restoring from an uncompressed backup, tar can often recover, losing only the file in which the error occurred. When tar finds a read error when restoring from a compressed backup, all the files in the backup are lost.

You should thus never use the compress (-z) option for creating critical backups.

Most modern tape drives (4mm, 8mm, and DLT) compress your data as it is written anyway, so using -z generally produces little in the way of space savings.


Restoring tar Backups

To restore files from a backup with the tar command, call tar in the following format:

 tar -x -v -f source [pattern ...] 

The required -x option tells tar that you want to extract (restore from) a backup. The optional -v option causes tar to display the name of each file as it is restored.

Replace source with the device or file that should hold the backed-up data. Refer to Table 36.1 for some common magnetic tape devices; and once again, source can also be the name of a backup file rather than a device.

If you don't supply a pattern, tar restores every file stored on the streaming tape device or in the backup file. Sometimes, however, you want to restore only a single file or small list of files. When this is the case, replace pattern with a list of quote-enclosed filenames you want to restore, or patterns you want to use to select which files are to be restored.

Be Careful When You Restore a tar Backup


When you restore a tar backup, any existing files to which you have write permission will be overwritten by the files being restored. For this reason, you should always be sure to see whether any existing files will be overwritten before starting a restore.


Note that when you restore using tar, the files in the backup are restored relative to your current working directory. This means that if you backed up the /var/www directory tree using tar and now want to restore the files to /var/www, you must first make the root directory (/) your current working directory using the cd command. If, for example, you try to restore the backup while /home/you is your current working directory, the restored files will end up in /home/you/var/www.

Restoring tar Backups Examples

Using what you've just learned about the tar command, let's step through a few restoration examples to illustrate.

To restore all the files on the magnetic tape in the first SCSI tape drive, listing each file as it is restored, use the following command:

 tar -x -v -f /dev/st0 

To restore only the files from the /var/www TRee stored on the magnetic tape in the second IDE tape drive, listing each file as it is restored, use the following command:

 tar -x -v -f /dev/ht1 "var/www/*" 

To silently (without listing) restore only the plaintext (.txt) files from the /home/you directory stored on the magnetic tape in the second SCSI tape drive, use the following command:

 tar -x -f /dev/st1 "home/you/*.txt" 

Silent backups or restores of this kind are often useful when you're writing scripts because you often want to keep output from shell scripts to a minimum.

To silently restore all the files from the tar backup file called backup-oct52004.tar stored on the removable disk mounted on the /mnt/opticaldisk directory, use the following command:

 tar -x -f /mnt/opticaldisk/backup-oct52004.tar 

Using tar's backup and restore capabilities, you can preserve any file on your system that you want to save and restore it later if something should happen to it.

Restoring from tar Can Be Slow


If you are restoring only one or two particular files from a large backup, you might find that the tar command takes some time to complete. The reason is that tar must read through every file in a backup, regardless of whether you chose to restore every file.


Testing and Listing Backups

Sometimes you want to verify the integrity of a magnetic tape, to make sure that the files on it can still be restored. You can accomplish this by calling tar using the -t option:

 tar -t -f source 

Replace source with the name of the device containing the magnetic tape or the name of the tar file that you want to test. If there are any errors in the backup (meaning that some of the files stored in it can't be restored), an error message is displayed. If tar reads the entire tape or the entire backup file and then exits silently, the backup is problem free.

To create an index of an existing backup tape or file, save the output of the tar command to a file.

For example, to save a list of the contents of the tape in the first SCSI tape drive to the file tape1contents.txt, use the following command:

 tar -t -f /dev/st0 > tape1contents.txt 

If, while you're testing a backup, tar reports errors, you should throw away the media immediately and begin backing up to a new tape or disk because tar errors usually indicate aging or failing magnetic media.

Magnetic Media Is Not Archive Material!

Although media verification does have its place, you shouldn't use it as a way to store information on magnetic media for archival (long-term preservation) purposes.

Magnetic media of all kinds should be considered useful only for short-term backup purposes because it will gradually degrade over time.


Automating tar Backups

One of the biggest advantages of tar's command-line nature is the fact that it can easily be used in scripts or called using the cron service. This makes automating a single backup or a series of backups very easy to do: You just use your favorite text editor to create shell scripts that use the tar command, as outlined in previous sections, and then call those shell scripts using cron.

More on cron and Shell Scripting

The cron service, which is used to schedule tasks to be carried out repeatedly at specific intervals, was discussed in "Using cron to Manage Periodic Jobs" in Chapter 28, "Command-Line System Administration."

Shell scripting was discussed in Chapter 25, "Harnessing the Power of the Shell."


Let's step through a detailed example for illustration purposes. Suppose that your computer system has two tape drives in it: one SCSI tape drive and one IDE tape drive. You are the system administrator and are responsible for backups, so you create the following backup policy:

  • Every morning at 2:30 a.m., you back up the entire /home directory tree, which contains all your users' files, to the tape in the SCSI tape drive, verifying the contents of the tape afterward and saving any error messages displayed during verification in a file called /var/log/NightlyErrors.txt.

  • Once a week at 5:00 p.m. on Sunday afternoon, you back up the contents of the /var/www TRee to the tape in the IDE tape drive, verifying the contents of the tape afterward and saving any error messages displayed during verification in a file called /var/log/WeeklyErrors.txt.

While logged in as root, using vi (or emacs, if you prefer), you create a script called system-backup, as shown in Listing 35.1; then you copy it to ~/bin and mark it as executable.

Listing 35.1. The system-backup Script You Create As Administrator
 #!/bin/sh if [ "$1" = nightly ]; then   tar -c -f /dev/st0 /home   tar -t -f /dev/st0 2> /var/log/NightlyErrors.txt fi if [ "$1" = weekly ]; then   tar -c -f /dev/ht0 /var/www   tar -t -f /dev/ht0 2> /var/log/WeeklyErrors.txt fi 

When called with the nightly argument, the system-backup script shown in Listing 35.1 backs up the /home directory tree to tape drive /dev/st0 and then tests the archive for integrity, making a log file in the process. When called with the weekly argument, it backs up the /var/www directory tree to tape drive /dev/ht0 and then tests that archive, again making a log file in the process.

After you create the script, you issue the crontab -e command to edit the root user's list of periodic jobs. In the special cron control file, you enter the following lines:

 30 2 * * * ~/bin/system-backup nightly 0 17 * * * ~/bin/system-backup weekly 

After entering these lines, you save and exit. Your backup regimen is now in place; you need to remember only to load or switch tapes as necessary. Linux, cron, and tar take care of the rest, and any errors encountered are saved as /var/log/backup.daily and /var/log/backup.weekly. Remember to check these logs often to make sure that your backups are being completed without media errors!

Your own backup automation techniques using cron, shell scripts, and tar can, of course, be much more complex and nuanced than these; you are limited only by your imagination and your ability to master shell scripting.



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net