Support forget / prune

This commit is contained in:
Einar Forselv 2019-12-04 23:25:15 +01:00
parent 7dd72ee5ce
commit 96beeab5bd
2 changed files with 29 additions and 8 deletions

View File

@ -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"""

View File

@ -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")