KVM backups with Virt-Manager

So if you like to mess around like I do, you’re probably familiar with the idea of virtual machines. I like to use a program called virt-manager under Linux to create and work in VMs. It’s quick and easy, and gets the job done. But you’re probably wondering how you backup said VMs. I’m here to show you.

First list the VMs you currently have available to make sure they’re shut down :

virsh list --all

 Id   Name          State
------------------------------
 52   ActualArch    running
 -    archlinux     shut off
 -    endeavoros    shut off
 -    fedora        running
 -    freebsd13.1   shut off
 -    Gentoo        shut off
 -    manjaro       shut off
 -    ubuntu22.04   shut off
 -    win11         shut off

In my case I’ll be backing up the “fedora” VM. Do this to shut it down :

virsh shutdown fedora

And repeat the listing to verify that it worked:

virsh list --all
 Id   Name          State
------------------------------
 52   ActualArch    running
 -    archlinux     shut off
 -    endeavoros    shut off
 -    fedora        shut off
 -    freebsd13.1   shut off
 -    Gentoo        shut off
 -    manjaro       shut off
 -    ubuntu22.04   shut off
 -    win11         shut off

Backing up a VM contains two components - the domain definition, which is an .xml file, and the actual data file, which is usually in qcow2 format. To see where the data files are located we do the following :

virsh domblklist fedora
Target   Source
---------------------------------------------------------------
 vda      /mnt/D0F0D4BFF0D4ACCA/virtual machines/fedora.qcow2
 sda      -

Now we want to dump the definition file for the Fedora VM to an XML at our backup location:

virsh dumpxml fedora > /location/to/backup/to/fedora.xml

And finally the data file, or hard drive:

cp /mnt/D0F0D4BFF0D4ACCA/virtual machines/fedora.qcow2 /location/to/backup/to

I will show you how to restore these backups in another tutorial coming soon!

2 Likes

Where is the .xml file stored?
If it is in the same place as the qcow2 file, one could just backup the whole partition
I think what you really mean is a snapshot rather than a backup… just one VM

2 Likes

When you do the dumpxml command you pipe it to an actual xml file. It saves it to wherever you tell it to save it to, preferably the location you’re going to ultimately be backing everyting up to. You need both the xml and the qcow2 for it to be a complete backup that’s readable by libvirt when you want to restore it.
Example :

virsh dumpxml fedora > /location/to/backup/to/fedora.xml

If you didn’t do the > fedora.xml it would just dump the contents of the xml to the display. This is for actual backups that you can keep somewhere else in case you have a hard drive failure/etc. Snapshots are good for getting out of breaking something, but I don’t consider them actual backups.

1 Like

Oh, I get it, it is not stored anywhere, you generate it.

We need the restore tutorial too.

2 Likes

Working on that as we speak :slight_smile:

1 Like