forget: add feature flag for safe --keep-tags behavior

This commit is contained in:
Michael Eischer 2024-05-24 20:41:19 +02:00
parent 7948912b31
commit 3cc6827f09
3 changed files with 8 additions and 1 deletions

View File

@ -8,6 +8,10 @@ The `forget` command now fails with an error if all snapshots in a snapshot
group would be deleted. This prevents the above example from deleting all group would be deleted. This prevents the above example from deleting all
snapshots. snapshots.
It is possible to temporarily disable the new check by setting the environment variable
`RESTIC_FEATURES=safe-forget-keep-tags=false`. Note that this feature flag
will be removed in the next minor restic version.
https://github.com/restic/restic/issues/4568 https://github.com/restic/restic/issues/4568
https://github.com/restic/restic/pull/4764 https://github.com/restic/restic/pull/4764
https://forum.restic.net/t/delete-all-snapshots-in-one-command-is-this-feature-intentional/6923/3 https://forum.restic.net/t/delete-all-snapshots-in-one-command-is-this-feature-intentional/6923/3

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/feature"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui/termstatus" "github.com/restic/restic/internal/ui/termstatus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -258,7 +259,7 @@ func runForget(ctx context.Context, opts ForgetOptions, pruneOptions PruneOption
keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy) keep, remove, reasons := restic.ApplyPolicy(snapshotGroup, policy)
if !policy.Empty() && len(keep) == 0 { if feature.Flag.Enabled(feature.SafeForgetKeepTags) && !policy.Empty() && len(keep) == 0 {
return fmt.Errorf("refusing to delete last snapshot of snapshot group \"%v\"", key.String()) return fmt.Errorf("refusing to delete last snapshot of snapshot group \"%v\"", key.String())
} }
if len(keep) != 0 && !gopts.Quiet && !gopts.JSON { if len(keep) != 0 && !gopts.Quiet && !gopts.JSON {

View File

@ -9,6 +9,7 @@ const (
DeprecateLegacyIndex FlagName = "deprecate-legacy-index" DeprecateLegacyIndex FlagName = "deprecate-legacy-index"
DeprecateS3LegacyLayout FlagName = "deprecate-s3-legacy-layout" DeprecateS3LegacyLayout FlagName = "deprecate-s3-legacy-layout"
DeviceIDForHardlinks FlagName = "device-id-for-hardlinks" DeviceIDForHardlinks FlagName = "device-id-for-hardlinks"
SafeForgetKeepTags FlagName = "safe-forget-keep-tags"
) )
func init() { func init() {
@ -17,5 +18,6 @@ func init() {
DeprecateLegacyIndex: {Type: Beta, Description: "disable support for index format used by restic 0.1.0. Use `restic repair index` to update the index if necessary."}, DeprecateLegacyIndex: {Type: Beta, Description: "disable support for index format used by restic 0.1.0. Use `restic repair index` to update the index if necessary."},
DeprecateS3LegacyLayout: {Type: Beta, Description: "disable support for S3 legacy layout used up to restic 0.7.0. Use `RESTIC_FEATURES=deprecate-s3-legacy-layout=false restic migrate s3_layout` to migrate your S3 repository if necessary."}, DeprecateS3LegacyLayout: {Type: Beta, Description: "disable support for S3 legacy layout used up to restic 0.7.0. Use `RESTIC_FEATURES=deprecate-s3-legacy-layout=false restic migrate s3_layout` to migrate your S3 repository if necessary."},
DeviceIDForHardlinks: {Type: Alpha, Description: "store deviceID only for hardlinks to reduce metadata changes for example when using btrfs subvolumes. Will be removed in a future restic version after repository format 3 is available"}, DeviceIDForHardlinks: {Type: Alpha, Description: "store deviceID only for hardlinks to reduce metadata changes for example when using btrfs subvolumes. Will be removed in a future restic version after repository format 3 is available"},
SafeForgetKeepTags: {Type: Beta, Description: "prevent deleting all snapshots if the tag passed to `forget --keep-tags tagname` does not exist"},
}) })
} }