Filippo Zanardo's Blog

to blog or not to blog

Posts Tagged ‘vm backup

XenServer Final Backup Script with or without snapshot

with 62 comments


Ok guys,
Xenserver 5.6 is out now, and i enhance my backup script i rewrite some part and add some more features/controls, that i will explain later.

UPDATED: 2.2 is now avaiable i rewrite the check avaiable space to fix a minor bug

You can donwload the 2.1 script here

http://xenbackup.googlecode.com/files/xenbackup2.1.zip

It’s simple to set up just modify config.txt and than you can run the script with this simple command line:

perl /path/to/backup.pl /path/to/config.txt

Now i will explain what you can configure and the features:

@skip: this variable contain the uuid of the machine to skip in the backup you can get with the command ‘xe vm-list’, add more uuid separeted by ‘,’

usesnapshot: if set to true backup script try to make a snapshot of the vm, else he shutdown the machine, export and power on the machine

removable: if set to true the script detach the removable storage, else snapshot fail, and then reattached in the correct order, just remember to set up the variable removableuuid with your removable uuid you can get with command ‘xe sr-list’

mailNotification: if set to true script send email to and from the email specified in the config file, i remove the script used before now script use ssmtp that is already installed in xenserver, just configure the file /etc/ssmtp/ssmtp.conf with your data and remember to uncomment this line FromLineOverride=YES

subfolder: Set to true to create a subfolder in the store for each backup based on vm name

versioning: Set to true to let the script manage to delete the backup older than a certain day or number or hours specified in the $delnumber variable

logging: if set to true the script write all the message with data into one log/file specified in the variable $logfile

automount: if set to true script try to mount the backupdir specified in mountcommand at start and umount at end, else no action taken and u have to mount dir manually

checkspace: if set to true the script check the avaiable space on the backup dir and if less than $spacerequired quit with a message, size is in MB

i add also some check to verify that the correct file is been written on the backup folder, if not backup error is notified.

That’s all i hope i explain well and you like the script, comment or post for bugs, feature etc etc.

Cheers!

Advertisements

XenServer Virtual Machine Backup

with 2 comments


Hi, my first post ever on this blog 🙂

I wanna share a simple script to backup virtual machine in Xen Server (5.5 actually).

This is a simple script check this one is better!

The script basically check if older backup exyst and delete the oldest one, than stop the virtual machine, export that to a file and then send an email about some basic stats for the backup.

Here’s the script:

#!/bin/sh

#Today’s date
TODAY=`date +%d%m%Y`
#start of script
time_start=`date +%s`
STARTED=`date`

#Control the folder backup for how may file contain
FILECOUNT=`ls /backup| wc -l`;
#check if files are more than 2
if [ $FILECOUNT -ge 2 ]
then
#remove oldest file as long as we need 2
cd /backup && ls -tr | head -n 1 | xargs rm -f;
echo “Removing oldest file”
else
echo “There are less than 2 file”
fi

#shutdown the virtual machine
xe vm-shutdown vm=”nameofyourvm”
#export vm into a file ex: yourname22112009
xe vm-export vm=”nameofyourvm” filename=/backup/nameofyourvm$TODAY.xva
#start the vm
xe vm-start vm=”nagios”

ENDED=`date`
#end of script
#calculate time execution of the script
time_end=`date +%s`
time_elapsed=$((time_end – time_start))
echo “script executed in $time_elapsed seconds”
echo $(( time_elapsed / 60 ))m $(( time_elapsed % 60 ))s

# email subject
SUBJECT=”BACKUP MY VM”
# Email To ?
EMAIL=”backu@mycompany.com”
# Email text/message
EMAILMESSAGE=”/tmp/emailmessage.txt”
echo “Backup OK” > $EMAILMESSAGE
echo “STARTED at: $STARTED” >> $EMAILMESSAGE
echo “ENDED at: $ENDED” >> $EMAILMESSAGE
echo “Execution time =” $(( time_elapsed / 60 ))m $(( time_elapsed %
60 ))s >> $EMAILMESSAGE
mail -s “$SUBJECT” “$EMAIL” < $EMAILMESSAGE

Just copy the code, change name and path and add it to a scheduled cron job, et voilà!

Hope it helps!

Written by pipposan

October 12, 2009 at 14:23

Posted in XenServer

Tagged with , ,