backups: better handle quiet mode (for tests)
This commit is contained in:
		
							parent
							
								
									e36488e8ec
								
							
						
					
					
						commit
						ec45308f1c
					
				| @ -451,8 +451,9 @@ def backup_do(base_backup_dir, files_to_backup, passphrase, | |||||||
| 
 | 
 | ||||||
|     global blocks_backedup |     global blocks_backedup | ||||||
|     blocks_backedup = 0 |     blocks_backedup = 0 | ||||||
|     progress = blocks_backedup * 11 / total_backup_sz |     if callable(progress_callback): | ||||||
|     progress_callback(progress) |         progress = blocks_backedup * 11 / total_backup_sz | ||||||
|  |         progress_callback(progress) | ||||||
| 
 | 
 | ||||||
|     backup_tmpdir = tempfile.mkdtemp(prefix="/var/tmp/backup_") |     backup_tmpdir = tempfile.mkdtemp(prefix="/var/tmp/backup_") | ||||||
|     running_backup_operation.tmpdir_to_remove = backup_tmpdir |     running_backup_operation.tmpdir_to_remove = backup_tmpdir | ||||||
| @ -481,8 +482,9 @@ def backup_do(base_backup_dir, files_to_backup, passphrase, | |||||||
|     def compute_progress(new_size, total_backup_sz): |     def compute_progress(new_size, total_backup_sz): | ||||||
|         global blocks_backedup |         global blocks_backedup | ||||||
|         blocks_backedup += new_size |         blocks_backedup += new_size | ||||||
|         progress = blocks_backedup / float(total_backup_sz) |         if callable(progress_callback): | ||||||
|         progress_callback(int(round(progress*100,2))) |             progress = blocks_backedup / float(total_backup_sz) | ||||||
|  |             progress_callback(int(round(progress*100,2))) | ||||||
| 
 | 
 | ||||||
|     to_send = Queue(10) |     to_send = Queue(10) | ||||||
|     send_proc = SendWorker(to_send, backup_tmpdir, backup_stdout) |     send_proc = SendWorker(to_send, backup_tmpdir, backup_stdout) | ||||||
| @ -876,7 +878,7 @@ class ExtractWorker(Process): | |||||||
|             raise e, None, exc_traceback |             raise e, None, exc_traceback | ||||||
| 
 | 
 | ||||||
|     def __run__(self): |     def __run__(self): | ||||||
|         if BACKUP_DEBUG: |         if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|             self.print_callback("Started sending thread") |             self.print_callback("Started sending thread") | ||||||
|             self.print_callback("Moving to dir "+self.base_dir) |             self.print_callback("Moving to dir "+self.base_dir) | ||||||
|         os.chdir(self.base_dir) |         os.chdir(self.base_dir) | ||||||
| @ -887,7 +889,7 @@ class ExtractWorker(Process): | |||||||
|             if filename == "FINISHED" or filename == "ERROR": |             if filename == "FINISHED" or filename == "ERROR": | ||||||
|                 break |                 break | ||||||
| 
 | 
 | ||||||
|             if BACKUP_DEBUG: |             if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|                 self.print_callback("Extracting file "+filename) |                 self.print_callback("Extracting file "+filename) | ||||||
| 
 | 
 | ||||||
|             if filename.endswith('.000'): |             if filename.endswith('.000'): | ||||||
| @ -910,7 +912,7 @@ class ExtractWorker(Process): | |||||||
|                                   "v" if BACKUP_DEBUG else ""), |                                   "v" if BACKUP_DEBUG else ""), | ||||||
|                     self.restore_pipe, |                     self.restore_pipe, | ||||||
|                     os.path.relpath(filename.rstrip('.000'))] |                     os.path.relpath(filename.rstrip('.000'))] | ||||||
|                 if BACKUP_DEBUG: |                 if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|                     self.print_callback("Running command "+ |                     self.print_callback("Running command "+ | ||||||
|                                         unicode(tar2_cmdline)) |                                         unicode(tar2_cmdline)) | ||||||
|                 self.tar2_process = subprocess.Popen(tar2_cmdline, |                 self.tar2_process = subprocess.Popen(tar2_cmdline, | ||||||
| @ -928,7 +930,7 @@ class ExtractWorker(Process): | |||||||
|                 continue |                 continue | ||||||
|             else: |             else: | ||||||
|                 self.collect_tar_output() |                 self.collect_tar_output() | ||||||
|                 if BACKUP_DEBUG: |                 if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|                     self.print_callback("Releasing next chunck") |                     self.print_callback("Releasing next chunck") | ||||||
|                 self.tar2_process.stdin.write("\n") |                 self.tar2_process.stdin.write("\n") | ||||||
|                 self.tar2_process.stdin.flush() |                 self.tar2_process.stdin.flush() | ||||||
| @ -994,7 +996,7 @@ class ExtractWorker(Process): | |||||||
|                         (self.tar2_current_file, details)) |                         (self.tar2_current_file, details)) | ||||||
| 
 | 
 | ||||||
|             # Delete the file as we don't need it anymore |             # Delete the file as we don't need it anymore | ||||||
|             if BACKUP_DEBUG: |             if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|                 self.print_callback("Removing file "+filename) |                 self.print_callback("Removing file "+filename) | ||||||
|             os.remove(filename) |             os.remove(filename) | ||||||
| 
 | 
 | ||||||
| @ -1017,7 +1019,7 @@ class ExtractWorker(Process): | |||||||
|                 # Finished extracting the tar file |                 # Finished extracting the tar file | ||||||
|                 self.tar2_process = None |                 self.tar2_process = None | ||||||
| 
 | 
 | ||||||
|         if BACKUP_DEBUG: |         if BACKUP_DEBUG and callable(self.print_callback): | ||||||
|             self.print_callback("Finished extracting thread") |             self.print_callback("Finished extracting thread") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -1058,9 +1060,10 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
| 
 | 
 | ||||||
|     global running_backup_operation |     global running_backup_operation | ||||||
| 
 | 
 | ||||||
|     if BACKUP_DEBUG: |     if callable(print_callback): | ||||||
|         print_callback("Working in temporary dir:"+restore_tmpdir) |         if BACKUP_DEBUG: | ||||||
|     print_callback("Extracting data: " + size_to_human(vms_size)+" to restore") |             print_callback("Working in temporary dir:"+restore_tmpdir) | ||||||
|  |         print_callback("Extracting data: " + size_to_human(vms_size)+" to restore") | ||||||
| 
 | 
 | ||||||
