pass config as arg to avoid reading it multiple times
This commit is contained in:
parent
07cbf162b7
commit
b432630bd8
3 changed files with 11 additions and 21 deletions
|
|
@ -1,21 +1,11 @@
|
||||||
import gotify
|
import gotify
|
||||||
import json
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
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:
|
class BackupManager:
|
||||||
def __init__(self):
|
def __init__(self, config):
|
||||||
self._config = read_config()
|
self._config = config
|
||||||
self._remotes = self._config["remotes"]
|
self._remotes = self._config["remotes"]
|
||||||
self._common = self._config["common"]
|
self._common = self._config["common"]
|
||||||
self._gotify = gotify.Gotify(self._common["GOTIFY_TOKEN"])
|
self._gotify = gotify.Gotify(self._common["GOTIFY_TOKEN"])
|
||||||
|
|
@ -41,7 +31,9 @@ class BackupManager:
|
||||||
borg_env["BORG_RSH"] = remote["BORG_RSH"]
|
borg_env["BORG_RSH"] = remote["BORG_RSH"]
|
||||||
borg_env["BORG_PASSPHRASE"] = remote["BORG_PASSPHRASE"]
|
borg_env["BORG_PASSPHRASE"] = remote["BORG_PASSPHRASE"]
|
||||||
repo = f"ssh://{backup_user}@{remote_host}/{repo_prefix}/{local_host}/{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(
|
||||||
cmd,
|
cmd,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import gotify
|
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
|
@ -17,8 +16,8 @@ def read_config():
|
||||||
|
|
||||||
|
|
||||||
class NextcloudManager(backup_manager.BackupManager):
|
class NextcloudManager(backup_manager.BackupManager):
|
||||||
def __init__(self):
|
def __init__(self, config):
|
||||||
super().__init__()
|
super().__init__(config=config)
|
||||||
|
|
||||||
def enable_maintenance(self):
|
def enable_maintenance(self):
|
||||||
cmd = "docker compose exec -i --user 1000:1000 app /var/www/html/occ maintenance:mode --on"
|
cmd = "docker compose exec -i --user 1000:1000 app /var/www/html/occ maintenance:mode --on"
|
||||||
|
|
@ -82,7 +81,7 @@ class NextcloudManager(backup_manager.BackupManager):
|
||||||
def main():
|
def main():
|
||||||
config = read_config()
|
config = read_config()
|
||||||
os.chdir(config["common"]["BACKUP_DOCKER_DIR"])
|
os.chdir(config["common"]["BACKUP_DOCKER_DIR"])
|
||||||
backup_manager = NextcloudManager()
|
backup_manager = NextcloudManager(config)
|
||||||
if not backup_manager.enable_maintenance():
|
if not backup_manager.enable_maintenance():
|
||||||
backup_manager.disable_maintenance()
|
backup_manager.disable_maintenance()
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import gotify
|
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
|
@ -17,8 +16,8 @@ def read_config():
|
||||||
|
|
||||||
|
|
||||||
class PaperlessManager(backup_manager.BackupManager):
|
class PaperlessManager(backup_manager.BackupManager):
|
||||||
def __init__(self):
|
def __init__(self, config):
|
||||||
super().__init__()
|
super().__init__(config=config)
|
||||||
|
|
||||||
def export_data(self):
|
def export_data(self):
|
||||||
cmd = "docker compose exec -it webserver document_exporter ../export -d -f --no-progress-bar"
|
cmd = "docker compose exec -it webserver document_exporter ../export -d -f --no-progress-bar"
|
||||||
|
|
@ -39,7 +38,7 @@ def main():
|
||||||
os.chdir(config["common"]["BACKUP_DOCKER_DIR"])
|
os.chdir(config["common"]["BACKUP_DOCKER_DIR"])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
backup_manager = PaperlessManager()
|
backup_manager = PaperlessManager(config)
|
||||||
if not backup_manager.export_data():
|
if not backup_manager.export_data():
|
||||||
exit(1)
|
exit(1)
|
||||||
if not backup_manager.borg_backup():
|
if not backup_manager.borg_backup():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue