Virt-Manager is one of the better open source, KVM virtualization management tools on Linux. With it, setting up multiple virtual machines is a simple, streamlined process. In this article today, we’ll be covering all of the various ways users can create a backup of a Virt-Manager virtual machine.
Note: Virt-Manager isn’t the only way to interact with KVM virtual machines on the Linux platform. If these methods don’t work for you, consider trying out other virtualization tools like VirtualBox or Gnome Boxes.
The easiest way to make sure that your Linux KVM virtual machine has a proper backup is to make use of the Virt Manager snapshot tool. With snapshots, it’s easy to save everything on the VM and restore it later. To create a snapshot, open up Virt-Manager and enter the root password. If you are backing up remotely, log into any remote VM connections you may have.
Note: An ideal way to use snapshots is to install everything on your VM required for it to run, then create a “base” snapshot to restore to, sort of like a fresh install.
When Virt-Manager is up and running, start-up the VM you’d like to create a snapshot of. Click “View”, then select the “Snapshots” button to open up the snapshot tool. Click the + button to open the Snapshot creation menu. In the menu, you’ll need to fill out the name of the snapshot, and the description.
Under the “Name” section, be sure to write the name of the operating system that is in the snapshot. Under “Description,” write out a detailed explanation of what is in the snapshot, and why you made it. This will help, especially if you have many different snapshots and can’t tell the difference between any of them.
After filling out all of the information in the snapshot creation tool, click finish to complete the process.
To restore a Virt-Manager virtual machine snapshot, click “View” in the main UI, and select “Snapshots”. Select a snapshot in the UI and click the play button. This will stop all current activity in the VM and restore it to the state the snapshot is in.
Need to delete a snapshot from Virt-Manager? Click on a snapshot, and then select the trash can icon. Selecting delete opens up a dialog that asks “are you sure you want to delete this snapshot?” Select the “yes” option, and the snapshot will be deleted.
Besides creating snapshots to backup a Virt-Manager virtual machine, another way to create a backup is as an archive of the volume used to store the operating system.
Note: The backup process outlined in this section only covers virtual hard drives. If your KVM setup allows virtual machines to run directly on the hard drive, the following process is not recommended. To create backups for that, check out our Gnome Disks tutorial.
Start off by opening up a terminal (connect to your server over SSH in the terminal if your VMs are remote), and use the CD command to move to /var/lib/libvirt/images/. This is the default directory for Virt-Manager, and all disk volumes go here. If you’ve changed the directory to something else, you’ll need to figure that out on your own.
Any user can CD into /var/lib/libvirt/images/, but a non-root user can’t use ls to view the contents of the directory, as the permissions aren’t correct. To solve this problem, gain root access.
Now that the terminal is using root, run the virsh tool to create a backup of the VM definitions file.
echo '' > nameofvm.xml
virsh dumpxml nameofvm > /var/lib/libvirt/images/nameofvm.xml
Use ls to find the name of the Qcow2 virtual hard drive in /var/lib/libvirt/images/. Copy the file name, and use mv to move it into a new sub-directory.
mkdir -p vm-backup c mv example.qcow2 vm-backup mv nameofvm.xml vm-backup
Using tar, create an archive of the VM disk and XML file.
tar -jcvfp my-vm-backup.tar.bz2 vm-backup mv my-vm-backup.tar.bz2 /home/username/
Backing up VM disk images takes a long time. Depending on the file size, it could take an hour or more. Best to just let the PC do its thing. When the backup is complete, feel free to move my-vm-backup.tar.bz2 to another server, cloud backup and etc.
To restore a backup, you’ll first need to extract it.
tar -xvfp my-vm-backup.tar.bz2
Use virsh to restore the XML file.
sudo -s virsh define --file /home/username/location/of/exctracted/archive/vm-backup/nameofvm.xml
Lastly, move the disk image to the images directory.
mv example.qcow2 /var/lib/libvirt/images/