added backup script for paperless-ngx

This commit is contained in:
Thies Lennart Alff 2024-11-29 10:00:37 +01:00
parent 3f74ae921a
commit 277cabc965
Signed by: lennartalff
GPG key ID: 4EC67D34D594104D
2 changed files with 94 additions and 0 deletions

81
paperless-ngx_backup Executable file
View file

@ -0,0 +1,81 @@
#!/usr/bin/env python3
import gotify
import json
import subprocess
import os
from pathlib import Path
def read_config():
source_path = Path(__file__).resolve()
secrets = source_path.parent / "secrets/paperless.json"
with open(secrets, "r") as f:
config = json.load(f)
return config
class BackupManager:
def __init__(self):
self._config = read_config()
self._gotify = gotify.Gotify(self._config["GOTIFY_TOKEN"])
def export_data(self):
cmd = "docker compose exec -it webserver document_exporter ../export -d -f"
try:
result = subprocess.run(cmd, shell=True, text=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
"❗💀❗ Exporting data failed",
f"stdout:\n{e.stdout}\nstderr:\n{e.stderr}",
)
return False
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"] = "ssh -i /home/lennartalff/.ssh/borg.ed25519"
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_error(
title="❗💀❗ Backup failed!",
text=f"stdout: \n{e.stdout}\nsterr: \n{e.stderr}",
)
return False
text = "\n".join([result.stdout, result.stderr])
self._gotify.send_info("✅ Backup completed", f"Result:\n{text}\n")
return True
def main():
config = read_config()
os.chdir(config["BACKUP_DOCKER_DIR"])
backup_manager = BackupManager()
if not backup_manager.export_data():
exit(1)
if not backup_manager.borg_backup():
exit(1)
exit(0)
if __name__ == "__main__":
main()

13
paperless.json.sample Normal file
View file

@ -0,0 +1,13 @@
// 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"
}