75 lines
2.3 KiB
Python
Executable file
75 lines
2.3 KiB
Python
Executable file
#!/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_subprocess_error("Exporting data failed", e)
|
|
return False
|
|
self._gotify.send_success("Data exported.", result)
|
|
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)
|
|
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()
|