Clone Ubuntu Server to new location

Imagine you have to move your server to another provider. It is a time-consuming task to set up a new server, install all the applications, databases, and so on… and then configure it all the same way as before.

Of course you would copy a lot of that from the old to the new server. You could even make an image from the old server and copy it to the new. But wait, is it that simple though? Well, considering a few things it isn’t all that hard. I didn’t fetch an image, but made a backup from the old server using rdiff-backup. Then I set up my new server with the same OS as the old server (Ubuntu 14.04). Then I would adapt a few things to the configs of the backup and play the backup back into the new server. Voil√†.

Remote Password-less Root Access

At first you need remote root access. For this to work you usually have to modify your ssh daemon config found at /etc/ssh/sshd_config

If you can access your remote server with ssh root@<your server> you’re good to go.

rdiff-backup

Install rdiff-backup on both machines (server and your local machine).

sudo apt-get install rdiff-backup

Then we backup our remote server. Create a directory where you can put the backup and ensure you have enough space left. Run this as the root user:

rdiff-backup --exclude '/tmp/*' --exclude '/proc/*' --exclude '/sys/*' --exclude '/media/*/*' <your server>::/ <your local backup folder>

Now we have a backup of our server.

Preparing the new server

You probably have some kind of control panel. Install the same OS as you had on the old server.

We then again need remote root access to the new server.

Restore

On your local machine as root user:

rdiff-backup --force -r now --exclude '/boot' --exclude '/dev' --exclude '/tmp' --exclude '/proc' --exclude '/sys' --exclude '/media' --exclude '/etc' <your local backup folder> <your new server>::/

You can see I excluded the etc directory to not overwrite some important configuration files that need to stay the same on the new server.

On your new server make a new temporary directory anywhere. From your local machine as root we now copy the old configuration files to the temporary directory. I use rsync for this:

rsync -av /etc <your new server>:<your tmp folder>

Then go back to the shell of your new server and copy the file system and network configuration back into the configuration from your old server.
cp -a /etc/network <tmp-folder>/etc/ # network configuration
cp -a /etc/fstab <tmp-folder>/etc/ # file system table
cp -a /etc/resolvconf <tmp-folder>/etc/ # nameserver information

Finally overwrite the etc folder from your new server:

cp -a <tmp-folder>/etc /

Everything should be up and running by now.

Flattr this!