Пример #1
0
def remove_all_but_new_password_from_repo(computer, password):
    if is_windows():
        ret = subprocess.run(
            get_restic_list_passwords_command(),
            env=get_restic_env(computer, password),
            stdout=subprocess.PIPE,
            creationflags=CREATE_NO_WINDOW,
        ).stdout
    elif is_mac():
        ret = subprocess.run(
            get_restic_list_passwords_command(),
            env=get_restic_env(computer, password),
            stdout=subprocess.PIPE,
        ).stdout
    keys = json.loads(ret)
    for key in sorted(keys, key=lambda k: k["created"])[:-1]:
        if is_windows():
            subprocess.run(
                get_restic_delete_password_command(key["id"]),
                env=get_restic_env(computer, password),
                creationflags=CREATE_NO_WINDOW,
            )
        elif is_mac():
            subprocess.run(
                get_restic_delete_password_command(key["id"]),
                env=get_restic_env(computer, password),
            )
Пример #2
0
 def run(self):
     paths = get_selected_nodes(self.snapshot_tree_widget)
     computer = get_computer(self.email, self.password, self.computer_id)
     log_file = os.path.join(LOGS_PATH,
                             f"restore-{datetime.date.today()}.txt")
     with open(log_file, "a", encoding="utf-8") as log_f:
         if is_windows():
             ret = subprocess.run(
                 get_restic_restore_command(self.snapshot_id, self.target,
                                            paths),
                 env=get_restic_env(computer, self.password),
                 stderr=log_f,
                 creationflags=CREATE_NO_WINDOW,
             )
         elif is_mac():
             ret = subprocess.run(
                 get_restic_restore_command(self.snapshot_id, self.target,
                                            paths),
                 env=get_restic_env(computer, self.password),
                 stderr=log_f,
             )
     if ret.returncode == 0:
         self.logger.info("Restore successful.")
         self.restored.emit(self.target)
     else:
         self.logger.error("Restore failed.")
         self.failed.emit()
Пример #3
0
def unlock_repo(computer, password):
    if is_windows():
        subprocess.run(
            get_restic_unlock_command(),
            env=get_restic_env(computer, password),
            creationflags=CREATE_NO_WINDOW,
        )
    elif is_mac():
        subprocess.run(
            get_restic_unlock_command(),
            env=get_restic_env(computer, password),
        )
Пример #4
0
def create_restic_repo_or_die(computer, password):
    if is_windows():
        ret = subprocess.run(
            get_restic_init_command(),
            env=get_restic_env(computer, password),
            creationflags=CREATE_NO_WINDOW,
        )
    elif is_mac():
        ret = subprocess.run(
            get_restic_init_command(),
            env=get_restic_env(computer, password),
        )
    if ret.returncode != 0:
        sys.exit()
Пример #5
0
def add_new_password_to_repo(computer, old_password, password):
    with tempfile.TemporaryDirectory() as root:
        password_file = os.path.join(root, "password.txt")
        with open(password_file, "w", encoding="utf-8") as f:
            f.write(password)
        if is_windows():
            subprocess.run(
                get_restic_add_password_command(password_file),
                env=get_restic_env(computer, old_password),
                creationflags=CREATE_NO_WINDOW,
            )
        elif is_mac():
            subprocess.run(
                get_restic_add_password_command(password_file),
                env=get_restic_env(computer, old_password),
            )
Пример #6
0
    def backup(self):
        password, computer = self.pre_backup()

        log_file = os.path.join(LOGS_PATH, f"backup-{datetime.date.today()}.txt")
        files_done, bytes_done, backup_finished = None, None, False
        with open(log_file, "a", encoding="utf-8") as log_f:
            restic_backup_command = get_restic_backup_command(
                config["general"]["max_upload_kibs"],
                config["general"]["backup_connected_file_systems"],
            )
            num_threads = config["general"]["num_backup_threads"]
            if is_windows():
                self.process = subprocess.Popen(
                    restic_backup_command,
                    env=get_restic_env(computer, password, num_threads),
                    stdout=subprocess.PIPE,
                    stderr=log_f,
                    creationflags=CREATE_NO_WINDOW,
                )
            elif is_mac():
                self.process = subprocess.Popen(
                    restic_backup_command,
                    env=get_restic_env(computer, password, num_threads),
                    stdout=subprocess.PIPE,
                    stderr=log_f,
                )
            self.logger.info(f'Backup command: {" ".join(restic_backup_command)}')
            while True:
                line = self.process.stdout.readline().rstrip()
                if not line:
                    break
                try:
                    message = json.loads(line)
                    files_done, bytes_done, backup_finished = self.handle_backup_output(
                        message, files_done, bytes_done
                    )
                except json.JSONDecodeError:
                    self.logger.error("Unable to decode json line.")

        self.post_backup(files_done, bytes_done, backup_finished)
Пример #7
0
 def run(self):
     computer = get_computer(self.email, self.password, self.computer_id)
     if is_windows():
         snapshots = json.loads(
             subprocess.run(
                 get_restic_snapshots_command(),
                 env=get_restic_env(computer, self.password),
                 stdout=subprocess.PIPE,
                 creationflags=CREATE_NO_WINDOW,
             ).stdout)
     elif is_mac():
         snapshots = json.loads(
             subprocess.run(
                 get_restic_snapshots_command(),
                 env=get_restic_env(computer, self.password),
                 stdout=subprocess.PIPE,
             ).stdout)
     sorted_snapshots = sorted(snapshots,
                               key=lambda x: x["time"],
                               reverse=True)
     self.logger.info("Snapshots loaded.")
     self.loaded.emit(sorted_snapshots)
Пример #8
0
 def run(self):
     computer = get_computer(self.email, self.password, self.computer_id)
     if is_windows():
         process = subprocess.run(
             get_restic_ls_command(self.snapshot_id),
             env=get_restic_env(computer, self.password),
             stdout=subprocess.PIPE,
             creationflags=CREATE_NO_WINDOW,
         )
     elif is_mac():
         process = subprocess.run(
             get_restic_ls_command(self.snapshot_id),
             env=get_restic_env(computer, self.password),
             stdout=subprocess.PIPE,
         )
     if process.returncode == 0:
         nodes = process.stdout.decode("utf-8").split("\n")
         tree = prepare_lazy_tree(nodes[1:-1])
         self.logger.info("Snapshot loaded.")
         self.loaded.emit(tree)
     else:
         self.logger.error("Snapshot load failed.")
         self.failed.emit()
Пример #9
0
import subprocess

from blobbackup.util import RESTIC_PATH, get_restic_env, get_password_from_keyring
from blobbackup.config import config
from blobbackup.api import get_computer

if __name__ == "__main__":
    email = config["meta"]["email"]
    password = get_password_from_keyring()
    computer_id = config["meta"]["computer_id"]
    computer = get_computer(email, password, computer_id)
    subprocess.run(
        [RESTIC_PATH, "stats", "--mode", "raw-data"],
        env=get_restic_env(computer, password),
    )