Free Backup for Citrix Xenserver Live Virtual Machines

A few days ago, I was looking for a free backup solution for live Citrix Xenserver 6.2 VMs and I came across a rather old script on this page

The script uses Xenserver’s XE commands to do the following:

  • Enumerates the VMs and finds their UUIds
  • Creates a snapshot for each live VM
  • Exports the snapshot to a xva file
  • And finally removes each snapshot

I edited the script to match my needs, for example my setup includes a pool of two Xenservers 6.2 connected to a NAS storage (running Synology‘s DSM ) that publish an iscsi and nfs shares. I wanted the Xenservers to mount the nfs share and save the backup. So I added a few more lines

cmd = "mount -t nfs /mnt/nfs"


cmd = "umount /mnt/nfs"

This script can be easily scheduled to run whenever you like. The final script is like this


import commands, time

def get_backup_vms():
result = []

cmd = "xe vm-list is-control-domain=false is-a-snapshot=false"
output = commands.getoutput(cmd)

for vm in output.split("\n\n\n"):
lines = vm.splitlines()
uuid = lines[0].split(":")[1][1:]
name = lines[1].split(":")[1][1:]
result += [(uuid, name)]

return result

def backup_vm(uuid, filename, timestamp):
cmd = "xe vm-snapshot uuid=" + uuid + " new-name-label=" + timestamp
snapshot_uuid = commands.getoutput(cmd)

cmd = "xe template-param-set is-a-template=false ha-always-run=false uuid=" + snapshot_uuid

cmd = "xe vm-export vm=" + snapshot_uuid + " filename=" + filename

cmd = "xe vm-uninstall uuid=" + snapshot_uuid + " force=true"
cmd = "mount -t nfs /mnt/nfs"
for (uuid, name) in get_backup_vms():
timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime())
print timestamp, uuid, name
filename = "\"/mnt/nfs/" + timestamp + " " + name + ".xva\""
backup_vm(uuid, filename, timestamp)
cmd = "umount /mnt/nfs"

First let’s see my NFS setup

My NFS setup

My NFS setup

You may notice that my two xenservers  have read/write access to the share folder and the NAS publish the nfs folder as /volume1/nfs. The nfs storage should be configured on xencenter’s pool by right clicking on your pool and selecting New SR… Check the NFS ISO library and type in the Share Name as <ip>:/<path>, in my case

NFS Storage Setup

NFS Storage Setup

Then ssh to pool master xenserver and create a new file e.g. (with vi), edit the aforementioned script to match your storage needs and paste it to the shell script, give execute permissions to it and run it. The output will be like this

Script Output

Script Output

the first field is the UUid of the VM and the second field is its name. During the backup process you can watch the steps of the script on XenCenter’s gui. First the ctration of vm’s snapshot.

Snapshot Creation

Snapshot Creation

Then the snapshot’s export.

VM's export

VM’s export

Finished export

Finished export

It took 12 minutes for a 4 GB snapshot, not bad for a 100Mbit LAN. Then the snapshot is erased.

No snapshot

No snapshot

Finally, let’s look the backed up files on NFS storage.

Backed up VM files

Backed up VM files

This is a free, simple, transparent and easy solution to backup your Xenserver VMs. It works with the latest 6.2 open source version. It is not that advanced to do deduplication or WAN acceleration or other fancy staff, but it works and it’s free and pretty enough for small setups or home labs.

Post Notes:

  • Due to wordpress preformatting problems, you may find the script at
  • The mount path should exist so execute mkdir /mnt/nfs to create the nfs directory

34 thoughts on “Free Backup for Citrix Xenserver Live Virtual Machines

    • Hello, this looks like a wordpress formating error. Python doesn’t like strange indentations. Try to paste the code on a notepad application like Notepad++ and replace tabs to spaces (Edit – Blank Operations – TAB to Space).

      • Hmm weird. Still no luck, same error. Tried through notepad++ with replace tabs to spaces. Tried to create the actual file with nano and vim. Last but not least tried to write the whole thing by hand from the beginning and same error.

      • Found the same issue, but changing the extension to .py and running with #python works ok. XenServer dom(0) platform version 6.2

  1. What if i already have a SR setup as a CIF and I want to just export to that SR. I do not need to create it each time or remove it. It is always attached. What line would I put the UUID

  2. Hello, I tried your script from the pastebin, i edited it for cifs and got this error
    20131018-0303 783524d3-800b-ec0c-1933-9af1a6cf823a mumble
    sh: -c: line 1: syntax error near unexpected token `(‘
    sh: -c: line 1: `status: Xml.Error(_)’
    sh: -c: line 1: syntax error near unexpected token `(‘
    sh: -c: line 1: `status: Xml.Error(_)’
    sh: -c: line 1: syntax error near unexpected token `(‘
    sh: -c: line 1: `status: Xml.Error(_)’

    Followed by a crashing host, had todo a hard restart

    • ignore me, all is working well. Had it mounting cifs to the host file system so it filled it up almost instantly and crashed xen. Changed the mount point and it worked flawlessly. Thank you very much.

  3. After using this script, it occurred to me that one could add the compress=true parameter to the export script for better disk space utilization.

    Also, I haven’t been able to figure out how to do this, but it seems like one could modify this script to only export the VMs that are running on the host the VMs are running on. You could then schedule this script on every XenServer host, and get a much faster backup time instead of a sequential backup waiting for each export to complete from one host. Since the compression parameter adds CPU overhead, I don’t think you’d want more than one export task running on a host, but if you have 5 XenServers in a pool, why not export / compress 5 at a time?

  4. Dear Nikos, I am from Brazil, thanks for explain. I use “ln -s” to made a symbolik link on nfs folder. Is the way to send to windows server, without NAS or other storage media. I think is easy too. If you made the connection via xencenter you could get this link very easy too. To help you I upload a file, with correct tab. More one time, thanks to explain and thanks to help.

    • Hello
      When i call the script to cleanup the old backups it delete all the backups in that directory. I want to leave the last backup on the store.
      What do i have to change ?

  5. Would is also be possible to keep one snapshot on the xenserver itself before exporting it? So each time this script would run, it would delete the old snapshot on xenserver, create new snapshot, and then export that snapshot?

  6. Hi I am little bit confused where should i need to configure…should i need to create one file and copy the code there or else need to do some thing differently. I have 4 machines running on XS and had NFS storage, can you explain exact steps to follow to take the backup of VM’s running on CITRIX XenServer.

  7. Pingback: Backup Your xenServer VMs Weekly to a CIFS share | Daniel's TechBlog

  8. Hey, Great script. But when I am trying to run this script every time I get an error

    “Failed”,”Exporting 20150602-1300
    Internal error: Unix.Unix_error(31, “write”, “”)”,””,”Jun 2, 2015 6:30 PM”

    Any clue?

      • Hi,

        Do you have any special characters in your VM’s name? I had this same error message with one VM that had a “/” in it’s name. Named it otherwise and no problem since.

  9. Hi Nikos

    Thanks again for your article, the script is working well for me with some tweaks to suit, but I am getting an issue with orphaned disks after snapshots are deleted. It occurs with any snapshot / VM that has more than 1 disk. The snapshot with the primary disk is removed but the secondary disk image / snapshot remains, slowly consuming disk space until my SR is full and backups fail. Just wondering if you had any experience with this issue?


  10. It’s a good post, but this script is very slow! 🙂
    It will run through 8-10 hours if you have 6-7 VMs with 60-80 GB image about or more.
    (and 6-7 VM are not too much)
    I tested it with local storage ( no any network disk [NFS, CIFS, iSCSI] ).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s