support for multiple remotes

This commit is contained in:
Thies Lennart Alff 2025-01-03 11:50:23 +01:00
parent 3a43b3383b
commit a07759a11d
Signed by: lennartalff
GPG key ID: 4EC67D34D594104D
2 changed files with 71 additions and 34 deletions

View file

@ -18,7 +18,9 @@ def read_config():
class BackupManager: class BackupManager:
def __init__(self): def __init__(self):
self._config = read_config() 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): def export_data(self):
cmd = "docker compose exec -it webserver document_exporter ../export -d -f" cmd = "docker compose exec -it webserver document_exporter ../export -d -f"
@ -33,16 +35,25 @@ class BackupManager:
return True return True
def borg_backup(self): def borg_backup(self):
backup_dirs = " ".join(self._config["BACKUP_DIRS"]) # common config for all remotes
exclude_dirs = " ".join(self._config["EXCLUDE_DIRS"]) backup_dirs = " ".join(self._common["BACKUP_DIRS"])
repo_subdir = self._config["REPO_SUBDIR"] exclude_dirs = " ".join(self._common["EXCLUDE_DIRS"])
time_format = self._config["TIME_FORMAT"] repo_subdir = self._common["REPO_SUBDIR"]
backup_user = self._config["BACKUP_USER"] time_format = self._common["TIME_FORMAT"]
hostname = os.uname().nodename
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 = os.environ.copy()
borg_env["BORG_RSH"] = self._config["BORG_RSH"] borg_env["BORG_RSH"] = remote["BORG_RSH"]
borg_env["BORG_PASSPHRASE"] = self._config["BORG_PASSPHRASE"] borg_env["BORG_PASSPHRASE"] = remote["BORG_PASSPHRASE"]
repo = f"ssh://{backup_user}@{backup_user}.your-storagebox.de:23/./backups/{hostname}/{repo_subdir}::{{{time_format}}}" 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}" cmd = f"borg create -v --stats {repo} {backup_dirs} --exclude {exclude_dirs}"
try: try:
result = subprocess.run( result = subprocess.run(
@ -57,9 +68,16 @@ class BackupManager:
self._gotify.send_subprocess_error("Backup failed", e) self._gotify.send_subprocess_error("Backup failed", e)
return False return False
self._gotify.send_backup_successful(result) 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 return True
def main(): def main():
config = read_config() config = read_config()
os.chdir(config["BACKUP_DOCKER_DIR"]) os.chdir(config["BACKUP_DOCKER_DIR"])

View file

@ -1,13 +1,32 @@
// vim: ft=jsonc // vim: ft=jsonc
// put this file into the secrets subdir // put this file into the secrets subdir
{ {
"BORG_RSH": "ssh -i /home/lennartalff/.ssh/borg.ed25519", "common": {
"BORG_PASSPHRASE": "the passphrase",
"GOTIFY_TOKEN": "gotify token", "GOTIFY_TOKEN": "gotify token",
"BACKUP_DOCKER_DIR": "path to the docker compose file", "BACKUP_DOCKER_DIR": "path to the docker compose file",
"BACKUP_DIRS": ["list of directories to backup"], "BACKUP_DIRS": ["list of directories to backup"],
"REPO_SUBDIR": "paperless-ngx",
"BACKUP_USER": "u433234",
"EXCLUDE_DIRS": ["list of directories to exclude"], "EXCLUDE_DIRS": ["list of directories to exclude"],
"TIME_FORMAT": "utcnow:%Y-%m-%d_%H:%M:%S" "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"
}
]
} }