Tiny optimization for copy_vm_xcp / backup_xva schemes

Both methods create a snap and make a copy of it, and both methods
ought to be used in the same backup process.  To avoid overwhelming
the storage, it is now possible to avoid deleting a snap in
copy_vm_xcp (delete_snapshot=no) so that it gets reused in
backup_xva (reuse_snapshot=yes).

Requested by Yvan.
This commit is contained in:
Jérémie Courrèges-Anglas 2014-11-20 15:48:00 +01:00
parent 4d054189c7
commit 059c94797a
2 changed files with 45 additions and 32 deletions

View File

@ -41,12 +41,12 @@ class backup_xva(backup_generic):
type = 'xen-xva'
required_params = backup_generic.required_params + ['xcphost','password_file','server_name']
optional_params = backup_generic.optional_params + ['enable_https', 'halt_vm', 'verify_export']
optional_params = backup_generic.optional_params + ['enable_https', 'halt_vm', 'verify_export', 'reuse_snapshot']
enable_https = "no"
halt_vm = "no"
verify_export = "no"
reuse_snapshot = "no"
def str2bool(self,v):
if type(v) != bool:
@ -64,7 +64,7 @@ class backup_xva(backup_generic):
raise Exception("File corrupt")
tar.close()
def export_xva(self, vdi_name, filename, halt_vm,dry_run,enable_https=True):
def export_xva(self, vdi_name, filename, halt_vm,dry_run,enable_https=True, reuse_snapshot="no"):
user_xen, password_xen, null = open(self.password_file).read().split('\n')
session = XenAPI.Session('https://'+self.xcphost)
@ -96,8 +96,15 @@ class backup_xva(backup_generic):
if self.str2bool(halt_vm) == False:
self.logger.debug("[%s] Check if previous tisbackups snapshots exist",vdi_name)
old_snapshots = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))
for old_snapshot in old_snapshots:
self.logger.debug("[%s] Old snaps count %s", vdi_name, len(old_snapshots))
if len(old_snapshots) == 1 and self.str2bool(reuse_snapshot) == True:
snapshot = old_snapshots[0]
self.logger.debug("[%s] Reusing snap \"%s\"", vdi_name, session.xenapi.VM.get_name_description(snapshot))
vm = snapshot # vm = session.xenapi.VM.get_by_name_label("tisbackup-%s"%(vdi_name))[0]
else:
self.logger.debug("[%s] Deleting %s old snaps", vdi_name, len(old_snapshots))
for old_snapshot in old_snapshots:
self.logger.debug("[%s] Destroy snapshot %s",vdi_name,session.xenapi.VM.get_name_description(old_snapshot))
try:
for vbd in session.xenapi.VM.get_VBDs(old_snapshot):
@ -115,6 +122,7 @@ class backup_xva(backup_generic):
self.logger.debug("[%s] Snapshot in progress",vdi_name)
try:
snapshot = session.xenapi.VM.snapshot(vm,"tisbackup-%s"%(vdi_name))
self.logger.debug("[%s] got snapshot %s", vdi_name, snapshot)
except XenAPI.Failure, error:
return("error when snapshot %s"%(error))
#get snapshot opaqueRef
@ -190,7 +198,7 @@ class backup_xva(backup_generic):
options = []
options_params = " ".join(options)
cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run)
cmd = self.export_xva( vdi_name= self.server_name,filename= dest_filename, halt_vm= self.halt_vm, enable_https=self.enable_https, dry_run= self.dry_run, reuse_snapshot=self.reuse_snapshot)
if os.path.exists(dest_filename):
stats['written_bytes'] = os.stat(dest_filename)[ST_SIZE]
stats['total_files_count'] = 1

View File

@ -40,11 +40,11 @@ class copy_vm_xcp(backup_generic):
type = 'copy-vm-xcp'
required_params = backup_generic.required_params + ['server_name','storage_name','password_file','vm_name','network_name']
optional_params = backup_generic.optional_params + ['start_vm','max_copies']
optional_params = backup_generic.optional_params + ['start_vm','max_copies', 'delete_snapshot']
start_vm = "no"
max_copies = 1
delete_snapshot = "yes"
def read_config(self,iniconf):
assert(isinstance(iniconf,ConfigParser))
@ -53,9 +53,11 @@ class copy_vm_xcp(backup_generic):
self.start_vm = iniconf.get('global','start_vm')
if self.max_copies == 1 and iniconf.has_option('global','max_copies'):
self.max_copies = iniconf.getint('global','max_copies')
if self.delete_snapshot == "yes" and iniconf.has_option('global','delete_snapshot'):
self.delete_snapshot = iniconf.get('global','delete_snapshot')
def copy_vm_to_sr(self, vm_name, storage_name, dry_run):
def copy_vm_to_sr(self, vm_name, storage_name, dry_run, delete_snapshot="yes"):
user_xen, password_xen, null = open(self.password_file).read().split('\n')
session = XenAPI.Session('https://'+self.server_name)
try:
@ -209,6 +211,10 @@ class copy_vm_xcp(backup_generic):
if not 'NULL' in vdi:
size_backup = size_backup + int(session.xenapi.VDI.get_record(vdi)['physical_utilisation'])
result = (0,size_backup)
if self.delete_snapshot == 'no':
return result
#delete the snapshot
try:
for vbd in session.xenapi.VM.get_VBDs(snapshot):
@ -223,14 +229,13 @@ class copy_vm_xcp(backup_generic):
result = (1,"error when destroy snapshot %s"%(error))
return result
result = (0,size_backup)
return result
def do_backup(self,stats):
try:
timestamp = int(time.time())
cmd = self.copy_vm_to_sr(self.vm_name, self.storage_name, self.dry_run)
cmd = self.copy_vm_to_sr(self.vm_name, self.storage_name, self.dry_run, delete_snapshot=self.delete_snapshot)
if cmd[0] == 0:
timeExec = int(time.time()) - timestamp