To protect against host failures and storage corruption, you can back up a virtual machine (VM) by saving its XML configuration and disk data by using libvirt utilities.
Procedure
-
Save the XML configuration of the VM to a separate file. For example, to save the configuration of the testguest1 VM to the
/home/backup/testguest1-backup.xml file, use the following command:
# virsh dumpxml testguest1 > /home/backup/testguest1-backup.xml
-
Back up the disks of the VM. To do so, use the
virsh backup-begin utility.
-
Optional: Customize your backup settings. By default,
libvirt creates copies of all disks in the VM in the same directory as the original disks. If you want to use a different configuration for your backup, create an XML file with your required settings. For example:
<domainbackup>
<disks>
<disk name='vda' backup='yes'/>
<disk name='vdb' type='file'>
<target file='/home/backup/vdb.backup'/>
<driver type='raw'/>
</disk>
<disk name='vdc' backup='no'/>
</disks>
</domainbackup>
This example configuration ensures that the vda disk is backed up by using the libvirt defaults, the vdb disk is backed up in the raw format as /home/backup/vdb.backup, and that the vdc disk is not backed up.
-
Start the VM.
-
Back up the disks of the VM by using the
virsh backup-begin utility. The following command uses the default backup settings, which creates copies of all disks that the VM uses, and saves them in the same directory as the original disks
# virsh backup-begin <vm-name>
Optionally, to apply custom backup settings, you can specify the backup XML configuration that you created previously.
# virsh backup-begin <vm-name> --backupxml <backup-XML-location>
Results
- Ensure that the backup operation has completed.
# virsh domjobinfo <vm-name> --completed
Example successful output:
Job type: Completed
Operation: Backup
Time elapsed: 20704 ms
File processed: 40.000 GiB
File remaining: 0.000 B
File total: 40.000 GiB
- Check the target location of the backup disks. For example, if you used the default backup configuration and the VM disks are located in the default
/var/lib/libvirt/images/ directory, use the following command:
# ls -l /var/lib/libvirt/images
Example successful output:
-rw-------. 1 qemu qemu 42956488704 Oct 10 15:49 RHEL_10_beta.qcow2
-rw-------. 1 root root 42956488704 Oct 29 16:12 RHEL_10_beta.qcow.21761750144
-rw-------. 1 qemu qemu 196688 Oct 10 15:49 extra-storage.qcow2
-rw-------. 1 root root 196688 Oct 29 16:12 extra-storage.qcow2.1761750144