added click interface
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import logging
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
from rich.progress import Progress
|
||||
|
||||
from restic.console import console, logger
|
||||
from restic.loki import send_to_loki
|
||||
|
||||
|
||||
def main(backup_dir: Path, loki_url: str):
|
||||
def run(backup_dir: Path, loki_url: str = None):
|
||||
cmd = f'restic backup {backup_dir} --json --tag python-script'
|
||||
|
||||
logger.debug(f'Running cmd [bright_black]{cmd}[/]')
|
||||
@@ -42,19 +42,27 @@ def main(backup_dir: Path, loki_url: str):
|
||||
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'
|
||||
)
|
||||
|
||||
send_to_loki(loki_url=loki_url, line=line, backup='summary')
|
||||
if loki_url is not None:
|
||||
send_to_loki(loki_url=loki_url, line=line, backup='summary')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
|
||||
from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
|
||||
@click.command()
|
||||
@click.option(
|
||||
'--backup-dir',
|
||||
type=click.Path(file_okay=False, readable=True, path_type=Path),
|
||||
help='Directory to backup',
|
||||
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):
|
||||
console = Console()
|
||||
|
||||
logging.basicConfig(
|
||||
level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)]
|
||||
)
|
||||
|
||||
main(Path(os.environ['BACKUP_DIR']), 'http://192.168.1.107:3100/loki/api/v1/push')
|
||||
run(backup_dir, loki_url)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user