User Tools

Site Tools


migrate_openvz

Migrate OpenVZ to another OpenVZ host without vzdump (using rsync)

EidolonHost does not support creating or restoring vzdump backups. If you'd like to attempt a semi-automated method of migrating OpenVZ without vzdump, read on.

Warning! This can easily hose your target system, especially if the transfer is disrupted.

Why?

Many parts of the linux environment are co-dependent. Of specific relevance to this operation are the interdependence of programs and library files; libc version, authentication framework and authentication file format; and availability of home folders. If these three things can be maintained, you can at least log in to the target system and run utilities after migration.

Please take care to back up your data first. Preferably from both systems.

Why?

Copying an entire system is a destructive process. The destination system at least will be (completely?) destroyed and restored as a replica of your old system; if you have anything you want to save on there, get it backed up off-system. If, by some unlucky event, you typed a bad command into the source system, you're operating (e.g., root and/or sudo) with enough privileges that the operating system can be unrecoverably damaged. So back up both system.

I cannot stress enough how easily this can leave your system in a state where it needs to be re-imaged.

Why?

No really, it's very easy. Should the connection get disrupted and you get a partially unpacked system, that system likely won't boot, your shell utilities probably won't work and in general it'll create serious headaches. So be careful and follow the instructions.

Never attempt to migrate an OpenVZ to a dedicated or KVM unless you know what you're doing.

Why?

OVZ images often do not include certain files that make no sense to it, for example a linux kernel or a /boot folder. These are critical to booting hardware or hardware emulated systems. At this point you've walked beyond the scope of this document: Beware, here be dragons.

This method works best run from the openvz system console. Most vps providers have a shell console you can access. Log into that shell on both machines and go through each active service and turn it off. Using kill may cause open files to be left in an inconsistent state, especially for SQL servers, so please shut them down gracefully. The only active service that should be running on the target machine is ssh.

Make sure rsync is installed on both the source and destination machines. This utility is usually installed by default.

If root login is disabled, enable root login for both the source and destination machines.

Create the file /dev/shm/exclude.txt with the following contents:

/dev/*
/proc/*
/sys/*

If you have any large data folders that can be copied after the core system has been transferred, exclude them now as well.

Run the following command on the source host:

rsync -avHAXWcSP --numeric-ids --progress --del --exclude-from=/dev/shm/exclude.txt / root@<DESTINATION_HOST>:/

Now pray that your connection is not interrupted.

If you were lucky, your entire system is now migrated to your new host. If you exempted large data folders before, remove them from the exclude.txt file now, and re-run the rsync command. This time, you will have to log in with your old vps's credentials. It should now transfer your large files only.

You may get a warning when re-running rsync that your sshd's host key has changed, that's normal. The previous command copied the host key from your old host.

Run the reboot command to restart the destination host.

A faster copy: tar then rsync

Since a linux system is largely small files, it is usually faster to tar them up before transfer as rsync checks each file. Instead of checking each file, this merely replaces everything it finds from the old system on the new system.

Caveat: This does not attempt to restore hardlinks. If you make use of hardlinks, you may find them unlinked in the result. Many backup storage methods use hardlinks to save space.

For the most part, the instructions are the same with the following exception. For the first pass, instead of rsync, run the following tar command:

tar cvpPSf - --numeric-owner -X /dev/shm/exclude.txt / | ssh root@<DESTINATION_HOST> "tar xpPSf - --numeric-owner -C /"

This will in-situ copy all non-excluded files to the remote host. Then run a second pass with rsync to clean up the old files as well as verify your original transfer is completed successfully.

You may get a warning here that your sshd's host key has changed, that's normal. The previous command copied the host key from your old host.

rsync -avHAXWcSP --numeric-ids --progress --del --exclude-from=/dev/shm/exclude.txt / root@<DESTINATION_HOST>:/

After everything is copied, restart your vps as normal

migrate_openvz.txt · Last modified: 2017/03/23 09:22 (external edit)