From 6a546c92ec7b8f84a3197ad7bf0667beae87b07c Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sun, 26 May 2024 23:42:22 -0500 Subject: [PATCH] added snapshots module --- src/restic/snapshots.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/restic/snapshots.py diff --git a/src/restic/snapshots.py b/src/restic/snapshots.py new file mode 100644 index 0000000..c51d90d --- /dev/null +++ b/src/restic/snapshots.py @@ -0,0 +1,44 @@ +import json +import logging +import subprocess + +import click +from rich.logging import RichHandler + +from restic.console import console, logger +from restic.loki import send_to_loki + + +def run(loki_url: str = None): + cmd = 'restic snapshots --json' + + logger.debug(f'Running cmd [bright_black]{cmd}[/]') + process = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True + ) + + with process.stdout: + for line in iter(process.stdout.readline, ''): + try: + data = json.loads(line) + except Exception as e: + logger.exception(e) + else: + console.print(data) + + if loki_url is not None: + send_to_loki(loki_url=loki_url, line=line, backup='snapshots') + + +@click.command() +@click.option('--loki-url', type=str, help='Loki URL for logging', envvar='LOKI_URL') +def main(loki_url: str = None): + logging.basicConfig( + level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)] + ) + + run(loki_url) + + +if __name__ == '__main__': + main()