2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-12 00:06:35 +00:00
restic/cmd/restic/cmd_repair_snapshots.go

177 lines
5.0 KiB
Go
Raw Normal View History

2020-08-05 19:32:15 +00:00
package main
import (
2022-12-10 16:18:04 +00:00
"context"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
2020-08-05 19:32:15 +00:00
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/walker"
2020-08-05 19:32:15 +00:00
"github.com/spf13/cobra"
)
2022-12-10 16:25:38 +00:00
var cmdRepairSnapshots = &cobra.Command{
Use: "snapshots [flags] [snapshot ID] [...]",
2020-08-05 19:32:15 +00:00
Short: "Repair snapshots",
Long: `
2022-12-28 14:32:00 +00:00
The "repair snapshots" command repairs broken snapshots. It scans the given
snapshots and generates new ones with damaged directories and file contents
removed. If the broken snapshots are deleted, a prune run will be able to
clean up the repository.
The command depends on a correct index, thus make sure to run "repair index"
first!
WARNING
=======
Repairing and deleting broken snapshots causes data loss! It will remove broken
directories and modify broken files in the modified snapshots.
If the contents of directories and files are still available, the better option
is to run "backup" which in that case is able to heal existing snapshots. Only
use the "repair snapshots" command if you need to recover an old and broken
snapshot!
2020-08-05 19:32:15 +00:00
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
`,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
2022-12-10 16:25:38 +00:00
return runRepairSnapshots(cmd.Context(), globalOptions, repairSnapshotOptions, args)
2020-08-05 19:32:15 +00:00
},
}
2021-02-20 19:56:03 +00:00
// RepairOptions collects all options for the repair command.
2020-08-05 19:32:15 +00:00
type RepairOptions struct {
DryRun bool
Forget bool
2022-12-10 16:18:04 +00:00
restic.SnapshotFilter
2020-08-05 19:32:15 +00:00
}
2022-12-10 16:25:38 +00:00
var repairSnapshotOptions RepairOptions
2020-08-05 19:32:15 +00:00
func init() {
2022-12-10 16:25:38 +00:00
cmdRepair.AddCommand(cmdRepairSnapshots)
flags := cmdRepairSnapshots.Flags()
2022-12-10 16:18:04 +00:00
flags.BoolVarP(&repairSnapshotOptions.DryRun, "dry-run", "n", false, "do not do anything, just print what would be done")
flags.BoolVarP(&repairSnapshotOptions.Forget, "forget", "", false, "remove original snapshots after creating new ones")
2022-12-10 16:25:38 +00:00
initMultiSnapshotFilter(flags, &repairSnapshotOptions.SnapshotFilter, true)
2020-08-05 19:32:15 +00:00
}
2022-12-10 16:25:38 +00:00
func runRepairSnapshots(ctx context.Context, gopts GlobalOptions, opts RepairOptions, args []string) error {
2022-12-10 16:18:04 +00:00
repo, err := OpenRepository(ctx, globalOptions)
2020-08-05 19:32:15 +00:00
if err != nil {
return err
}
if !opts.DryRun {
var lock *restic.Lock
var err error
lock, ctx, err = lockRepoExclusive(ctx, repo, gopts.RetryLock, gopts.JSON)
defer unlockRepo(lock)
if err != nil {
return err
}
} else {
repo.SetDryRun()
}
snapshotLister, err := backend.MemorizeList(ctx, repo.Backend(), restic.SnapshotFile)
2020-08-05 19:32:15 +00:00
if err != nil {
return err
}
2022-12-10 16:18:04 +00:00
if err := repo.LoadIndex(ctx); err != nil {
2020-08-05 19:32:15 +00:00
return err
}
// Three error cases are checked:
// - tree is a nil tree (-> will be replaced by an empty tree)
// - trees which cannot be loaded (-> the tree contents will be removed)
// - files whose contents are not fully available (-> file will be modified)
rewriter := walker.NewTreeRewriter(walker.RewriteOpts{
RewriteNode: func(node *restic.Node, path string) *restic.Node {
if node.Type != "file" {
return node
}
ok := true
var newContent restic.IDs = restic.IDs{}
var newSize uint64
// check all contents and remove if not available
for _, id := range node.Content {
if size, found := repo.LookupBlobSize(id, restic.DataBlob); !found {
ok = false
} else {
newContent = append(newContent, id)
newSize += uint64(size)
}
}
if !ok {
Verbosef(" file %q: removed missing content\n", path)
} else if newSize != node.Size {
Verbosef(" file %q: fixed incorrect size\n", path)
}
// no-ops if already correct
node.Content = newContent
node.Size = newSize
return node
},
RewriteFailedTree: func(nodeID restic.ID, path string, _ error) (restic.ID, error) {
if path == "/" {
2022-12-28 11:15:39 +00:00
Verbosef(" dir %q: not readable\n", path)
// remove snapshots with invalid root node
return restic.ID{}, nil
}
// If a subtree fails to load, remove it
2022-12-28 11:15:39 +00:00
Verbosef(" dir %q: replaced with empty directory\n", path)
emptyID, err := restic.SaveTree(ctx, repo, &restic.Tree{})
if err != nil {
return restic.ID{}, err
}
return emptyID, nil
},
AllowUnstableSerialization: true,
})
changedCount := 0
for sn := range FindFilteredSnapshots(ctx, snapshotLister, repo, &opts.SnapshotFilter, args) {
Verbosef("\nsnapshot %s of %v at %s)\n", sn.ID().Str(), sn.Paths, sn.Time)
changed, err := filterAndReplaceSnapshot(ctx, repo, sn,
func(ctx context.Context, sn *restic.Snapshot) (restic.ID, error) {
return rewriter.RewriteTree(ctx, repo, "/", *sn.Tree)
}, opts.DryRun, opts.Forget, "repaired")
if err != nil {
return errors.Fatalf("unable to rewrite snapshot ID %q: %v", sn.ID().Str(), err)
2020-08-05 19:32:15 +00:00
}
if changed {
changedCount++
2020-08-05 19:32:15 +00:00
}
}
Verbosef("\n")
if changedCount == 0 {
if !opts.DryRun {
Verbosef("no snapshots were modified\n")
} else {
Verbosef("no snapshots would be modified\n")
2020-08-05 19:32:15 +00:00
}
} else {
if !opts.DryRun {
Verbosef("modified %v snapshots\n", changedCount)
} else {
Verbosef("would modify %v snapshots\n", changedCount)
}
2020-08-05 19:32:15 +00:00
}
2020-08-05 19:32:15 +00:00
return nil
}