fleshed out prune module

This commit is contained in:
John Lancaster
2024-05-27 19:20:47 -05:00
parent 33260592f5
commit 9127b34739
2 changed files with 44 additions and 14 deletions

View File

@@ -7,6 +7,7 @@ import click
from pydantic import BaseModel
from rich.logging import RichHandler
from restic import size, snapshots
from restic.console import console, logger
from restic.loki import send_to_loki
@@ -26,7 +27,7 @@ class KeepStrategy(BaseModel):
def forget(loki_url: str = None, dry_run: bool = False, **kwargs):
cmd = ['restic', 'forget', '--json', '--prune']
cmd = ['restic', 'forget', '--json']
if dry_run:
cmd.append('--dry-run')
@@ -36,18 +37,24 @@ def forget(loki_url: str = None, dry_run: bool = False, **kwargs):
cmd.extend(args)
logger.debug(f'Running cmd [bright_black]{" ".join(cmd)}[/]')
with console.status('Forgetting...'):
result = subprocess.run(cmd, capture_output=True, text=True)
try:
with console.status('Forgetting...'):
result = subprocess.run(cmd, capture_output=True, text=True)
line = result.stdout
data = json.loads(line)
except json.JSONDecodeError:
logger.error('Error decoding JSON')
logger.error(line)
if result.returncode == 0:
try:
line = result.stdout
data = json.loads(line)
except json.JSONDecodeError:
logger.error('Error decoding JSON')
logger.error(line)
else:
if loki_url is not None and not dry_run:
send_to_loki(loki_url=loki_url, line=line, backup='forget')
finally:
logger.debug('Done')
else:
if loki_url is not None and not dry_run:
send_to_loki(loki_url=loki_url, line=line, backup='forget')
logger.error(f'Error running command')
logger.error(result.stderr)
@click.command()
@@ -72,7 +79,13 @@ def main(loki_url: str, dry_run: bool, **kwargs):
logging.getLogger('docker.utils.config').setLevel('WARNING')
logging.getLogger('urllib3.connectionpool').setLevel('WARNING')
forget(loki_url, dry_run=dry_run, **kwargs)
try:
forget(loki_url, dry_run=dry_run, **kwargs)
except Exception:
console.print_exception()
else:
snapshots.snapshot(loki_url)
size.get_size(loki_url)
if __name__ == '__main__':