When making a backup of your virtual machine images, you need to make sure that the data in the VM disk image isn't changing while you are doing the backup. Otherwise you might end up with a useless corrupted backup image.
First of all make sure you have the qemu guest agent qemu-ga installed in your VM. This allows for libvirt to tell the guest OS that it needs to make sure everything is sync'd to disk ready for the backup.
To prepare the VM for backup you need to create an overlay image which the VM will use for any changes to make sure it doesn't change your actual VM image while the backup is ongoing.

virsh snapshot-create-as --quiesce --no-metadata --domain myvm overlay1 --diskspec vda,file=/var/lib/libvirt/images/myvm-overlay1.qcow2 --disk-only

The option --quiesce will tell the qemu guest agent to sync the disk state, and --no-metadata tells libvirt not to save the overlay metadata since it is going to be removed soon anyway.
You can now make a backup of your main VM disk image. After the backup is done you can tell libvirt to start using the main image again and commit any changes from the overlay image. The overlay can then be removed.

virsh blockcommit myvm vda --active --verbose --pivot
rm -v /var/lib/libvirt/images/myvm-overlay1.qcow2

Previous Post Next Post