From ced6fc672bb4d1a82630021d760f890c58950e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 28 Nov 2013 03:50:17 +0100 Subject: [PATCH] backups: Update for new API - backup code moved to qubes.backup module Also some improvements/fixes for handling new backup format --- qubesmanager/backup.py | 44 +++++++++++++++++------------- qubesmanager/backup_utils.py | 27 ++++++++++-------- qubesmanager/restore.py | 53 ++++++++++++++++++++++++++---------- 3 files changed, 79 insertions(+), 45 deletions(-) diff --git a/qubesmanager/backup.py b/qubesmanager/backup.py index 81b88f6..3219c6b 100644 --- a/qubesmanager/backup.py +++ b/qubesmanager/backup.py @@ -31,6 +31,7 @@ from qubes.qubes import QubesVmCollection from qubes.qubes import QubesException from qubes.qubes import QubesDaemonPidfile from qubes.qubes import QubesHost +from qubes import backup from qubes import qubesutils import qubesmanager.resources_rc @@ -65,9 +66,9 @@ class BackupVMsWindow(Ui_Backup, QWizard): self.shutdown_vm_func = shutdown_vm_func self.dev_mount_path = None - self.backup_dir = None + self.backup_location = None self.func_output = [] - self.excluded = [] + self.selected_vms = [] for vm in self.qvm_collection.values(): if vm.qid == 0: @@ -116,7 +117,7 @@ class BackupVMsWindow(Ui_Backup, QWizard): if vm.qid == 0: local_user = grp.getgrnam('qubes').gr_mem[0] home_dir = pwd.getpwnam(local_user).pw_dir - self.size = qubesutils.get_disk_usage(home_dir) + self.size = backup.get_disk_usage(home_dir) else: self.size = self.get_vm_size(vm) super(BackupVMsWindow.VmListItem, self).__init__(vm.name+ " (" + qubesutils.size_to_human(self.size) + ")") @@ -231,16 +232,14 @@ class BackupVMsWindow(Ui_Backup, QWizard): def validateCurrentPage(self): if self.currentPage() is self.select_vms_page: - for i in range(self.select_vms_widget.selected_list.count()): - if self.check_running() == True: - QMessageBox.information(None, "Wait!", "Some selected VMs are running. Running VMs can not be backuped. Please shut them down or remove them from the list.") - return False + if self.check_running(): + QMessageBox.information(None, "Wait!", "Some selected VMs are running. Running VMs can not be backuped. Please shut them down or remove them from the list.") + return False + + self.selected_vms = [] + for i in range(self.select_vms_widget.selected_list.count()): + self.selected_vms.append(self.select_vms_widget.selected_list.item(i).vm) - del self.excluded[:] - for i in range(self.select_vms_widget.available_list.count()): - vmname = self.select_vms_widget.available_list.item(i).vm.name - self.excluded.append(vmname) - return True def gather_output(self, s): @@ -260,8 +259,13 @@ class BackupVMsWindow(Ui_Backup, QWizard): msg = [] try: - qubesutils.backup_do_copy(str(self.backup_dir), self.files_to_backup, str(self.passphrase_line_edit.text()), self.update_progress_bar, encrypt=self.encryption_checkbox.isChecked(), appvm=self.target_appvm) - #simulate_long_lasting_proces(10, self.update_progress_bar) + backup.backup_do(str(self.backup_location), + self.files_to_backup, + str(self.passphrase_line_edit.text()), + progress_callback=self.update_progress_bar, + encrypt=self.encryption_checkbox.isChecked(), + appvm=self.target_appvm) + #simulate_long_lasting_proces(10, self.update_progress_bar) except Exception as ex: print "Exception:",ex msg.append(str(ex)) @@ -276,14 +280,16 @@ class BackupVMsWindow(Ui_Backup, QWizard): if self.currentPage() is self.confirm_page: self.target_appvm = None - if self.appvm_combobox.currentText() != "None": #An existing appvm chosen - self.target_appvm = str(self.appvm_combobox.currentText()) - - # FIXME: ensure that at least a non empty passphrase has been provided + if self.appvm_combobox.currentIndex() != 0: #An existing appvm chosen + self.target_appvm = self.qvm_collection.get_vm_by_name( + self.appvm_combobox.currentText()) del self.func_output[:] try: - self.files_to_backup = qubesutils.backup_prepare(str(self.backup_dir), exclude_list = self.excluded, print_callback = self.gather_output) + self.files_to_backup = backup.backup_prepare( + self.selected_vms, + print_callback = self.gather_output, + hide_vm_names=self.encryption_checkbox.isChecked()) except Exception as ex: print "Exception:",ex QMessageBox.critical(None, "Error while preparing backup.", "ERROR: {0}".format(ex)) diff --git a/qubesmanager/backup_utils.py b/qubesmanager/backup_utils.py index 54a253a..937ef8c 100644 --- a/qubesmanager/backup_utils.py +++ b/qubesmanager/backup_utils.py @@ -121,7 +121,7 @@ def dev_combobox_activated(dialog, idx): #there was a change dialog.dir_line_edit.setText("") - dialog.backup_dir = None + dialog.backup_location = None if dialog.dev_mount_path != None: dialog.dev_mount_path = umount_device(dialog.dev_mount_path) @@ -161,30 +161,35 @@ def dev_combobox_activated(dialog, idx): if dialog.dev_mount_path != None: # Initialize path with root of mounted device dialog.dir_line_edit.setText(dialog.dev_mount_path) - dialog.backup_dir = dialog.dev_mount_path + dialog.backup_location = dialog.dev_mount_path dialog.select_dir_page.emit(SIGNAL("completeChanged()")) - -def select_path_button_clicked(dialog): - dialog.backup_dir = dialog.dir_line_edit.text() + +def select_path_button_clicked(dialog, select_file = False): + dialog.backup_location = dialog.dir_line_edit.text() file_dialog = QFileDialog() file_dialog.setReadOnly(True) + if select_file: + file_dialog_function = file_dialog.getOpenFileName + else: + file_dialog_function = file_dialog.getExistingDirectory + new_appvm = None new_path = None - if dialog.appvm_combobox.currentText() != "None": #An existing appvm chosen + if dialog.appvm_combobox.currentIndex() != 0: #An existing appvm chosen new_appvm = str(dialog.appvm_combobox.currentText()) elif dialog.dev_mount_path != None: - new_path = file_dialog.getExistingDirectory(dialog, "Select backup directory.", dialog.dev_mount_path) + new_path = file_dialog_function(dialog, "Select backup location.", dialog.dev_mount_path) else: - new_path = file_dialog.getExistingDirectory(dialog, "Select backup directory.", "~") - + new_path = file_dialog_function(dialog, "Select backup location.", "~") + if new_path != None: dialog.dir_line_edit.setText(new_path) - dialog.backup_dir = new_path + dialog.backup_location = new_path - if (new_path or new_appvm) and len(dialog.backup_dir) > 0: + if (new_path or new_appvm) and len(dialog.backup_location) > 0: dialog.select_dir_page.emit(SIGNAL("completeChanged()")) def simulate_long_lasting_proces(period, progress_callback): diff --git a/qubesmanager/restore.py b/qubesmanager/restore.py index f4ebe6b..be7c2ae 100644 --- a/qubesmanager/restore.py +++ b/qubesmanager/restore.py @@ -40,6 +40,7 @@ import time from operator import itemgetter from thread_monitor import * +from qubes import backup from qubes import qubesutils from ui_restoredlg import * @@ -61,7 +62,7 @@ class RestoreVMsWindow(Ui_Restore, QWizard): self.blk_manager = blk_manager self.dev_mount_path = None - self.backup_dir = None + self.backup_location = None self.restore_options = None self.backup_vms_list = None self.func_output = [] @@ -101,7 +102,7 @@ class RestoreVMsWindow(Ui_Restore, QWizard): @pyqtSlot(name='on_select_path_button_clicked') def select_path_button_clicked(self): - select_path_button_clicked(self) + select_path_button_clicked(self, True) def on_ignore_missing_toggled(self, checked): self.restore_options['use-default-template'] = checked @@ -120,13 +121,25 @@ class RestoreVMsWindow(Ui_Restore, QWizard): self.select_vms_widget.selected_list.clear() self.select_vms_widget.available_list.clear() - - self.target_appvm = None - if self.appvm_combobox.currentText() != "None": #An existing appvm chosen - self.target_appvm = str(self.appvm_combobox.currentText()) - self.restore_tmpdir, qubes_xml = qubesutils.backup_restore_header(str(self.backup_dir), str(self.passphrase_line_edit.text()), self.encryption_checkbox.isChecked(), appvm=self.target_appvm) - self.vms_to_restore = qubesutils.backup_restore_prepare(str(self.backup_dir),os.path.join(self.restore_tmpdir, qubes_xml), str(self.passphrase_line_edit.text()), options=self.restore_options, host_collection=self.qvm_collection, encrypt=self.encryption_checkbox.isChecked(), appvm=self.target_appvm) + self.target_appvm = None + if self.appvm_combobox.currentIndex() != 0: #An existing appvm chosen + self.target_appvm = self.qvm_collection.get_vm_by_name( + str(self.appvm_combobox.currentText())) + + self.restore_tmpdir, qubes_xml = backup.backup_restore_header( + str(self.backup_location), + str(self.passphrase_line_edit.text()), + encrypted=self.encryption_checkbox.isChecked(), + appvm=self.target_appvm) + self.vms_to_restore = backup.backup_restore_prepare( + str(self.backup_location), + os.path.join(self.restore_tmpdir, qubes_xml), + str(self.passphrase_line_edit.text()), + options=self.restore_options, + host_collection=self.qvm_collection, + encrypt=self.encryption_checkbox.isChecked(), + appvm=self.target_appvm) for vmname in self.vms_to_restore: self.select_vms_widget.available_list.addItem(vmname) @@ -134,8 +147,8 @@ class RestoreVMsWindow(Ui_Restore, QWizard): def __init_restore_options__(self): if not self.restore_options: self.restore_options = {} - qubesutils.backup_restore_set_defaults(self.restore_options) - + backup.backup_restore_set_defaults(self.restore_options) + if 'use-default-template' in self.restore_options and 'use-default-netvm' in self.restore_options: val = self.restore_options['use-default-template'] and self.restore_options['use-default-netvm'] self.ignore_missing.setChecked(val) @@ -164,8 +177,17 @@ class RestoreVMsWindow(Ui_Restore, QWizard): err_msg = [] self.qvm_collection.lock_db_for_writing() try: - qubesutils.backup_restore_do(str(self.backup_dir), self.restore_tmpdir, str(self.passphrase_line_edit.text()), self.vms_to_restore, self.qvm_collection, encrypted=self.encryption_checkbox.isChecked(), appvm=self.target_appvm, print_callback=self.restore_output, error_callback=self.restore_error_output, progress_callback -=self.update_progress_bar) + backup.backup_restore_do( + str(self.backup_location), + self.restore_tmpdir, + str(self.passphrase_line_edit.text()), + self.vms_to_restore, + self.qvm_collection, + encrypted=self.encryption_checkbox.isChecked(), + appvm=self.target_appvm, + print_callback=self.restore_output, + error_callback=self.restore_error_output, + progress_callback=self.update_progress_bar) except Exception as ex: print "Exception:",ex err_msg.append(str(ex)) @@ -194,7 +216,8 @@ class RestoreVMsWindow(Ui_Restore, QWizard): del self.vms_to_restore[str(vmname)] del self.func_output[:] - qubesutils.backup_restore_print_summary(self.vms_to_restore, print_callback = self.gather_output) + backup.backup_restore_print_summary( + self.vms_to_restore, print_callback = self.gather_output) self.confirm_text_edit.setReadOnly(True) self.confirm_text_edit.setFontFamily("Monospace") self.confirm_text_edit.setText("\n".join(self.func_output)) @@ -226,8 +249,8 @@ class RestoreVMsWindow(Ui_Restore, QWizard): self.done(0) def has_selected_dir(self): - return self.backup_dir != None - + return self.backup_location != None + def has_selected_vms(self): return self.select_vms_widget.selected_list.count() > 0