Chapter 24. Backing up and recovering virtual machines
To ensure that you do not lose the setup and data of a virtual machine (VM) if your current host becomes unavailable or non-functional, back up the configuration and storage of the VM. Afterwards, for disaster recovery, you can create a new VM that uses the backed up configuration and storage.
If you plan to create a backup to recover a VM while the host remains functional, it might be more efficient to use snapshots instead. For details, see Saving and restoring virtual machine state by using snapshots.
24.1. Backing up a virtual machine Copy linkLink copied to clipboard!
To ensure that you do not lose the setup and data of a virtual machine (VM) if your current host becomes unavailable or non-functional, back up the configuration and disks of the VM. This allows you to later recover the backup on a functional host.
Procedure
Save the XML configuration of the VM to a separate file. For example, to save the configration of the testguest1 VM to the
/home/backup/testguest1-backup.xmlfile, use the following command:# virsh dumpxml testguest1 > /home/backup/testguest1-backup.xmlOptional: Customize your backup settings. By default,
libvirtcreates 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
vdadisk is backed up by using thelibvirtdefaults, thevdbdisk is backed up in therawformat as/home/backup/vdb.backup, and that thevdcdisk is not backed up.Start the VM.
# virsh start <vm-name>Back up the disks of the VM by using the
virsh backup-beginutility. 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>
Verification
Ensure that the backup operation has completed.
# virsh domjobinfo <vm-name> --completedExample 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 GiBCheck 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/imagesExample 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
Next steps
24.2. Recovering a virtual machine Copy linkLink copied to clipboard!
For disaster recovery of your virtual machines (VMs), you can create a new VM that uses the XML configuration and storage that you previously backed up.
Prerequisites
- You have created a backup of the VM’s XML configuration and storage. For instructions, see Backing up a virtual machine.
Procedure
Inspect the backup XML configuration of the VM for the specific storage settings.
# cat </path/to/backup.xml>Example output section:
<disk type='file' device='disk'> <driver name='qemu' type='qcow2' discard='unmap'/> <source file='/var/lib/libvirt/images/vm-name.qcow2' index='3'/>- Move the backup disks to the locations specified in the backup XML and ensure that their names correspond with the values in the XML.
Define a new VM based on the backup XML.
# virsh define --file </path/to/backup.xml>Optional: Modify the XML configuration of the newly created VM to adjust for settings that might be different from the original host, such as the network.
# virsh edit <vm-name>
Verification
Start the VM and check that the guest operating system has been recovered correctly.
# virsh start <vm-name>