Cloning A Linux (maybe others) System

I used to use partimage to backup and clone machines I had setup, however, this requires the disks be the exact same size and you can't resize partitions (easily at least). Now I've discovered the nice rsync tool combined with the System Rescue CD cloning a drive can be quite easy including moving to smaller/bigger partitions.
First start by booting both computers with the Rescue CD. It's easiest to connect them together with an Ethernet crossover cable, but a network switch will work too. Set both of their ip addresses (make sure they're on the same subnet, but different values):

DESTINATION COMPUTER:

# ifconfig eth0 10.10.10.1

SOURCE COMPUTER:

# ifconfig eth0 10.10.10.2

Next you need to mount the source partition. If you are not using RAID arrays ignore all the commands that have to to with mdadm and replace /dev/md0 with normal SCSI or IDE devices (i.e. /dev/sda1, /dev/hda1, ...). If they are in a raid configuration and the RAID drives weren't automatically loaded, then use the following for each raid device you have on the source computer. (Replace sda1 with one of the partitions from that RAID device and md0 with the raid device you're loading):

SOURCE COMPUTER:

# mdadm -A -ayes -u`mdadm -E /dev/sda1 | grep UUID | awk '{print $3}'` /dev/md0
# mdadm -A -ayes -u`mdadm -E /dev/sda2 | grep UUID | awk '{print $3}'` /dev/md1
# mdadm -A -ayes -u`mdadm -E /dev/sda3 | grep UUID | awk '{print $3}'` /dev/md2

On the destination computer you'll need to create the RAID array. The parameters below are or a RAID level 1 with 2 partitions from separate drives. If you've already configured the RAID devices, but it's not loaded you can use the same method from the source computer. I normally use the Rescue CD to partition and create the RAID array on the destination machine:

DESTINATION COMPUTER:

# mdadm -C /dev/md0 -ayes -l1 -n2 /dev/sda1 /dev/sdb1
# mdadm -C /dev/md1 -ayes -l1 -n2 /dev/sda2 /dev/sdb2
# mdadm -C /dev/md2 -ayes -l1 -n2 /dev/sda3 /dev/sdb3

Now you'll need to mount each partion you want to copy over in the same locations as they are in the running system under the /mnt/chroot directory (we'll create it). In the above example /dev/md1 is my swap space, so I won't be copying that over.

SOURCE COMPUTER:

# mkdir /mnt/chroot
# mount /dev/md2 /mnt/chroot
# mount /dev/md0 /mnt/chroot/boot

DESTINATION COMPUTER:

# mkdir /mnt/chroot
# mount /dev/md2 /mnt/chroot
# mkdir /mnt/chroot/boot
# mount /dev/md0 /mnt/chroot/boot

Now for rsync and copying the files over. First we'll need to set a root password on the source computer so that we can access it remotely using secure shell.

SOURCE COMPUTER:

# su -
# passwd
(enter password twice)

And now we'll copy the contents (preserving symbolic links, permissions, etc) for the entire source system to the destination computer.

DESTINATION COMPUTER:

# rsync -avu -e ssh root@10.10.10.2:/ /mnt/chroot
(enter root password you setup on source computer)

Now you have a system with all the partitions mirrored, but it will not boot without GRUB installed and for RAID it will be necessary to change the UUID's for the RAID devices on the cloned system. You'll need to modify the below for your system's partition scheme. For me md0 is my boot partition and md2 is my root partition. We're going to chroot into the cloned system and install grub.

DESTINATION COMPUTER:

# mount -t proc none /mnt/chroot/proc
# mount -o bind /dev /mnt/chroot/dev
# chroot /mnt/chroot /bin/bash
(you're now running inside you're cloned system)
# grub
grub> root (hd0,0)
grub> setup (hd0)
grub> root (hd1,0)
grub> setup (hd1)
grub> quit

Now we need to modify the RAID configuration file (this is for RHEL or CentOS). If you're using a different distribution you'll have to figure out how they build the RAID array. CentOS uses the file /etc/mdadm.conf to build the array based on the UUID of the partitions (they match for drives belonging to the same array). You'll have to edit the file (be careful the lines are not in order, i.e. /dev/md0 is not always the first line in the file) to change the UUID for each RAID device. To find the UUID of the three drives in this example run:

DESTINATION COMPUTER:

# mdadm -E /dev/sda1 | grep UUID | awk '{print $3}'
# mdadm -E /dev/sda2 | grep UUID | awk '{print $3}'
# mdadm -E /dev/sda3 | grep UUID | awk '{print $3}'

After you change all those UUID's in /etc/mdadm.conf you're ready to exit the chroot'd environment and unmount all the partitions and boot your newly cloned linux system.

DESTINATION COMPUTER:

# exit
(you're now back on the Rescue CD)
# umount /mnt/chroot/proc
# umount /mnt/chroot/dev
# umount /mnt/chroot/boot
# umount /mnt/chroot

Now everything is cloned and you can reboot the destination computer and all should work fine.