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.
There are, however, a few disadvantages to backing up to a hard drive:
This method is not well suited to keeping historical archives of the many revisions of your files because the hard drive will eventually fill up.
The backups cannot be stored offsite, so if the server is destroyed, your data is lost.
Because of these disadvantages, you should probably not use a hard drive as your only backup medium. Instead, combine it with some sort of removable media backup such as a tape drive. Hard-drive mirroring can provide a snapshot backup that will get you up and running again quickly, while removable backups can provide longer-term storage of data and protection from more catastrophic failures. Hard-drive mirroring is discussed in the next section.
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.
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 its official Web site at http://mirrordir.sourceforge.net. It can be downloaded by selecting the Download RPM button from that site.
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 -i /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.