How to Backup KVM with rsync
I had to gather information from several web sites in order to do this. A lot of web sites will tell you about how to make snapshots and they may have their place. But what I have always done is use rsync to make backups no matter what type of service I have. I use rsync for regular shared hosting as well as dedicated servers. I have VPS’s that use Xen and LVM storage, OpenVZ that uses SIMFS and PLOOP. And now Virtuozzo KVM that uses a disk image harddisk.hdd.
I am going to focus this post on the Virtuozzo KVM and the Virtualizor control panel which uses the harddisk.hdd. This is for mounting a live running system and using rsync to copy to the files to a remote server. The operating system I am using is CentOS 7. Other operating systems may use similar commands but maybe not exact.
The first thing is to make sure of which VPS you want to backup. This will show you all of the KVM VPSs whether they are currently running or stopped:
virsh list --all
You will get output that will look something like this:
Id Name State --------------------------------- 49 v1001 running
In my example the VPS I am backing up is v1001. Yours will likely be different.
Now you will need to install libguestfs-tools:
yum install -y libguestfs-tools
Then we need to change the backend that is used by libguestfs.
export LIBGUESTFS_BACKEND=direct
And we need to create a mount point for the image.
mkdir /mnt/v1001
Now we will be able to mount the live running system in read-only mode. Its important to use read-only so that the data that is on the live image is not corrupted.
guestmount -r -d v1001 -i /mnt/v1001/
Check to make sure it is properly mounted:
ls -l /mnt/v1001
You should get a list of directories and some files. Now we will be able to use the rsync command to backup to another directory or even a remote server. I recommend backing up to a remote server because really that is the whole point, in case something really bad happens to your host server. Make sure you have the remote server set up to receive the files. You should have ssh keys set up so rsync can be used without a password.
/usr/bin/rsync -av --delete -e '/usr/bin/ssh' /mnt/v1001/* root@remoteserver:/home/KVM/v1001/
You will also want to copy the configuration files. This is a little hard because a unique uuid is used. We can find it by doing a dump:
virsh dumpxml v1001 | grep "source file"
The output will look like this:
<source file="/vz/vmprivate/cf4cd085-d1b3-4d4c-8741-76d8a339a51e/harddisk.hdd" startuppolicy="optional">
So in this example the config files are located in:
/vz/vmprivate/cf4cd085-d1b3-4d4c-8741-76d8a339a51e/
So now we can rsync the configs to the remote server. Again make sure you have a directory set up for the config files. We want to exclude the harddisk.hdd because we have already copied the files off of it.
rsync -av --exclude 'harddisk.hdd' --delete -e '/usr/bin/ssh' /vz/vmprivate/cf4cd085-d1b3-4d4c-8741-76d8a339a51e/* root@remoteserver:/home/KVMconfigs/v1001/
Now the raw files and the configuration files are backed up. The good thing about using rsync is that only the files that have changed will need to be transferred. If you need to do a restore you basically have to reverse this process.