2015-06-28 22:22:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-07-11 14:00:49 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-12-06 16:29:31 +00:00
|
|
|
"time"
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2015-12-06 16:29:31 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
|
|
|
"restic/checker"
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2015-06-28 22:22:25 +00:00
|
|
|
)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var cmdCheck = &cobra.Command{
|
|
|
|
Use: "check [flags]",
|
|
|
|
Short: "check the repository for errors",
|
|
|
|
Long: `
|
|
|
|
The "check" command tests the repository for errors and reports any errors it
|
|
|
|
finds. It can also be used to read all data and therefore simulate a restore.
|
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runCheck(checkOptions, globalOptions, args)
|
|
|
|
},
|
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
// CheckOptions bundle all options for the 'check' command.
|
|
|
|
type CheckOptions struct {
|
|
|
|
ReadData bool
|
|
|
|
CheckUnused bool
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
var checkOptions CheckOptions
|
|
|
|
|
2015-06-28 22:22:25 +00:00
|
|
|
func init() {
|
2016-09-17 10:36:05 +00:00
|
|
|
cmdRoot.AddCommand(cmdCheck)
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
f := cmdCheck.Flags()
|
|
|
|
f.BoolVar(&checkOptions.ReadData, "read-data", false, "Read all data blobs")
|
|
|
|
f.BoolVar(&checkOptions.CheckUnused, "check-unused", false, "Find unused blobs")
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func newReadProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress {
|
|
|
|
if gopts.Quiet {
|
2015-12-06 16:29:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:13:47 +00:00
|
|
|
readProgress := restic.NewProgress()
|
2015-12-06 16:29:31 +00:00
|
|
|
|
|
|
|
readProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
status := fmt.Sprintf("[%s] %s %d / %d items",
|
|
|
|
formatDuration(d),
|
|
|
|
formatPercent(s.Blobs, todo.Blobs),
|
|
|
|
s.Blobs, todo.Blobs)
|
|
|
|
|
|
|
|
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
|
|
|
|
if err == nil {
|
|
|
|
if len(status) > w {
|
|
|
|
max := w - len(status) - 4
|
|
|
|
status = status[:max] + "... "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:13:47 +00:00
|
|
|
PrintProgress("%s", status)
|
2015-12-06 16:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
|
|
|
|
fmt.Printf("\nduration: %s\n", formatDuration(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
return readProgress
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error {
|
2015-06-28 22:22:25 +00:00
|
|
|
if len(args) != 0 {
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("check has no arguments")
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
repo, err := OpenRepository(gopts)
|
2015-06-28 22:22:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if !gopts.NoLock {
|
|
|
|
Verbosef("Create exclusive lock for repository\n")
|
2015-11-10 20:41:22 +00:00
|
|
|
lock, err := lockRepoExclusive(repo)
|
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-12 15:09:48 +00:00
|
|
|
chkr := checker.New(repo)
|
2015-06-28 22:22:25 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("Load indexes\n")
|
2015-10-25 15:26:50 +00:00
|
|
|
hints, errs := chkr.LoadIndex()
|
|
|
|
|
2015-10-25 16:24:52 +00:00
|
|
|
dupFound := false
|
2015-10-25 15:26:50 +00:00
|
|
|
for _, hint := range hints {
|
2016-09-17 10:36:05 +00:00
|
|
|
Printf("%v\n", hint)
|
2015-10-25 16:24:52 +00:00
|
|
|
if _, ok := hint.(checker.ErrDuplicatePacks); ok {
|
|
|
|
dupFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dupFound {
|
2016-09-17 10:36:05 +00:00
|
|
|
Printf("\nrun `restic rebuild-index' to correct this\n")
|
2015-10-25 15:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
for _, err := range errs {
|
2016-09-17 10:36:05 +00:00
|
|
|
Warnf("error: %v\n", err)
|
2015-10-25 15:26:50 +00:00
|
|
|
}
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("LoadIndex returned errors")
|
2015-06-28 22:22:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 23:44:19 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
2015-07-11 14:00:49 +00:00
|
|
|
errorsFound := false
|
2015-07-11 23:44:19 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("Check all packs\n")
|
2015-07-12 15:09:48 +00:00
|
|
|
go chkr.Packs(errChan, done)
|
2015-07-11 23:44:19 +00:00
|
|
|
|
|
|
|
for err := range errChan {
|
2015-07-11 14:00:49 +00:00
|
|
|
errorsFound = true
|
2015-07-12 15:09:48 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("Check snapshots, trees and blobs\n")
|
2015-07-11 23:44:19 +00:00
|
|
|
errChan = make(chan error)
|
2015-07-12 15:09:48 +00:00
|
|
|
go chkr.Structure(errChan, done)
|
2015-07-11 23:44:19 +00:00
|
|
|
|
|
|
|
for err := range errChan {
|
2015-07-11 14:00:49 +00:00
|
|
|
errorsFound = true
|
2015-10-11 17:13:45 +00:00
|
|
|
if e, ok := err.(checker.TreeError); ok {
|
|
|
|
fmt.Fprintf(os.Stderr, "error for tree %v:\n", e.ID.Str())
|
|
|
|
for _, treeErr := range e.Errors {
|
|
|
|
fmt.Fprintf(os.Stderr, " %v\n", treeErr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
}
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if opts.CheckUnused {
|
2015-11-08 19:46:52 +00:00
|
|
|
for _, id := range chkr.UnusedBlobs() {
|
2016-09-17 10:36:05 +00:00
|
|
|
Verbosef("unused blob %v\n", id.Str())
|
2015-11-08 19:46:52 +00:00
|
|
|
errorsFound = true
|
|
|
|
}
|
2015-07-12 15:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if opts.ReadData {
|
|
|
|
Verbosef("Read all data\n")
|
2015-12-06 16:09:06 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
p := newReadProgress(gopts, restic.Stat{Blobs: chkr.CountPacks()})
|
2015-12-06 16:09:06 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
|
2015-12-06 16:29:31 +00:00
|
|
|
go chkr.ReadData(p, errChan, done)
|
2015-12-06 16:09:06 +00:00
|
|
|
|
|
|
|
for err := range errChan {
|
|
|
|
errorsFound = true
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 14:00:49 +00:00
|
|
|
if errorsFound {
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatal("repository contains errors")
|
2015-07-11 14:00:49 +00:00
|
|
|
}
|
2015-06-28 22:22:25 +00:00
|
|
|
return nil
|
|
|
|
}
|