-
Notifications
You must be signed in to change notification settings - Fork 61
/
prune.go
72 lines (60 loc) · 1.89 KB
/
prune.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cli
import (
"fmt"
"github.com/k8up-io/k8up/v2/restic/cfg"
"github.com/k8up-io/k8up/v2/restic/logging"
)
// Prune will enforce the retention policy onto the repository
func (r *Restic) Prune(tags ArrayOpts) error {
prunelogger := r.logger.WithName("prune")
prunelogger.Info("pruning repository")
args := []string{"--prune"}
keepN := map[string]int{
"--keep-last": cfg.Config.PruneKeepLast,
"--keep-hourly": cfg.Config.PruneKeepHourly,
"--keep-daily": cfg.Config.PruneKeepDaily,
"--keep-weekly": cfg.Config.PruneKeepWeekly,
"--keep-monthly": cfg.Config.PruneKeepMonthly,
"--keep-yearly": cfg.Config.PruneKeepYearly,
}
for argName, argVal := range keepN {
if argVal > 0 {
args = append(args, argName, fmt.Sprintf("%d", argVal))
}
}
keepWithin := map[string]string{
"--keep-within": cfg.Config.PruneKeepWithin,
"--keep-within-hourly": cfg.Config.PruneKeepWithinHourly,
"--keep-within-daily": cfg.Config.PruneKeepWithinDaily,
"--keep-within-weekly": cfg.Config.PruneKeepWithinWeekly,
"--keep-within-monthly": cfg.Config.PruneKeepWithinMonthly,
"--keep-within-yearly": cfg.Config.PruneKeepWithinYearly,
}
for argName, argVal := range keepWithin {
if argVal != "" {
args = append(args, argName, argVal)
}
}
if cfg.Config.PruneKeepTags {
args = append(args, "--keep-tag")
}
if cfg.Config.Hostname != "" {
args = append(args, "--host="+cfg.Config.Hostname)
}
resticPruneLogger := prunelogger.WithName("restic")
opts := CommandOptions{
Path: r.resticPath,
Args: r.globalFlags.ApplyToCommand("forget", args...),
StdOut: logging.NewInfoWriter(resticPruneLogger),
StdErr: logging.NewErrorWriter(resticPruneLogger),
}
if len(tags) > 0 {
opts.Args = append(opts.Args, tags.BuildArgs()...)
}
cmd := NewCommand(r.ctx, prunelogger, opts)
cmd.Run()
if cmd.FatalError == nil {
r.sendSnapshotList()
}
return cmd.FatalError
}