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?


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 backup-images-1.0.sh 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 backup-images-1.0.sh written by Radovan Brezula, who wrote backup-images-1.0.sh.

  • http://brezular.com/2015/06/04/cloning-remote-linux-machines/

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

dd if=/path/to/disk/image ibs=4096 conv=notrunc,noerror | (ssh 132.183.12.128 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