
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:
xe vm-list is-control-domain=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"
output = commands.getoutput(cmd)
for vm in output.split("nnn"):
lines = vm.split("n")
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)
filename = filename.replace(" ", " ")
filename = filename.replace("(", "(")
filename = filename.replace(")", ")")
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)