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

#!/bin/bash 
#dpthakar@gmail.com
#http://www.linuxreaders.com
#
#this basic script is created to take xenserver guest snapshots
#ths script is tested only on standalone server NOT ON SERVER POOL
 
 
PATH=/opt/xensource/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
SNAPDATE=`date +%Y-%m-%d`
ADMIN=dpthakar@gmail.com
 
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`
COUNT=0
until [ $VMLIST = $COUNT ]
do
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 ]
then
echo -e "snapshot is Successful for $VM \n"
echo -e "snapshot is Successful for $VM \n" >> /tmp/xen_snapshot.mail
else
echo -e "snapshot failed for $VM \n" 
echo -e "snapshot failed for $VM \n" >> /tmp/xen_snapshot.mail
fi 
done
 
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
mailhub=SMTP-SERVER
rewriteDomain=-DOMAIN-NAME

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