Skip to content

Instantly share code, notes, and snippets.

@maltzsama
Last active December 16, 2015 07:29
Show Gist options
  • Select an option

  • Save maltzsama/5398724 to your computer and use it in GitHub Desktop.

Select an option

Save maltzsama/5398724 to your computer and use it in GitHub Desktop.
Creation of snapshot on xenserver using python script.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
###########################################
# xensnapshot.py
# Desc: xenserver snapshots
# Author: Demetrius Albuquerque
# mail: demetrius.albuquerque@yahoo.com.br
# version: 0.1 data: 02/01/2013
###########################################
import commands, time
def get_name_vms():
result = []
cmd = "xe vm-list is-control-domain=false"
output = commands.getoutput(cmd)
print output
for vm in output.split("\n\n\n"):
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 --compress 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_name_vms():
timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime())
print timestamp, uuid, name
filename = name + "-" + timestamp + ".xva"
backup_vm(uuid, filename, timestamp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment