attribution | Migrating a Linux Server From the Command Line |
---|
About
Migrating a physical server to another physical server (P2P) can be performed relatively easily but it will require a little background work. The approach we take is to use rsync’s “exclude file” to ensure we do not rsync items that will cause a problem (like /proc or /etc/host).
Terminology
- source machine: the machine to copy/migrate
- destination machine: the new machine (which will be a copy of the source machine)
- backup machine: the backup machine (you should have one)
How It Works
- Create the exclude file on the source machine
- Stop all possible services on the source machine
- Use a rsync push to copy all items that are not excluded from the source machine to the destination machine
- Handle any manual sync items (e.g. database dumps, etc.)
Considerations
- make sure your destination machine/partitions have enough space to hold all of the data from your source machine (take special care here if you are moving from no/few partitions to a more partitioned architecture on your destination machine)
- rsync considerations
- ports - adjust the rsync command with the
-e 'ssh -p 9999'
flag if your destination machine has ssh running on a non-standard port - compression - you may want to use the
-z|--compress
flag; however, this is a balance between cpu/memory and network bandwidth (it takes more cpu/ram to compress but saves bandwidth). My servers all tend to be on the same LAN and older models so bandwidth is not the premium but cpu/memory often is.
- ports - adjust the rsync command with the
- basic “exclude file”
/boot
/dev
/etc/conf.d/net
/etc/crypttab
/etc/fstab
/etc/hostname
/etc/HOSTNAME
/etc/hosts
/etc/init.d/nova-agent*
/etc/modprobe*
/etc/modules
/etc/mtab
/etc/network/interfaces
/etc/networks
/etc/rc.conf
/etc/resolv.conf
/etc/sysconfig/network*
/etc/sysconfig/hwconf
/etc/sysconfig/ip6tables-config
/etc/sysconfig/kernel
/initrd.img
/lib/modules
/net
/proc
/sys
/tmp
/usr/share/nova-agent*
/usr/sbin/nova-agent*
/var/lock
- note: *I recommend checking the "exclude file" into version control*
- in-use files (e.g. databases, encrypted files, etc.):
- shut as many services down prior to running rsync
- note: use the dump and restore of the database if possible. if this is not feasible/desired, at least shut the db/application down prior to rsync’ing
- shut as many services down prior to running rsync
The Migration
NOTICE | use the '--dry-run' option BEFORE running this script and check out the results to ensure you are going to get (or not get) everything you expect |
---|
- destination machine: login to machine (ensure you have a root session) and keep the session open
- I recommend backing this machine up now just in case ;-)
- source machine: create your “exclude file” (referred to here as “excludes” located in ~/root/tmp)
- source machine: issue the following rsync command:
rsync -e 'ssh' -aPx --delete-after --exclude-from="/home/demo/exclude.txt" / root@*destinationMachine*:/
- destination machine: verify everything on the machine and reboot
Post Migration Backups
A side benefit to using rsync and an “exclude file” to migrate a server is that you also have a start on your backup script. All you will need is to put the “exclude file” (that we created on the source machine) on the destination machine and change your rsync script a bit. I recommend the following but please adapt for your needs: rsync -e 'ssh' -aPx --delete-after --exclude-from="/home/demo/exclude.txt" / root@backupMachine:/
Here is a slightly enhanced script (based off of an ArchWiki article): rsync -e 'ssh' -aAX --numeric-ids --info=progress2 --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* user@server:/path/to/backups
- note: the above rsync is a full backup, it will take time and space (you may want to exclude items that do not need backup or that are backed up in other processes/logic)