|     passphrase = passphrase.encode('utf-8') |     passphrase = passphrase.encode('utf-8') | ||||||
|     header_data = None |     header_data = None | ||||||
| @ -1103,7 +1106,7 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
|         # limit as 2*(10*COUNT_OF_VMS+TOTAL_SIZE/100MB) |         # limit as 2*(10*COUNT_OF_VMS+TOTAL_SIZE/100MB) | ||||||
|         tar1_env['UPDATES_MAX_FILES'] = str(2*(10*len(vms_dirs) + |         tar1_env['UPDATES_MAX_FILES'] = str(2*(10*len(vms_dirs) + | ||||||
|                                                int(vms_size/(100*1024*1024)))) |                                                int(vms_size/(100*1024*1024)))) | ||||||
|     if BACKUP_DEBUG: |     if BACKUP_DEBUG and callable(print_callback): | ||||||
|         print_callback("Run command"+unicode(tar1_command)) |         print_callback("Run command"+unicode(tar1_command)) | ||||||
|     command = subprocess.Popen(tar1_command, |     command = subprocess.Popen(tar1_command, | ||||||
|             stdin=backup_stdin, |             stdin=backup_stdin, | ||||||
| @ -1132,7 +1135,7 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
|         if not appvm: |         if not appvm: | ||||||
|             nextfile = filelist_pipe.readline().strip() |             nextfile = filelist_pipe.readline().strip() | ||||||
| 
 | 
 | ||||||
|         if BACKUP_DEBUG: |         if BACKUP_DEBUG and callable(print_callback): | ||||||
|             print_callback("Got backup header and hmac: %s, %s" % (filename, |             print_callback("Got backup header and hmac: %s, %s" % (filename, | ||||||
|                                                                    hmacfile)) |                                                                    hmacfile)) | ||||||
| 
 | 
 | ||||||
| @ -1219,7 +1222,7 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
|             else: |             else: | ||||||
|                 filename = filelist_pipe.readline().strip() |                 filename = filelist_pipe.readline().strip() | ||||||
| 
 | 
 | ||||||
|             if BACKUP_DEBUG: |             if BACKUP_DEBUG and callable(print_callback): | ||||||
|                 print_callback("Getting new file:"+filename) |                 print_callback("Getting new file:"+filename) | ||||||
| 
 | 
 | ||||||
|             if not filename or filename=="EOF": |             if not filename or filename=="EOF": | ||||||
| @ -1235,14 +1238,14 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
|             if not appvm: |             if not appvm: | ||||||
|                 nextfile = filelist_pipe.readline().strip() |                 nextfile = filelist_pipe.readline().strip() | ||||||
| 
 | 
 | ||||||
|             if BACKUP_DEBUG: |             if BACKUP_DEBUG and callable(print_callback): | ||||||
|                 print_callback("Getting hmac:"+hmacfile) |                 print_callback("Getting hmac:"+hmacfile) | ||||||
|             if not hmacfile or hmacfile=="EOF": |             if not hmacfile or hmacfile=="EOF": | ||||||
|                 # Premature end of archive, either of tar1_command or vmproc exited with error |                 # Premature end of archive, either of tar1_command or vmproc exited with error | ||||||
|                 break |                 break | ||||||
| 
 | 
 | ||||||
|             if not any(map(lambda x: filename.startswith(x), vms_dirs)): |             if not any(map(lambda x: filename.startswith(x), vms_dirs)): | ||||||
|                 if BACKUP_DEBUG: |                 if BACKUP_DEBUG and callable(print_callback): | ||||||
|                     print_callback("Ignoring VM not selected for restore") |                     print_callback("Ignoring VM not selected for restore") | ||||||
|                 os.unlink(os.path.join(restore_tmpdir, filename)) |                 os.unlink(os.path.join(restore_tmpdir, filename)) | ||||||
|                 os.unlink(os.path.join(restore_tmpdir, hmacfile)) |                 os.unlink(os.path.join(restore_tmpdir, hmacfile)) | ||||||
| @ -1278,10 +1281,10 @@ def restore_vm_dirs (backup_source, restore_tmpdir, passphrase, vms_dirs, vms, | |||||||
|     else: |     else: | ||||||
|         to_extract.put("FINISHED") |         to_extract.put("FINISHED") | ||||||
| 
 | 
 | ||||||
|     if BACKUP_DEBUG: |     if BACKUP_DEBUG and callable(print_callback): | ||||||
|         print_callback("Waiting for the extraction process to finish...") |         print_callback("Waiting for the extraction process to finish...") | ||||||
|     extract_proc.join() |     extract_proc.join() | ||||||
|     if BACKUP_DEBUG: |     if BACKUP_DEBUG and callable(print_callback): | ||||||
|         print_callback("Extraction process finished with code:" + \ |         print_callback("Extraction process finished with code:" + \ | ||||||
|                 str(extract_proc.exitcode)) |                 str(extract_proc.exitcode)) | ||||||
|     if extract_proc.exitcode != 0: |     if extract_proc.exitcode != 0: | ||||||
| @ -1784,12 +1787,14 @@ def backup_restore_do(restore_info, | |||||||
|             if verify_only: |             if verify_only: | ||||||
|                 raise |                 raise | ||||||
|             else: |             else: | ||||||
|                 print_callback("Some errors occurred during data extraction, " |                 if callable(print_callback): | ||||||
|  |                     print_callback("Some errors occurred during data extraction, " | ||||||
|                                "continuing anyway to restore at least some " |                                "continuing anyway to restore at least some " | ||||||
|                                "VMs") |                                "VMs") | ||||||
|     else: |     else: | ||||||
|         if verify_only: |         if verify_only: | ||||||
|             print_callback("WARNING: Backup verification not supported for " |             if callable(print_callback): | ||||||
|  |                 print_callback("WARNING: Backup verification not supported for " | ||||||
|                            "this backup format.") |                            "this backup format.") | ||||||
| 
 | 
 | ||||||
|     if verify_only: |     if verify_only: | ||||||
| @ -1808,7 +1813,8 @@ def backup_restore_do(restore_info, | |||||||
|                 break |                 break | ||||||
|             if not vm.__class__ == vm_class: |             if not vm.__class__ == vm_class: | ||||||
|                 continue |                 continue | ||||||
|             print_callback("-> Restoring {type} {0}...".format(vm.name, type=vm_class_name)) |             if callable(print_callback): | ||||||
|  |                 print_callback("-> Restoring {type} {0}...".format(vm.name, type=vm_class_name)) | ||||||
|             retcode = subprocess.call (["mkdir", "-p", os.path.dirname(vm.dir_path)]) |             retcode = subprocess.call (["mkdir", "-p", os.path.dirname(vm.dir_path)]) | ||||||
|             if retcode != 0: |             if retcode != 0: | ||||||
|                 error_callback("*** Cannot create directory: {0}?!".format( |                 error_callback("*** Cannot create directory: {0}?!".format( | ||||||
| @ -1853,7 +1859,7 @@ def backup_restore_do(restore_info, | |||||||
|                 error_callback("*** Some VM property will not be restored") |                 error_callback("*** Some VM property will not be restored") | ||||||
| 
 | 
 | ||||||
|             try: |             try: | ||||||
|                 new_vm.appmenus_create(verbose=True) |                 new_vm.appmenus_create(verbose=callable(print_callback)) | ||||||
|             except Exception as err: |             except Exception as err: | ||||||
|                 error_callback("ERROR during appmenu restore: {0}".format(err)) |                 error_callback("ERROR during appmenu restore: {0}".format(err)) | ||||||
|                 error_callback("*** VM '{0}' will not have appmenus".format(vm.name)) |                 error_callback("*** VM '{0}' will not have appmenus".format(vm.name)) | ||||||
| @ -1895,8 +1901,9 @@ def backup_restore_do(restore_info, | |||||||
|             backup_dom0_home_dir = os.path.join(restore_tmpdir, backup_path) |             backup_dom0_home_dir = os.path.join(restore_tmpdir, backup_path) | ||||||
|         restore_home_backupdir = "home-pre-restore-{0}".format (time.strftime("%Y-%m-%d-%H%M%S")) |         restore_home_backupdir = "home-pre-restore-{0}".format (time.strftime("%Y-%m-%d-%H%M%S")) | ||||||
| 
 | 
 | ||||||
|         print_callback("-> Restoring home of user '{0}'...".format(local_user)) |         if callable(print_callback): | ||||||
|         print_callback("--> Existing files/dirs backed up in '{0}' dir".format(restore_home_backupdir)) |             print_callback("-> Restoring home of user '{0}'...".format(local_user)) | ||||||
|  |             print_callback("--> Existing files/dirs backed up in '{0}' dir".format(restore_home_backupdir)) | ||||||
|         os.mkdir(home_dir + '/' + restore_home_backupdir) |         os.mkdir(home_dir + '/' + restore_home_backupdir) | ||||||
|         for f in os.listdir(backup_dom0_home_dir): |         for f in os.listdir(backup_dom0_home_dir): | ||||||
|             home_file = home_dir + '/' + f |             home_file = home_dir + '/' + f | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Marek Marczykowski-Górecki
						Marek Marczykowski-Górecki