KVM external snapshots for multiple disks

Issues related to applications and software problems
Post Reply
poxpo
Posts: 4
Joined: 2018/04/05 14:15:25

KVM external snapshots for multiple disks

Post by poxpo » 2018/04/13 15:00:13

We have a bash script that creates an external snapshot of our VMs that get backed up offsite via borgbackup. This has worked well, but until now all our VMs only used a single disk image. The new VM uses 2 disk images, one for / and another mounted as a storage for a new Nextcloud instance.

The backup script is:

Code: Select all

#!/bin/bash
# Script to snapshot running VMs, and conduct a Borg backup

# Get list of running VMs
RUNNINGVMS=$(virsh list | grep running | awk '{ print $2 }')

# Create a disk-only external snapshot of all running VMs
for vm in $RUNNINGVMS
do
        virsh snapshot-create-as --domain $vm --diskspec vda,file=/srv/kvm/images/$vm.snap.qcow2 --disk-only --atomic --no-metadata $vm-snap
done

# Do a Borg backup of VM snapshots
REPOSITORY=/srv/borg
export BORG_PASSPHRASE='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
borg create -v --stats \
        $REPOSITORY::'{hostname}-kvm-{now:%Y-%m-%d-%H%M}' \
        /srv/kvm/images \
        --exclude '/srv/kvm/images/*.snap.qcow2' \
        --exclude /srv/kvm/images/image_backups
# Remove and consolidate old backups
borg prune -v --list $REPOSITORY --prefix '{hostname}-kvm-' \
        --keep-daily=7 --keep-weekly=4 --keep-monthly=6

for vm in $RUNNINGVMS
do
        virsh blockcommit $vm vda --active --pivot --shallow --verbose
done

# Cleanup snapshot images
rm -f /srv/kvm/images/*.snap.qcow2
As I said, this works well for single-disk VMs, but creates an issue when there are 2 disks assigned to a VM. I imagine the issue lies with

Code: Select all

--diskspec vda,file=/srv/kvm/images/$vm.snap.qcow2
where vdb should be included as well.

My question here is: What is the best way to modify this script to work well for both single- and multiple-disk VMs?

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: KVM external snapshots for multiple disks

Post by TrevorH » 2018/04/13 16:32:49

You probably need to use virsh domblklist $vm to get a list of the disks attached and then loop round them.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

Post Reply