The entire process to backup kvm virtual machine begins with a command that you need to put in a command line interface to see the list of your machines and their current status:

> virsh list –all

If the command is entered correctly, you’ll be able to see a list of your VMs in a table of sorts, with three different columns: Id, Name and State.

The next step is recommended, but not mandatory – you should turn off the VM you want to make a backup of. The command is as follows:

> virsh shutdown Ubuntu4

To check if the shutdown operation was performed correctly, you can request a list of your VMs once again, via the following command:

> virsh list –all

The next step is to backup the virtual machine’s data into the XML file using the following command:

> virsh dumpxml Ubuntu4 > /Mybackup/Ubuntu4.xml

The last part with Mybackup folder is used to specify the location of the backup XML file. The other way of doing it is to copy the existing XML file from the system directory to your backup folder using the cp command. The default location of the XML file for the VMs is in the /etc/libvirt/qemu.

Another important part of creating a KVM backup is about copying the disk file (.qcow2 format). The default location of this file for an existing VM is at /var/lib/libvirt/images/. You can also use the following command to locate the disk file in question:

>virsh domblklist Ubuntu4

Of course, you’ll have to copy the file in question to the backup location, as well, using the cp or scp commands:

> cp /var/lib/libvirt/images/Ubuntu4.qcow2 /Mybackup/Ubuntu4.qcow2

This pretty much concludes the backup process. You can see all of your backups in a list form using this command:

> ls –lash

It’s important to mention that you can also perform all of these actions on a working VM, as well. It is not particularly recommended, since it could potentially lead to some errors in the copying process. But in cases where the VM itself needs to stay online no matter what, the backup process could – in principle – be performed without shutting it down.

KVM Recovery process

The recovery process (after, of course, having completed the backup of the kvm virtual machine) begins with performing one of two actions on your existing XML file of the VM you want to recover: you can delete the file entirely, or you can just undefine it, using this command:

> virsh undefine Ubuntu4

After the file in question is either deleted or undefined, you should not be able to see the virtual machine anymore. Use this command:

> virsh list –all

The second part of the first step consists of deleting the disk file of that same VM using the following command:

> rm /var/lib/libvirt/images/Ubuntu4.qcow2

After this we can finally start with the restoration process. First, we copy the backup disk (.qcow2 format) from the backup directory to the system directory:

> cp /Mybackup/Ubuntu4.qcow2 /var/lib/libvirt/images/

Next we’ll have to define the backup XML file using the command below (or we can just let the file be copied to the correct directory):

> virsh define –file /Mybackup/Ubuntu4.xml

The last part of the process is about starting up the VM to complete the restoration process by bringing the backed up VM back online:

> virsh start Ubuntu4

As you can see, this process can be done via basic Linux command line, but it can be somewhat complicated for less tech savvy people. This is why a lot of free third-party KVM backup software prioritize UI friendliness and overall ease-of-use as their prime advantages over the ‘built-in method’.