Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create backup for all Citrix Xen vms using temporary snapshots, and upload to a CIFS Share. VMs are compressed using gzip.
#/bin/sh
#
# Script to backup all vms in a Xen Server, to a CIFS Share
#
# Copyright (C) 2011 SIMB Tecnologia Ltda
# Based on post from Amador Pahim: <http://olamundo.org/posts/xenserver-backup-automatico-de-vms>
#
# This is free software, licensed under the GNU General Public License v2.
# See www.gnu.org for more information.
CIFSSHARE="//CIFSSERVER/SHARING"
USER="USERNAME"
PASSWD="PASSWORD"
BACKUPPATH="backup/vms/$(date --date 'now' +%y%m%d)"
IFS="
"
# Mount backup dir
tmpmntdir=$(mktemp -d)
dirBack=${tmpmntdir}/${BACKUPPATH}
mount -t cifs -o username=${USER},password=${PASSWD} ${CIFSSHARE} ${tmpmntdir} && \
{ logger -t "BackupVMs" -s "Mount ${CIFSSHARE} succesfully"; } || \
{ logger -t "BackupVMs" -s "Cannot mount ${CIFSSHARE}, exiting."; exit 1; }
mkdir -p ${dirBack}
vms="$(xe vm-list | grep 'name-label' | grep -v 'Control domain' | tr -s ' ' | cut -d ' ' -f 5)"
for vm in $vms; do
time=$(date --date "now" +%d-%m-%y_%H-%M)
snapName=${vm}-${time}
ID=$(xe vm-snapshot vm=$vm new-name-label=$snapName && \
{ logger -t "BackupVMs" -s "$vm: Created Snapshot successfully"; } || \
{ logger -t "BackupVMs" -s "$vm: Error on creating snapshot, aborting"; continue; })
[ "$ID" == "1" ] && { logger -t "BackupVMs" -s "$vm: ID invalido recebido de vm-snapshot"; continue; }
xe template-param-set is-a-template=false uuid=$ID && \
{ logger -t "BackupVMs" -s "$vm: Created VM successfully"; } || \
{ logger -t "BackupVMs" -s "$vm: Error on creating VM, aborting"; continue; }
xe vm-export vm="${snapName}" filename="${dirBack}/${snapName}.xva" && \
{ logger -t "BackupVMs" -s "$vm: Export VM successfully"; } || \
{ logger -t "BackupVMs" -s "$vm: Error on exporting VM, aborting"; continue; }
xe vm-uninstall vm="${snapName}" force=true && \
{ logger -t "BackupVMs" -s "$vm: Removed VM successfully"; } || \
{ logger -t "BackupVMs" -s "$vm: Error on removing VM, aborting"; continue; }
gzip "${dirBack}/${snapName}.xva" && \
{ logger -t "BackupVMs" -s "$vm: GZipped VM successfully"; } || \
{ logger -t "BackupVMs" -s "$vm: Error on GZipping VM, aborting"; continue; }
done;
umount ${tmpmntdir}
rmdir ${tmpmntdir}
logger -t "BackupVMs" -s "Backup successfully all VMs";
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment