How to Setup Live Xenservers Backups

JCB1

Daemon Poster
Messages
1,302
In this guide I will show you how to setup live xenserver backups and schedule them to run automatically. You can use the below script to backup vms when they are live (turned on), the backup will also be a full backup of the xenservers vm (Exports to a .xva file) so you can import the vm on to another xenserver if you wish.


Introduction To : How to Setup Live Xenservers Backups


The below script below was written by Andy Burton Andy Burton - PHP & MySQL Development, Servers and Virtualisation I will show you how to install and configure the script which will backup your live xenserver vms to an external cif share as well as how to configure the script for Xenserver versions 5.5 and 5.6.

I have found a bug in xenserver 5.5 and 5.6 where if you take a snapshot of a VM the snapshot will take up diskspace on the storage, But when you delete the snapshot the diskspace it was using was not free'd up. To get the free disk space back you need to run a command on the xenservers console. I will show you how you can add this command to the backup script.


What Does this Xenserver Live Backups Script Do?


The script can be run manually or automatically via a linux cronjob. The first thing it will do is to take a snapshot of the live vm, The snapshot will put the live vm in to suspended state which means the vm will be down for 5-10 seconds, as soon as the snapshot has finished the vm will be returned to the running / live state. This snapshot will then be combined with the vms core metadata and exported to your backups folder. Another command will then be run to delete the snapshot and reclaim the disk space it was using.


How to Setup Live Xenservers Backups


Connect to the console of the xenserver you want to configure live backups for, and log on to the command prompt. Now run the following commands

Code:
>> mkdir /home/backup
>> mkdir /VM_Backup
>> mount -t cifs "//192.168.0.20/VM_Backup" -o username=username,password=password /VM_Backup *
>> cd /VM_Backup
>> wget http://www.andy-burton.co.uk/files/xenserver_backup/xenserver_backup.tar.gz
>> tar -xzf xenserver_backup.tar.gz
>> chmod 777 v*
>> cd /home/backup
>> vi vmbackups.sh **

* Replace 192.168.0.20 with the IP address of the server with the CIFS share, and also replace VM_Backup with the share name. This command will mount the CIFs share to the local /VM_Backup folder. If you dont want to backup your xenserver vms to a CIF share dont run this commend and your backups will be stored on the local disk. You will also need to change the username and password.

** This file is used to easily start the live backups of your xenserver vms. Simply run the above command and then put the following in to the file.

Code:
#!/bin/bash
set -x

  umount -f /VM_Backup

mount -t cifs "//192.168.0.20/VM_Backup" -o username=username,password=password /VM_Backup

/VM_Backup/CLEARDOWN.sh

  /VM_Backup/vm_backup.sh

  /VM_Backups/CLEARDOWN.sh

  sleep 120

  /VM_Backups/CLEARDOWN.sh

Again replace the IP / share / username and password as required. I always unmount the cifs share and remount it just to make sure it is mapped. The CLEARDOWN.sh script is what we will use to reclaim disk space from deleted snapshots and vm_backup.sh is the script that will start the live backups of our xenserver vms.


How To Configure The Scripts For Live Xenserver Backups


We are now going to edit the script in a way that any xenserver can run this script and it will place the backups in to a folder with the xenservers name. While you are still connected to the consoles command prompt run the following

Code:
>>  cd /VM_Backup
>>  ls

You should see 3 files named vm_backup.cfg vm_backup.lib vm_backup.sh we now need to edit these files. We will edit the vm_backup.cfg file first, make the following changes

Code:
>> vi vm_backup.cfg

Find At The Top
#
# Settings
#

And underneath enter
VM_XENHOST=$(xe vm-list | grep 'Control domain on host' | awk -F ': ' '{ print $3 }')

VM_XENUUID=$(xe vm-list params uuid=$(xe vm-list | grep -B1 $VM_XENHOST | grep uuid | awk -F ': ' '{ print $2 }') | grep resident-on | awk -F ': ' '{ print $2 }')

The above command will get the uuid of the xenserver host as well as the xenservers host name, This uuid will be used later to clean up a removed snapshot and reclaim diskspace.

Find
# Set log path

And Make Sure The Next Line Reads
log_path="/VM_Backup/"$VM_XENHOST"/"$date".log"

