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

http://www.jansipke.nl/creating-backups-of-running-vms-in-xenserver/

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 192.168.178.225:/volume1/nfs/backups /mnt/nfs"
commands.getoutput(cmd)

and

cmd = "umount /mnt/nfs"
commands.getoutput(cmd)

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

#!/usr/bin/python

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
commands.getoutput(cmd)

cmd = "xe vm-export vm=" + snapshot_uuid + " filename=" + filename
commands.getoutput(cmd)

cmd = "xe vm-uninstall uuid=" + snapshot_uuid + " force=true"
commands.getoutput(cmd)
cmd = "mount -t nfs 192.168.178.225:/volume1/nfs/backups /mnt/nfs"
commands.getoutput(cmd)
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"
commands.getoutput(cmd)

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 192.168.178.225:/volume1/nfs.

NFS Storage Setup

NFS Storage Setup

Then ssh to pool master xenserver and create a new file e.g. backup.sh (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 http://pastebin.com/5E6wJS7u
  • The mount path should exist so execute mkdir /mnt/nfs to create the nfs directory