Esempio n. 1
0
    def _generate_backup(self, app_config: dict, root_password: Optional[str]) -> bool:
        if app_config['backup']['mode'] not in ('only_one', 'incremental'):
            self.show_message(title=self.i18n['error'].capitalize(),
                              body='{}: {}'.format(self.i18n['action.backup.invalid_mode'],bold(app_config['backup']['mode'])),
                              type_=MessageType.ERROR)
            self.change_substatus('')
            return False

        handler = ProcessHandler(self)
        if app_config['backup']['mode'] == 'only_one':
            remove_method = app_config['backup']['remove_method']

            if remove_method not in BACKUP_REMOVE_METHODS:
                remove_method = BACKUP_DEFAULT_REMOVE_METHOD

            delete_failed = False

            if remove_method == 'self':
                previous_snapshots = tuple(timeshift.read_created_snapshots(root_password))

                if previous_snapshots:
                    substatus = f"[{self.i18n['core.config.tab.backup'].lower()}] {self.i18n['action.backup.substatus.delete']}"
                    self.change_substatus(substatus)

                    for snapshot in reversed(previous_snapshots):
                        deleted, _ = handler.handle_simple(timeshift.delete(snapshot, root_password))

                        if not deleted:
                            delete_failed = True
            else:
                deleted, _ = handler.handle_simple(timeshift.delete_all_snapshots(root_password))
                delete_failed = not deleted

            if delete_failed and not self.request_confirmation(title=self.i18n['core.config.tab.backup'],
                                                               body=f"{self.i18n['action.backup.error.delete']}. "
                                                                    f"{self.i18n['action.backup.error.proceed']}",
                                                               confirmation_label=self.i18n['yes'].capitalize(),
                                                               deny_label=self.i18n['no'].capitalize()):
                self.change_substatus('')
                return False

        self.change_substatus('[{}] {}'.format(self.i18n['core.config.tab.backup'].lower(), self.i18n['action.backup.substatus.create']))
        created, _ = handler.handle_simple(timeshift.create_snapshot(root_password, app_config['backup']['type']))

        if not created and not self.request_confirmation(title=self.i18n['core.config.tab.backup'],
                                                         body='{}. {}'.format(self.i18n['action.backup.error.create'],
                                                                              self.i18n['action.backup.error.proceed']),
                                                         confirmation_label=self.i18n['yes'].capitalize(),
                                                         deny_label=self.i18n['no'].capitalize()):
            self.change_substatus('')
            return False

        self.change_substatus('')
        return True
Esempio n. 2
0
    def _generate_backup(self, app_config: dict, i18n: I18n,
                         root_password: str) -> bool:
        if timeshift.is_available():
            if app_config['backup']['mode'] not in ('only_one', 'incremental'):
                self.show_message(title=self.i18n['error'].capitalize(),
                                  body='{}: {}'.format(
                                      self.i18n['action.backup.invalid_mode'],
                                      bold(app_config['backup']['mode'])),
                                  type_=MessageType.ERROR)
                return False

            if not user.is_root() and not root_password:
                root_pwd, valid = self.request_root_password()
            else:
                root_pwd, valid = root_password, True

            if not valid:
                return False

            handler = ProcessHandler(self)
            if app_config['backup']['mode'] == 'only_one':
                self.change_substatus('[{}] {}'.format(
                    i18n['core.config.tab.backup'].lower(),
                    i18n['action.backup.substatus.delete']))
                deleted, _ = handler.handle_simple(
                    timeshift.delete_all_snapshots(root_pwd))

                if not deleted and not self.request_confirmation(
                        title=i18n['core.config.tab.backup'],
                        body='{}. {}'.format(
                            i18n['action.backup.error.delete'],
                            i18n['action.backup.error.proceed']),
                        confirmation_label=i18n['yes'].capitalize(),
                        deny_label=i18n['no'].capitalize()):
                    return False

            self.change_substatus('[{}] {}'.format(
                i18n['core.config.tab.backup'].lower(),
                i18n['action.backup.substatus.create']))
            created, _ = handler.handle_simple(
                timeshift.create_snapshot(root_pwd,
                                          app_config['backup']['type']))

            if not created and not self.request_confirmation(
                    title=i18n['core.config.tab.backup'],
                    body='{}. {}'.format(i18n['action.backup.error.create'],
                                         i18n['action.backup.error.proceed']),
                    confirmation_label=i18n['yes'].capitalize(),
                    deny_label=i18n['no'].capitalize()):
                return False

        return True