David Ramsden

Migrating Server to a New Host

When I migrated my server to a new VPS (different provider), I needed to figure out how to do this without needing to copy over individual config files, install all the required packages etc. Here were my thoughts and what I actually did.

I first tested the migration in to another virtual machine which I created so I could take snapshots at various stages.

The migration went very well using this method.

Brain Dump

  • Prep work:
    • Install same distribution on destination(architecture must be the same!). Only install minimal system + openssh-server.
    • Update destination and source (e.g. aptitude update && aptitude safe-upgrade).
    • Name the destination host the same as the source. May need to tweak /etc/hosts.
  • Do not rsync any of the following:
    • /boot
    • /lib/modules
    • /usr/src
    • /etc/ssh
    • /etc/udev
    • /etc/hosts
    • /etc/hostname
    • /etc/network/interfaces
    • /etc/fstab
    • /etc/mtab
    • /lost+found (or any lost+found)
    • /sys
    • /proc
    • /dev
    • /root/.cache
  • After rsync has completed:
    • Fix up any configs that bind to a certain IP address (i.e. Postfix, Apache etc).
    • Run GRUB/LILO.
  • After first boot:
    • Ensure new server boots correctly.
    • Copy over /etc/ssh and fix up IPs etc.

Could continue to use rsync with previous exclusions to ensure the new host is a replica of the old host, up until the point that the new host goes live.

What I Actually Did

  1. Installed base OS (minimal).
  2. “aptitude update && aptitude safe-upgrade” on destination.
  3. On new server temporarily enable root and allow root logins via SSH.
  4. On old server create /tmp/excluded containing things not to copy with rsync.
  5. On old server: rsync –exclude-from=/tmp/excluded -avz / root@new.host:/
  6. On new server, search and replace IPs in configs: grep -lrI -e '1.1.1.1' * | xargs sed -i 's/1.1.1.1/2.2.2.2/g'
  7. Re-run postmap on any files that may have changed from the above.
  8. Fixup resolv.conf
  9. Reboot and then review logs etc.
  10. If everything appears OK, on old server stop all services (check ps, kill, repeat). Ensure no services running on new server (clean up /var/run etc manually). Run a final sync, fix IPs on new server, check reslov, reboot.
  11. Replace sshd_config. Restart SSH.
  12. Keep old server services shutdown and change DNS.