Ubuntu KVM 5: Backing Up and Restoring a VM

Posted by Jared Kipe on | 0 Comments

Tags: , , , , , ,

There comes a time when simply running a virtual machine is not enough.  Maybe you need to mount the VMs disk to grab or modify a file.  Maybe you want to move a VM to a different computer.  In this article I will explore methods to duplicate, import and modify existing VMs.

Note* For these articles I'm going to assume a clean Ubuntu 14.04 Trusty server install with minimal extras (nano, htop, openssh-server etc.) and/or the outcome of previous articles.

Backing Up a KVM Virtual Machine

The VM Definition (domain)

There are two components that need to be backed up. The first is the definition of the virtual machine. This is roughly analogous to the hardware of a physical machine, and it includes information such as the network MAC addresses, and resource allocations (RAM, vCPUs ...). If you added network adapters or serial consoles, it is especially important to backup this configuration. Luckily, virsh provides an easy command to view or save this information. virsh dumpxml <domain_name> will output the file on stdout. Usually that means you will want to redirect that into a file.

# assuming /media/backupvol exists and you have write privileges there
virsh dumpxml demo1 > /media/backupvol/demo1.xml

This will overwrite /media/backupvol/demo1.xml with the contents of the currend domain definition for the VM named 'demo1'.

The VM Disk Image

The domain definition alone will not provide you with any of the data, or internal configuration, of a machine. To backup the underlying data, you will first need to know where the VM is currently storing it. Inspect the .xml file you just created, or use the virsh dumpxml command again, to look for a <disk/> node with a child <source file="x"/> node. That file attribute will be the path of your disk image. To back it up, you need only stop the virtual machine and copy the file. There is also the virsh domblklist command to get this information from virsh itself.

virsh shutdown demo1
cp /home/jaredkipe/demo1/demo1.qcow2 /media/backupvol/
virsh start demo1 #assuming you want it running in the first place

Along with the domain XML file, this image is a complete backup of the virtual machine, and is suitable for a complete restore or relocation to a different KVM server.

Restoring a Pervious Backup

Data only

If the domain configuration didn't change, but for some other reason you want to roll back to a prior version of the VM, you need only copy back the old image.

virsh shutdown demo1
rm -f /home/jaredkipe/demo1/demo1.qcow2 #more likely move (mv) to a different location/name
cp /media/backupvol/demo1.qcow2 /home/jaredkipe/demo1/
virsh start demo1

Complete Restoration or Migration

If you undefined the domain in virsh, or are moving the VM to a new computer, the domain XML file will be used to define a new virtual machine. If the domain's name or uuid are already defined, on this host, you will need to change them or undefine them in the destination hypervisor (e.g. the same goes for duplicating a VM). It is also important that there are no missing, mislabeled, or otherwise incorrect network settings (the same is true inside the VM if you're using statically assigned IP addresses).

# used to remove the current definition
#virsh destroy demo1
#virsh undefine demo1
virsh define --file /media/backupvol/demo1.xml
rm -f /home/jaredkipe/demo1/demo1.qcow2
cp /media/backupvol/demo1.qcow2 /home/jaredkipe/demo1/
virsh start demo1 #bring up the fully restored VM

Advanced Methods

As you will notice, every time I wanted to move or backup a VM, I needed to stop the running instance. This is not always practical, especially if you do not know if the VM can boot successfully and run its services without user interaction.

There are more advanced ways to migrate and backup virtual machines. Migrations in particular can be done with very little down time provided you you do not need to physically move the HDD disk image.

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments