Files
kopia/cli/command_policy_set_retention.go
Jarek Kowalski 90df511609 fix(snapshots): treat empty retention policy as retaining ALL, not NONE (#1733)
This is a safety measure which addresses P0 improvement for #1732.

Given that retention policies that retain nothing make no sense, this
is not considered a breaking change.
2022-02-07 11:40:27 -08:00

51 lines
2.1 KiB
Go

package cli
import (
"context"
"github.com/alecthomas/kingpin"
"github.com/kopia/kopia/snapshot/policy"
)
type policyRetentionFlags struct {
policySetKeepLatest string
policySetKeepHourly string
policySetKeepDaily string
policySetKeepWeekly string
policySetKeepMonthly string
policySetKeepAnnual string
}
func (c *policyRetentionFlags) setup(cmd *kingpin.CmdClause) {
cmd.Flag("keep-latest", "Number of most recent backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepLatest)
cmd.Flag("keep-hourly", "Number of most-recent hourly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepHourly)
cmd.Flag("keep-daily", "Number of most-recent daily backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepDaily)
cmd.Flag("keep-weekly", "Number of most-recent weekly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepWeekly)
cmd.Flag("keep-monthly", "Number of most-recent monthly backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepMonthly)
cmd.Flag("keep-annual", "Number of most-recent annual backups to keep per source (or 'inherit')").PlaceHolder("N").StringVar(&c.policySetKeepAnnual)
}
func (c *policyRetentionFlags) setRetentionPolicyFromFlags(ctx context.Context, rp *policy.RetentionPolicy, changeCount *int) error {
cases := []struct {
desc string
max **policy.OptionalInt
flagValue string
}{
{"number of annual backups to keep", &rp.KeepAnnual, c.policySetKeepAnnual},
{"number of monthly backups to keep", &rp.KeepMonthly, c.policySetKeepMonthly},
{"number of weekly backups to keep", &rp.KeepWeekly, c.policySetKeepWeekly},
{"number of daily backups to keep", &rp.KeepDaily, c.policySetKeepDaily},
{"number of hourly backups to keep", &rp.KeepHourly, c.policySetKeepHourly},
{"number of latest backups to keep", &rp.KeepLatest, c.policySetKeepLatest},
}
for _, c := range cases {
if err := applyOptionalInt(ctx, c.desc, c.max, c.flagValue, changeCount); err != nil {
return err
}
}
return nil
}