Instantly share code, notes, and snippets.

What would you like to do?
example vm
class VM(object):
def __init__(self, uuid, name, status):
self.uuid = uuid = name
self.status = status
def __repr__(self):
return '%s - %s (%s)' % (self.uuid,, self.status)
def export(self, directory_name=None):
start = time.time()
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime())
file_name = 'backup-%s--%s.xva' % (timestamp,
if directory_name is not None:
file_name = os.path.join(directory_name, file_name)
# create a snapshot
snapshot_uuid = check_output(['xe', 'vm-snapshot', 'uuid=' + self.uuid,
'new-name-label=backup-' +]).strip()'xe vm-snapshot uuid=%s new-name-label=backup-' % (self.uuid) + + '...OK')
# change some params so that the snapshot can be exported
cmd = "xe template-param-set is-a-template=false ha-always-run=false uuid=" + snapshot_uuid
check_output(cmd, shell=True) + '...OK')
# export snapshot
cmd = 'xe vm-export vm=%s filename="%s" compress=true' % (snapshot_uuid, file_name)
check_output(['xe', 'vm-export', 'vm=' + snapshot_uuid,
'filename=' + file_name, 'compress=true']) + '...OK')
# remove old snapshot again
cmd = "xe vm-uninstall uuid=%s force=true" % snapshot_uuid
check_output(cmd, shell=True) + '...OK')'Exported VM "%s" in %s seconds.' % (, time.time() - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment