Skip to content

Instantly share code, notes, and snippets.

@wintermute000
Forked from cabal95/vm-backup.sh
Last active April 8, 2018 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wintermute000/fa18080b306ec511c5ea76d6e981ec03 to your computer and use it in GitHub Desktop.
Save wintermute000/fa18080b306ec511c5ea76d6e981ec03 to your computer and use it in GitHub Desktop.
I use this script to backup my QEMU/KVM/libVirt virtual machines. The script 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 instead of the other way around. Script does NOT handle spaces in paths.
#!/bin/bash
#
BACKUPDEST="$1"
DOMAIN="$2"
MAXBACKUPS="$3"
OWNER="$4"
GROUP="$5"
if [ -z "$BACKUPDEST" -o -z "$DOMAIN" ]; then
echo "Usage: ./vm-backup <backup-folder> <domain> [max-backups] [owner] [group]"
exit 1
fi
if [ -z "$MAXBACKUPS" ]; then
MAXBACKUPS=3
fi
if [ -z "$OWNER" ]; then
OWNER=$USER
fi
if [ -z "$GROUP" ]; then
GROUP=`groups | awk '{print $1;}'`
fi
echo "Beginning backup for $DOMAIN"
#
# Generate the backup path
#
BACKUPDATE=`date "+%Y-%m-%d.%H%M%S"`
BACKUPDOMAIN="$BACKUPDEST/$DOMAIN"
BACKUP="$BACKUPDOMAIN/$BACKUPDATE"
TARNAME="$DOMAIN-$BACKUPDATE"
mkdir -p "$BACKUP"
#
# Get the list of targets (disks) and the image paths.
#
TARGETS=`virsh domblklist "$DOMAIN" --details | grep ^file | grep -v 'cdrom' | grep -v 'floppy' | awk '{print $3}'`
IMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | grep -v 'cdrom' | grep -v 'floppy' | awk '{print $4}'`
#
# Create the snapshot.
#
DISKSPEC=""
for t in $TARGETS; do
DISKSPEC="$DISKSPEC --diskspec $t,snapshot=external"
done
virsh snapshot-create-as --domain "$DOMAIN" --name backup --no-metadata \
--atomic --disk-only $DISKSPEC >/dev/null
if [ $? -ne 0 ]; then
echo "Failed to create snapshot for $DOMAIN"
exit 1
fi
#
# Copy disk images
#
for t in $IMAGES; do
NAME=`basename "$t"`
cp "$t" "$BACKUP"/"$NAME"
done
#
# Merge changes back.
#
BACKUPIMAGES=`virsh domblklist "$DOMAIN" --details | grep ^file | grep -v 'cdrom' | grep -v 'floppy' | 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
fi
done
#
# Cleanup left over backup images.
#
for t in $BACKUPIMAGES; do
rm -f "$t"
done
#
# Dump the configuration information.
#
virsh dumpxml "$DOMAIN" >"$BACKUP/$DOMAIN.xml"
#
# Cleanup older backups.
#
LIST=`ls -r1 "$BACKUPDOMAIN" | grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]+$'`
i=1
for b in $LIST; do
if [ $i -gt "$MAXBACKUPS" ]; then
echo "Removing old backup "`basename $b`
rm -rf "$b"
fi
i=$[$i+1]
done
# Tar output
echo "Compressing backup"
tar -cv --use-compress-program=pigz --remove-files -f $BACKUPDOMAIN/$TARNAME.tar $BACKUP
chown -R $OWNER $BACKUPDOMAIN/$TARNAME.tar
chgrp -R $GROUP $BACKUPDOMAIN/$TARNAME.tar
echo "Finished backup"
echo ""
@wintermute000
Copy link
Author

Added TAR and chown/chgrp functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment