KVM Hot Online Virtual Machines Backup with Bacula bpipe

This script aims to backup QEMU/KVM/libVirt virtual machines. It requires KVM 2.1+ since it uses the live blockcommit mode. This means the data in the snapshot disk is rolled back into the original at the end of the backup.

Bpipe Script

# FileSet Include (bacula-dir.conf)  
#    Plugin = "\|/etc/bacula/kvm_bpipe.sh"
DOMAIN=`virsh list --all | tail -n -2 | head -n -1 | awk '{print $2}'`

for d in $DOMAIN; do

  TARGETS=`virsh domblklist "$DOMAIN" --details | grep ^file | grep -v cdrom | awk '{print $3}'`
  IMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | awk '{print $4}'`

  for t in $TARGETS; do
    virsh snapshot-create-as --domain $DOMAIN --name backup --no-metadata -atomic --disk-only --diskspec $t,snapshot=external >/dev/null

  for t in $IMAGES; do
    NAME=`basename "$t"`
    echo "bpipe:/mnt/$NAME.bkp:cat $t:dd of=/tmp/$NAME.bkp"
    echo "bpipe:/mnt/$NAME.xml.bkp:virsh dumpxml $DOMAIN:dd/tmp/$NAME.xml.bkp"

ClientRunAfterJob Script

# KVM Backup Job (bacula-dir.conf)
#    ClientRunAfterJob = "/etc/bacula/kvm_commit.sh"

DOMAIN=`virsh list --all | tail -n -2 | head -n -1 | awk '{print $2}'`

BACKUPIMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | awk '{print $4}'`
for t in $TARGETS; do
    virsh blockcommit "$DOMAIN" "$t" --active --pivot >/dev/null
    if [ $? -ne 0 ]; then
        echo "Could not merge changes for disk $t of $DOMAIN. VM may be in invalid state."
        exit 1



Disponível em: pt-brPortuguês (Portuguese (Brazil))enEnglishesEspañol (Spanish)

This Post Has 6 Comments

  1. Steve

    I am trying to use this but am getting:
    Fatal error: Error running program: /opt/bacula/scripts/kvm_bpipe.sh. ERR=Child exited with code 2
    Not sure how to debug it

    1. hfaria

      Hello Steve,

      It looks like you didn’t create the client script in the correct path.


  2. Steve

    I figured that part out. How do you backup KVM guests on other hosts to backup, I am getting failed to connect to hypervisor. I have all the certificates in place and virsh -c qemu+tls:// host/system works from the bacula server

  3. Steve

    *How do you backup KVM guests on other hosts to bacula*

  4. Steve

    fd_plugins.c:606 Command plugin “error: failed to connect to the hypervisor” not found.

  5. Steve

    Is this script meant to backup a remote LVM host or local, I am not able to get the remote system to backup. A little more documentation would be great

Leave a Reply