Migrating a system to Hetzner cloud using REAR and kexec
I needed to migrate an existing system to an Hetzner cloud VPS. While it is possible to attach KVM consoles and custom ISO images to dedicated servers, i didn’t find any way to do so with regular cloud instances.
For system migrations i usually use REAR, which has never failed me. (and also has saved my ass during recovery multiple times). It’s an awesome utility!
It’s possible to do this using the Hetzner recovery console too, but using REAR is very convenient here, because it handles things like re-creating the partition layout and network settings automatically!
The steps are:
- Create bootable REAR rescue image on the source system.
- Register a target system in Hetzner Cloud with at least the same disk size as the source system.
- Boot the REAR image’s initrd and kernel from the running VPS system using kexec
- Make the REAR recovery console accessible via SSH (or use Hetzners console).
- Let REAR do its magic and re-partition the system.
- Restore the system data to the freshly partitioned disks
- Let REAR handle the bootloader and network re-configuration.
To create a rescue image on the source system:
My source system had a 128 GB disk, so i registered an instance on Hetzner cloud with greater disk size to make things easier:
Now copy the ISO image to the newly created instance and extract its data:
Install kexec if not installed already:
Note down the current gateway configuration, this is required later on to make the REAR recovery console reachable via SSH:
Reboot the running VPS instance into the REAR recovery image using somewhat the same kernel cmdline:
Now watch the system on the Console booting into the REAR system:
Login the recovery console (root without password) and fix its default route to make it reachable:
The network configuration might differ, the source system in this example used DHCP, as the target does. If REAR detects changed static network configuration it guides you through the setup pretty nicely.
Login via SSH (REAR will store your ssh public keys in the image) and start the recovery process, follow the steps as suggested by REAR:
This step re-recreates your original disk layout and mounts it to /mnt/local/ (this example uses a pretty lame layout, but usually REAR will handle things like lvm/btrfs just nicely):
Now clone your source systems data to /mnt/local/ with whatever utility you like to use and exit the recovery step. After confirming everything went well, REAR will setup the bootloader (and all other config details like fstab entries and adjusted network configuration) for you as required:
Now reboot the recovery console and watch it boot into your target systems configuration:
Being able to use this procedure for complete disaster recovery within Hetzner cloud VPS (using off-site backups) gives me a better feeling, too.