Filippo Zanardo's Blog

to blog or not to blog

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!

Advertisements

Written by pipposan

October 12, 2009 at 14:23

Posted in XenServer

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. Hello,

    when I copy your code in a file, change the path and VM and execute the script i get these messages:

    [root@xen02 scripts]# ./export_BackUpTest03.sh
    : command not found03.sh: line 2:
    : command not found03.sh: line 8:
    : command not found03.sh: line 10:
    ./export_BackUpTest03.sh: line 48: syntax error: unexpected end of file

    I am using Citrix XenServer 5.5.

    Mario

    November 24, 2009 at 15:19

    • seems bad text format, try this and let me know:

      tr -d ’15’ yourscript.sh yourscript2.sh

      or:

      awk ‘{ sub(“\r$”, “”); print }’ winfile.txt > unixfile.txt

      pipposan

      November 27, 2009 at 10:18


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: