diff --git a/repository_restic.go b/repository_restic.go index b78a3f2e1ca44eff19b8e423b71338da3a5c1218..2570f5b40b62936724888e2a7bf6b9fd22ef888f 100644 --- a/repository_restic.go +++ b/repository_restic.go @@ -24,6 +24,7 @@ type resticRepository struct { shell *Shell excludes []string excludeFiles []string + autoPrune bool } func (r *resticRepository) resticCmd() string { @@ -80,13 +81,17 @@ func newResticRepository(params map[string]interface{}, shell *Shell) (Repositor ex, _ := params["exclude"].([]string) exf, _ := params["exclude_files"].([]string) - bin, _ := params["restic_binary"].(string) - if bin == "" { - bin = "restic" + bin := "restic" + if s, ok := params["restic_binary"].(string); ok { + bin = s } if err := checkResticVersion(bin); err != nil { return nil, err } + autoPrune := true + if b, ok := params["autoprune"].(bool); ok { + autoPrune = b + } tmpf, err := ioutil.TempFile("", "restic-pw-") if err != nil { @@ -108,6 +113,7 @@ func newResticRepository(params map[string]interface{}, shell *Shell) (Repositor excludes: ex, excludeFiles: exf, shell: shell, + autoPrune: autoPrune, }, nil } @@ -125,6 +131,9 @@ func (r *resticRepository) Init(ctx context.Context) error { } func (r *resticRepository) Prepare(ctx context.Context, backup Backup) error { + if !r.autoPrune { + return nil + } return r.shell.Run(ctx, fmt.Sprintf( "%s forget --host %s --keep-last 10 --prune", r.resticCmd(), @@ -221,6 +230,9 @@ type resticSnapshot struct { } func parseResticSnapshots(output []byte) (snapshots []resticSnapshot, err error) { - err = json.Unmarshal(output, &snapshots) + // An empty input is not necessarily an error. + if len(output) > 0 { + err = json.Unmarshal(output, &snapshots) + } return } diff --git a/repository_restic_test.go b/repository_restic_test.go index 68de2da081b974b3a9474c13e233a21216e79e3d..8ecc989a453394a88809b787aeabdad5446231c6 100644 --- a/repository_restic_test.go +++ b/repository_restic_test.go @@ -15,6 +15,8 @@ import ( // Create a temporary directory with two subdirs: 'data', for // the test backup data, and 'repo' to store the (remote) // repository. Populate 'data' with two tiny files. +// +// nolint func createTempDirWithData(t *testing.T) string { tmpdir, err := ioutil.TempDir("", "") if err != nil { @@ -112,10 +114,13 @@ func TestRestic(t *testing.T) { } // Check the 'restic snapshots' output. - output, _ := exec.Command("env", "RESTIC_PASSWORD=testpass", "restic", "-r", tmpdir+"/repo", "snapshots", "--json").Output() + output, err := exec.Command("env", "RESTIC_PASSWORD=testpass", "restic", "-r", tmpdir+"/repo", "snapshots", "--json").Output() + if err != nil { + t.Fatalf("'restic snapshots' failed: %v", err) + } snaps, err := parseResticSnapshots(output) if err != nil { - t.Fatalf("parsing restic snaphots output: %v", err) + t.Fatalf("parsing restic snaphots output: %v, output:\n%s", err, string(output)) } if len(snaps) != 1 { t.Fatalf("wrong number of snapshots: %+v", snaps)