diff --git a/paperless-ngx_backup b/paperless-ngx_backup index aadb977..02c9d15 100755 --- a/paperless-ngx_backup +++ b/paperless-ngx_backup @@ -18,7 +18,9 @@ def read_config(): class BackupManager: def __init__(self): self._config = read_config() - self._gotify = gotify.Gotify(self._config["GOTIFY_TOKEN"]) + self._remotes = self._config["remotes"] + self._common = self._config["common"] + self._gotify = gotify.Gotify(self._common["GOTIFY_TOKEN"]) def export_data(self): cmd = "docker compose exec -it webserver document_exporter ../export -d -f" @@ -33,33 +35,49 @@ class BackupManager: return True def borg_backup(self): - backup_dirs = " ".join(self._config["BACKUP_DIRS"]) - exclude_dirs = " ".join(self._config["EXCLUDE_DIRS"]) - repo_subdir = self._config["REPO_SUBDIR"] - time_format = self._config["TIME_FORMAT"] - backup_user = self._config["BACKUP_USER"] - hostname = os.uname().nodename - borg_env = os.environ.copy() - borg_env["BORG_RSH"] = self._config["BORG_RSH"] - borg_env["BORG_PASSPHRASE"] = self._config["BORG_PASSPHRASE"] - repo = f"ssh://{backup_user}@{backup_user}.your-storagebox.de:23/./backups/{hostname}/{repo_subdir}::{{{time_format}}}" - cmd = f"borg create -v --stats {repo} {backup_dirs} --exclude {exclude_dirs}" - try: - result = subprocess.run( - cmd, - shell=True, - check=True, - text=True, - capture_output=True, - env=borg_env, - ) - except subprocess.CalledProcessError as e: - self._gotify.send_subprocess_error("Backup failed", e) - return False - self._gotify.send_backup_successful(result) + # common config for all remotes + backup_dirs = " ".join(self._common["BACKUP_DIRS"]) + exclude_dirs = " ".join(self._common["EXCLUDE_DIRS"]) + repo_subdir = self._common["REPO_SUBDIR"] + time_format = self._common["TIME_FORMAT"] + + disabled_remotes = [] + for remote in self._remotes: + remote_host = remote["HOSTNAME"] + if not remote["enabled"]: + disabled_remotes.append(remote_host) + continue + local_host = os.uname().nodename + backup_user = remote["BACKUP_USER"] + repo_prefix = remote["REPO_PREFIX"] + borg_env = os.environ.copy() + borg_env["BORG_RSH"] = remote["BORG_RSH"] + borg_env["BORG_PASSPHRASE"] = remote["BORG_PASSPHRASE"] + repo = f"ssh//{backup_user}@{remote_host}/{repo_prefix}/{local_host}/{repo_subdir}::{{{time_format}}}" + cmd = f"borg create -v --stats {repo} {backup_dirs} --exclude {exclude_dirs}" + try: + result = subprocess.run( + cmd, + shell=True, + check=True, + text=True, + capture_output=True, + env=borg_env, + ) + except subprocess.CalledProcessError as e: + self._gotify.send_subprocess_error("Backup failed", e) + return False + self._gotify.send_backup_successful(result) + + if disabled_remotes: + text = "Skipped disabled remotes:\n" + "\n".join(disabled_remotes) + self._gotify.send_info(title="Skipped remotes", text=text) + return True + + def main(): config = read_config() os.chdir(config["BACKUP_DOCKER_DIR"]) diff --git a/paperless.json.sample b/paperless.json.sample index 81be49b..60f7dc4 100644 --- a/paperless.json.sample +++ b/paperless.json.sample @@ -1,13 +1,32 @@ // vim: ft=jsonc // put this file into the secrets subdir { - "BORG_RSH": "ssh -i /home/lennartalff/.ssh/borg.ed25519", - "BORG_PASSPHRASE": "the passphrase", - "GOTIFY_TOKEN": "gotify token", - "BACKUP_DOCKER_DIR": "path to the docker compose file", - "BACKUP_DIRS": ["list of directories to backup"], - "REPO_SUBDIR": "paperless-ngx", - "BACKUP_USER": "u433234", - "EXCLUDE_DIRS": ["list of directories to exclude"], - "TIME_FORMAT": "utcnow:%Y-%m-%d_%H:%M:%S" + "common": { + "GOTIFY_TOKEN": "gotify token", + "BACKUP_DOCKER_DIR": "path to the docker compose file", + "BACKUP_DIRS": ["list of directories to backup"], + "EXCLUDE_DIRS": ["list of directories to exclude"], + "TIME_FORMAT": "utcnow:%Y-%m-%d_%H:%M:%S", + "REPO_SUBDIR": "paperless-ngx" + }, + "remotes": [ + { + "enabled": true, + "HOSTNAME": "myuser.your-storagebox.de:23", + "BORG_RSH": "ssh -i /home/lennartalff/.ssh/borg.ed25519", + "BORG_PASSPHRASE": "the passphrase", + // the resulting repo path is REPO_PREFIX/hostname/REPO_SUBDIR/ + "REPO_PREFIX": "backups", + "BACKUP_USER": "u433234" + }, + { + "enabled": true, + "HOSTNAME": "mySecondaryBackupServer", + "BORG_RSH": "ssh -i /home/lennartalff/.ssh/borg.ed25519", + "BORG_PASSPHRASE": "the passphrase", + // the resulting repo path is REPO_PREFIX/hostname/REPO_SUBDIR/ + "REPO_PREFIX": "backups", + "BACKUP_USER": "u433234" + } + ] }