KVM backup overview

Kernel-based Virtual Machine (KVM, for short) is a virtualization architecture with an open source code that’s made for Linux distributions. Its main advantages are lower ownership cost, powerful guest isolation capabilities and more flexibility than most, These and other qualities mean a lot of companies are using KVM already or planning to migrate to it. The list of guest OS’s supported is also substantial, including Linux, Mac, Windows, Solaris, and so on.

Like almost any hypervisor type, KVM can be prone to data corruption, data breach, and other disasters. This is where various KVM backup solutions come into play, helping with, at the very least, mitigating the potentially devastating effects of an entire VM’s data getting corrupted beyond repair.

Of course, there are plenty of different third-party solutions that offer free KVM backup capabilities. But at the same time there are other means of creating these backups, using only KVM’s features itself, and without any additional KVM backup solution to begin with.

KVM Backup process

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’.

Open Source KVM Backup Tool from Bacula

Or the third-party backup solutions available, Bacula Community is one of the more powerful options; performing KVM backup and recovery with a choice of either command line or GUI interfaces. It also offers enterprises the opportunity to exploit its sophisticated range of management options, tools and scheduling possibilities – and is scalable up to extremely high numbers of VM’s. As an enterprise-level solution, it can be used to backup an especially wide range of other VM types,  databases and applications. Bacula Community is a free open source solution with a large list of capabilities and features, including different backup types, different target storage devices, and so on. Learn more about Bacula Community’s capabilities and changelog here.

If you’re also interested in Server Backup, check out Bacula’s solution.