Chapter 21. 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.
21.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.xml
# virsh dumpxml testguest1 > /home/backup/testguest1-backup.xmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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>
# virsh start <vm-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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 disksvirsh backup-begin <vm-name>
# virsh backup-begin <vm-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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>
# virsh backup-begin <vm-name> --backupxml <backup-XML-location>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Ensure that the backup operation has completed.
virsh domjobinfo <vm-name> --completed
# virsh domjobinfo <vm-name> --completedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example successful output:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
# ls -l /var/lib/libvirt/imagesCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
-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.1761750144Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Next steps
21.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>
# cat </path/to/backup.xml>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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'/><disk type='file' device='disk'> <driver name='qemu' type='qcow2' discard='unmap'/> <source file='/var/lib/libvirt/images/vm-name.qcow2' index='3'/>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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>
# virsh define --file </path/to/backup.xml>Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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>
# virsh edit <vm-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Start the VM and check that the guest operating system has been recovered correctly.
virsh start <vm-name>
# virsh start <vm-name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow