Cloning a Linux Hard Drive

In this TechBuilder Recipe, I&ll show you how to set up a hard drive mirror clone--as opposed to a new drive mirror (or what&s known as a “bare metal restore”)--for Linux-based systems.

You don't have to be a Linux expert to get these procedures to work. I've tried to keep them simple and specific. Follow them exactly, and you should be able to save yourself days, even weeks, of work. Fixing it yourself can also spare you from the expense of hiring a specialized hard-drive recovery shop, some of which charge thousands of dollars for their services.

In an earlier TechBuilder Recipe, Backing Up Linux Workstations Without Tears, I detailed the process of creating a new drive mirror and set up a backup system drive mirror and DVD-R archives. But that procedure took nearly a week of experimenting to get exactly right. The procedures in this Recipe will enable you to skip that painful experimentation period and get right to drive cloning.

With a hard-drive mirror in place, recovery from a drive crash is as simple as unbolting a hard drive from a mobile rack and replacing the failed hard drive along with it. The whole process should take only 15 minutes. Remember, the completeness of a disk recovery always depends on how recent your last backup is.

id
unit-1659132512259
type
Sponsored post

For this Recipe, most of the processes will be executed within a terminal at the command line. What you see in monospace type will refer to either the processes or programs, or the files or prompts you will actually see (or enter) within a terminal in monospace font itself. Specific command-line instructions can be cut and pasted directly from the article into a terminal window. This reduces the odds of a system builder making a mess by incorrectly entering a command-line switch.

Ingredients

For the process of setting up any hard drive mirror, you'll need a mobile rack. I've been using Newegg&s SANMAX PMD-96I, and it&s worked very well. This particular mobile hard-drive rack uses a standard PCB pin-socket connector.

By contrast, most lower-cost mobile racks use the Centronics parallel connector. I have recommended the Centronics connector in the past, mainly because it&s inexpensive. Unfortunately, the Centronics parallel connector is intended for relatively low-speed data transfer (megabyte/second range), such as those seen from a computer to a parallel printer. While that&s fine for a parallel printer, it's definitely not fine for the data-transfer requirements of modern, high-speed IDE drives. This problem is exacerbated if there&s any significant wear generated on the Centronics connector. On the Newegg rack, the DIN PCB pin-socket connector accurately transfers data at full IDE bus speed, drastically reducing error rates.

Also, if you have the usual flat-ribbon IDE cables installed, consider replacing them with round cables with shielding. The price is of round cables is now comparable to that for flat ones, and the round cables do a better job of handling high-speed drive data transfers. They&re also easier to work with inside the computer case.

You'll also need either a working drive to copy or a full archival image backup set on DVD-R.

My earlier Linux backup Recipe included three scripts: a dd drive-cloning script; rsync backup script (intended to provide a simple differential backup taking a few minutes to run); and dar script, which creates “compressed-like” files suitable for archiving to a DVD-R backup set. Unfortunately, while the dd script worked well at first, it has since started giving me increasing problems. Finally, it became unusable. As there is no replacement dd script in my earlier Recipe, instead, use the rsync_backup.sh script below for direct drive-to-drive copies.

Setting Up a Drive Mirror in 12 Steps

To set up a mirror which should clone any drive in workable condition:

rsync_backup.sh
sh rsync_backup.sh
sh rsync_backup.sh
/mnt/temp  
/mnt/temp1

Below, you&ll find the modified rsync and dar scripts for you to cut and paste, which will automatically mount and back up the boot partition. Be aware that the very first boot-up on a new drive will be slower than you&re probably used to, and it might even hang for a minute or two. This does not indicate a problem unless the boot sequence completely shuts down instead of resuming.

Also, if you do have a boot sector modified from the default, run the modified rsync backup script specified in the first item in the list as instructed below as soon as possible. That way, the boot sector information will exist in the backup for you to copy. dar restore from a DVD-R backup set

This following section assumes that you&ve backed up to DVD-R using dar in the way detailed in my previous Linux backup article, which I&ve been citing above.

You can also install to a KDE desktop during Linux installation. To accomplish this, use the file manager with root access found at START > System Tools > More System Tools >”Superuser File Manager to do most of the file/directory/zipfile operations (described below). I provided the command-line instructions because they work with almost any system configuration.

I did the system restore (as described below) using the gnome desktop. If this doesn&t work the first time, you can try the "failsafe terminal session" that you access at the login prompt under Sessions that does not include a Desktop.

After installing a minimal FC2 installation:

Restoring the Boot Partition

As way of background, the boot partition is the portion of the primary hard drive that stores the information required for a Linux workstation to boot Linux. The use of “partition” here actually means the same as it does on a Windows system, though the labeling is a bit different. On Windows, it&s likely to be C: and a logical D: drive. On a Linux box, it&s likely to be hda1 and hda2.

If you&ve never changed the boot setup on your regular desktop, skip to the next section, “Main Partition Restore.” Otherwise:

Copy the boot directory.

mount /mnt/cdrom

Note: adding mount point directory is not necessary.

mkdir /mnt/temp mount -t ext3 /dev/hdb1 /mnt/temp cd / mkdir bootzip

(bootzip is a temporary directory to unzip the boot.zip directory to. The actual boot directory is in /mnt/temp inside the boot.zip file.)

cd bootzip cd mnt cd temp

Do a

ls

and you'll see the original boot directory. Now, copy the boot files into the boot partition:

cp -af --reply=yes * /mnt/temp

The files are now copied. If you want to check, do this:

cd /mnt/temp ls

Now leave the /mnt/temp directory so you can unmount the boot directory:

cd / umount /dev/hdb1

In terminal mode, the lines in the command history can be read back via the up and down arrows. Any command you find that way can be entered with the Enter key. This is good for dealing with repetitive commands, such as the ones described in the next section. Main Partition Restore

The main partition is where the Linux applications and user data usually go. To get started:

cd /mnt/cdrom cp dar_static /etc/

Next, highlight and copy the base name of the dar backup:

2005-08-13__data.1.dar2005-08-13__data

is the base name one gives dar to identify the backup set. See the dar command line below to show how it is used in this context. Note: To start a restore, dar must inspect the first and the last files in the backup set. Next, insert the first disk of the backup set if it isn't already in the drive.

mount /mnt/cdrom /etc/dar_static -x /mnt/cdrom/2005-08-13__data -b -w

You should see the following response:

[root@localhost /]# /etc/dar_static -x /mnt/cdrom/2005-08-13__data -b -w Reading config file: /root/.darrc The last file of the set is not present in /mnt/cdrom , please provide it. [return = OK | esc = cancel]

Hit control-Z to suspend dar:

umount /mnt/cdrom

Eject first disk, put in the LAST disk of the backup set:

mount /mmt/cdrom fg

Hit ENTER a second time. The dar program expects it and will respond with:

Continuing...

/mnt/cdrom/2005-08-13__data.1.dar is required for further operation, please provide the file. [return = OK | esc = cancel]

Enter control-Z:

umount /mnt/cdrom

Reinsert the first disk.

mount /mnt/cdrom

fg

Note that fg is the “resume suspended operation” command.

Hit ENTER a second time. Wait until dar demands a file that isn't in the first DVD-R (prompt similar to above). Then enter control-Z.

umount /mnt/cdrom

Now, insert the next disk.

mount /mnt/cdrom fg

Hit ENTER a second time. Then repeat the process from control-Z to "hit ENTER a second time" process until your backup set is complete. Don't be surprised if your current working desktop changes in the process; the file icons that were on your desktop before (that aren't part of the FC2 installation) will be deposited on the default desktop during installation. Once you see a display similar to the following, the root prompt will reappear in a minute or two.

575964 file(s) restored 0 file(s) not restored (not saved in archive) 0 file(s) ignored (excluded by filters) 0 file(s) less recent than the one on filesystem 0 file(s) failed to restore (filesystem error) 0 file(s) deleted

Total number of file considered: 575964

Once the root prompt reappears, you&re done! Go ahead and reboot, and your regular Desktop should reappear.

Backup Scripts

The general nature of the following scripts are discussed in the “Ingredients” section above. The scripts themselves are internally documented in the # lines.

Modified rsync drive-to-drive backup script:

Note: Before running the script, create the following directories in /mnt/ as follows:

cd mnt mkdir temp mkdir temp1

Note: The script will blow up if the directories are not added.

=============== cut

#!/bin/sh

#/usr/local/bin/rsync_backup.sh

echo "Mounting backup drive."

mount -t ext3 /dev/hdd2 /mnt/linux_backup

#change ext3 to match the backup drive filesystem

#if not ext3

#change /dev/hdd2 to match your actual backup drive

#and/or partition assignment if different

#change /mnt/linux_backup to match your actual mount

#point (directory)

#if your boot drive is not /dev/hdb1, change script accordingly.

#delete or comment out mount line above if backup drive

#permanently mounted.

echo "Executing incremental backup script"

echo "Backing up /bin"

rsync -aHv --delete-after --

progress /bin/ /mnt/linux_backup/bin/

echo "Backing up /etc"

rsync -aHv --delete-after --

progress /etc/ /mnt/linux_backup/etc/

echo "Backing up /home"

rsync -aHv --delete-after --

progress /home/ /mnt/linux_backup/home/

echo "Backing up /lib"

rsync -aHv --delete-after --

progress /lib/ /mnt/linux_backup/lib/

echo "Backing up /misc"

rsync -aHv --delete-after --

progress /misc/ /mnt/linux_backup/misc/

echo "Backing up /opt"

rsync -aHv --delete-after --

progress /opt/ /mnt/linux_backup/opt/

echo "Backing up /root"

rsync -aHv --delete-after --

progress /root/ /mnt/linux_backup/root/

echo "Backing up /sbin"

rsync -aHv --delete-after --

progress /sbin/ /mnt/linux_backup/sbin/

echo "Backing up /usr"

rsync -aHv --exclude *.dar --delete-after --

progress /usr/ /mnt/linux_backup/usr/

echo "Backing up /var"

rsync -aHv --delete-after --

progress /var/ /mnt/linux_backup/var/

#e2fsck /dev/hdb2

#the -c, (--checksum) option provides better security, but takes

#several times as much time to run - recommended if the mirror drive

#is your ONLY backup or if you don't have a lot to back up.

#see rsync man file for more information (type man rsync from the

#command line

echo "mounting /boot partition"

mount -t ext3 /dev/hdb1 /mnt/temp

mount -t ext3 /dev/hdd1 /mnt/temp1

echo "backing up /boot partition"

rsync -aHv --delete-after --

progress /mnt/temp/* /mnt/temp1

umount /dev/hdb1

umount /dev/hdd1

echo "backup complete"

=============== paste

Next, here's the modified dar backup script:

=============== cut

#!/bin/sh

#/usr/local/bin/dar_backup.sh

#if your boot drive is not /dev/hdb1, change script accordingly.

echo "Executing archival backup script"

echo "mounting /boot partition"

mount -t ext3 /dev/hdb1 /mnt/temp

zip -ry /usr/local/backups/boot.zip /mnt/temp/*

umount /dev/hdb1

echo "unmounting /boot partition"

cd /usr/local/backups

dar -z5 -s 1450M -D -R / -c `date \

-I`__data -Z "*.gz" -Z "*.bz2" -Z "*.zip" -Z "*.png" -P tmp \

-P dev -P proc -P mnt -P sys -P selinux \

-P usr/local/backups -P home/alizard/.opera/cache4

=============== paste

It&s a good idea to save supplemental restore information and a copy of the backup program with every backup.

Once you have put these supplemental files in the K3b (or the DVD burner of your choice) Project files, they&ll be automatically included in future backup sets. These files don&t take up much room on a DVD: Each full disk will include three 1400-MB dar backup files, leaving about 100 megs left over. If files are duplicated, there&s less chance of a DVD defect making the file you need unusable.

Below is a list of files to put on every disk in the backup set as well as the dar backup archive files themselves:

/usr/src/redhat/BUILD/dar-2.1.2/doc
locate dar

Now, following is the content of the backup_readme.txt file included in the above list. Cut and paste it into a text editor, and put it somewhere convenient so you can find it when you put together your DVD backup files:

=============== cut

Note: the /dev/hdb1 /boot partition files are backed up in mnt/temp inside bootzip.zip

Make sure the boot sector is on the hdb MBR.

/etc/dar_static -x /mnt/cdrom/2005-08-13__data -b –w

When dar calls for more disks, stop with control-z, change DVDs, restart with fg [ENTER][ENTER]

=============== paste

Congratulations, you now have the tool kit you need to protect your customer&s data on Linux workstations in case of an emergency. Let's hope you never need it!

A. Lizard is an Internet consultant in the San Francisco Bay Area. He has been writing for technology magazines and Web sites since 1987.