Replicating running Linux machines

Replicating running Linux machines

Problem

Digital Ocean products and services are awesome. Support is always trying to be helpful and most of the time I don't have any problem with them.

The first time I encountered their strange decision was when my client asked me to decrease the expenses of our infrastructure. We have several running Linux droplets to experiment with. At some point bill for them got higher than the client was happy to pay. He asked me to check if we use all resources and how to optimize them. Of course, such machines were found, otherwise, you wouldn't read this note. I was surprised when I found out that you cannot decrease droplet resources with Digital Ocean, only increase them. I am not sure if there is any technical reason for them to do so. From a Sales point of view, it seems pretty reasonable. I had to find a way to replicate running machines on my own, ideally without any downtime.

digital-ocean-resizing-down.jpg

Solution

The steps described below were tested on Ubuntu 18 LTS, but they must work with most Linux-based distributions.

Set up a new machine and make sure ssh and rsync are installed.

Try to stop all running services which are currently not used. For example, if you are doing it remotely like me you mustn't disable sshd. Otherwise, most of them can be disabled.

Next, we need to create a file containing excluded directories. Rsync doesn't have to copy all files and directories since some of them are hardware-specific, auto-generated, or default. Let's call the file replication_exclude.

/boot/
/lib/modules
/etc/modules
/etc/lilo.conf
/etc/fstab
/etc/mtab
/proc
/dev
lost+found/
/var/log
/etc/network/interfaces

And then we can run the sync command:

rsync -e ssh -avz --exclude-from=/root/replication_exclude username@old_server:/ /

Depending on the server setup you are trying to clone you may get sync errors on specific directories and files. You can add such entries in the replication_exclude file and rerun rsync.

When it's done you can turn off the old server and enjoy your new one.