How-To: Clone VPS to VPS

Posted by Pascal Querner on 05-06-19 14:53

Reading time (calculated automatically): 5 minutes, 0 seconds

I recently moved my server from provider A to provider B and was looking for a way to completely move my stuff without recreating everything and installing everything from scratch at the new provider. Sure, I have many stuff running in docker and its a no brainer. But there is still many things I haven't dockerized.

Backups

Do yourself a favor and backup everything if the data is keen to you. If its data you can loose and won't hurt you, sure take the risk of not having a backup. Your choice.

How to backup

I did my backups two ways. First, I am lucky enough that my provider allows snapshots. Snapshots are used on VPS which are always emulated (via KVM or OpenVZ or what not) and therefore can be saved at the exact state that they currently are. If you do it on live mode (while the server is running) those snapshots are called Online Snapshots. There is also Offline Snapshots which is the opposite. The server is turned off before (shutdown) and then a snapshot is created. The difference between the two is, that the online snapshot might snapshot files in the middle of moving, which could be dangerous if the server is moved or restored back to this state. This cannot happen of course with a offline server.

So I'd go with offline snapshots as they are cleaner.

Then I cloned my VPS harddrive (SSD storage) via Clonezilla to my local NAS (via SSH). Basically I followed this nice guide: https://www.raymond.cc/blog/guide-for-making-full-windows-backup-to-network-share-using-clonezilla/

Of course I checked if I could restore the two before taking the next step. Always check if your created backups are working. If they are not restoreable, then they are worthless.

Copy VPS to VPS

I tried many solutions to copy data from VPS A to VPS B. From here on now A is the source host and B is the receiver host. ("A" copies everything to "B")

I've tried Clonezilla even, which did a fantastic job at creating the (backup) image. However, I was unable to clone image to disk live over the internet via SSH.
In the end I've used a simple "dd" command which would bring the entire disk from VPS A to VPS B.

Systemrescue

Before you attempt this, you should properly shutdown your server, then bring it into rescue mode. Both of my providers offer solutions to provide a custom iso image to boot from. I've chosen Systemrescue CD since it brings all the neccesary tools (SSH, dd, gparted) needed for the next steps. You can get it here: http://www.system-rescue-cd.org/

Once its booted you should see a shell. Login, if you need to. Otherwise it should autologin to the root user. You may change the keyboard layout with the command

keymap

This should open a TUI (text-based user interface) where you can change the loaded keyboard layout.

Issueing the command

startx

will start the GUI.

Noteable tools while in GUI are: terminal, gparted

dd

This command assumes VPS B has a equal or bigger harddisk size available. If VPS B's harddisk is smaller, you need to shrink the harddisk first on VPS A's end. Luckily for me my target harddisk was bigger so I didnt have to go through this trouble. Here comes the trick. Start both VPS into rescuemode, then issue the following command on VPS A.

dd if=/dev/sda | gzip | ssh root@target 'gzip -d | dd of=/dev/sda'  

This will

  1. Read everything from /dev/sda (main disk on this server, with all partitions)
  2. gzip the data (shrinks the size to pass to VPS B)
  3. opens a SSH connection at VPS B (change the target IP address)
  4. extracts the given gzip stream and
  5. pipes everything to the main local harddisk (in my case again /dev/sda) (with partitions)

This might take a while, depending on your disk size of VPS A of course.

After its done you then might need to change the partition size. Thats done with the tool "gparted" which is available on the SystemrescueCD image. Launch the tool in GUI mode and select the partition with the system on it. Rightclick it to open the context menu and choose "Resize/Move". Move the slider of that partition to the right to grab all that unalloced space. Begin the resizing process (shouldnt take that long) by click the green "OK" button.

When that process is finished, you are almost done.

Change IP, hostname, ...

You may now start the server and boot from the harddisk (so detach the SystemrescueCD first). You should notice that some things might not work.

These can be for example

  • No network available
  • Hostname is wrong
  • Service XYZ not working

This might be tricky but I will try and cover the basics.

No network available

On VPS A there might be DHCP enabled or the network adapter was names "eth0" while on VPS B its something else. Its best to lookup the adapter name and configure a static IP in the file

/etc/network/interface

to assure network is running. I've only issued IPv4 so far, so here is my config as an example:

auto eth0
allow-hotplug eth0
iface eth0 inet static
address your.external.server.ip
netmask 255.255.255.0
gateway 192.51.100.1

When you're done, issue

dhclient -r

to release the used network config and

dhclient

to let it reconfigure the network.

Test the network by ie. pinging google.com

Hostname

In the file

/etc/hostname

you may change your hostname. The new hostname is read at boot so to get the new hostname, you must reboot your server.

Services

Froxlor

I am using Froxlor https://www.froxlor.org/ to manage stuff on my server. The server IP is saved into a database and you have to manually change it to the new (external) IP address. Luckily there is a script available.

In version >= 0.9.37, < 0.10 this can be done by calling the script Froxlor/install/scripts/switch-server-ip.php

Other services

Grep or try to remember where you hardcoded your old servers IP address and switch is manually.

Done

After thats all done, consider yourself brave enough to do all this just because you are too lazy to start fresh on a new server. Like myself. :)

Comment if you want: