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

  • 2
    That's one of the reasons I like having my virtual machines reside on logical volumes. They have inherent snapshot support.
    – Halfgaar
    Aug 18, 2015 at 9:02

2 Answers 2


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.

  • The only thing this answer is missing is snapshot removal after backup :)
    – dyasny
    Aug 18, 2015 at 15:16
  • 2
    @BoscoD'mello Unless you have very bad hard drives, it should be less than a fraction of a second. It's not worth worrying about. Aug 19, 2015 at 5:55
  • 1
    Alas, the snapshot did not work for me. When attempting to virsh domfsfreeze $VM_NAME, I get permission denied on my guest filesystem. My guest filesystems are encrypted, not sure that matters.
    – a coder
    May 23, 2017 at 15:10
  • 2
    @Edward_178118 It depends on whether your workload can stand to be suspended for a fraction of a second. Dec 29, 2019 at 19:49
  • 2
    @Edward_178118 Extremely low latency network operations, perhaps? I've never run into anything that was a problem. Dec 30, 2019 at 7:00

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

  • 1
    If you need your backed up disk again later, which comes from a running vm, how would you restore? Don't you need the ram contents too for restoring? Dec 26, 2019 at 0:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .