From 96beeab5bd7299344f3ea8d60a33f64fa7b514eb Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Wed, 4 Dec 2019 23:25:15 +0100 Subject: [PATCH] Support forget / prune --- restic_compose_backup/cli.py | 22 ++++++++++++++++++---- restic_compose_backup/config.py | 15 +++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/restic_compose_backup/cli.py b/restic_compose_backup/cli.py index 1faa231..4700c9a 100644 --- a/restic_compose_backup/cli.py +++ b/restic_compose_backup/cli.py @@ -150,12 +150,26 @@ def start_backup_process(config, containers): if errors: exit(1) + # Only run cleanup if backup was successful + result = cleanup(config, container) + logger.debug('cleanup exit code: %s', errors) + if result != 0: + exit(1) + def cleanup(config, containers): - # restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --keep-yearly 3 - # restic snapshots 5fecf605 - logger.info('Running forget/prune') - + """Run forget / prune to minimize storage space""" + logger.info('Forget outdated snapshots') + forget_result = restic.forget( + config.repository, + config.keep_daily, + config.keep_weekly, + config.keep_monthly, + config.keep_yearly, + ) + logger.info('Prune stale data freeing storage space') + prune_result = restic.prune(config.repository) + return forget_result == 0 and prune_result == 0 def snapshots(config, containers): """Display restic snapshots""" diff --git a/restic_compose_backup/config.py b/restic_compose_backup/config.py index 04f2238..0adf4b2 100644 --- a/restic_compose_backup/config.py +++ b/restic_compose_backup/config.py @@ -4,16 +4,23 @@ import os class Config: """Bag for config values""" def __init__(self, check=True): - self.repository = os.environ['RESTIC_REPOSITORY'] - self.password = os.environ['RESTIC_PASSWORD'] + # Mandatory values + self.repository = os.environ.get('RESTIC_REPOSITORY') + self.password = os.environ.get('RESTIC_REPOSITORY') self.docker_base_url = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock" + # forget / keep + self.keep_daily = os.environ.get('KEEP_DAILY') or "7" + self.keep_weekly = os.environ.get('KEEP_WEEKLY') or "4" + self.keep_monthly = os.environ.get('KEEP_MONTHLY') or "12" + self.keep_yearly = os.environ.get('KEEP_YEARLY') or "3" + if check: self.check() def check(self): if not self.repository: - raise ValueError("CONTAINER env var not set") + raise ValueError("RESTIC_REPOSITORY env var not set") if not self.password: - raise ValueError("PASSWORD env var not set") + raise ValueError("RESTIC_REPOSITORY env var not set")