We have recently started using Citrix Xenserver in production at work (fantastic product, see my review for more information) and needed a simple backup solution. Our VMs run from an iSCSI SAN and are backed up daily through various methods - e.g. Bacula for the Unix/Linux systems. However, we wanted the ability to quickly roll back to a previous VM snapshot, and get up and running quickly if our SAN failed for whatever reason. Our solution was to create a large shared NFS storage repository, and periodically snapshot VMs and copy the templates over to this SR. Doing this means that if the SAN fails, we can create a new VM quickly from this NFS store (using the Xenserver's local disks, or even the NFS SR itself as storage). Once up and running, we can bring VMs back up to date by restoring the latest backup to them.
In order to automate this, I wrote a quick script which I thought may prove useful to someone else, so decided to post it here : snapback.sh.
Update: This script is now being hosted at GitHub. This means you can check out the latest version from there, by doing :
git clone https://github.com/markround/XenServer-snapshot-backup.git
or accessing the raw script file at https://github.com/markround/XenServer-snapshot-backup/raw/master/snapback.sh
It is very simple, and although it may serve well as your only backup solution, it's really intended as an image-level compliment to your primary file-system based backup system such as Bacula, Amanda, Netbackup etc. It also has not had much testing, and I fully appreciate the scripting is pretty rudimentary and could do with some optimisation - there's no error checking, for instance. I kept it pretty verbose on purpose though, so you can get a good idea of exactly what it's doing at each step; it may be better to think of this as a template you can base your own scripts off!
Overview
The script creates a snapshot of a running VM on a configurable schedule, and then creates a template from this snapshot. It will copy all these backup templates over to a configurable storage repository, and then clean up any old backups according to a specified retention policy. These backups are full backups, so if you have a 10GB VM and keep 7 previous copies you will need a total of 80GB disk space on your backup VM. Non-running VMs, and those not configured (as detailed below) will be skipped.
Important: See http://support.citrix.com/article/CTX123400. After backing up each VM, you will end up with a new VDI, so you may need to manually coalesce your VDIs again to reclaim disk space.
Installation and usage
First, copy the script to your Xenserver pool master, and make it executable. A good location for this is /usr/local/bin/snapback.sh.
Next, create a cron entry for the script - to make it run daily just after 1AM, you'd create /etc/cron.d/backup with the following contents :
2 1 * * * root /usr/local/bin/snapback.sh > /var/log/snapback.log 2>&1
This will also record a log of it's actions to /var/log/snapback.log. You now need to edit the script and change the DEST_SR variable to the UUID of your backup storage repository. You can find this value by clicking on the SR in Xencenter; the UUID will be displayed as a value like "2c01dc26-f525-70d6-dedf-00baaec76645".
Lastly, you need to configure your backup and retention policy for your VMs. In Xencenter, right click your VM, and select "Properties". Click on "Custom Fields", and then "Edit Custom Fields". You should add two text fields :
- backup : Can be one of "daily", "weekly", or "monthly". If it is set to weekly, it will by default run on a Sunday, and if it set to monthly, it will run on the first Sunday of the month. This day can be changed at the top of the script - see the WEEKLY_ON and MONTHLY_ON variables.
- retain : How many previous backups (in addition to the currently running backup) to keep. So, setting this to a value of "2" would mean that after a backup has run, you would end up with 3 backups in total.
The script will look for these fields when it is run, and will skip any VM that doesn't have them set. You can also see them in the Xencenter summary and properties for the VM :
You can now either run the script manually, or wait until the cron job kicks off. It will produce a detailed log to the console (or log file if run through cron), and when it's finished, you'll see your template backup VMs listed in Xencenter, similar to this :
If you find that this clutters up the Xencenter view a little, you can always hide them (View->Server View->Custom Templates). To restore a VM from a backup, just right click, and choose "New template from backup". Anyway, I hope this helps someone else!
Monday, September 6. 2010 at 09:40 (Link) (Reply)
This one allows you to back up to an external hardrive or even span muliple hardrive's togeather to make one large backup volume
Monday, September 6. 2010 at 12:31 (Reply)
You should also consider the drawback of using a concatenated LVM volume - if one drive dies, you may well loose all your backups. Far better to create a RAID setup the can handle drive failure. Of course, that may be acceptable for your situation...
Friday, September 17. 2010 at 19:20 (Reply)
Friday, September 17. 2010 at 20:04 (Reply)
Glad you're finding it useful. Yes, you can use these snapshots on a new system - just re-install XenServer, re-attach the SR and create a new VM from the desired backup template.
-Mark
Wednesday, November 17. 2010 at 16:15 (Reply)
I would be pleased to understand how to use backup VMs saved over NFS SR in case of XenServer host crash-down.
I tried to attach NFS SR to an other XenServer host(not in a pool) but I do not succeed to create a new VM from this NFS store.
Thursday, November 25. 2010 at 09:55 (Link) (Reply)
Is your script works on XenServer 5.5?
I haven't upgraded to 5.6 yet since I read on the Citrix forum many have performance issue after upgrading.
Cheers,
Jay
Thursday, November 25. 2010 at 10:10 (Reply)
Friday, November 26. 2010 at 08:27 (Link) (Reply)
Monday, December 20. 2010 at 07:34 (Link) (Reply)
Monday, December 20. 2010 at 08:03 (Reply)
-Mark
Tuesday, February 8. 2011 at 03:55 (Reply)
I'm Citrix Enginner in korea.
Your backup script is very well.
Thanks for your help but I want one thing. Now script paly snapshot all VM in XenServer Pool.How can I play snapshot one server in XenServer Pool?
I waanna your help.plz..
Wednesday, February 23. 2011 at 13:23 (Link) (Reply)
Works like charm
I really like the Custom field option!
Thursday, April 14. 2011 at 19:25 (Reply)
Thanks.
Friday, April 15. 2011 at 09:57 (Reply)
Monday, April 18. 2011 at 17:15 (Reply)
It seams that ISCSI don't get mounted on /var/run/sr-mount/
The only SR I see on that path is my NFS ISO library...
Thursday, April 28. 2011 at 14:12 (Reply)
If you are interested in my version of the script I can send it to you...
Thursday, April 28. 2011 at 14:27 (Reply)
Thursday, April 18. 2013 at 16:52 (Reply)
This script is really good and has been very useful. Recently though the script has not been destroying the snapshots once it meets its retention, and as a result it has been hanging the Xenserver.
Unfortunately I haven't much joy with the logs as I had to clear the filesystem just to get Xenserver to start up again.
I'm fairly new to Xenserver so could you provide any advice?
Friday, January 27. 2012 at 08:25 (Reply)
Monday, February 27. 2012 at 12:51 (Reply)
http://sysadminnotebook.blogspot.com/2011/06/xenserver-backup-solution.html
I've also have a script to set the custom fields here:
http://sysadminnotebook.blogspot.com/2011/07/set-xenserver-vms-custom-fields-with.html
Tuesday, August 23. 2011 at 00:19 (Reply)
What could I be doing wrong?
Wednesday, October 19. 2011 at 11:49 (Reply)
script message:
= Creating snapshot backup =
Created snapshot with UUID : xxxx
= Copying snapshot to SR =
You attempted an operation that was not allowed.
reason: Cannot copy a VM's disks to an ISO SR
Done.
The DEST_SR is exist,mounted well and writable
Friday, November 18. 2011 at 17:53 (Reply)
Thanks
Friday, December 9. 2011 at 15:35 (Link) (Reply)
Thanks for publishing this.
Do you or anybody know if this will work with XenServer6.
Also does XenServer6 (Free) offer any other ways of achieving this?
Friday, December 16. 2011 at 23:50 (Reply)
Has anyone used this script with XenServer 6? Seems to stop when copying snapshot to the SR.
Thanks
Wednesday, February 1. 2012 at 17:03 (Reply)
Thursday, February 16. 2012 at 20:49 (Reply)
Friday, February 17. 2012 at 23:29 (Reply)
Checking snapshots for CentOS-Server =
Done.
= Creating snapshot backup =
Created snapshot with UUID : 13a588a5-0505-293a-464a-f4d1630b2edf
= Copying snapshot to SR =
The uuid you supplied was invalid.
type: SR
uuid: UUID:2882635b-49eb-2add-f3ec-c83e8e0b8107
Done.
= Removing temporary snapshot backup =
Deleting snapshot VDI : b179cbe4-5a36-4fcc-bd11-264a18ee435f
Removing snapshot with UUID : 13a588a5-0505-293a-464a-f4d1630b2edf
The following items are about to be destroyed
VM : 13a588a5-0505-293a-464a-f4d1630b2edf (CentOS-Server-snapback)
All objects destroyed
Done.
= Removing old backups =
= Renaming template =
The uuid you supplied was invalid.
type: VM
uuid:
Done.
== Backup for CentOS-Server finished at Fri Feb 17 15:13:48 MST 2012 ==
snapback.sh: line 208: /var/run/sr-mount/UUID:2882635b-49eb-2add-f3ec-c83e8e0b8107/mapping.txt: No such file or directory
snapback.sh: line 209: /var/run/sr-mount/UUID:2882635b-49eb-2add-f3ec-c83e8e0b8107/vbd-mapping.txt: No such file or directory
=== Snapshot backup finished at Fri Feb 17 15:13:48 MST 2012 ===
Tuesday, September 18. 2012 at 22:02 (Link) (Reply)
Your solutions is simple and awesome! Works very good for me. Thank you some much for sharing!
Wednesday, October 3. 2012 at 12:26 (Reply)
In Xencenter for the VM add another custom field named; backup_day
in your script comment the lines:
WEEKLY_ON="Sun" and MONTHLY_ON="Sun" (default days, could be different"
And add the following line under SCHEDULE=$... and RETAIN=$...
[code] DAY=$(xe vm-param-get uuid=$VM param-name=other-config param-key=XenCenter.CustomFields.backup_day) WEEKLY_ON=$DAY MONTHLY_ON=$DAY [/code]
It's fairly simple as I'm not a script kiddy, but
Wednesday, October 17. 2012 at 20:20 (Link) (Reply)
Can you please provide me a copy of your script, I am very stuck. ´snapback-vxa.dh : line 55: syntaxys error near unexpected token '
´snapback-vxa.sh: line 55: ´function xe_param()
thanks in advance.