With XenServer it is possible to create backups of VMs, even if they are running. The process is as follows:

  • Search for the uuid of the VMs to backup
  • Create a snapshot of each (running) VM
  • Save the snapshot to file
  • Remove the created snapshot

First look for the uuid of the VMs to backup. We don’t want to backup the control domain itself, so we add is-control-domain=false to the vm-list command and we also don’t want backups of snapshots so we add is-a-snapshot=false:

xe vm-list is-control-domain=false is-a-snapshot=false

Now we create a snapshot of the VMs we want to backup, replacing the uuid one by one with the ones we found with the previous command. Also replace the name of the snapshot if desired:

xe vm-snapshot uuid=d61bfc1a-33b2-5406-7ea5-76e4f7113220 new-name-label=snapshotname

This command has a return value: the uuid of the created snapshot. Then we transform the snapshot into a VM to be able to save it to a file, replacing uuid with the return value of the previous command:

xe template-param-set is-a-template=false ha-always-run=false uuid=b759625c-eab5-4e0f-be5e-a05bcbad869a

In the next step we save the snapshot to a file, replacing uuid with the snapshot uuid and providing a meaningful filename:

xe vm-export vm=b759625c-eab5-4e0f-be5e-a05bcbad869a filename=filename.xva

In the final step we delete the snapshot:

xe vm-uninstall uuid=b759625c-eab5-4e0f-be5e-a05bcbad869a force=true

Python is installed by default on XenServer hosts, so the following script will work out of the box. Download the script from this location to save it to your XenServer host, replacing the .txt extension with .py.

#!/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("nnn"):
      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)

