restic/src/cmds/restic/cmd_check.go

166 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
2015-07-11 14:00:49 +00:00
"fmt"
"os"
2015-12-06 16:29:31 +00:00
"time"
2015-12-06 16:29:31 +00:00
"golang.org/x/crypto/ssh/terminal"
"restic"
"restic/checker"
2016-09-01 20:17:37 +00:00
"restic/errors"
)
type CmdCheck struct {
2016-09-15 20:29:49 +00:00
ReadData bool `long:"read-data" description:"Read data blobs"`
CheckUnused bool `long:"check-unused" description:"Check for unused blobs"`
global *GlobalOptions
}
func init() {
_, err := parser.AddCommand("check",
"check the repository",
"The check command check the integrity and consistency of the repository",
&CmdCheck{global: &globalOpts})
if err != nil {
panic(err)
}
}
func (cmd CmdCheck) Usage() string {
return "[check-options]"
}
2015-12-06 16:29:31 +00:00
func (cmd CmdCheck) newReadProgress(todo restic.Stat) *restic.Progress {
if !cmd.global.ShowProgress() {
return nil
}
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] + "... "
}
}
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
}
func (cmd CmdCheck) Execute(args []string) error {
if len(args) != 0 {
2016-09-01 20:17:37 +00:00
return errors.Fatal("check has no arguments")
}
repo, err := cmd.global.OpenRepository()
if err != nil {
return err
}
2015-11-13 11:33:59 +00:00
if !cmd.global.NoLock {
cmd.global.Verbosef("Create exclusive lock for repository\n")
lock, err := lockRepoExclusive(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
}
chkr := checker.New(repo)
cmd.global.Verbosef("Load indexes\n")
hints, errs := chkr.LoadIndex()
2015-10-25 16:24:52 +00:00
dupFound := false
for _, hint := range hints {
cmd.global.Printf("%v\n", hint)
2015-10-25 16:24:52 +00:00
if _, ok := hint.(checker.ErrDuplicatePacks); ok {
dupFound = true
}
}
if dupFound {
cmd.global.Printf("\nrun `restic rebuild-index' to correct this\n")
}
if len(errs) > 0 {
for _, err := range errs {
cmd.global.Warnf("error: %v\n", err)
}
2016-09-01 20:17:37 +00:00
return errors.Fatal("LoadIndex returned errors")
}
done := make(chan struct{})
defer close(done)
2015-07-11 14:00:49 +00:00
errorsFound := false
errChan := make(chan error)
2015-07-11 14:00:49 +00:00
cmd.global.Verbosef("Check all packs\n")
go chkr.Packs(errChan, done)
for err := range errChan {
2015-07-11 14:00:49 +00:00
errorsFound = true
fmt.Fprintf(os.Stderr, "%v\n", err)
2015-07-11 14:00:49 +00:00
}
cmd.global.Verbosef("Check snapshots, trees and blobs\n")
errChan = make(chan error)
go chkr.Structure(errChan, done)
for err := range errChan {
2015-07-11 14:00:49 +00:00
errorsFound = true
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
}
if cmd.CheckUnused {
for _, id := range chkr.UnusedBlobs() {
cmd.global.Verbosef("unused blob %v\n", id.Str())
errorsFound = true
}
}
2015-12-06 16:09:06 +00:00
if cmd.ReadData {
2015-12-06 16:29:31 +00:00
cmd.global.Verbosef("Read all data\n")
2015-12-06 16:09:06 +00:00
2015-12-06 16:29:31 +00:00
p := cmd.newReadProgress(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
}
return nil
}