A few days ago I wanted to Migrate an Ubuntu VPS from one provider to another. I tried to take a snapshot and try to migrate, but it did not work. The VPS had few random services, so migrating services one by one was not easy. What I did was set up a new Ubuntu VPS with the same OS version, rsync files from source VPS to new VPS.
Make sure you take a backup of your server before doing it. In case anything goes wrong, you should be able to restore from backup or OS reload.
Also take a backup of /etc on the new server before you copy anything. If the new server fails to boot after migration, you can restore its original config files from this backup.
tar czf /root/etc-before-migration.tgz /etc
Install rsync on source and destination servers.
For Ubuntu/Debian
apt install rsync
For RHEL/CentOS/AlmaLinux
yum install rsync
On the new server, create a file
vi /root/no_copy
add the following content
# Pseudo / runtime filesystems
/sys/
/proc/
/dev/
/run/
lost+found/
# Disk / LVM / RAID — per-server hardware IDs
/etc/fstab
/etc/mtab
/etc/lvm/
/etc/mdadm.conf
/etc/crypttab
/etc/multipath/
/etc/multipath.conf
# Boot / kernel
/boot/
/lib/modules
/usr/lib/modules
/etc/modules
/etc/default/grub
/etc/default/grub.d/
/etc/kernel/
/etc/dracut.conf.d/
# Network config — IPs, MACs, interface names
/etc/NetworkManager/system-connections/
/var/lib/NetworkManager/
/etc/sysconfig/network-scripts/
/etc/sysconfig/network
/etc/systemd/network/
/etc/udev/rules.d/70-persistent-net.rules
/etc/resolv.conf
/etc/netplan/
/etc/network/
# Machine identity / access
/etc/hostname
/etc/machine-id
/var/lib/systemd/random-seed
/etc/ssh/
/root/.ssh/
# Swap, caches, logs, misc
/swapfile
/swap.img
/var/cache/
/var/log/journal/
/usr/tmpDSK
/backup/
/home/virtfs/
These files won’t be copied to the new VPS. If you have any other folders or files that you don’t want to be copied to the new server, you can add them to the list.
The exclude list skips files that are specific to each server: disk and LVM IDs, network config (IP address, MAC, interface names), boot config, hostname and SSH keys. If you copy these from the old server, the new server may not boot, or may come up with the old server IP address.
This is especially important on RHEL 9 / AlmaLinux 9 / Rocky Linux 9. These versions use an LVM devices file (/etc/lvm/devices/system.devices) that locks LVM to the old server’s disks by hardware ID. If it gets copied, the new server boots into emergency mode with “Timed out waiting for device /dev/vg_xxx/lv_xxx”.
Before copying, do a dry run on /etc and check what will change. Look for anything that mentions disks, IP addresses, interfaces or UUIDs.
rsync -avPHAXn --exclude-from=/root/no_copy root@OLD_SERVER_IP:/etc/ /etc/ | less
Now run rsync on the new server to copy over files from the old server
rsync -avzPHAX --exclude-from=/root/no_copy root@OLD_SERVER_IP:/ /
In the command above, replace OLD_SERVER_IP with the old server IP address.
If both servers are in the same data center, you can remove the z option (compression). It only adds CPU load on a fast network.
rsync does not delete files on the new server that were deleted on the old server. This is safer. If you need an exact copy of a folder, run rsync for that folder with the –delete option.
When we do rsync, all files get copied to the new server. After the first rsync run, you can run the rsync command any number of times. Only changed files are copied, so it will finish much faster.
After the first rsync is completed, stop all services on the source and destination servers, like MySQL, Apache, Nginx, etc., so no data gets changed while we do the rsync. Now do another rsync.
Important: /etc/shadow is copied, so the root password on the new server is now the root password of the old server. Make sure you know it. You will need it if you have to log in from the console (IPMI/KVM/iDRAC).
Before you reboot, check that the new server will boot fine. findmnt checks /etc/fstab, lvs should list all your LVM volumes, and the grep should return nothing (or only files you expect).
findmnt --verify
lvs
grep -rl "OLD_SERVER_IP" /etc
Also check /etc/hosts. It is not excluded and may still have the old server hostname and IP address.
Now reboot the destination server and see if everything works fine. After the reboot, update all packages. This syncs the package database copied from the old server with the kernel installed on the new server.
For RHEL/CentOS/AlmaLinux
dnf update
For Ubuntu/Debian
apt update && apt upgrade
If the server boots into emergency mode with “Timed out waiting for device /dev/vg_xxx/lv_xxx”, the LVM devices file was copied from the old server. Log in with the root password, then restore the new server’s original file. LVM keeps backups of it in /etc/lvm/devices/backup/. Use the newest one dated before the migration.
mount -o remount,rw /
ls -lt /etc/lvm/devices/backup/
cp /etc/lvm/devices/backup/system.devices-YYYYMMDD.HHMMSS.NNNN /etc/lvm/devices/system.devices
systemctl reboot
See rsync