more work

This commit is contained in:
John Lancaster
2024-05-27 00:08:38 -05:00
parent 6a546c92ec
commit 17f9fa203e
5 changed files with 33 additions and 19 deletions

View File

@@ -8,12 +8,13 @@ from rich.console import Console
from rich.logging import RichHandler
from rich.progress import Progress
from restic import loki, snapshots
from restic.console import console, logger
from restic.loki import send_to_loki
def run(backup_dir: Path, loki_url: str = None):
cmd = f'restic backup {backup_dir} --json --tag python-script'
def run(backup_dir: Path, loki_url: str = None, tag: str = 'python-script'):
cmd = f'restic backup {backup_dir} --json --tag {tag}'
logger.debug(f'Running cmd [bright_black]{cmd}[/]')
process = subprocess.Popen(
@@ -54,14 +55,16 @@ def run(backup_dir: Path, loki_url: str = None):
envvar='BACKUP_DIR',
)
@click.option('--loki-url', type=str, help='Loki URL for logging', envvar='LOKI_URL')
def main(backup_dir: Path, loki_url: str = None):
@click.option('--tag', type=str, help='Tag to use in restic')
def main(backup_dir: Path, loki_url: str = None, tag: str = None):
console = Console()
logging.basicConfig(
level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)]
)
run(backup_dir, loki_url)
run(backup_dir, loki_url, tag)
snapshots.run(loki_url)
if __name__ == '__main__':

View File

@@ -22,4 +22,9 @@ def send_to_loki(loki_url: str, line: str, backup: str):
except Exception as e:
logger.exception(e)
else:
logger.info(f'Sent line to loki at {loki_url} {resp.text}')
if resp.status_code == 204:
logger.info(f'Sent line to loki at {loki_url}')
else:
logger.error(
f'Problem sending log line to Loki. Response status code {resp.status_code}: {resp.text}'
)

View File

@@ -10,21 +10,15 @@ from restic.loki import send_to_loki
def run(loki_url: str = None):
cmd = 'restic snapshots --json'
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 console.status('Getting snapshots...'):
logger.debug(f'Running cmd [bright_black]{" ".join(cmd)}[/]')
result = subprocess.run(cmd, capture_output=True, text=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)
line = result.stdout
data = json.loads(line)
console.print(data)
if loki_url is not None:
send_to_loki(loki_url=loki_url, line=line, backup='snapshots')