Find
backup_dir="

And Make Sure That Line Reads
backup_dir="/VM_Backup/"$VM_XENHOST"/"

Find
backup_vms="running"

Change To
Set This to what you want, I keep it set to running

save the changes and exit the editor. (To save changes press etc then type :wq > enter)

Now we need to edit the vm_backup.lib file, type the following

Code:
>> vi vm_backup.lib

Find
log_message "VM $uuid backup failed"

Underneath add the following

If Xenserver version is 5.5 add
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid

If Xenserver version is 5.6 add
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid
sleep 10
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid
sleep 10
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid

Find
log_message "VM $uuid backup succeeded"

Underneath add the following

If Xenserver version is 5.5 add
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid

If Xenserver version is 5.6 add
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid
sleep 10
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid
sleep 10
xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid

save the changes and exit the editor. (To save changes press etc then type :wq then hit enter) Then at the command prompt type the following

Code:
>> chmod 777 CLEARDOWN.sh

These lines we are adding above will clean up the shapshots after they have been deleted and reclaim free space. We need to run them more than 1 time as sometimes this command fails, but run it again and its ok, I think something might have a lock on the file??


Create the CLEARDOWN.sh Script For Xenserver Live Backups


We need to create a script that can be run on our xenserver hosts that will reclaim disk space from deleted snapshots. I then run this script before I backup my live xenserver vms just to make sure that free disk space will not be an issue when I run my backups. To create this script log on to the xenservers console and go to the command prompt and type the following

Code:
>>  cd /VM_Backup
>>  vi CLEARDOWN.sh

then in this file put the following
#!/bin/bash

coalesce-leaf -u $uuid
coalesce-leaf -u $uuid
coalesce-leaf -u $uuid

xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid

xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid

xe host-call-plugin host-uuid=$VM_XENUUID plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid=$uuid


Running Live Xenservers Backups


We are now ready to run a backup on our live xenserver vms :) We can do this either manually or schedule them to run automatically.

Manually Run The Backups

To run the backups manually run the following commands

Code:
>> cd /home/backup
>> ./vmbackups.sh

Automatically Schedule The Backups To Run

I schedule my xenservers to backup once a month on a particular day and time (Out of hours) To do this type in the following at the command prompt.

Code:
>> crontab -e

and enter the following
00 19 3 * * /home/backup/vmbackups.sh

This will then run the backups every 3rd day of the month @ 19:00. For more examples of schedules take a look at......................................................................................


How To Zip Up The Live Xenserver Backups

When a xenservers vm has been backuped the file will be a minimium size of 8gb. When the backup has finished we can then zip all the backup files which usually saves us 50% - 70% of disk space. I have written a batch file that will automatically zip all the *.xva files in a directory and then delete the .xva file once it has been zipped. You need to log on to the windows box. I then use windows scheduled tasks to run the below batch file. We need to create 2 batch files

Create a file called zip.bat and enter the following line in to the file
for %%f in (*.xva) do runner.bat %%f

Create a file called runner.bat and enter the following lines in to the file
WINZIP32.EXE -a %1.zip %1
del /a /f /q %1


How To Automatically Delete Old Xenserver VM Backups

I use another script to delete old xenserver vm backups that are 99 days old, In the script below it looks in the "E:\VM_Backups" folder for any *.zip files that are 99 days or older, If these files exist they will be deleted.

Create a file called cleardown.bat and enter the following line in to the file, change as needed
Forfiles -p "E:\VM_Backups" -s -m *.zip -d -99 -c "Cmd /C del @FILE"


General Tips For : How to Setup Live Xenservers Backups


1. Make sure your default SR has enough free space to take snapshops of your VMs. Only one snapshot will run at a time as they get deleted after the backup has run. Look at the VM which uses the most storage and esitmate how big the snapshot will be and make sure there is enough free space.

2. Run the backups out of hours, There is a small amount of downtime per vm that gets backuped. When the snapshot is run the vm will be put in to suspended state, the snapshot usually takes 5-15 seconds to run, on vms with larger storage it should take longer to run

3. I would advise you backup the vms on to remote storage, this way if the xenserver does have a problem you can restore the VMs on to another xenserver

Frequently Asked Questions : How to Setup Live Xenservers Backups

Q:
 
Back
Top Bottom