Files
restic-scripts/src/restic/snapshots.py
John Lancaster e82f6155ad renamed
2024-05-27 18:33:46 -05:00

39 lines
979 B
Python

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 snapshot(loki_url: str = None):
cmd = ['restic', 'snapshots', '--json']
with console.status('Getting snapshots...'):
logger.debug(f'Running cmd [bright_black]{" ".join(cmd)}[/]')
result = subprocess.run(cmd, capture_output=True, text=True)
line = result.stdout
data = json.loads(line)
logger.info(f'Got {len(data)} snapshots')
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)]
)
snapshot(loki_url)
if __name__ == '__main__':
main()