XenServer- Take VM Snapshots using Bash

Following is the script that can be used to take snapshots of running vm,
once run it will send mail to ADMIN for the list of the VM snapshots

#this basic script is created to take xenserver guest snapshots
#ths script is tested only on standalone server NOT ON SERVER POOL
export PATH
SNAPDATE=`date +%Y-%m-%d`
echo -e "subject: snapshots done on $HOSTNAME \n" > /tmp/xen_snapshot.mail
#generate list of RUNNING VM ONLY
echo -e "Generating List of Running VMs"
xe vm-list power-state=running|grep name-label| grep -v "Control domain on host:"  | sed 's/     name-label ( RW): //g' > /tmp/xen_snapshot1
echo -e "List of the VM for snapshot" >> /tmp/xen_snapshot.mail
cat /tmp/xen_snapshot1 >> /tmp/xen_snapshot.mail
echo -e "\n" >> /tmp/xen_snapshot.mail
#create a snapshot for each vm
VMLIST=`cat /tmp/xen_snapshot1 | wc -l`
until [ $VMLIST = $COUNT ]
COUNT=`expr $COUNT + 1`
VM=`gawk FNR==$COUNT /tmp/xen_snapshot1`
#create snapshot 
echo "Creating Snapshot for $VM"
echo -e "Creating Snapshot for $VM" >> /tmp/xen_snapshot.mail
#SNAPSHOT=`xe vm-snapshot vm="$VM"  new-name-label="$VM"-$SNAPDATE new-name-description="backup snapshot" `
xe vm-snapshot vm="$VM"  new-name-label="$VM"-$SNAPDATE new-name-description="backup snapshot" 
#or you can use following
#xe vm-snapshot-with-quiesce vm="$VM"  new-name-label="$VM"-$SNAPDATE new-name-description="backup snapshot"
if [ $? -eq 0 ]
echo -e "snapshot is Successful for $VM \n"
echo -e "snapshot is Successful for $VM \n" >> /tmp/xen_snapshot.mail
echo -e "snapshot failed for $VM \n" 
echo -e "snapshot failed for $VM \n" >> /tmp/xen_snapshot.mail
cat /tmp/xen_snapshot.mail | sendmail  $ADMIN 
rm -rf /tmp/xen_snapshot*

For mails to work, make sure followings are mentioned in /etc/ssmtp/ssmtp.conf

if you find any missing point in here, please let us know in comment section or tweet us at @linuxreaders. To get more articles like this, subscribe to our RSS feeds / Mails.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trending Posts