How to clone a Linux server (either for disaster recovery/ bare metal restore or to stand up a virtual instance)
Posted by rbTech Staff on 08 March 2013 11:25 AM

Through this document, I'll use the terms target and production to describe the new clone and existing servers, respectively.

To clone a physical instance of CentOS, the biggest trick is the fact that the filesystem layout will not be the same on the target system.  This means that you'll clone your files over but then when you reboot the system will kernel panic and crash on your.

That said, the easiest way to get a working copy of a machine is to prep the target server (be it a virtual or physical instance) by spinning an installation CD that is the same version *or older* than the production server.  Partition your disks and get a working, basic system.

Reboot the target into Linux rescue mode.  Mount your filesystems and set up your network adapter so you can communicate with the machine you're cloning (production).  Mount your filesystems at /mnt/sysimage (the rescue image will prompt you for this and will do it automatically).

cd /mnt/sysimage

chroot . (this will put you 'into' the new machine).

perform ssh key exchange between the target and production servers.

Perform a full rsync of the entire filesystem from the production machine:
rsync -av --delete --exclude /proc --exclude /dev --exclude /sys --exlude /etc/fstab --exclude /root/.ssh --exclude /var/named/chroot/proc $productionserverIP:/ /

You'll notice I exlcluded the ssh key folder and the /etc/fstab from the rsync...

Once the rsync is complete, you'll need to modify your boot loader:  In /boot/grub/grub.conf change the filesystems (often /dev/mapper/VolGroup00/LogVol00 or similar) to be reflective of the target server's filesystem layout.  You can cheat and do this at the grub command line as well!

Delete the /boot/initrd from the old server, and re-run mkinitrd:
rm -f /boot/initrd-2.6.18-348.2.1.el5.img
mkinitrd /boot/initrd-2.6.18-348.2.1.el5.img 2.6.18-348.2.1.el5 (change the versions to be correct for your system of course) 

Before you reboot one more time, you *must* update your MBR:
grub-install --recheck /dev/sda (or your boot device).

If you've done it all right, you'll reboot and the server will stand up on the new filesystem!

(9 vote(s))
Not helpful

Comments (0)
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).