func runInit(gopts GlobalOptions, args []string) error { if gopts.Repo == "" { return errors.Fatal("Please specify repository location (-r)") } be, err := create(gopts.Repo) if err != nil { return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err) } if gopts.password == "" { gopts.password, err = ReadPasswordTwice(gopts, "enter password for new backend: ", "enter password again: ") if err != nil { return err } } s := repository.New(be) err = s.Init(gopts.password) if err != nil { return errors.Fatalf("create key in backend at %s failed: %v\n", gopts.Repo, err) } Verbosef("created restic backend %v at %s\n", s.Config().ID[:10], gopts.Repo) Verbosef("\n") Verbosef("Please note that knowledge of your password is required to access\n") Verbosef("the repository. Losing your password means that your data is\n") Verbosef("irrecoverably lost.\n") return nil }
// OpenRepository reads the password and opens the repository. func OpenRepository(opts GlobalOptions) (*repository.Repository, error) { if opts.Repo == "" { return nil, errors.Fatal("Please specify repository location (-r)") } be, err := open(opts.Repo) if err != nil { return nil, err } s := repository.New(be) if opts.password == "" { opts.password, err = ReadPassword(opts, "enter password for repository: ") if err != nil { return nil, err } } err = s.SearchKey(opts.password, maxKeys) if err != nil { return nil, errors.Fatalf("unable to open repo: %v", err) } return s, nil }
// ReadPassword reads the password from a password file, the environment // variable RESTIC_PASSWORD or prompts the user. func ReadPassword(opts GlobalOptions, prompt string) (string, error) { if opts.PasswordFile != "" { s, err := ioutil.ReadFile(opts.PasswordFile) return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile") } if pwd := os.Getenv("RESTIC_PASSWORD"); pwd != "" { return pwd, nil } var ( password string err error ) if stdinIsTerminal() { password, err = readPasswordTerminal(os.Stdin, os.Stderr, prompt) } else { password, err = readPassword(os.Stdin) } if err != nil { return "", errors.Wrap(err, "unable to read password") } if len(password) == 0 { return "", errors.Fatal("an empty password is not a password") } return password, nil }
func deleteKey(repo *repository.Repository, name string) error { if name == repo.KeyName() { return errors.Fatal("refusing to remove key currently used to access repository") } err := repo.Backend().Remove(restic.KeyFile, name) if err != nil { return err } Verbosef("removed key %v\n", name) return nil }
// ReadPasswordTwice calls ReadPassword two times and returns an error when the // passwords don't match. func ReadPasswordTwice(gopts GlobalOptions, prompt1, prompt2 string) (string, error) { pw1, err := ReadPassword(gopts, prompt1) if err != nil { return "", err } pw2, err := ReadPassword(gopts, prompt2) if err != nil { return "", err } if pw1 != pw2 { return "", errors.Fatal("passwords do not match") } return pw1, nil }
// filterExisting returns a slice of all existing items, or an error if no // items exist at all. func filterExisting(items []string) (result []string, err error) { for _, item := range items { _, err := fs.Lstat(item) if err != nil && os.IsNotExist(errors.Cause(err)) { continue } result = append(result, item) } if len(result) == 0 { return nil, errors.Fatal("all target directories/files do not exist") } return }
func runList(opts GlobalOptions, args []string) error { if len(args) != 1 { return errors.Fatalf("type not specified") } repo, err := OpenRepository(opts) if err != nil { return err } if !opts.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } } var t restic.FileType switch args[0] { case "packs": t = restic.DataFile case "index": t = restic.IndexFile case "snapshots": t = restic.SnapshotFile case "keys": t = restic.KeyFile case "locks": t = restic.LockFile default: return errors.Fatal("invalid type") } for id := range repo.List(t, nil) { Printf("%s\n", id) } return nil }
func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { if len(args) != 0 { return errors.Fatal("check has no arguments") } repo, err := OpenRepository(gopts) if err != nil { return err } if !gopts.NoLock { Verbosef("Create exclusive lock for repository\n") lock, err := lockRepoExclusive(repo) defer unlockRepo(lock) if err != nil { return err } } chkr := checker.New(repo) Verbosef("Load indexes\n") hints, errs := chkr.LoadIndex() dupFound := false for _, hint := range hints { Printf("%v\n", hint) if _, ok := hint.(checker.ErrDuplicatePacks); ok { dupFound = true } } if dupFound { Printf("\nrun `restic rebuild-index' to correct this\n") } if len(errs) > 0 { for _, err := range errs { Warnf("error: %v\n", err) } return errors.Fatal("LoadIndex returned errors") } done := make(chan struct{}) defer close(done) errorsFound := false errChan := make(chan error) Verbosef("Check all packs\n") go chkr.Packs(errChan, done) for err := range errChan { errorsFound = true fmt.Fprintf(os.Stderr, "%v\n", err) } Verbosef("Check snapshots, trees and blobs\n") errChan = make(chan error) go chkr.Structure(errChan, done) for err := range errChan { 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) } } if opts.CheckUnused { for _, id := range chkr.UnusedBlobs() { Verbosef("unused blob %v\n", id.Str()) errorsFound = true } } if opts.ReadData { Verbosef("Read all data\n") p := newReadProgress(gopts, restic.Stat{Blobs: chkr.CountPacks()}) errChan := make(chan error) go chkr.ReadData(p, errChan, done) for err := range errChan { errorsFound = true fmt.Fprintf(os.Stderr, "%v\n", err) } } if errorsFound { return errors.Fatal("repository contains errors") } return nil }
func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error { if len(args) != 1 { return errors.Fatalf("no snapshot ID specified") } if opts.Target == "" { return errors.Fatal("please specify a directory to restore to (--target)") } if len(opts.Exclude) > 0 && len(opts.Include) > 0 { return errors.Fatal("exclude and include patterns are mutually exclusive") } snapshotIDString := args[0] debug.Log("restore %v to %v", snapshotIDString, opts.Target) repo, err := OpenRepository(gopts) if err != nil { return err } if !gopts.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } } err = repo.LoadIndex() if err != nil { return err } var id restic.ID if snapshotIDString == "latest" { id, err = restic.FindLatestSnapshot(repo, opts.Paths, opts.Host) if err != nil { Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Host:%v", err, opts.Paths, opts.Host) } } else { id, err = restic.FindSnapshot(repo, snapshotIDString) if err != nil { Exitf(1, "invalid id %q: %v", snapshotIDString, err) } } res, err := restic.NewRestorer(repo, id) if err != nil { Exitf(2, "creating restorer failed: %v\n", err) } res.Error = func(dir string, node *restic.Node, err error) error { Warnf("error for %s: %+v\n", dir, err) return nil } selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(opts.Exclude, item) if err != nil { Warnf("error for exclude pattern: %v", err) } return !matched } selectIncludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(opts.Include, item) if err != nil { Warnf("error for include pattern: %v", err) } return matched } if len(opts.Exclude) > 0 { res.SelectFilter = selectExcludeFilter } else if len(opts.Include) > 0 { res.SelectFilter = selectIncludeFilter } Verbosef("restoring %s to %s\n", res.Snapshot(), opts.Target) return res.RestoreTo(opts.Target) }
func runCat(gopts GlobalOptions, args []string) error { if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) { return errors.Fatalf("type or ID not specified") } repo, err := OpenRepository(gopts) if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } tpe := args[0] var id restic.ID if tpe != "masterkey" && tpe != "config" { id, err = restic.ParseID(args[1]) if err != nil { if tpe != "snapshot" { return errors.Fatalf("unable to parse ID: %v\n", err) } // find snapshot id with prefix id, err = restic.FindSnapshot(repo, args[1]) if err != nil { return err } } } // handle all types that don't need an index switch tpe { case "config": buf, err := json.MarshalIndent(repo.Config(), "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "index": buf, err := repo.LoadAndDecrypt(restic.IndexFile, id) if err != nil { return err } _, err = os.Stdout.Write(append(buf, '\n')) return err case "snapshot": sn := &restic.Snapshot{} err = repo.LoadJSONUnpacked(restic.SnapshotFile, id, sn) if err != nil { return err } buf, err := json.MarshalIndent(&sn, "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "key": h := restic.Handle{Type: restic.KeyFile, Name: id.String()} buf, err := backend.LoadAll(repo.Backend(), h, nil) if err != nil { return err } key := &repository.Key{} err = json.Unmarshal(buf, key) if err != nil { return err } buf, err = json.MarshalIndent(&key, "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "masterkey": buf, err := json.MarshalIndent(repo.Key(), "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil case "lock": lock, err := restic.LoadLock(repo, id) if err != nil { return err } buf, err := json.MarshalIndent(&lock, "", " ") if err != nil { return err } fmt.Println(string(buf)) return nil } // load index, handle all the other types err = repo.LoadIndex() if err != nil { return err } switch tpe { case "pack": h := restic.Handle{Type: restic.DataFile, Name: id.String()} buf, err := backend.LoadAll(repo.Backend(), h, nil) if err != nil { return err } hash := restic.Hash(buf) if !hash.Equal(id) { fmt.Fprintf(stderr, "Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String()) } _, err = os.Stdout.Write(buf) return err case "blob": for _, t := range []restic.BlobType{restic.DataBlob, restic.TreeBlob} { list, err := repo.Index().Lookup(id, t) if err != nil { continue } blob := list[0] buf := make([]byte, blob.Length) n, err := repo.LoadBlob(restic.DataBlob, id, buf) if err != nil { return err } buf = buf[:n] _, err = os.Stdout.Write(buf) return err } return errors.Fatal("blob not found") case "tree": debug.Log("cat tree %v", id.Str()) tree, err := repo.LoadTree(id) if err != nil { debug.Log("unable to load tree %v: %v", id.Str(), err) return err } buf, err := json.MarshalIndent(&tree, "", " ") if err != nil { debug.Log("error json.MarshalIndent(): %v", err) return err } _, err = os.Stdout.Write(append(buf, '\n')) return nil default: return errors.Fatal("invalid type") } }