Cloning Hard Drives – Remotely

Posted on

Problem :

I have two servers, A and B.
I would like to clone server A to B. A is running Linux, and they are both remote, I don’t have a physical access to them

What is the best way to approach this?

Solution :

There are al least three different ways to do this.

1 scripted setups.

This option is mostly useful in large corporations with proper documentation and likely expands into setups with PXE booting and puppet or similar setups.

Basically you repeat the same steps which you used to configure server A on server B.

This works very well if your setup is big enough (e.g. you are running 100 servers and want to spin up server 101. PXE boot, tell your puppet config that it needs to be a server of $type and come back an hour later. Done!).

A lot of work to set up, but perfect afterwards. And with all the setup work probably not worth it for copying a setup once.

2 filesystem copy.

You can copy all files from server A to server B, preserving their rights and users. rsync is probably the best tool for this.

For this you want to prepare the target server with a similar setup (e.g. copy the partition setup) and then rsync most files over. The tricky part here is that you do not want to rsync some files (e.g. skip /proc and /dev) and that you need to have something to receive the rsync files on server B.

For the last you probably want to boot server B from a different disk, or use the ILO/DRAC/.. to boot from an liveCD iso image on your desktop, leaving its disk bare and ready to receive the rsync files.

3 Copy disk.

This is diffent from copying the filesystem. It is much easier and probably a lot faster, but raw disk access (e.g. with dd) ignores all filesystem changes. This means that you really do not want to copy a running filesystem.

To help you understand this easier, imaging a book. This book has an index and four stories. The rest of the book is blank and this is also nicely indicated in the books index.

Now we start copying. We copy the index. We copy story 1, we copy…
** hold briefly, let me add a new story here and change the index**
We copy stopry 2, 3, 4 and 5…

We now have aq book with an index showing four stories (not five, since we copied that before writing story 5, and five stories)… a corrupt book.

Something similar can (and will) happen if you use raw disk access to copy a running system. So before using a raw disk copy make sure that the OS is off-line. Either boot something different (e.g. from a second disk, from a local CD, or via the network (PXE, DRAC, …) and then copy.

Details on how to do this are already in plenty posts here on [su].

Cloning remote Linux machines

The bash script automates the process of cloning disks of remote Linux machines. It uses the dd command together with ssh and gzip commands to copy and compress remote disks and send them on the fly to a local disk over an SSH connection.

Examples of cloning a remote disk to a local disk using written by Radovan Brezula, who wrote

Example of cloning a local disk to a remote disk using dd

dd if=/path/to/disk/image ibs=4096 conv=notrunc,noerror | (ssh dd of=/remote-archive/test/remote-archive-test.img obs=4096) 
  • ibs=4096 read up to 4096 bytes at a time

  • conv=notrunc,noerror do not truncate the output file and continue after read errors

  • obs=4096 write 4096 bytes at a time

I believe rsync is the solution here. I am not sure whether all files will be writable during an rsync session, though…

Have you concidered making a dd clone of the partitions on server A and transferring it to server B? It sounds a bit wild, but perhaps as a last resort?

For disk imaging, I like clonezilla, but you need physical access to boot from the disk to make it work. Honestly, I don’t think you can do a completely lights-out remote full-system copy. rsync will only get you so far. If both have full systems already installed, and they are otherwise identical, then you can get the same apps installed on host B as are on host A. Then it’s just a matter of copying the necessary config files from A to B. You don’t necessarily need to copy everything.

Leave a Reply

Your email address will not be published. Required fields are marked *