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:

104 thoughts on “Updated Citrix XenServer 5.5 Automatic VM Backup Scripts

  1. Pingback: How to backup Xen Server VMs | Steve Garriques's Professional Blog

  2. Very nice! I have it running with three servers with an NFS SAN, backing up ~30 guests to a local USB disk on one of the servers, and the darn thing just works. I like it!

    I bought one of those USB “Toasters” that you just drop the drive into, and swap those out daily. Minor tweak to the script to mount and unmount when it’s done and off we went.

    Much appreciated!

  3. Great Script – works a charm for exporting the VM. However it is failing everytime when trying to import them back in, Are there some options that I need to enable or disable? Running 5.6 but tried it on 5.5 to no avail.

  4. Pingback: How to backup XenServer VMs | Stephen Garriques

  5. Pingback: Backing up live servers on Citrix XenServer for free « Layer 8 Consulting

    • Andy, I’m still using your great script! I have built it up a little bit and made some tweaks to the script(s) to allow for mounting/unmounting/cleanup of an external backup disk. I also made some other scripts to backup the metadata, the xenserver(s) itself, and create an audit dump of the pool’s DB that will be very helpful in piecing things back together if there is a problem. I also added smtp support to the scripts to email the backup listings, auditlog, etc. and available disk space etc. upon completion.

      The files are here http://www.8layer8.com/xen/ and a post of how to piece it all together is here http://www.8layer8.com/?p=260
      Nice work!

  6. It must be me, but the script keeps telling me that I do not have enought space left.
    Basicaly I’m running Xenserver 5.5 on a single HDD with at the moment one VM running on it.
    The HDD is 250GB, and the VM about 4GB
    I install the ‘re-written’ script as suggested in the blog and run it.

    Am I missing something here? Does it only work with a multi HDD system?

    • I’d guess that the space is taken up by snapshots that haven’t correctly finished, i had this problem just the other day when a snapshot failed because disk was low.

      It allocated the remaining 20GB, but didn’t remove it when the snapshot failed, meaning i had 0GB remaining.

      Do you still have the problem?

      This article explains how to fix the problem in both 5.5 and 5.6


  7. Just started using the script in production environment. JUST GREAT! Backup 7 Instances from a single server to usb disk without any problems.

    Thanks a lot for this great work!

  8. Hello,

    Possibly a really stupid question :) however, can this script support setting the backup location to another Linux box as a backup server or even to an FTP server?


  9. Hi Andy,

    I use the script to backup all running VM’s, but now I want to try instead of all running, I will specify the VM (via uuid) to be backed up.
    The problem with this is all the VM’s that has been exported with this type of selection does not have file extensions. The normal exported VM like “windows_vm.xva” now shows as “windows_vm”.
    Aany ideas why this is happening?

  10. Hi,
    Your script works fine, but it creates orphaned VDI after the snapshot is destroyed.
    I ran the script to backup 20 VMs and I noticed that the “base copy” VDIs were not deleted and used a lot of SR space.
    Do you have a solution to this problem?
    Nowadays I have to use the “coalesce-leaf” tool to clean these VDIs and reclaim used space, but I need to shutdown VMs.
    Thanks in advance.

  11. Pingback: Wie gebruikt "Alike" om Xen snapshots te maken ? - webhostingtalk.nl

  12. As with some of your ealier posters, I have found the scripts excellent and very straightforward to configure. The deletion of the snapshots is very useful, but as other correspondents, I am still left with orphaned base copy VDIs taking up space on the SR. Admittly the amount of space is not that much, but if I were using it in a production enviroment, which I will be, I would need to factor in a coalesce-leaf command to clear out the base copies after each backup. My assumption is that in doing so, one no longer has the problem from chains of snapshots.

    Coalesce-leaf is a somewhat cumbersome tool, not least in that when you enter the uuid for the VM, it must be entered in full and can not be tab completed. It also requires that the VM be suspended. Not all SRs support this, some Linux boxes, particularly those without PV drivers, won’t do it, and on some machines, the suspend and resume proceedure can take up to 25 minutes. In certain circumstances, it is easier to write a script which shutsdown the VM out of office hours, allowing the tool to do its thing, and then restarts it.

    I plan to combine the scripts with automated coalesce-leaf scripts so that when a particular VM has been copied over, the second one is used to reclaim the space. My main production Hypervisors suspend and resume quite quickly, so I hope to get it working well there. Again many thanks,
    Tony Corley.

  13. I have run a number of scipts where certain xva run but don’t seem to finish properley. I get the following in the vm_backup.log and just wondered if anyone else had encountered them..

    [2011-02-03_12-19-39] Backup VM 7cb56118-3411-0d23-22e8-771357f1a88d
    [2011-02-03_12-19-39] Get VM label 7cb56118-3411-0d23-22e8-771357f1a88d
    [2011-02-03_12-19-40] get_vm_label succeeded: Copy_F_of_SAGESERV
    [2011-02-03_12-19-41] Snapshotting VM 7cb56118-3411-0d23-22e8-771357f1a88d
    [2011-02-03_12-19-43] Snapshot with quiesce 7cb56118-3411-0d23-22e8-771357f1a88d as Copy_F_of_SAGESERV_snapshot
    Error code: VM_SNAPSHOT_FAILED
    Error parameters: Quiesce snapshot not supported
    [2011-02-03_12-19-45] Quiesce snapshot failed:
    [2011-02-03_12-19-45] Attempting normal snapshot
    [2011-02-03_12-19-45] Snapshot 7cb56118-3411-0d23-22e8-771357f1a88d as Copy_F_of_SAGESERV_snapshot
    [2011-02-03_12-20-52] snapshot_vm succeeded: e952e477-ec0c-a793-8482-86d88b972191
    [2011-02-03_12-20-54] Remove template status e952e477-ec0c-a793-8482-86d88b972191
    [2011-02-03_12-20-54] remove_template succeeded:
    [2011-02-03_12-20-54] Disable HA e952e477-ec0c-a793-8482-86d88b972191
    [2011-02-03_12-20-54] disable_ha succeeded:
    [2011-02-03_12-20-54] Export VM e952e477-ec0c-a793-8482-86d88b972191 as Copy_F_of_SAGESERV-2011-02-03_04-00-24.xva
    You gave an invalid object reference. The object may have recently been deleted. The class parameter gives the type of reference given, and the handle parameter echoes the bad value given.
    class: task
    handle: OpaqueRef:7f612a66-253b-e294-cdef-19dc862c58bf
    [2011-02-04_12-47-31] export_vm failed:
    [2011-02-04_12-47-31] VM 7cb56118-3411-0d23-22e8-771357f1a88d backup failed

  14. Hi Andy…

    your scripts running good on my citrix.

    i have little question.

    when i try to import xva as a “new” VM why ip configuration is back to default with no IP(s)?
    is there no change to include the IP in the snapshot?

  15. Andy,

    As everyone else has said, great script! It’s saved me a lot of reading and writing of code. One thing that I have noticed however is that if you use colons in you VM Labels, then the script won’t translate them into something that won’t break Windows file naming rules. Perhaps I shouldn’t be using colons in VM Labels! But as it is I am.

    Simple tweak – pipe in another tr!

    # The following line has been edited by NRM to enable colons(:) to be
    # used in VM Labels, without breaking Windows file name rules.
    # echo “$echo” | tr ‘ ‘ _ | tr -d ‘(‘ | tr -d ‘)’
    echo “$echo” | tr ‘ ‘ _ | tr ‘:’ ‘ – ‘ | tr -d ‘(‘ | tr -d ‘)’


  16. Dear Andy,

    Thanks for this nice script, we have this running now on our development envirionment and works really good!

    One question, we currenly have one VM with multiple Virtual Disks attached (for example C:\ and D:\). Is it possible to backup this VM and only his C:\ Disk?. We use our D:\ disk for data (400+ GB) and we won’t like to have these in our snapshots.


  17. Hi Andy,

    Brilliant script and solution to the back up issues with XenServer and VMs.

    Is there a way to backup onto a external HDD using this script? I’m not too sure what would need changing in the script to allow this.

    Thanks in advance.



  19. Hi Andy,
    Just a suggestion;
    Having a lot of space on local disk, I use your script to make a daily local backup, and local+remote backup once a week. However, I wanted each server to backup it’s own vms.
    I added to vm_backup.lib the method “local”, to backup all vms hosted on the current server, instead of all pool’s vms :

    in vm_backup.sh :

    if [ $vm_log_enabled ]; then
    log_message “Backup All local VMs”

    in vm_backup.lib :

    function get_all_local_vms
    if [ $vm_log_enabled ]; then
    log_message “Get all local VMs”

    xenhost=`xe host-list hostname=$xenhostname`
    xenuuid=`echo “$xenhost” | grep ^uuid | awk ‘{print $NF}’`

    echo=`xe vm-list is-control-domain=false resident-on=$xenuuid 2>>$log_path`

    if [ $? -ne 0 ]; then
    return 1

    echo “$echo” | grep “uuid” | awk ‘{print $NF}’
    return 0

    function set_all_local_vms
    if [ $vm_log_enabled ]; then
    log_message “Set all VMs”



    if [ $? -ne 0 ]; then
    if [ $vm_log_enabled ]; then
    log_message “get_all_local_vms failed: $all_vms”
    return 1

    for uuid in $all_vms; do
    add_to_backup_list $uuid

    return 0

  20. Hi !
    Our environment is using the following systems.
    -LVM by iSCSI (Storage :HP LefthandP4300).
    -Xen Server 5.5.0 Update 2.

    We have executed the following shell a few times.

    Then We checked the usage of storage and vm_backup.log.
    So, the usage of storage was full, and we checked the error message(insufficient space) on vm_backup.log.
    But, we could not delete VDI and checked the following error.
    -Error: The VDI is not available
    I’d like to reduce the usage of storage.
    If you know, please tell me how to delete command or deal with it.

  21. Great script!

    Does anyone know why it creates a file called “0″ in the folder where the script is located at the end of the backup?

    It looks like this happens at the time it says “export_vm succeeded: Export succeeded”

    I’m using this script with XCP 1.1 (Xen Cloud Platform) but underneath this is XenServer 5.6.0 SP2

  22. Good job! Thanks for sharing!
    Two questions:
    - How can i restore the .xva? Using xe vm-import?
    - Any solution to send a mail with the status of backups scripts?

    Thanks for your work!

  23. Great script, Andy. However, I am not sure why it seems adding more space in backup file? For example, I have 2 VPS, both set 40GB space
    - VPS1, used 5GB, the backup file is around 8GB
    - VPS2, used 21GB, the backup file is around 49GB

    Is it normal? I am running your script on Xenserver 5.6

  24. Great script >!
    Any one think about to include progress bar and / or message upon completion ?


  25. I haven’t sat down and started to pick the scripts and revision suggestions by the other commenters, but I had a quick question about using an alternate iscsi lun as the destination for the backup. I have 2 8TB iomega storage devices and I want to basically clone our main SR between the two devices to increase the resilience of our cluster. Would this be possible with this script by initiating the iscsi target first and then proceeding with the rest of the script as instructed? Even better would be a way to instrument this through a XenCenter plugin. Any ideas?

  26. Helo Man!
    Thank u 4 share!
    When I use the option list, its perfect, excluding VMs snap and lvms.
    I got one problem using the the option all, the lvms(snapshots) are not beeing deleted, the storage is full off orphans lvms:
    [root@xen01 ~]# lvdisplay /dev/VG_XenStorage-4a13990b-a755-73f2-48f6-08cd5b40f7c3/

    Anyone knows how to remove it?

  27. I have been searching for a free automated way to back up my vms and this particular script continues to popup in the searches. I see that this script was written for XenServer 5.5 and I wanted to know if this script will also work for XenServer 5.6 FP1? Also, I hope to upgrade soon to 6.1, will this also work for that version?

  28. Pingback: Backup XenServer 6 to Network Share No Cost and No Downtime

  29. Pingback: Back up XenServer 6 to Network Share with Fancy Scripting

Leave a Reply to Jay Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>