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 08: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 11: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 18:20 (Reply)
Friday, September 17. 2010 at 19: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 15: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 08: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 09:10 (Reply)
Friday, November 26. 2010 at 07:27 (Link) (Reply)
Monday, December 20. 2010 at 06:34 (Link) (Reply)
Monday, December 20. 2010 at 07:03 (Reply)
-Mark
Tuesday, February 8. 2011 at 02: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 12:23 (Link) (Reply)
Works like charm
I really like the Custom field option!
Thursday, April 14. 2011 at 18:25 (Reply)
Thanks.
Friday, April 15. 2011 at 08:57 (Reply)
Monday, April 18. 2011 at 16: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 13:12 (Reply)
If you are interested in my version of the script I can send it to you...
Thursday, April 28. 2011 at 13:27 (Reply)
Monday, August 22. 2011 at 23:19 (Reply)
What could I be doing wrong?