Backup Virtual Machine

Sources:

 

1. As root, locate the virtual machine you want to back up using virsh:

virsh -c qemu:///system list # shows only running

or

virsh list --all # shows all

 

2. Dump the configuration for your virtual machine:

virsh -c qemu:///system dumpxml MACHINE_NAME > /var/lib/libvirt/images/safe/MACHINE_NAME.datestamp.xml

 

3. You may need to locate the image directory:

virsh -c qemu:///system domblklist MACHINE_NAME

 

4. Now prepare the backup of this machine using a snapshot:

virsh snapshot-create-as --domain MACHINE_NAME IMAGE_NAME.snapshot --diskspec sda,file=/var/lib/libvirt/images/safe/IMAGE_NAME.snapshot.qcow2 --disk-only --atomic

Note: If you get an error, then the disk is not converted to virtio-scsi, and should be vda, not sda.

After creating the snapshot, all disk operations from the guest will be directed to it so that you can copy the original disk image to a safe place.

You can establish this by running, as example:

virsh -c qemu:///system domblklist ispa

Target     Source
------------------------------------------------
hda        -
sda        /var/lib/libvirt/images/safe/ispa.snapshot.qcow2

 

5. Copy to external server and include both the qcow2 disk image and the machine configuration (the XML file):

su - yourusername   # you must be in libvirt-qemu group

rsync -hav --progress /var/lib/libvirt/images/IMAGE_NAME.qcow2 192.168.100.2:backups/ # IP address of prionyx on 1GB VLAN

 

6. Merge snapshot:

After the file transfer is completed, you should merge changes written to snapshot, back to original disk image. This operation is called 'active block commit':

virsh blockcommit MACHINE_NAME sda --active --pivot --shallow --verbose

Check block devices again. If the block commit completed successfuly your virtual machine should use its original image again.

virsh -c qemu:///system domblklist MACHINE_NAME

 

Example Backup script for running VMs:

jeff@sheri:~$ cat /var/lib/libvirt/images/scripts/ispa.backup.sh 
#!/bin/bash
################################################################################
#
# This script takes a snaphot of the running VM, which leaves the main qcow2
# image in a known state. All VM activity is then recorded in the snapshot,
# while the main qcow2 image can be rsync'd to a remote backup server.
# After the image has been transferred, the snapshot is merged back to the
# main image, and then deleted.
#
# NB. This script must be run by a user who is a member of libvirt-qemu group.
#
################################################################################


virsh -c qemu:///system dumpxml ISPA > /var/lib/libvirt/images/xml/ispa.xml
scp /var/lib/libvirt/images/xml/ispa.xml 192.168.100.4:/var/lib/libvirt/images/xml/
virsh snapshot-create-as --domain ISPA ISPA.snapshot --diskspec sda,file=/var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2 --disk-only --atomic
rsync --rsync-path="ionice -c 3 nice rsync" -havW --progress /var/lib/libvirt/images/ispa.qcow2 192.168.100.4:/var/lib/libvirt/images/sheriff/
virsh blockcommit ISPA sda --active --pivot --shallow --verbose
virsh snapshot-delete ISPA --metadata ISPA.snapshot
rm -rf /var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2
#rm /var/lib/libvirt/images/xml/ispa.xml

 

Category