diff --git a/.gitignore b/.gitignore index c0f52fa..9e58681 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -*.egg-info \ No newline at end of file +*.egg-info +build/ \ No newline at end of file diff --git a/README.md b/README.md index 07d964c..303d322 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,18 @@ ## Usage +### Python + +```shell +python -m restic.backup +``` + +```shell +python -m restic.prune +``` + +### Shell Scripts + ```shell ./restic_command.sh snapshots ``` diff --git a/pyproject.toml b/pyproject.toml index 7d67656..4869009 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,4 +12,4 @@ authors = [ license = { file = "LICENSE" } requires-python = ">=3.10" -dependencies = ["rich", "requests"] +dependencies = ["rich", "requests", "click"] diff --git a/src/restic/backup.py b/src/restic/backup.py index 590d4b0..9eea237 100755 --- a/src/restic/backup.py +++ b/src/restic/backup.py @@ -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()