Go Back   Computer Forums > Articles | Tutorials | Tech Notes > Articles | Computer Hardware and Servers
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-29-2011, 03:07 PM   #1
JCB
Daemon Poster
 
JCB's Avatar
 
Join Date: Oct 2004
Posts: 1,302
Default How to Setup Live Xenservers Backups

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
Quote:
#
# Settings
#
And underneath enter
Quote:
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
Quote:
# Set log path
And Make Sure The Next Line Reads
Quote:
log_path="/VM_Backup/"$VM_XENHOST"/"$date".log"
Find
Quote:
backup_dir="
And Make Sure That Line Reads
Quote:
backup_dir="/VM_Backup/"$VM_XENHOST"/"
Find
Quote:
backup_vms="running"
Change To
Quote:
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
Quote:
log_message "VM $uuid backup failed"
Underneath add the following

If Xenserver version is 5.5 add
Quote:
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
If Xenserver version is 5.6 add
Quote:
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
Quote:
log_message "VM $uuid backup succeeded"
Underneath add the following

If Xenserver version is 5.5 add
Quote:
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
sleep 10
coalesce-leaf -u $uuid
If Xenserver version is 5.6 add
Quote:
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
Quote:
#!/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
Quote:
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
Quote:
for %%f in (*.xva) do runner.bat %%f
Create a file called runner.bat and enter the following lines in to the file
Quote:
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
Quote:
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:
__________________

__________________
Check Out My Computer Related YouTube Channel @ Click Here
JCB is offline   Reply With Quote
Old 02-01-2011, 05:13 PM   #2
Fully Optimized
 
~Darkseeker~'s Avatar
 
Join Date: Jan 2010
Location: Welwyn Garden City, United Kingdom
Posts: 2,494
Default Re: How to Setup Live Xenservers Backups

wow thats alot to take in.

nice one, JCB.
__________________
EVGA SLI Micro Z68 // Intel i5-2500k @ 4.4GHz // 8GB Corsair Vengeance 1866MHz // Overclocked ASUS GTX 660 Ti // Corsair Carbide SPEC-03 // Kingston Hyper-X 120GB // 2TB WD Green + 500GB WD Black
#JC4PM
~Darkseeker~ is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:46 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0