Quantcast
Viewing latest article 2
Browse Latest Browse All 10

XenServer- Take VM Backup using Bash

Following is the basic bash script to take vm backup using bash & send confirmation mail.
This script assumes you already have your backup storage mounted as /mnt/back/

#!/bin/bash 
#dpthakar@gmail.com
#http://www.linuxreaders.com
#
#this basic script is created to take xenserver guest backup using snapshots
#it will first of all create a snapshot for a specific vm, convert that snapshot to template, take backup of template & delete it
#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
BACKDATE=`date +%Y-%m-%d`
ADMIN=dpthakar@gmail.com
 
BACKDIR=/mnt/back/$BACKDATE/$HOSTNAME
echo -e "subject: backup done on $HOSTNAME \n" > /tmp/xen_back.mail
 
#create backup dir
if [ -d $BACKDIR ]
	then
		echo "Backup Dir Exists"
		echo "$BACKDIR Exists" >> /tmp/xen_back.mail
	else
		echo "Bakcup Dir does not exists, Creating"
		echo "$BACKDIR does not Exists, Creating" >> /tmp/xen_back.mail
		mkdir -p $BACKDIR
fi
 
 
#generate list of RUNNING VM ONLY
echo "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_back1
echo -e "\n List of the VM for backup" >> /tmp/xen_back.mail
cat /tmp/xen_back1 >> /tmp/xen_back.mail
 
if [ $? -eq 0 ]
then
echo -e "vm list sucessfully generated \n"
echo -e "vm list sucessfully generated \n" >> /tmp/xen_back.mail
else
echo -e "vm list failed \n" 
echo -e "vm list failed \n" >> /tmp/xen_back.mail
fi 
 
 
#create a snapshot for each vm
VMLIST=`cat /tmp/xen_back1 | wc -l`
COUNT=0
until [ $VMLIST = $COUNT ]
do
COUNT=`expr $COUNT + 1`
VM=`gawk FNR==$COUNT /tmp/xen_back1`
 
#create snapshot 
echo "Creating Snapshot for $VM"
echo "Creating Snapshot for $VM" >> /tmp/xen_back.mail
SNAPSHOT=`xe vm-snapshot vm="$VM"  new-name-label="$VM"-$BACKDATE new-name-description="temporary snapshot" `
#or you can use following
#xe vm-snapshot-with-quiesce vm="$VM"  new-name-label="$VM"-$BACKDATE new-name-description="temporary snapshot"
 
if [ $? -eq 0 ]
then
echo -e "Snapshot for $VM Sucessfully created \n"
echo -e "Snapshot for $VM Sucessfully created \n" >> /tmp/xen_back.mail
else
echo -e "Snapshot for $VM FAILED \n"
echo -e "Snapshot for $VM FAILED \n" >> /tmp/xen_back.mail
fi 
 
#set snapshot as template
echo "Setting snapshot $SNAPSHOT as Template for $VM backup"
echo "Setting snapshot $SNAPSHOT as Template for $VM backup" >> /tmp/xen_back.mail
xe template-param-set is-a-template=false uuid=$SNAPSHOT
 
if [ $? -eq 0 ]
then
echo -e "Setting snapshot $SNAPSHOT as Template for $VM backup was SUCESSFULL \n"
echo -e "Setting snapshot $SNAPSHOT as Template for $VM backup was SUCESSFULL \n" >> /tmp/xen_back.mail
else
echo -e "Setting snapshot $SNAPSHOT as Template for $VM backup was FAILED \n"
echo -e "Setting snapshot $SNAPSHOT as Template for $VM backup was FAILED \n" >> /tmp/xen_back.mail
fi 
 
#backup from snapshot 
echo "Backing $SNAPSHOT for $VM"
echo "Export UUID=$SNAPSHOT" >> /tmp/xen_back.mail
xe vm-export vm=$SNAPSHOT filename=$BACKDIR/"$VM".xva compress=true
 
if [ $? -eq 0 ]
then
echo -e "Backup snapshot $SNAPSHOT for $VM was SUCESSFULL \n"
echo -e "Backup snapshot $SNAPSHOT for $VM was SUCESSFULL \n" >> /tmp/xen_back.mail
else
echo -e "Backup snapshot $SNAPSHOT for $VM was FAILED \n"
echo -e "Backup snapshot $SNAPSHOT for $VM was FAILED \n" >> /tmp/xen_back.mail
fi 
 
#uninstall snapshot template
echo "Removing $SNAPSHOT for $VM"
echo "Remove UUID=$SNAPSHOT">> /tmp/xen_back.mail
xe vm-uninstall uuid=$SNAPSHOT force=true
 
if [ $? -eq 0 ]
then
echo -e "Uninstall of temporary snapshot $SNAPSHOT for $VM was SUCESSFULL \n"
echo -e "Uninstall of temporary snapshot $SNAPSHOT for $VM was SUCESSFULL \n" >> /tmp/xen_back.mail
else
echo -e "Uninstall of temporary snapshot $SNAPSHOT for $VM was FAILED \n"
echo -e "Uninstall of temporary snapshot $SNAPSHOT for $VM was FAILED \n" >> /tmp/xen_back.mail
fi 
echo -e "\n" >> /tmp/xen_back.mail
done
 
cat /tmp/xen_back.mail | sendmail $ADMIN
rm -rf cat /tmp/xen_back*

(Visited 1,354 times, 1 visits today)

Viewing latest article 2
Browse Latest Browse All 10

Trending Articles