Backup of running KVM qcow2 VPS

Bosco D'mello asked:

I want a robust method of taking backup of KVM VPS of qcow2 file, while running.
After searching for a long time I found that we can use rsync for live backup.
I used following command for rsync:

rsync -avh /vms/base.qcow2 /backup/backup.qcow2

It worked properly. But I am afraid it may corrupt backup qcow2 image if backup is taken while VPS is being changed due to write and delete operations (Like installing new software or update).

Therefore taking backup from snapshot of live VPS seems more interesting option.

I used following commands for taking backup of running VPS:

1.Create Snapshot:

qemu-img create -f qcow2 -b base.qcow2 snapshot.qcow2

2.Convert snapshot in raw image format (.img) :

qemu-img convert -O raw snapshot.qcow2 /backup/backup.img

To restore VPS :

3.Convert raw image in qcow2 image:

qemu-img convert -O qcow2 /backup/backup.img base.qcow2

Then we can start VPS by virsh create base.xml command.

Even though this method also work perfectly I am still not sure if this is proper method to take backup of running VPS.
I found nice documentation on qemu-img commands here and here.
But they didn’t mention anything about converting snapshot of live VPS in raw image.

Which method is more suitable for live VPS backup? Is converting snapshot in raw image safe?

If I could get nice detailed documentation on how snapshot and base files behave in certain conditions that will be very helpful.

Maybe we can also convert base qcow2 image into raw backup file instead of converting snapshot.
So command flow will be :
1. Create snapshot
2. Convert base qcow2 into backup raw image
3. Commit snapshot changes into base image
4. Delete snapshot

I tried this and it worked fine on small VPS of 5GB. But is there any possibility of error or VPS corruption when VPS size is huge?

Thanks in advance…

My answer:

The only thing you are missing is to quiesce the guest filesystem before taking the snapshot, to ensure that it is consistent. This can be done with virsh domfsfreeze if you are using libvirtd.

For example, the order of operations is:

# Freeze guest filesystems
virsh domfsfreeze $VM_NAME

# Create snapshot
qemu-img create -f qcow2 -b $VM_NAME.qcow2 snapshot.qcow2

# Thaw guest filesystems
virsh domfsthaw $VM_NAME

# Take backup from snapshot
qemu-img convert -O raw snapshot.qcow2 /backup/backup.img

Note well that this requires the qemu-guest-agent be installed and running in the VM. Depending on the distribution and installation choices, this may or may not be the case.

The qemu-guest-agent is also extensible; you can add scripts to it to support specific software. For instance, it includes a sample script to put MySQL in a consistent state.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.