2020-05-05 19:03:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-06 20:30:45 +00:00
|
|
|
"fmt"
|
2023-11-27 20:04:24 +00:00
|
|
|
"time"
|
2020-05-05 19:03:57 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-09-09 20:32:39 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-05-05 19:03:57 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2022-09-06 20:30:45 +00:00
|
|
|
"github.com/restic/restic/internal/walker"
|
2020-05-05 19:03:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRewrite = &cobra.Command{
|
2022-09-06 20:37:56 +00:00
|
|
|
Use: "rewrite [flags] [snapshotID ...]",
|
2022-10-24 23:01:47 +00:00
|
|
|
Short: "Rewrite snapshots to exclude unwanted files",
|
2020-05-05 19:03:57 +00:00
|
|
|
Long: `
|
2022-10-24 23:01:47 +00:00
|
|
|
The "rewrite" command excludes files from existing snapshots. It creates new
|
|
|
|
snapshots containing the same data as the original ones, but without the files
|
|
|
|
you specify to exclude. All metadata (time, host, tags) will be preserved.
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-10-24 23:01:47 +00:00
|
|
|
The snapshots to rewrite are specified using the --host, --tag and --path options,
|
|
|
|
or by providing a list of snapshot IDs. Please note that specifying neither any of
|
|
|
|
these options nor a snapshot ID will cause the command to rewrite all snapshots.
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-10-24 23:01:47 +00:00
|
|
|
The special tag 'rewrite' will be added to the new snapshots to distinguish
|
|
|
|
them from the original ones, unless --forget is used. If the --forget option is
|
|
|
|
used, the original snapshots will instead be directly removed from the repository.
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-10-24 23:01:47 +00:00
|
|
|
Please note that the --forget option only removes the snapshots and not the actual
|
|
|
|
data stored in the repository. In order to delete the no longer referenced data,
|
|
|
|
use the "prune" command.
|
2020-05-05 19:03:57 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
2024-06-30 15:52:50 +00:00
|
|
|
Exit status is 0 if the command was successful.
|
|
|
|
Exit status is 1 if there was any error.
|
2024-07-10 19:46:26 +00:00
|
|
|
Exit status is 10 if the repository does not exist.
|
2024-06-30 15:53:33 +00:00
|
|
|
Exit status is 11 if the repository is already locked.
|
2024-07-30 23:06:18 +00:00
|
|
|
Exit status is 12 if the password is incorrect.
|
2020-05-05 19:03:57 +00:00
|
|
|
`,
|
2024-08-23 21:48:45 +00:00
|
|
|
GroupID: cmdGroupDefault,
|
2020-05-05 19:03:57 +00:00
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-09-06 19:48:20 +00:00
|
|
|
return runRewrite(cmd.Context(), rewriteOptions, globalOptions, args)
|
2020-05-05 19:03:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:04:24 +00:00
|
|
|
type snapshotMetadata struct {
|
|
|
|
Hostname string
|
|
|
|
Time *time.Time
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
type snapshotMetadataArgs struct {
|
2023-11-27 20:04:24 +00:00
|
|
|
Hostname string
|
|
|
|
Time string
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
func (sma snapshotMetadataArgs) empty() bool {
|
|
|
|
return sma.Hostname == "" && sma.Time == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sma snapshotMetadataArgs) convert() (*snapshotMetadata, error) {
|
2023-12-24 14:03:45 +00:00
|
|
|
if sma.empty() {
|
2023-11-27 20:34:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeStamp *time.Time
|
|
|
|
if sma.Time != "" {
|
|
|
|
t, err := time.ParseInLocation(TimeFormat, sma.Time, time.Local)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Fatalf("error in time option: %v\n", err)
|
|
|
|
}
|
|
|
|
timeStamp = &t
|
|
|
|
}
|
|
|
|
return &snapshotMetadata{Hostname: sma.Hostname, Time: timeStamp}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:00:37 +00:00
|
|
|
// RewriteOptions collects all options for the rewrite command.
|
2020-05-05 19:03:57 +00:00
|
|
|
type RewriteOptions struct {
|
2023-12-24 13:36:27 +00:00
|
|
|
Forget bool
|
|
|
|
DryRun bool
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2023-12-24 14:03:45 +00:00
|
|
|
Metadata snapshotMetadataArgs
|
2023-02-17 15:13:46 +00:00
|
|
|
restic.SnapshotFilter
|
2022-09-07 21:01:45 +00:00
|
|
|
excludePatternOptions
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var rewriteOptions RewriteOptions
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdRewrite)
|
|
|
|
|
|
|
|
f := cmdRewrite.Flags()
|
2022-10-24 23:01:47 +00:00
|
|
|
f.BoolVarP(&rewriteOptions.Forget, "forget", "", false, "remove original snapshots after creating new ones")
|
2020-05-05 19:03:57 +00:00
|
|
|
f.BoolVarP(&rewriteOptions.DryRun, "dry-run", "n", false, "do not do anything, just print what would be done")
|
2023-12-24 14:03:45 +00:00
|
|
|
f.StringVar(&rewriteOptions.Metadata.Hostname, "new-host", "", "replace hostname")
|
|
|
|
f.StringVar(&rewriteOptions.Metadata.Time, "new-time", "", "replace time of the backup")
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2023-02-17 15:13:46 +00:00
|
|
|
initMultiSnapshotFilter(f, &rewriteOptions.SnapshotFilter, true)
|
2022-09-07 21:01:45 +00:00
|
|
|
initExcludePatternOptions(f, &rewriteOptions.excludePatternOptions)
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
type rewriteFilterFunc func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error)
|
|
|
|
|
2022-09-09 20:38:58 +00:00
|
|
|
func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *restic.Snapshot, opts RewriteOptions) (bool, error) {
|
2020-05-05 19:03:57 +00:00
|
|
|
if sn.Tree == nil {
|
|
|
|
return false, errors.Errorf("snapshot %v has nil tree", sn.ID().Str())
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:54:52 +00:00
|
|
|
rejectByNameFuncs, err := opts.excludePatternOptions.CollectPatterns()
|
2020-05-05 19:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-12-24 14:03:45 +00:00
|
|
|
metadata, err := opts.Metadata.convert()
|
2023-11-27 20:34:57 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:40:12 +00:00
|
|
|
var filter rewriteFilterFunc
|
|
|
|
|
|
|
|
if len(rejectByNameFuncs) > 0 {
|
|
|
|
selectByName := func(nodepath string) bool {
|
|
|
|
for _, reject := range rejectByNameFuncs {
|
|
|
|
if reject(nodepath) {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
2023-12-24 13:40:12 +00:00
|
|
|
return true
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 14:24:00 +00:00
|
|
|
rewriteNode := func(node *restic.Node, path string) *restic.Node {
|
|
|
|
if selectByName(path) {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
Verbosef(fmt.Sprintf("excluding %s\n", path))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rewriter, querySize := walker.NewSnapshotSizeRewriter(rewriteNode)
|
2023-12-24 13:40:12 +00:00
|
|
|
|
|
|
|
filter = func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error) {
|
2024-07-11 14:24:00 +00:00
|
|
|
id, err := rewriter.RewriteTree(ctx, repo, "/", *sn.Tree)
|
|
|
|
if err != nil {
|
|
|
|
return restic.ID{}, err
|
|
|
|
}
|
|
|
|
ss := querySize()
|
|
|
|
if sn.Summary != nil {
|
|
|
|
sn.Summary.TotalFilesProcessed = ss.FileCount
|
|
|
|
sn.Summary.TotalBytesProcessed = ss.FileSize
|
|
|
|
}
|
|
|
|
return id, err
|
2023-12-24 13:40:12 +00:00
|
|
|
}
|
2024-07-11 14:24:00 +00:00
|
|
|
|
2023-12-24 13:40:12 +00:00
|
|
|
} else {
|
2024-02-10 21:58:10 +00:00
|
|
|
filter = func(_ context.Context, sn *restic.Snapshot) (restic.ID, error) {
|
2023-12-24 13:40:12 +00:00
|
|
|
return *sn.Tree, nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 10:04:28 +00:00
|
|
|
|
2022-12-27 20:05:21 +00:00
|
|
|
return filterAndReplaceSnapshot(ctx, repo, sn,
|
2023-12-24 13:40:12 +00:00
|
|
|
filter, opts.DryRun, opts.Forget, metadata, "rewrite")
|
2022-12-27 20:05:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
func filterAndReplaceSnapshot(ctx context.Context, repo restic.Repository, sn *restic.Snapshot,
|
|
|
|
filter rewriteFilterFunc, dryRun bool, forget bool, newMetadata *snapshotMetadata, addTag string) (bool, error) {
|
2022-12-27 20:05:21 +00:00
|
|
|
|
2022-09-09 20:32:39 +00:00
|
|
|
wg, wgCtx := errgroup.WithContext(ctx)
|
|
|
|
repo.StartPackUploader(wgCtx, wg)
|
|
|
|
|
|
|
|
var filteredTree restic.ID
|
|
|
|
wg.Go(func() error {
|
2022-12-27 20:05:21 +00:00
|
|
|
var err error
|
|
|
|
filteredTree, err = filter(ctx, sn)
|
2022-09-09 20:32:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-09-09 20:32:39 +00:00
|
|
|
return repo.Flush(wgCtx)
|
|
|
|
})
|
2022-12-27 20:05:21 +00:00
|
|
|
err := wg.Wait()
|
2020-05-05 19:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-12-27 20:31:04 +00:00
|
|
|
if filteredTree.IsNull() {
|
|
|
|
if dryRun {
|
|
|
|
Verbosef("would delete empty snapshot\n")
|
|
|
|
} else {
|
2024-05-09 23:16:23 +00:00
|
|
|
if err = repo.RemoveUnpacked(ctx, restic.SnapshotFile, *sn.ID()); err != nil {
|
2022-12-27 20:31:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
debug.Log("removed empty snapshot %v", sn.ID())
|
|
|
|
Verbosef("removed empty snapshot %v\n", sn.ID().Str())
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
if filteredTree == *sn.Tree && newMetadata == nil {
|
2022-09-06 20:30:45 +00:00
|
|
|
debug.Log("Snapshot %v not modified", sn)
|
2020-05-05 19:03:57 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:30:45 +00:00
|
|
|
debug.Log("Snapshot %v modified", sn)
|
2022-12-27 20:05:21 +00:00
|
|
|
if dryRun {
|
2022-09-09 20:38:58 +00:00
|
|
|
Verbosef("would save new snapshot\n")
|
2022-10-24 22:59:18 +00:00
|
|
|
|
2022-12-27 20:05:21 +00:00
|
|
|
if forget {
|
2022-10-24 22:59:18 +00:00
|
|
|
Verbosef("would remove old snapshot\n")
|
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
if newMetadata != nil && newMetadata.Time != nil {
|
|
|
|
Verbosef("would set time to %s\n", newMetadata.Time)
|
2023-11-27 20:34:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
if newMetadata != nil && newMetadata.Hostname != "" {
|
2024-01-31 20:48:37 +00:00
|
|
|
Verbosef("would set hostname to %s\n", newMetadata.Hostname)
|
2023-11-27 20:34:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 19:03:57 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:40:41 +00:00
|
|
|
// Always set the original snapshot id as this essentially a new snapshot.
|
|
|
|
sn.Original = sn.ID()
|
2022-12-27 20:05:21 +00:00
|
|
|
sn.Tree = &filteredTree
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-12-27 20:05:21 +00:00
|
|
|
if !forget {
|
|
|
|
sn.AddTags([]string{addTag})
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
if newMetadata != nil && newMetadata.Time != nil {
|
|
|
|
Verbosef("setting time to %s\n", *newMetadata.Time)
|
|
|
|
sn.Time = *newMetadata.Time
|
2023-11-27 20:34:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 13:36:27 +00:00
|
|
|
if newMetadata != nil && newMetadata.Hostname != "" {
|
|
|
|
Verbosef("setting host to %s\n", newMetadata.Hostname)
|
|
|
|
sn.Hostname = newMetadata.Hostname
|
2023-11-27 20:34:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 19:03:57 +00:00
|
|
|
// Save the new snapshot.
|
2022-09-06 19:48:20 +00:00
|
|
|
id, err := restic.SaveSnapshot(ctx, repo, sn)
|
2020-05-05 19:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-04-17 19:00:45 +00:00
|
|
|
Verbosef("saved new snapshot %v\n", id.Str())
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-12-27 20:05:21 +00:00
|
|
|
if forget {
|
2024-05-09 23:16:23 +00:00
|
|
|
if err = repo.RemoveUnpacked(ctx, restic.SnapshotFile, *sn.ID()); err != nil {
|
2020-05-05 19:03:57 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2022-10-24 22:59:18 +00:00
|
|
|
debug.Log("removed old snapshot %v", sn.ID())
|
|
|
|
Verbosef("removed old snapshot %v\n", sn.ID().Str())
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:48:20 +00:00
|
|
|
func runRewrite(ctx context.Context, opts RewriteOptions, gopts GlobalOptions, args []string) error {
|
2023-12-24 14:03:45 +00:00
|
|
|
if opts.excludePatternOptions.Empty() && opts.Metadata.empty() {
|
2023-11-27 20:34:57 +00:00
|
|
|
return errors.Fatal("Nothing to do: no excludes provided and no new metadata provided")
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 14:19:02 +00:00
|
|
|
var (
|
|
|
|
repo *repository.Repository
|
|
|
|
unlock func()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if opts.Forget {
|
|
|
|
Verbosef("create exclusive lock for repository\n")
|
|
|
|
ctx, repo, unlock, err = openWithExclusiveLock(ctx, gopts, opts.DryRun)
|
|
|
|
} else {
|
|
|
|
ctx, repo, unlock, err = openWithAppendLock(ctx, gopts, opts.DryRun)
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-24 14:19:02 +00:00
|
|
|
defer unlock()
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2023-10-01 11:05:56 +00:00
|
|
|
snapshotLister, err := restic.MemorizeList(ctx, repo, restic.SnapshotFile)
|
2022-09-06 19:48:20 +00:00
|
|
|
if err != nil {
|
2020-05-05 19:03:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
if err = repo.LoadIndex(ctx, bar); err != nil {
|
2022-09-06 19:48:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
|
|
|
|
changedCount := 0
|
2023-02-17 15:13:46 +00:00
|
|
|
for sn := range FindFilteredSnapshots(ctx, snapshotLister, repo, &opts.SnapshotFilter, args) {
|
2024-01-06 19:19:17 +00:00
|
|
|
Verbosef("\n%v\n", sn)
|
2022-09-09 20:38:58 +00:00
|
|
|
changed, err := rewriteSnapshot(ctx, repo, sn, opts)
|
2020-05-05 19:03:57 +00:00
|
|
|
if err != nil {
|
2022-09-09 20:38:58 +00:00
|
|
|
return errors.Fatalf("unable to rewrite snapshot ID %q: %v", sn.ID().Str(), err)
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
changedCount++
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 23:19:58 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
|
2022-09-09 20:38:58 +00:00
|
|
|
Verbosef("\n")
|
2020-05-05 19:03:57 +00:00
|
|
|
if changedCount == 0 {
|
2022-10-24 22:59:18 +00:00
|
|
|
if !opts.DryRun {
|
|
|
|
Verbosef("no snapshots were modified\n")
|
|
|
|
} else {
|
|
|
|
Verbosef("no snapshots would be modified\n")
|
|
|
|
}
|
2020-05-05 19:03:57 +00:00
|
|
|
} else {
|
|
|
|
if !opts.DryRun {
|
|
|
|
Verbosef("modified %v snapshots\n", changedCount)
|
|
|
|
} else {
|
2022-09-09 20:38:58 +00:00
|
|
|
Verbosef("would modify %v snapshots\n", changedCount)
|
2020-05-05 19:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|