Updated Citrix XenServer 5.5 Automatic VM Backup Scripts

I have re-written the popular Citrix XenServer 5.5 Automated Live VM Backup scripts from scratch, with some big improvements:

  • Separate backup configuration script – So you don’t need to touch the key execution and function scripts to configure backups.
  • VDI removal – Run in addition to the standard vm-uninstall command to stop snapshotted VMs allocating all your disk space
  • Logging – Enable/disable functionality with more advanced function logging and file path customisation.
  • Backup VM selection – The facility to select between all, running, none and specifically set (by VM uuid) virtual machines.
  • Quiesce snapshots – To take advantage of the quiesce snapshot functionality if the VM supports it.
  • HA disabled – High availability is disabled on exported virtual machines to stop configuration issues when re-importing.

This is a complete re-write from scratch and as such is relatively untested other than on my systems – please provide feedback regarding any successful or problematic installations.

The latest release of the updated Citrix XenServer Backup Scripts can be downloaded from http://www.andy-burton.co.uk/files/xenserver_backup/xenserver_backup.tar.gz as a tar gzip archive.

Installation instructions:


[root@xs /]# mkdir /home/backup
[root@xs /]# cd /home/backup
[root@xs backup]# wget http://www.andy-burton.co.uk/files/xenserver_backup/xenserver_backup.tar.gz
...
23:45:25 (12.9 MB/s) - `xenserver_backup.tar.gz' saved [3181/3181]
[root@xs backup]# tar -xzf xenserver_backup_3.0.tar.gz
[root@xs backup]# chmod 700 vm_backup.*
[root@xs backup]# ./vm_backup.sh
[root@xs backup]# nano vm_backup.log

Open and edit vm_backup.cfg to configure the backup.

Run vm_backup.sh to run the backup process, this script does not need to be modified.

The virtual machine backup functions are located in vm_backup.lib and do not need to be modified.

As usual, all comments, negative, neutral or positive are all very welcome.

For those that require it, I will write additional information and a more detailed installation procedure in due course.

Update Log:

  1. John says:

    Looks great!
    I’ll give it a try soon

  2. Torsten says:

    Thank you for your script, it works great on my two XenServers. A little bit strange is that after running your script an empty file named “0″ appears in the script directory. Improvements: Cool would be an integrated rotation of the vm backups.

    Cheers,
    Torsten

    • Andy Burton says:

      Interesting! Is there anything in the 0 file? What user/group owns it?

      The new structure with the config file is so that i can modify the executeable script to accept seperate config files. That way you can setup seperate cron jobs for different dates to run different backup processes. Is that what you were after?

      Cheers, Andy

      • Torsten says:

        The 0 file is empty. User + Group is root (-rw-r–r– 1 root root 0 Nov 30 13:03 0). Backup went through flawlessly. The 0 file appeared on different XS 5.5 (all hotfixes) machines.

        At the moment I’m rotating the xva files via “find /backup/VMs/*.xva -mtime +3 -exec rm {} \;” which is quite dangerous. I’m looking after a setting that rotates a vm backup due to a defined counter. E.g. to have 3 backups of a vm rotated. This “find … ” thing works, but won’t check if a newer backup exist, for example.

        Cheers, Torsten

  3. Prakash says:

    This worked great for me. As I am new to unix I have a small issue which keeps me incomplete with this task. I am backing up my VMs to a NAS storage. I was able to mount the share folder and export also working fine. The issue is I lost the mount information if I reboot the Xen server. I’ll need to again input the mount commands in order to get the share folder mounted.

    Please let me know how to get the share folder permanently.

    Cheers, Prakash

  4. Daniel says:

    The install instructions should be:

    mkdir /home/backup
    mkdir /backup
    [root@xs /]# cd /home/backup
    [root@xs backup]# wget http://www.andy-burton.co.uk/files/xenserver_backup/xenserver_backup.tar.gz

    23:45:25 (12.9 MB/s) – `xenserver_backup.tar.gz’ saved [3181/3181]
    [root@xs backup]# tar -xzf xenserver_backup.tar.gz
    [root@xs backup]# chmod 700 vm_backup.*
    [root@xs backup]# ./vm_backup.sh
    [root@xs backup]# nano vm_backup.log

  5. Prakash says:

    Hi

    This worked great for me. Thanks for such an awesome script. I am using NAS storage to store my backups. I was able to mount the share folder successfully using the mount commands you have provided. But I have problem that I lost the mount information if I reboot the xen server. I am new to unix. Please advice how to eternally mount the share folder on xen server.

    Now I manually inputs the mount commands whenever I reboot the xen server to get the share folder mounted.

    Cheers, Prakash

  6. Brian says:

    Very nice. I do have one issue though. I have one machine that was imported using XenConvert, and it appears the XVA is only including one of the drives. This machine has two. I noticed that the OS drive is in position hda and the data drive is in position 2. The data drive appears to be the only drive getting into the XVA.

  7. Will says:

    Andy,

    Would this script work with on a Shared Pool?

    • I am in the process of setting up our shared pool so will be trying this out soon.

      From what i can gather –
      I guess that it will work through each machine individually, so to get round this, just install on all of your pool servers and mount them to the same share and then stagger the cronjob on each machine. set the backup level to ‘running’.

      I will post my findings when when i have set it up

      Will says:
      16/12/2009 at 6:31 pm

      Andy,

      Would this script work with on a Shared Pool?

  8. Vadim Kolontsov says:

    Very nice script, works like a charm. Thanks a lot!

  9. Awesome script, thank you.. More than appreciated…

  10. Marcoserver says:

    Very nice script, but unfortunately i have i big problem during the export of VM.

    Script is able to create a snapshot but not to export it into an .xva file. I give ” Permission denied: open, /media/backup_vm/ITCXTEST02-2010-01-07_11-47-15.xva.”
    I mounted an NFS share on /media/backup_vm/, and the strange thing is that i can create a directory within this share but not export snapshot.

    Have you got any advice about this issue?

  11. Lucky Luciano says:

    Hi

    This worked great for me. Thanks for such an awesome script. I use this script for disaster recovery but when I import the vm_????-??-??_??-??-??.vxa file to another xenserver, it is imported as a snapshot and I can’t start it.

    Maybe I’m wrong here…

    Thanks a lot!

    • Lucky Luciano says:

      I’m sorry, it’s my fault. I didn’t realise that the “operation” was unsuccessful.

      Best regards.

  12. Kevin says:

    Outstanding script and great work.

    Any possible suggestions to compress these files once they land? I have seen some vmware scripts that compress the files once they have been backed up and could save a ton on space. Thanks again.

  13. Nathan says:

    I am trying to get this to work without having to have the NFS backup share mounted to each xenserver, but attached to the pool, is this possible?

  14. Chris says:

    Nice script, very usefull.

    I would just recommand to upgrade first to XS 5.5 Update 1 to fix the problem of wasted space for snapshot on some kind of storage repository.

  15. Ernie says:

    Sorry guys, I am Linux/Unix dumb. Can i run these commands from the XenServer console or do i need to ssh from my PC? Would anyone be willing to do some actual step by step instructions for us dumb folk? Thanks for an awesome much needed function!!

  16. Ronald says:

    How do I restore my VM’s? Is it possible to make a script for that?

  17. John Georgiadis says:

    Nice script. Thanks

    You can add the following in vm_backup.cfg to support remote backups:
    xe_remote=”-s xen.company.com -u user -pw password”

    And in vm_backup.lib, replace all occurrences of ‘xe ‘ with:
    xe $xe_remote

    E.g.
    xe vm-list $xe_remote power-state=running is-control-domain=false 2>>$log_path

  18. Naga says:

    Perfect, just the one I’ve been looking for …many thx. If the script can become a little interactive for adding backup location like a USB HDD it will be great for people like me working with non-profit organisation..thx again.

  19. James Smith says:

    We have a small XenServer pool, 5 nodes at present.

    All VM storage is based on two iSCSI SANs. I am backing up the VMs, using this script, to an NFS share on another server.

    I have added the NFS share as a “NFS VHD” in XenCentre. This enables each node access to the share, although it is not neccessary for them all to have access to it.

    The script will gather a list of all running VMs across the pool and back them up to the NFS share mount via the master node.

    All you do is change the backup location to the mount location, which will be: /var/run/sr-mount/uuid, full details can be obtained by running: df -h

    Some handling on retention would be good also. As these snapshots will start to take up lots of space.

    Regards,
    James

  20. Thanassis says:

    It works like a charm, everything I need is there. My setup is one XenServer with 5 virtual machines on it, Linuxes and Windows 2008. Thank you very much.

  21. Tobbe says:

    Nice script. Worked great. New to linux. Any tips on how to compress backup (remove “white space” from guest)?

  22. Michael Olesen says:

    Hello.

    Very nice script.

    Im using HP XenServer version (USB dongle) and most of the filesystem is read-only. (no directory /home)

    I can make a folder in /etc/…

    Is it possible to modify the script.?

    Regards Michael

  23. Michael Olesen says:

    Awesome.

    I’ll give it a go :-)

  24. ubay25 says:

    To make sure that your backup destination folder is always mounted when running the script, insert this on the vm_backup.sh (just after the load functions and config lines)

    umount /backup
    mount -t nfs 192.168.x.x:/mnt/vol/nfs/vm-backup /backup
    if [ "$?" != "0" ]; then
    log_message “mount failed”
    exit
    fi

  25. Kris Guntzelman says:

    LOVE THIS!!!!! I’m having trouble getting CRONTAB to actually run automatically – still need to play. Did the automout of the CIFS share in FSTAB – works wonderfully.

    Suggested additions:
    –Upon script failure (unable to write to backup folder for whatever reason), the snapshot stays with the VM rather than being delieted. Its like the script stops but doesn’t remove the parts it created. I haven’t tested that with multiple VMs yet, but if it didn’t complete the removal of the snapshot from the one machine, I’m thinking it could potentially halt the scritp completely on any failure (again, not positive on that).

    –My CIFS shares utilize “previous versions” and a “recycle bin”. This means when I replace a file with a newer version, the old one is maintained for 90 days. It would be cool if I could make these backups with the same filename and let my windows server (where they’re being backed up) handle the multiple backups (previous versions). However, in testing, the script fails with a “file already exists” type of message. Not sure how to force an overwrite. I did remove the date/time stamps from the filename so it was just {vmname} for the backup, which should always overright itself.

    Just my .02 THANK YOU SO MUCH for making this…

    If anyone has any thoughts on why my CRONTAB isn’t running at night, let me know.

    Thx.

    Kris

  26. Gideon says:

    I’m testing the script for disaster recovery purposes. on Xenserver 5.5 sp2.

    When i double-click the XVA, and import it into another Xenserver, it starts importing the machine, but after about 10 seconds i get the error message: The file could not be imported.

    I’ve tested this on different machines. And both with the import template or import VM option. When i manually make an export, it just works fine importing the machine.

    Thnx!

    Gideon

  27. Tracy Acord says:

    If anyone here is trying to do a backup to a NAS and the format is XFS (Buffalo NAS) and you are having issues with your backup failing, the problem has to do with the mount format.

    instead of doing this:

    mount -t cifs //NAS IP/backup/Xen -o username=USERNAME,password=PASSWORD /backup

    remove the “-t cifs” because the format isn’t cifs. by removing, linux will figure it out”

    So, just use:

    mount //NAS IP/backup/Xen -o username=USERNAME,password=PASSWORD /backup

    This script works well.

    I modified vm_backup.sh and directly under the Load function, I added the following:

    umount /backup
    mount //192.168.10.5/backup/Xen -o username=admin,password=passowrd /backup

    You will need to change the ip address to reflect your NAS and if you didn’t change the Buffalo NAS login and password, this is the default.

    I also added the cron job per instructions with this small change:

    type: crontab -e
    MAILTO=your@emailaddress.com
    0 2 * * * /home/backup/vm_backup

    Some people might not understand how to use the vi commands to add text and save, this is what you need to do as an exampe for the crontab function.

    type crontab -e to access the crontab file, or if you need to open a file, type vi “filename”

    once in the file, to insert a line, just press i and hit enter, you can then arrow up the the space created, hit “esc” then type your command as normal, otherwise, just type out what you need to do.

    when finished with the file and you want to save, hold down shift and press “q”.
    to save, just type “w”, then “q”. to quite without saving, just type “q!

    Hope this helps,

    Tracy

  28. ubay25 says:

    Hi, in addition to my comment above I have added more functions that I think would be helpful to you guys out there!

    After the load functions I have added the following:
    1. Check if the share is mounted
    2. Delete files based on age (useful when you don’t have much space)
    3. Check the share’s available space is sufficient

    *******************************************
    # Load functions and config

    . $dir”/vm_backup.lib”
    . $dir”/vm_backup.cfg”

    # Check to see if the destination of the backup is mounted

    umount /backup
    mount -t nfs 192.168.x.x:/share/folder /backup
    if [ "$?" != "0" ]; then
    log_message “Mount failed.”
    exit
    fi
    log_message “Backup destination mounted.”

    # Delete backup files older than 7 days
    find /backup/* -type f -mtime +7 -exec rm -f {} \;
    sleep 10

    # Check to see if available space on the backup destination folder is sufficient

    spaceleft=$(df -B M | grep backup | awk ‘{ print $3 }’ | sed ‘s/M//g’)

    # Enter the number in MB the required space for the backup
    space_required=307200
    if [ $spaceleft -lt $space_required ]; then
    log_message “Available space on the backup destination is insufficient.”
    exit
    fi
    log_message “Backup destination has sufficient space.”

    *******************************************

    • Richard Potter says:

      Thanks Andy and ubay25! I am using this script with ubay25′s additions, and it is working great. I am using a nfs mount to a D-Link DNS-321 2-Bay Network Storage Enclosure I had kicking around to store the backups.

      • ubay25 says:

        It’s good to know it’s working for you. Probably next on the list is to deduplicate the backups!

  29. Shippyt says:

    Thanks Andy, the script works a treat. Is there a way to get the script to run automatically on a daily/weekly basis?

    Cheers

    Tim

    • ubay25 says:

      Shppyt, please read the posts. There’s a detailed instruction on how to schedule the script to run.

  30. David Majchrzak says:

    Hi Andy,

    I just saw that XS 5.6 beta has online snapshots with VDI and memory state included now. Seems they’re getting somewhere ;)

    How exactly are the VDIs copied since they’re active during the progress? I’ve recently tried your script and restored two machines, one of which had active websites during the backup. It came up with a need to run a filecheck while the other one was fine since it was more or less just and OS install without services on it. Are we getting inconsistent data?

    Kind regards
    David Majchrzak

  31. Tracy Acord says:

    Andy,

    I like your script but I have ran into an issue where when I restore a backup from this script, if the server had drive c and drive d, i get a double copy of drive d.

    Now, I have noticed that if I actually stop the virtual server and do an export, I have no problem restoring to any Xen 5.5 server.

    Do you have any script that I can automate with a cron job to shut down my virtual servers in 5.5 and export as backup? I trust the export as backup because I have never had an issue with restoring them.

    Also there is a bug with Xen in regards to snapshots. You have to enable Xen VSS on the server first and if you ever try to do a standard windows backup (multiple backup methods are recommended), you get a windows backup failure because VSS isn’t correct. You have to run the bat file under Xen to turn off Xen VSS and turn back on windows VSS. Real pain in the Arse!

    If you or anyone has a backup job (export) virtual servers after shutting them down, then turning them back on after backup, I would appreciate it.


    Fatal error: Maximum execution time of 30 seconds exceeded in E:\home\andyburton\public_html\blog\wp-includes\comment-template.php on line 618