Skip to main content

Create and restore external backups of virtual machines with libvirt

A common need for deployments in production, is to have the possibility of taking backups of your working virtual machines, and export them to some external storage.
Although libvirt offers the possibility of taking snapshots and restore them, those snapshots are intended to be managed locally, and are lost when you destroy your virtual machines.
There may be the need to just trash all your environment, and re-create the virtual machines from an external backup, so this article offers a procedure to achieve it.

First step, create an external snapshot

So the first step will be taking an snapshot from your running vm. The best way to take an isolated backup is using blockcopy virsh command. So, how to proceed?

1. First you need to extract all the disks that your vm has. This can be achieved with domblklist command: 
DISK_NAME=$(virsh domblklist {{domain}} --details | grep 'disk' | awk '{print $3}')

This will extract the name of the device that the vm is using (vda, hda, etc...)

2. Second step will be to backup the xml of the virtual machine, because we will need to undefine it on next steps. So let's take the backup *: 
virsh dumpxml --inactive {{domain}} > /tmp/domain_backup.xml

3. Once we have the xml backup, let's undefine the vm temporarily:
virsh undefine {{ domain }}

4. Now it's time to take the backup. We are going to do it for each of the disks defined in our virtual machine:
virsh blockcopy {{domain}} $DISK_NAME /path/to/backups/{{domain}}_backup.qcow2 --wait --verbose --finish

Do it for all the vms and disks that you want to backup, and store on a safe place.

5. Define the VM again, so it goes up and running:
virsh define {{ domain }}

* You can store this xml dump also in the backups directory, so you could recreate the vm in the future with the same exact content.

Second step, consume the snapshot

Creating a new virtual machine using this snapshot is so easy. You just need to use the qcow2 that we created on the backup, as a path for the new created vm. Do it for all the disks. As a sample:
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/path/to/backups/{{ domain }}_backup.qcow2'/>
      <target dev='${DISK_NAME}' bus='ide'/>
      <alias name='ide0-0-0'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>


So this method provides a simple way to preserve the state of your vms, either for backup or for accelerating your testing by reusing vms on a known and good status.

Comments

  1. Thanks for the article! Wish you do not stop in creating content! If you need a solid and reliable VM backup software just click on the link

    ReplyDelete
  2. Hi there,
    Thank you for this detailed method on making external backup of vm. I have no problem for the first 3 steps but run into an odd issue in step 4. The command tells me that `error: failed to get domain '{{domain}}'`. My guess is that is because I have undefined the domain in step 3. What do you think?
    In case it matters, I am running a virsh 6.1.0 on an openSUSE Tumbleweed box.
    Cheers

    ReplyDelete
    Replies
    1. I got the same issue, and my problem was that the qcow2 filename differed from the domain name. I edited the XML file and renamed the vm file using the proper domain name. After that, the blockcopy worked fine.

      Delete
  3. Your style is so unique compared to other people I have read stuff from. Many thanks forposting when you have the opportunity, Guess I will just bookmark this site Debut Drive SnapShot

    ReplyDelete

Post a Comment

Popular posts from this blog

Enable UEFI PXE boot in Supermicro SYS-E200

When provisioning my Supermicro SYS-E200-8D machines (X10 motherboard), i had the need to enable UEFI boot mode, and provision through PXE. This may seem straightforward, but there is a set of BIOS settings that need to be changed in order to enable it. First thing is to enable EFI on LAN , and enable Network Stack. To do that, enter into BIOS > Advanced > PCIe/PCI/PnP configuration and check that your settings match the following: See that PCI-E have EFI firmware loaded. Same for Onboard LAN OPROM and Onboard Video OPROM. And UEFI Network stack is enabled , as well as IPv4 PXE/IPv6 PXE support. Next thing is to modify boot settings. The usual boot order for PXE is to first add hard disk and second PXE network . The PXE tools (for example Ironic) will set a temporary boot order for PXE (one time) to enable the boot from network, but then the reboot will be done from hard disk. So be sure that your boot order matches the following: See that the first order is hard d...

Test API endpoint with netcat

Do you need a simple way to validate that an API endpoint is responsive, but you don't want to use curl? There is a simple way to validate the endpoint with nc, producing an output that can be redirected to a logfile and parsed later: URL=$1 PORT=$2 while true; do     RESULT=$(nc -vz $URL $PORT 2>&1)     DATE=$(date)     echo $DATE $RESULT     sleep 1 done You can all this script with the API URL as first parameter, and API port as the second. netcat will be accessing to that endpoint and will report the results, detecting when the API is down. We also can output the date to have a reference when failures are detected. The produced output will be something like: vie jun 26 08:19:28 UTC 2020 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connected to 192.168.111.3:6443. Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds. vie jun 26 08:19:29 UTC 2020 Ncat: Version 7.70 ( https://nmap.org/ncat ) Ncat: Connec...