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:
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"])

View file

@ -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"
}
]
}