added click interface

This commit is contained in:
John Lancaster
2024-05-26 22:53:44 -05:00
parent b05b361dd4
commit a01ea3219b
4 changed files with 34 additions and 13 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
__pycache__ __pycache__
*.egg-info *.egg-info
build/

View File

@@ -2,6 +2,18 @@
## Usage ## Usage
### Python
```shell
python -m restic.backup
```
```shell
python -m restic.prune
```
### Shell Scripts
```shell ```shell
./restic_command.sh snapshots ./restic_command.sh snapshots
``` ```

View File

@@ -12,4 +12,4 @@ authors = [
license = { file = "LICENSE" } license = { file = "LICENSE" }
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = ["rich", "requests"] dependencies = ["rich", "requests", "click"]

View File

@@ -1,18 +1,18 @@
#!/usr/bin/env python3
import json import json
import logging import logging
import subprocess import subprocess
from pathlib import Path from pathlib import Path
import click
from rich.console import Console from rich.console import Console
from rich.logging import RichHandler
from rich.progress import Progress from rich.progress import Progress
from restic.console import console, logger from restic.console import console, logger
from restic.loki import send_to_loki 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' cmd = f'restic backup {backup_dir} --json --tag python-script'
logger.debug(f'Running cmd [bright_black]{cmd}[/]') 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' 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__': @click.command()
import os @click.option(
'--backup-dir',
from rich.console import Console type=click.Path(file_okay=False, readable=True, path_type=Path),
from rich.logging import RichHandler 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() console = Console()
logging.basicConfig( logging.basicConfig(
level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)] 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()