add retention policies

This commit is contained in:
dave 2022-12-12 21:49:40 -08:00
parent d8046ae41b
commit 5635cf8c32

View File

@ -144,17 +144,67 @@ def cmd_backup(args, parser):
raise Exception(stdout)
# run backup
backup_cmd = [RESTIC_BIN, "backup", "./", "--json"]
backup_cmd = [RESTIC_BIN, "backup", "./"]
for k, v in backup_tags.items():
backup_cmd.append("--tag")
backup_cmd.append("{}={}".format(k, v))
proc = subprocess.Popen(backup_cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, _ = proc.communicate(input=None, timeout=None)
stdout = stdout.decode()
proc = subprocess.Popen(backup_cmd, env=env)
proc.communicate(input=None, timeout=None)
# perform retention
schedule = backup_config.get('schedule')
if not schedule:
return
retention_args = get_retention_args(schedule)
if not retention_args:
return
proc = subprocess.Popen([RESTIC_BIN, "forget"] + retention_args, env=env)
proc.communicate(input=None, timeout=None)
# perform junk deletion
proc = subprocess.Popen([RESTIC_BIN, "prune"], env=env)
proc.communicate(input=None, timeout=None)
def get_retention_args(schedule):
"""
given a retention schedule, return restic command(s) needed to make it so
"""
mode = schedule["function"]
if mode == "forever":
# do not perform deletions
return None
elif mode == "keep":
# just keep the last X snapshots
return ["--keep-last", str(mode["count"])]
elif mode == "cycle":
# keep the last $last backups.
# keep a daily backup after that for the past $daily days.
# keep a weekly backup after that for the past $weekly weeks.
# keep a monthly backup after that for the past $monthly months.
cmd = []
last = schedule.get("last")
if last is not None:
cmd.extend(["--keep-last", str(last)])
daily = schedule.get("daily")
if daily is not None:
cmd.extend(["--keep-daily", str(daily)])
weekly = schedule.get("weekly")
if weekly is not None:
cmd.extend(["--keep-weekly", str(weekly)])
monthly = schedule.get("monthly")
if monthly is not None:
cmd.extend(["--keep-monthly", str(monthly)])
return cmd or None
else:
raise Exception("unknown retention function {}".format(mode))
def cmd_restore(args, parser):