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
+4 -3
View File
@@ -74,9 +74,10 @@ type Consistency struct {
}
type Retention struct {
Daily int `json:"daily"`
Weekly int `json:"weekly"`
Monthly int `json:"monthly"`
KeepWithinDays int `json:"keepWithinDays,omitempty"`
Daily int `json:"daily"`
Weekly int `json:"weekly"`
Monthly int `json:"monthly"`
}
type Repository struct {
+4 -1
View File
@@ -87,9 +87,12 @@ func ValidateJob(j Job) error {
if j.Consistency.Mode != "live" && j.Consistency.Mode != "stop" {
return errors.New("consistency mode must be live or stop")
}
if j.Retention.Daily < 0 || j.Retention.Weekly < 0 || j.Retention.Monthly < 0 {
if j.Retention.KeepWithinDays < 0 || j.Retention.Daily < 0 || j.Retention.Weekly < 0 || j.Retention.Monthly < 0 {
return errors.New("retention values cannot be negative")
}
if j.Retention.KeepWithinDays == 0 && j.Retention.Daily == 0 && j.Retention.Weekly == 0 && j.Retention.Monthly == 0 {
return errors.New("retention must keep at least one age or calendar policy")
}
for _, exclude := range j.Excludes {
if strings.ContainsAny(exclude, "\x00\r\n") {
return errors.New("exclude contains forbidden control characters")
+12 -1
View File
@@ -36,7 +36,7 @@ func TestRepositoryCannotBeSharedByJobs(t *testing.T) {
}
func TestDockerJobRequiresWorkloadSources(t *testing.T) {
job := Job{SchemaVersion: 1, ID: "docker-job", Name: "Docker", Type: JobDocker, RepositoryID: "repo", Sources: []Source{{Path: "/mnt/user/appdata"}}, Consistency: Consistency{Mode: "live"}, Compression: "auto"}
job := Job{SchemaVersion: 1, ID: "docker-job", Name: "Docker", Type: JobDocker, RepositoryID: "repo", Sources: []Source{{Path: "/mnt/user/appdata"}}, Consistency: Consistency{Mode: "live"}, Compression: "auto", Retention: Retention{KeepWithinDays: 30}}
if err := ValidateJob(job); err == nil {
t.Fatal("docker path source accepted without workload selection")
}
@@ -45,3 +45,14 @@ func TestDockerJobRequiresWorkloadSources(t *testing.T) {
t.Fatalf("valid docker workload rejected: %v", err)
}
}
func TestRetentionRequiresAtLeastOneRule(t *testing.T) {
job := Job{SchemaVersion: 1, ID: "job", Name: "Share", Type: JobShare, RepositoryID: "repo", Sources: []Source{{Path: "/mnt/user/data"}}, Consistency: Consistency{Mode: "live"}, Compression: "auto"}
if err := ValidateJob(job); err == nil {
t.Fatal("empty retention policy accepted")
}
job.Retention.KeepWithinDays = 30
if err := ValidateJob(job); err != nil {
t.Fatalf("age retention rejected: %v", err)
}
}
+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)
}
}