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...


Solution 1:

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.

Solution 2:

There is another approach which involves creating temporary snapshots during the backup and committing them back afterwards.

# Create a temporary backup snapshot which records changes during backup
virsh snapshot-create-as vm backup-tmp --disk-only --atomic --quiesce --no-metadata --diskspec sda,file=/tmp/sda_temp.qcow2
# Now the main image is frozen and the changes are going into /tmp/sda_temp.qcow2

# Do your backup here
...

# Now we commit back data from temp snapshot into the main image
virsh blockcommit vm sda --active --pivot
# Remove the temporary snapshot file
rm /tmp/sda_temp.qcow2

For more information see: https://wiki.libvirt.org/page/Live-disk-backup-with-active-blockcommit