As noted in the simple backup procedure in the beginning of this chapter, removable media such as tapes, DVDs, and CDs are not the only choice for backing up your data. You may find it useful to install a second hard drive in your system and use that drive for backups. This has several advantages over other backup media:
Data can be backed up quickly and throughout the day; thus, backed-up data will be more current in the event of a crash.
There’s no medium to load. Data can be located and recovered more quickly.
You can configure the second disk to be a virtual clone of the first one. If the first disk crashes, you can simply boot off of the second disk rather than installing new hardware. With disk mirroring software, this process can even be automated.
With new, cost-effective removable hard drives (including those connected via USB and Firewire), you have the convenience of removable media with what was once usually thought of as non-removable media.
There are, however, a few disadvantages to backing up to a hard drive. For example, the hard drive backup method is not well suited to keeping historical archives of the many revisions of your files, because the hard drive will eventually fill up. This problem can be reduced substantially, however, by using rsync snapshots, which store changes that are applied to modified files.
The simplest form of second-hard-drive backup is to simply copy important files to the other drive using the cp or tar command. The most sophisticated method is to provide fault- tolerant disk mirroring using RAID software. If you don't need the high level of protection from data loss that RAID disk mirroring provides, but you still like the idea of having a complete duplicate of your data on-hand and ready for use, there is an alternative—mirrordir.
The mirrordir package is a way of doing hard-drive mirroring. Mirrordir is a powerful tool that enables you to make and maintain an exact copy of a hierarchy of directories. You can find the official mirrordir Web site at http://mirrordir.sourceforge.net. To download the package, click the Download RPM button.
After downloading the file, install it in the same way you install any rpm. For example, if you have downloaded the rpm file to /tmp, you can type:
# rpm -Uhv /tmp/mirrordir*rpm
Now that you have mirrordir installed, you can use it to clone a directory. Suppose you have a second hard drive with a partition large enough to hold a copy of your /home partition. Your first step is to create a directory to mount the partition on, and then mount it. Log in as root and type the following:
# mkdir -p /mirror/home # mount /dev/hdb5 /mirror/home
In this example, you are mounting the fifth partition (5) of the second hard drive (hdb), hence the device name /dev/hdb5. The b refers to the second disk, and the 5 refers to the partition number. You may use a different drive or partition. If so, adjust the device name accordingly. Assuming the mount command successfully mounted the drive, you are ready to copy your /home partition. Enter the following command:
# mirrordir /home /mirror/home
This command causes the contents of the /home directory to be exactly duplicated in the /mirror/home directory.
It is very important that you get the order of these parameters correct. If you reverse them, the entire contents of your /home directory will be deleted! You will, in essence, be copying an empty directory back to your /home directory.
You now have a backup of your entire /home partition. You can run mirrordir again in the future, and it will again make the /mirror/home directory an exact duplicate of /home. It will copy to /mirror/home only those files that have been added or modified since the previous run of mirrordir. Also, it will delete any files from /mirror/home that are no longer on /home. Thus, the mirror is kept current without copying the entire /home partition each time. If the disk with the /home partition should ever crash, you can replace the disk and then copy the file system back by issuing the mirrordir command with the parameters reversed:
# mirrordir /mirror/home /home
Furthermore, you can be up and running even faster by simply turning the mirrored partition into the actual home partition. Just edit the /etc/fstab file and change the device name for /home so that it matches the mirrored directory. Now unmount and remount the /home partition (or reboot the computer), and the mirrored directory will be mounted to /home. Your users will never be the wiser. You may even consider mirroring every partition on your main disk. Then, even a complete disk crash can be recovered from quickly. Simply turn the secondary disk into the primary disk, and you are up and running again.
To automate the mirroring process, I suggest creating a small script that mounts the mirror partition, runs the mirrordir command, and then unmounts the partition. Leaving the partition unmounted when not in use reduces the chance of data being accidentally deleted from it. Create a script named mirror.sh and place it in the /usr/local/sbin directory. Something similar to the following should do the trick:
#!/bin/sh # # mirror.sh: Mirror the /home partition to a second hard drive # /bin/mount /dev/hdb5 /mirror/home mirrordir /home /mirror/home /bin/umount /mirror/home
Now tell cron to periodically run this script. Invoke the cron command with the -e option. This brings up an editor containing root’s cron jobs. Add a line similar to the following at the end of the list:
0 * * * * /usr/local/sbin/mirror.sh
Save and exit the editor. This cron entry causes the mirror.sh script to run once an hour. If you decide to mirror other partitions, you can do it quite easily by creating appropriate mount points in /mirror and then adding matching sections to the mirror.sh script.
A downside of mirrordir is that if you accidentally remove or damage data in your home directory in the above example, you will have a copy of the damaged data if you don't catch the problem before the next mirrordir runs within the hour.