for (uuid, name) in get_backup_vms():
   timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime())
   print timestamp, uuid, name
   filename = """ + timestamp + " " + name + ".xva""
   backup_vm(uuid, filename, timestamp)
Creating backups of running VMs in XenServer
Tagged on:

40 thoughts on “Creating backups of running VMs in XenServer

  • 2011-04-20 at 14:43
    Permalink

    This is a great tutorial, thanks for the insights!
    –P

    Reply
  • 2012-02-15 at 15:59
    Permalink

    Very nice tutorial thanks, I’m not a Python programmer and I have a question, why are you doing this?:
    filename = filename.replace(” “, ” “)
    filename = filename.replace(“(“, “(“)
    filename = filename.replace(“)”, “)”)

    it looks like you are replacing the characters with identical ones… is there any subtle reason?

    Thanks in advance

    Reply
    • 2013-02-19 at 15:38
      Permalink

      In posting this to WordPress, the backslash characters were removed. This section used to make it possible to have spaces and brackets in filenames. Now it is replaced with better code, thanks to some comments made below.

      Reply
  • 2012-05-02 at 17:55
    Permalink

    Thank u, works 100%

    Reply
  • 2012-08-02 at 14:38
    Permalink

    lines = vm.split(“n”)
    this line doesnt work if vm have space in name
    Put this instead
    lines = vm.splitlines()

    Reply
  • 2012-08-02 at 14:51
    Permalink

    and
    filename = timestamp + ” ” + name + “.xva”
    to
    filename = “”” + timestamp + ” ” + name + “.xva” +”””

    Reply
  • 2012-09-18 at 12:01
    Permalink

    I think that control character are being removed from the quoted script. A lot of the splitting just doesn’t make sense and nor does the regex stuff if this isn’t the case. Good ideas though.

    Reply
    • 2013-02-19 at 15:42
      Permalink

      Fixed. You are right, the control characters were removed by WordPress.

      Reply
  • 2012-11-21 at 18:33
    Permalink

    What happens if you don’t delete the snapshot in the last step? Would it enable you to only backup a delta (file) since the last backup? I looking for such a possibility since the total size of our VM is quite large for a daily backup.

    Reply
  • 2013-02-26 at 16:31
    Permalink

    So if I want to export the created backup to another disk or mount the source will be:


    for (uuid, name) in get_backup_vms():
    timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime())
    print timestamp, uuid, name
    filename = ""/mnt/backups/" + timestamp + " " + name + ".xva""
    backup_vm(uuid, filename, timestamp)

    Reply
  • 2013-03-01 at 22:19
    Permalink

    When I run the python script these are the results I’m getting… why? What do I need to change?

    cmd = “xe template-param-set is-a-template=false ha-always-run=false uuid=” +
    ^
    SyntaxError: invalid syntax

    Reply
      • 2013-06-25 at 20:56
        Permalink

        There appears to be a carriage return between the + and snapshot_uuid if you cut and paste the script.
        Delete anything between those two and add just a space between them, that should work.

        Ron

        Reply
  • Pingback:Creating backups of running VMs in XenServer | SysAdmin

  • 2013-08-08 at 18:40
    Permalink

    This is really cool! What is considered the ‘control domain’? Thanks!

    Reply
  • Pingback:Free Backup for Citrix Xenserver Live Virtual Machines | Curiosity killed the cat

  • 2013-09-29 at 04:00
    Permalink

    add a “–compress” option to the vm-export which will cause VM exports to be compressed via gzip saving some percent space

    Reply
  • 2013-10-16 at 12:29
    Permalink

    I have VM with disks 50 GB and 100 GB. How I can get minimum size of template.
    Thanks

    Reply
  • 2014-01-22 at 06:48
    Permalink

    Thanks for the real and nice tutorial

    thanks a lot

    Reply
  • 2014-01-22 at 12:12
    Permalink

    using uuid is some what complicated we can use vm=vmname instead of uuid=xxdxxxxxxxxxxxxxxxxxxxxxxxx in the xe commands as it can we use full

    Reply
  • 2014-03-12 at 19:37
    Permalink

    Hello, I have 3 vms running on my server, when running the script, make 3 times the backup procedure, but 3 times in the same vm, the first found in the vm-list command.
    How can I fix this?

    Reply
  • 2014-03-14 at 19:06
    Permalink

    Hi Guys, i am using XenServer 6.2 and i have 2 VMs running on the Host. I wan to backup my VMs in a attached external drive. Can i use the above script and i did not understood why the python script is used for ? Kindly suggest

    Reply
  • Pingback:Linux Scripts | navindj

  • 2014-06-10 at 04:11
    Permalink

    Where are these xva files supposed to be backing up to?

    Reply
    • 2014-06-10 at 08:12
      Permalink

      I’m backing up to an NFS share

      Reply
  • 2014-06-27 at 11:01
    Permalink

    Hi,
    Is it possible to show a minimal procedure to restore the backup from this .xva files, preferably using Xencenter?
    Thx

    Reply
  • 2014-07-24 at 13:31
    Permalink

    Hi. It is possible that the problem is resolved. The “xe vm-export” command breaks the filename into the firts space. To avoid it, its sufficient to put a double quotes at the beginning and the end of the filename:

    filename = “”” + timestamp + ” ” + name + “.xva”””

    or

    filename = “”” + timestamp + ” ” + name + “.xva””

    Reply
    • 2014-07-24 at 13:32
      Permalink

      Oh, I dont understand why not show backslashes.

      Reply
  • 2014-08-26 at 22:36
    Permalink

    when i run this i got this error
    anybody knows about this ???
    Error parameters: INTERNAL_ERROR: [ Unix.Unix_error(56, “write”, “”) ]

    Reply
  • 2014-11-19 at 16:03
    Permalink

    Is is possible to get the Backupfile into a new Directory with the name of the VM like this:

    filename = “”/mnt/backups/VMname” + timestamp + ” ” + name + “.xva”” ?

    Appreciate every answer

    Reply
  • 2015-03-21 at 19:46
    Permalink

    asi debe ser

    for (uuid, name) in get_backup_vms():
    timestamp = time.strftime(“%Y%m%d-%H%M”, time.gmtime())
    print timestamp, uuid, name
    filename =”\”/media/backup/” + timestamp + ” ” + name + “.xva\””
    backup_vm(uuid, filename, timestamp)

    Reply
  • 2015-03-27 at 15:50
    Permalink

    Hello,
    I have more one vm in my xenserver and the script return error after the first one :

    20150327-1436 c81eb79e-28c0-fc34-4532-96ef057f493f CentOS 6 (32-bit) (Test video)
    20150327-1437 3314dae3-e226-fdef-df18-5337098a8095 CentOS 7
    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(_)’
    20150327-1437 852bd9fd-164b-9290-56df-07bbd00149b0 CentOS6-64
    sh: -c: line 1: syntax error near unexpected token `(‘
    sh: -c: line 1: `status: Xml.Error(_)’…

    Thanks for answer (and apologise for my poor english)

    Reply
  • Pingback:Citrix XenServer 6.5 : Script de sauvegarde automatique | Blog technophile tfrichet.fr

  • 2015-04-26 at 23:34
    Permalink

    what can i do?

    Ps.: My only change –> localtime, Brazil

    Line 33 timestamp = time.strftime(“%Y%m%d-%H%M”, time.localtime())

    [root@xenserver-1 7b396b0f-7d19-d8f9-a839-a5c2be20bd77]# ./backup-vms.py
    20150426-1921 21624a2c-931f-e531-d787-8f6eb9c0ce33 SRV_SAMBA
    20150426-1927 0e3e77a4-8475-b1a0-ddf3-f4e5400d8974 SRV_PROXY
    20150426-1928 9f1b48aa-39ff-848f-29bf-feca6a2381f8 SRV_XENCENTER
    sh: -c: line 3: syntax error near unexpected token `(‘
    sh: -c: line 3: `stderr: Traceback (most recent call last):’
    sh: -c: line 3: syntax error near unexpected token `(‘
    sh: -c: line 3: `stderr: Traceback (most recent call last):’
    sh: -c: line 3: syntax error near unexpected token `(‘
    sh: -c: line 3: `stderr: Traceback (most recent call last):’

    Reply
  • 2016-03-23 at 17:09
    Permalink

    Hello Jan, could you help me with this issue? What does this mean and how do I resolve it?

    [root@ent-xen-002 ~]# xe vm-snapshot uuid=7acf1443-8f66-3ee9-4f7f-1d832369f342 new-name-label=ENT-APP-AFRT1_SnapTest1
    Error code: SR_BACKEND_FAILURE_82
    Error parameters: , Failed to snapshot VDI [opterr=Command [‘/usr/sbin/vhd-util’, ‘query’, ‘–debug’, ‘-vsf’, ‘-n’, ‘/dev/VG_XenStorage-24b411f9-ad00-aa26-3b58-a7db7d28740e/VHD-eb4b9461-7f10-4296-a4c5-cded62266259’] failed (5): ],
    [root@ent-xen-002 ~]#

    Reply
  • Pingback:Homelab: a few weeks with XenServer and Nutanix CE

Leave a Reply

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