Add per-job backup retention

This commit is contained in:
Mikei386
2026-06-14 13:27:25 +02:00
parent 7e2c604c5f
commit 4f76d86b9e
13 changed files with 83 additions and 14 deletions
+13 -1
View File
@@ -58,7 +58,19 @@ func (r *Runner) Backup(ctx context.Context, repo model.Repository, job model.Jo
}
func (r *Runner) Forget(ctx context.Context, repo model.Repository, job model.Job) error {
args := []string{"forget", "--tag", "job:" + job.ID, "--keep-daily", fmt.Sprint(job.Retention.Daily), "--keep-weekly", fmt.Sprint(job.Retention.Weekly), "--keep-monthly", fmt.Sprint(job.Retention.Monthly)}
args := []string{"forget", "--tag", "job:" + job.ID}
if job.Retention.KeepWithinDays > 0 {
args = append(args, "--keep-within", fmt.Sprintf("%dd", job.Retention.KeepWithinDays))
}
if job.Retention.Daily > 0 {
args = append(args, "--keep-daily", fmt.Sprint(job.Retention.Daily))
}
if job.Retention.Weekly > 0 {
args = append(args, "--keep-weekly", fmt.Sprint(job.Retention.Weekly))
}
if job.Retention.Monthly > 0 {
args = append(args, "--keep-monthly", fmt.Sprint(job.Retention.Monthly))
}
return r.run(ctx, repo, args, nil, nil)
}
+25
View File
@@ -45,3 +45,28 @@ func TestBackupUsesPasswordFileAndStructuredArguments(t *testing.T) {
t.Fatal("password file was not provided")
}
}
func TestForgetUsesAgeAndCalendarRetention(t *testing.T) {
dir := t.TempDir()
argsPath := filepath.Join(dir, "args")
script := filepath.Join(dir, "restic")
body := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' \"$@\" > '%s'\n", argsPath)
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
t.Fatal(err)
}
runner := &Runner{Binary: script, RuntimeDir: dir, Secrets: fakeSecrets{"password": "secret"}}
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo", PasswordRef: "password"}
job := model.Job{ID: "job", Retention: model.Retention{KeepWithinDays: 30, Weekly: 4, Monthly: 12}}
if err := runner.Forget(context.Background(), repo, job); err != nil {
t.Fatal(err)
}
args, _ := os.ReadFile(argsPath)
for _, expected := range []string{"--keep-within", "30d", "--keep-weekly", "4", "--keep-monthly", "12"} {
if !strings.Contains(string(args), expected) {
t.Fatalf("retention arguments missing %q: %s", expected, args)
}
}
if strings.Contains(string(args), "--keep-daily") {
t.Fatalf("disabled daily retention was included: %s", args)
}
}