added snapshots module

This commit is contained in:
John Lancaster
2024-05-26 23:42:22 -05:00
parent a01ea3219b
commit 6a546c92ec

44
src/restic/snapshots.py Normal file
View File

@@ -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()