2018-08-23 20:09:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-20 18:37:21 +00:00
|
|
|
"context"
|
2018-08-23 20:09:05 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2021-11-06 00:14:24 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2018-08-23 20:09:05 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/spf13/cobra"
|
2021-08-07 20:52:05 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2018-08-23 20:09:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRecover = &cobra.Command{
|
|
|
|
Use: "recover [flags]",
|
2021-07-20 18:37:21 +00:00
|
|
|
Short: "Recover data from the repository not referenced by snapshots",
|
2018-08-23 20:09:05 +00:00
|
|
|
Long: `
|
2020-09-30 15:44:34 +00:00
|
|
|
The "recover" command builds a new snapshot from all directories it can find in
|
2021-07-20 18:37:21 +00:00
|
|
|
the raw data of the repository which are not referenced in an existing snapshot.
|
|
|
|
It can be used if, for example, a snapshot has been removed by accident with "forget".
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2018-08-23 20:09:05 +00:00
|
|
|
`,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runRecover(globalOptions)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdRecover)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRecover(gopts GlobalOptions) error {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := OpenRepository(gopts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:24:47 +00:00
|
|
|
lock, err := lockRepo(gopts.ctx, repo)
|
2018-08-23 20:09:05 +00:00
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:14:24 +00:00
|
|
|
snapshotLister, err := backend.MemorizeList(gopts.ctx, repo.Backend(), restic.SnapshotFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:09:05 +00:00
|
|
|
Verbosef("load index files\n")
|
|
|
|
if err = repo.LoadIndex(gopts.ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// trees maps a tree ID to whether or not it is referenced by a different
|
|
|
|
// tree. If it is not referenced, we have a root tree.
|
|
|
|
trees := make(map[restic.ID]bool)
|
|
|
|
|
2022-08-19 18:04:39 +00:00
|
|
|
repo.Index().Each(gopts.ctx, func(blob restic.PackedBlob) {
|
2021-07-20 18:37:21 +00:00
|
|
|
if blob.Type == restic.TreeBlob {
|
|
|
|
trees[blob.Blob.ID] = false
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
2022-08-19 18:04:39 +00:00
|
|
|
})
|
2018-08-23 20:09:05 +00:00
|
|
|
|
2021-07-20 18:37:21 +00:00
|
|
|
Verbosef("load %d trees\n", len(trees))
|
|
|
|
bar := newProgressMax(!gopts.Quiet, uint64(len(trees)), "trees loaded")
|
2018-08-23 20:09:05 +00:00
|
|
|
for id := range trees {
|
2022-06-12 12:38:19 +00:00
|
|
|
tree, err := restic.LoadTree(gopts.ctx, repo, id)
|
2018-08-23 20:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
Warnf("unable to load tree %v: %v\n", id.Str(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range tree.Nodes {
|
2021-07-20 18:37:21 +00:00
|
|
|
if node.Type == "dir" && node.Subtree != nil {
|
|
|
|
trees[*node.Subtree] = true
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-20 18:37:21 +00:00
|
|
|
bar.Add(1)
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
2021-07-20 18:37:21 +00:00
|
|
|
bar.Done()
|
|
|
|
|
|
|
|
Verbosef("load snapshots\n")
|
2021-11-06 00:14:24 +00:00
|
|
|
err = restic.ForAllSnapshots(gopts.ctx, snapshotLister, repo, nil, func(id restic.ID, sn *restic.Snapshot, err error) error {
|
2021-07-20 18:37:21 +00:00
|
|
|
trees[*sn.Tree] = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Verbosef("done\n")
|
2018-08-23 20:09:05 +00:00
|
|
|
|
|
|
|
roots := restic.NewIDSet()
|
|
|
|
for id, seen := range trees {
|
2021-07-20 18:37:21 +00:00
|
|
|
if !seen {
|
|
|
|
Verboseff("found root tree %v\n", id.Str())
|
|
|
|
roots.Insert(id)
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-20 18:37:21 +00:00
|
|
|
Printf("\nfound %d unreferenced roots\n", len(roots))
|
2018-08-23 20:09:05 +00:00
|
|
|
|
2021-07-20 18:37:21 +00:00
|
|
|
if len(roots) == 0 {
|
|
|
|
Verbosef("no snapshot to write.\n")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-23 20:09:05 +00:00
|
|
|
|
2021-09-26 15:18:42 +00:00
|
|
|
tree := restic.NewTree(len(roots))
|
2018-08-23 20:09:05 +00:00
|
|
|
for id := range roots {
|
|
|
|
var subtreeID = id
|
|
|
|
node := restic.Node{
|
2018-08-23 20:22:34 +00:00
|
|
|
Type: "dir",
|
|
|
|
Name: id.Str(),
|
|
|
|
Mode: 0755,
|
|
|
|
Subtree: &subtreeID,
|
|
|
|
AccessTime: time.Now(),
|
|
|
|
ModTime: time.Now(),
|
|
|
|
ChangeTime: time.Now(),
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
2021-07-20 18:37:21 +00:00
|
|
|
err := tree.Insert(&node)
|
2021-01-30 16:25:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 20:52:05 +00:00
|
|
|
wg, ctx := errgroup.WithContext(gopts.ctx)
|
|
|
|
repo.StartPackUploader(ctx, wg)
|
|
|
|
|
|
|
|
var treeID restic.ID
|
|
|
|
wg.Go(func() error {
|
|
|
|
var err error
|
2022-06-12 12:38:19 +00:00
|
|
|
treeID, err = restic.SaveTree(ctx, repo, tree)
|
2021-08-07 20:52:05 +00:00
|
|
|
if err != nil {
|
2022-05-07 20:23:59 +00:00
|
|
|
return errors.Fatalf("unable to save new tree to the repository: %v", err)
|
2021-08-07 20:52:05 +00:00
|
|
|
}
|
2018-08-23 20:09:05 +00:00
|
|
|
|
2021-08-07 20:52:05 +00:00
|
|
|
err = repo.Flush(ctx)
|
|
|
|
if err != nil {
|
2022-05-07 20:23:59 +00:00
|
|
|
return errors.Fatalf("unable to save blobs to the repository: %v", err)
|
2021-08-07 20:52:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
err = wg.Wait()
|
2018-08-23 20:09:05 +00:00
|
|
|
if err != nil {
|
2021-08-07 20:52:05 +00:00
|
|
|
return err
|
2018-08-23 20:09:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 18:37:21 +00:00
|
|
|
return createSnapshot(gopts.ctx, "/recover", hostname, []string{"recovered"}, repo, &treeID)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSnapshot(ctx context.Context, name, hostname string, tags []string, repo restic.Repository, tree *restic.ID) error {
|
|
|
|
sn, err := restic.NewSnapshot([]string{name}, tags, hostname, time.Now())
|
2018-08-23 20:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Fatalf("unable to save snapshot: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-07-20 18:37:21 +00:00
|
|
|
sn.Tree = tree
|
2018-08-23 20:09:05 +00:00
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
id, err := restic.SaveSnapshot(ctx, repo, sn)
|
2018-08-23 20:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Fatalf("unable to save snapshot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Printf("saved new snapshot %v\n", id.Str())
|
|
|
|
return nil
|
|
|
|
}
|