From fb95a111e470e857f783b710c85760e067529513 Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sat, 25 May 2024 21:52:11 -0500 Subject: [PATCH] improved python backup script --- .gitignore | 1 + src/restic/backup.py | 33 +++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a66c47a..c0f52fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +__pycache__ *.egg-info \ No newline at end of file diff --git a/src/restic/backup.py b/src/restic/backup.py index fec943a..590d4b0 100755 --- a/src/restic/backup.py +++ b/src/restic/backup.py @@ -3,26 +3,40 @@ import json import logging import subprocess +from pathlib import Path from rich.console import Console from rich.progress import Progress -from .console import console, logger -from .loki import send_to_loki +from restic.console import console, logger +from restic.loki import send_to_loki -def main(command: str, loki_url: str): +def main(backup_dir: Path, loki_url: str): + cmd = f'restic backup {backup_dir} --json --tag python-script' + + logger.debug(f'Running cmd [bright_black]{cmd}[/]') process = subprocess.Popen( - command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True ) with process.stdout, Progress(console=console, transient=True) as progress: task = progress.add_task('Running backup', total=1.0) for line in iter(process.stdout.readline, ''): - data = json.loads(line) - if data['message_type'] == 'status': - progress.update(task, completed=data['percent_done']) - progress.update(task, completed=1.0, refresh=True) + try: + data = json.loads(line) + except Exception as e: + logger.exception(e) + else: + if data['message_type'] == 'status': + progress.update(task, completed=data['percent_done']) + + progress.update( + task, + completed=1.0, + refresh=True, + description=f'Finished [yellow]{data["snapshot_id"]}[/]', + ) logger.info( f'[yellow]{data["snapshot_id"]}[/] done, with {data["files_new"]} new files, {data["files_changed"]} files changed. Added {data["data_added"] / 10**6:.1f} MB out of {data["total_bytes_processed"] / 10**6:.1f} MB' @@ -43,5 +57,4 @@ if __name__ == '__main__': level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)] ) - cmd = f'restic backup {os.environ["BACKUP_DIR"]} --tag python-script --json' - main(cmd, 'http://192.168.1.107:3100/loki/api/v1/push') + main(Path(os.environ['BACKUP_DIR']), 'http://192.168.1.107:3100/loki/api/v1/push')