This script will make an export of all running machines in a XEN Source host and will be saved as .xva file.
This script mount a NFS shared resource (you can comment that part and use a local folder only).
This is the script I use for backing up my running Virtual machines each week.
#!/bin/bash
#
# Re-written by: Carlos V
#
#
DATE=`date +%d%B%y`
XSNAME=`echo $HOSTNAME` #This is the host where the script will run
BKPFOLDER=/mnt/exporta #folder to mount the NFS external exported share
NFSFOLDER=/export/Xen/xen2/backups #NFS shared folder exported
UUIDFILE=/tmp/uuids.txt #temporary file with the uuids of the running machines
TARGET=10.0.1.127 #NFS IP of the server
LOGFILE=/var/log/backup
MACADDRESS="e8:fc:af:e4:f3:a3"
echo "#################################################### start backup of running machines #################################################### " >> $LOGFILE
echo "backup of running machines started at `date` " >> $LOGFILE
### Mounting remote nfs share backup drive, we test the ping and in case it is not available, we send the magic packet to wakeonlan the server
### If you are planning to do not use any NFS, please, comment from here to ....
mountpoint -q $BKPFOLDER; [ $? -ne 0 ]
{
ping -c 1 $TARGET 2>&1> /dev/null
if [ $? -ne 0 ]
then
echo "Host is not Alive! Try again later.." >> $LOGFILE;
while ! ping -c 1 $TARGET 2>&1> /dev/null
do
echo "starting server with wakeonlan and waiting 70 secs at `date` " >> $LOGFILE;
/usr/local/bin/wakeonlan $MACADDRESS
sleep 70;
done
while ! mount -t nfs $TARGET:$NFSFOLDER $BKPFOLDER
do
sleep 5
done
else
echo "Yes! Host is Alive!" >> $LOGFILE
mount -t nfs $TARGET:$NFSFOLDER $BKPFOLDER
echo "mounted $TARGET:$NFSFOLDER on $BKPFOLDER at `date` " >> $LOGFILE
fi
} ||{ echo "folder is already mounted at $BKPFOLDER at `date` " >> $LOGFILE;}
##### Comment until here if you do not have NFS server or backups will be local
BACKUPPATH=$BKPFOLDER/$XSNAME
mkdir -p $BACKUPPATH
[ ! -d $BACKUPPATH ] && { echo "No backup directory found for $XSNAME and exiting with error 145" >> $LOGFILE ; exit 145; }
# Fetching list UUIDs of all VMs running on XenServer
echo "Fetching list UUIDs of all VMs running on XenServer" >> $LOGFILE
xe vm-list is-control-domain=false is-a-snapshot=false power-state=running | grep uuid | cut -d":" -f2 > $UUIDFILE
[ ! -f $UUIDFILE ] && { echo "No UUID list file found and exiting with error 146" >> $LOGFILE; exit 146; }
while read VMUUID
do
VMNAME=`xe vm-list uuid=$VMUUID | grep name-label | cut -d":" -f2 | sed 's/^ *//g'`
echo "backing up $VMNAME at `date` " >> $LOGFILE
SNAPUUID=`xe vm-snapshot uuid=$VMUUID new-name-label="$VMNAME"`
xe template-param-set is-a-template=false ha-always-run=false uuid=$SNAPUUID
echo "snapshot created for $VMNAME at `date` " >> $LOGFILE
FILENAME="$BACKUPPATH/$VMNAME.xva"
[ -f $FILENAME ] && { echo "Backup already exist, removing it $FILENAME at `date` " >> $LOGFILE; rm $FILENAME; }
echo "starting export of $VMNAME at `date` " >> $LOGFILE
xe vm-export vm=$SNAPUUID filename="$BACKUPPATH/$VMNAME.xva" >> $LOGFILE
echo "ended export of $VMNAME at `date` " >> $LOGFILE
xe vm-uninstall uuid=$SNAPUUID force=true >> $LOGFILE
echo "removed snapshot of $FILENAME at `date` " >> $LOGFILE
echo >> $LOGFILE
done < $UUIDFILE
### comment below two lines if no NFS share will be used
umount $BKPFOLDER
echo "umounted $BKPFOLDER " >> $LOGFILE
echo "backup of all running machines terminated at `date`" >> $LOGFILE
echo "#################################################### end backup of running machines #################################################### " >> $LOGFILE
echo >> $LOGFILE
