Created
February 1, 2021 13:58
-
-
Save marcelcaraciolo/4402893a99ce40518041a8eb9537abef to your computer and use it in GitHub Desktop.
Xenserver functions for snapshots, export XVA Backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functions | |
class VMs(object): | |
def __init__(self, name, uuid, sr_uuid): | |
self.name = name | |
self.uuid = uuid | |
#-------------------------# | |
#Info about vms snapshots, templates and vdi/vm backup | |
#sr-uuid is the storage place where vdi is. | |
#-------------------------# | |
self.name_parameter = name | |
self.snap_uuid = None | |
self.snap_label = None | |
self.sruuid = sr_uuid | |
self.vm_backup_name = None | |
self.vm_backup_uuid = None | |
self.place_to_backup = None | |
def create_snapshot(self): | |
try: | |
date = str(functions.request_time(0)) | |
snap_label = 'Snapshot' + self.name + date | |
snap_cmd = 'xe vm-snapshot uuid=' + self.uuid + ' new-name-label=' + snap_label | |
create_snapshot_uuid = functions.run_cmd(snap_cmd) | |
self.snap_label = snap_label | |
self.snap_uuid = create_snapshot_uuid[:-1] | |
functions.writing_log('create_snap', self.snap_label) | |
functions.writing_log('create_snap_uuid', self.snap_uuid) | |
except: | |
functions.writing_except_log('An error was occured while creating snapshot') | |
def convert_snap_template(self): | |
try: | |
temp_cmd = 'xe template-param-set is-a-template=false uuid=' + self.snap_uuid | |
convert = functions.run_cmd(temp_cmd) | |
functions.writing_log('create_template') | |
except: | |
functions.writing_except_log('An error was occured while converting snapshot in a template') | |
def convert_template_vm(self): | |
try: | |
date = str(functions.request_time(0)) | |
vm_label = "Backup-VM-" + self.name + '-' + date | |
temp_cmd = 'xe vm-copy vm=' + self.snap_label + ' sr-uuid=' + self.sruuid + ' new-name-label=' + vm_label | |
vm_backup = functions.run_cmd(temp_cmd) | |
self.vm_backup_name = vm_label | |
self.vm_backup_uuid = vm_backup[:-1] | |
functions.writing_log('create_vm', self.vm_backup_name) | |
except: | |
functions.writing_except_log('An error was occured while converting template in a VM') | |
def export_vm(self): | |
try: | |
export_cmd = 'xe vm-export vm=' + self.vm_backup_uuid + ' filename=' + self.place_to_backup + self.vm_backup_name + '.xva' | |
functions.run_cmd(export_cmd) | |
functions.writing_log('export_vm', self.vm_backup_name) | |
except: | |
functions.writing_except_log('An error was occured while converting template in a VM') | |
def delete_vm_vdi(self): | |
try: | |
delete_vm_cmd = 'xe vm-uninstall vm=' + self.vm_backup_name + ' force=true' | |
functions.run_cmd(delete_vm_cmd) | |
except: | |
functions.writing_except_log('An error was occured while deleting backup VM') | |
def delete_snapshot(self): | |
try: | |
delete_snap_cmd = 'xe snapshot-destroy snapshot-uuid=' + self.snap_uuid + ' force=true' | |
functions.run_cmd(delete_snap_cmd) | |
except: | |
functions.writing_except_log('An error was occured while deleting backup Snapshot') | |
def backup_vm(vm_name, vm_uuid, sr_uuid): | |
backup_dir = '/root/backups/backps_vms/' + vm_name | |
vm = VMs(vm_name, vm_uuid, sr_uuid) | |
functions.create_dir(backup_dir) | |
vm.place_to_backup = backup_dir + '/' | |
#CREATE A SNAPSHOT: | |
vm.create_snapshot() | |
#CONVERT SNAPSHOT ON A TEMPLATE: | |
vm.convert_snap_template() | |
#CONVERT TEMPLATE ON A VM: | |
vm.convert_template_vm() | |
#EXPORT VM: | |
vm.export_vm() | |
#DELETE SNAPSHOT AND VDI | |
vm.delete_vm_vdi() | |
vm.delete_snapshot() | |
functions.remove_old_snaps(vm.place_to_backup) | |
import functions | |
class VMs(object): | |
def __init__(self, name, uuid, sr_uuid): | |
self.name = name | |
self.uuid = uuid | |
#-------------------------# | |
#Info about vms snapshots, templates and vdi/vm backup | |
#sr-uuid is the storage place where vdi is. | |
#-------------------------# | |
self.name_parameter = name | |
self.snap_uuid = None | |
self.snap_label = None | |
self.sruuid = sr_uuid | |
self.vm_backup_name = None | |
self.vm_backup_uuid = None | |
self.place_to_backup = None | |
def create_snapshot(self): | |
try: | |
date = str(functions.request_time(0)) | |
snap_label = 'Snapshot' + self.name + date | |
snap_cmd = 'xe vm-snapshot uuid=' + self.uuid + ' new-name-label=' + snap_label | |
create_snapshot_uuid = functions.run_cmd(snap_cmd) | |
self.snap_label = snap_label | |
self.snap_uuid = create_snapshot_uuid[:-1] | |
functions.writing_log('create_snap', self.snap_label) | |
functions.writing_log('create_snap_uuid', self.snap_uuid) | |
except: | |
functions.writing_except_log('An error was occured while creating snapshot') | |
def convert_snap_template(self): | |
try: | |
temp_cmd = 'xe template-param-set is-a-template=false uuid=' + self.snap_uuid | |
convert = functions.run_cmd(temp_cmd) | |
functions.writing_log('create_template') | |
except: | |
functions.writing_except_log('An error was occured while converting snapshot in a template') | |
def convert_template_vm(self): | |
try: | |
date = str(functions.request_time(0)) | |
vm_label = "Backup-VM-" + self.name + '-' + date | |
temp_cmd = 'xe vm-copy vm=' + self.snap_label + ' sr-uuid=' + self.sruuid + ' new-name-label=' + vm_label | |
vm_backup = functions.run_cmd(temp_cmd) | |
self.vm_backup_name = vm_label | |
self.vm_backup_uuid = vm_backup[:-1] | |
functions.writing_log('create_vm', self.vm_backup_name) | |
except: | |
functions.writing_except_log('An error was occured while converting template in a VM') | |
def export_vm(self): | |
try: | |
export_cmd = 'xe vm-export vm=' + self.vm_backup_uuid + ' filename=' + self.place_to_backup + self.vm_backup_name + '.xva' | |
functions.run_cmd(export_cmd) | |
functions.writing_log('export_vm', self.vm_backup_name) | |
except: | |
functions.writing_except_log('An error was occured while converting template in a VM') | |
def delete_vm_vdi(self): | |
try: | |
delete_vm_cmd = 'xe vm-uninstall vm=' + self.vm_backup_name + ' force=true' | |
functions.run_cmd(delete_vm_cmd) | |
except: | |
functions.writing_except_log('An error was occured while deleting backup VM') | |
def delete_snapshot(self): | |
try: | |
delete_snap_cmd = 'xe snapshot-destroy snapshot-uuid=' + self.snap_uuid + ' force=true' | |
functions.run_cmd(delete_snap_cmd) | |
except: | |
functions.writing_except_log('An error was occured while deleting backup Snapshot') | |
def backup_vm(vm_name, vm_uuid, sr_uuid): | |
# backup_dir = '/root/darwin_backups/backps_vms/' + vm_name | |
backup_dir = '/root/mendel_backups/backps_vms/' + vm_name | |
vm = VMs(vm_name, vm_uuid, sr_uuid) | |
functions.create_dir(backup_dir) | |
vm.place_to_backup = backup_dir + '/' | |
#CREATE A SNAPSHOT: | |
vm.create_snapshot() | |
#CONVERT SNAPSHOT ON A TEMPLATE: | |
vm.convert_snap_template() | |
#CONVERT TEMPLATE ON A VM: | |
vm.convert_template_vm() | |
#EXPORT VM: | |
vm.export_vm() | |
#DELETE SNAPSHOT AND VDI | |
vm.delete_vm_vdi() | |
vm.delete_snapshot() | |
functions.remove_old_snaps(vm.place_to_backup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment