func (cmd CmdLs) Execute(args []string) error { if len(args) < 1 || len(args) > 2 { return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } id, err := restic.FindSnapshot(repo, args[0]) if err != nil { return err } sn, err := restic.LoadSnapshot(repo, id) if err != nil { return err } cmd.global.Verbosef("snapshot of %v at %s:\n", sn.Paths, sn.Time) return cmd.printTree("", repo, sn.Tree) }
func (c CmdFind) Execute(args []string) error { if len(args) != 1 { return fmt.Errorf("wrong number of arguments, Usage: %s", c.Usage()) } var err error if c.Oldest != "" { c.oldest, err = parseTime(c.Oldest) if err != nil { return err } } if c.Newest != "" { c.newest, err = parseTime(c.Newest) if err != nil { return err } } repo, err := c.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } c.pattern = args[0] if c.Snapshot != "" { snapshotID, err := restic.FindSnapshot(repo, c.Snapshot) if err != nil { return fmt.Errorf("invalid id %q: %v", args[1], err) } return c.findInSnapshot(repo, snapshotID) } done := make(chan struct{}) defer close(done) for snapshotID := range repo.List(backend.Snapshot, done) { err := c.findInSnapshot(repo, snapshotID) if err != nil { return err } } return nil }
func (cmd CmdRestore) Execute(args []string) error { if len(args) != 1 { return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } if cmd.Target == "" { return errors.New("please specify a directory to restore to (--target)") } if len(cmd.Exclude) > 0 && len(cmd.Include) > 0 { return errors.New("exclude and include patterns are mutually exclusive") } snapshotIDString := args[0] debug.Log("restore", "restore %v to %v", snapshotIDString, cmd.Target) repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } id, err := restic.FindSnapshot(repo, snapshotIDString) if err != nil { cmd.global.Exitf(1, "invalid id %q: %v", snapshotIDString, err) } res, err := restic.NewRestorer(repo, id) if err != nil { cmd.global.Exitf(2, "creating restorer failed: %v\n", err) } res.Error = func(dir string, node *restic.Node, err error) error { cmd.global.Warnf("error for %s: %+v\n", dir, err) return err } selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(cmd.Exclude, item) if err != nil { cmd.global.Warnf("error for exclude pattern: %v", err) } return !matched } selectIncludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(cmd.Include, item) if err != nil { cmd.global.Warnf("error for include pattern: %v", err) } return matched } if len(cmd.Exclude) > 0 { res.SelectFilter = selectExcludeFilter } else if len(cmd.Include) > 0 { res.SelectFilter = selectIncludeFilter } cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), cmd.Target) err = res.RestoreTo(cmd.Target) if err != nil { return err } return nil }
func (cmd CmdRestore) Execute(args []string) error { if len(args) < 2 || len(args) > 3 { return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } id, err := restic.FindSnapshot(repo, args[0]) if err != nil { cmd.global.Exitf(1, "invalid id %q: %v", args[0], err) } target := args[1] // create restorer res, err := restic.NewRestorer(repo, id) if err != nil { cmd.global.Exitf(2, "creating restorer failed: %v\n", err) } res.Error = func(dir string, node *restic.Node, err error) error { cmd.global.Warnf("error for %s: %+v\n", dir, err) // if node.Type == "dir" { // if e, ok := err.(*os.PathError); ok { // if errn, ok := e.Err.(syscall.Errno); ok { // if errn == syscall.EEXIST { // fmt.Printf("ignoring already existing directory %s\n", dir) // return nil // } // } // } // } return err } // TODO: a filter against the full path sucks as filepath.Match doesn't match // directory separators on '*'. still, it's better than nothing. if len(args) > 2 { res.Filter = func(item string, dstpath string, node *restic.Node) bool { matched, err := filepath.Match(item, args[2]) if err != nil { panic(err) } return matched } } cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), target) err = res.RestoreTo(target) if err != nil { return err } return nil }
func (cmd CmdCat) Execute(args []string) error { if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) { return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } tpe := args[0] var id backend.ID if tpe != "masterkey" && tpe != "config" { id, err = backend.ParseID(args[1]) if err != nil { if tpe != "snapshot" { return 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(backend.Index, id) if err != nil { return err } _, err = os.Stdout.Write(append(buf, '\n')) return err case "snapshot": sn := &restic.Snapshot{} err = repo.LoadJSONUnpacked(backend.Snapshot, 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": rd, err := repo.Backend().Get(backend.Key, id.String()) if err != nil { return err } dec := json.NewDecoder(rd) var key repository.Key err = dec.Decode(&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": rd, err := repo.Backend().Get(backend.Data, id.String()) if err != nil { return err } _, err = io.Copy(os.Stdout, rd) return err case "blob": blob, err := repo.Index().Lookup(id) if err != nil { return err } buf := make([]byte, blob.Length) data, err := repo.LoadBlob(blob.Type, id, buf) if err != nil { return err } _, err = os.Stdout.Write(data) return err case "tree": debug.Log("cat", "cat tree %v", id.Str()) tree := restic.NewTree() err = repo.LoadJSONPack(pack.Tree, id, tree) if err != nil { debug.Log("cat", "unable to load tree %v: %v", id.Str(), err) return err } buf, err := json.MarshalIndent(&tree, "", " ") if err != nil { debug.Log("cat", "error json.MarshalIndent(): %v", err) return err } _, err = os.Stdout.Write(append(buf, '\n')) return nil default: return errors.New("invalid type") } }
func (cmd CmdBackup) Execute(args []string) error { if len(args) == 0 { return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) } target := make([]string, 0, len(args)) for _, d := range args { if a, err := filepath.Abs(d); err == nil { d = a } target = append(target, d) } target, err := filterExisting(target) if err != nil { return err } repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } var parentSnapshotID backend.ID // Force using a parent if !cmd.Force && cmd.Parent != "" { parentSnapshotID, err = restic.FindSnapshot(repo, cmd.Parent) if err != nil { return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) } cmd.global.Verbosef("found parent snapshot %v\n", parentSnapshotID.Str()) } // Find last snapshot to set it as parent, if not already set if !cmd.Force && parentSnapshotID == nil { parentSnapshotID, err = findLatestSnapshot(repo, target) if err != nil { return err } if parentSnapshotID != nil { cmd.global.Verbosef("using parent snapshot %v\n", parentSnapshotID) } } cmd.global.Verbosef("scan %v\n", target) selectFilter := func(item string, fi os.FileInfo) bool { matched, err := filter.List(cmd.Excludes, item) if err != nil { cmd.global.Warnf("error for exclude pattern: %v", err) } return !matched } stat, err := restic.Scan(target, selectFilter, cmd.newScanProgress()) if err != nil { return err } arch := restic.NewArchiver(repo) arch.Excludes = cmd.Excludes arch.SelectFilter = selectFilter arch.Error = func(dir string, fi os.FileInfo, err error) error { // TODO: make ignoring errors configurable cmd.global.Warnf("\x1b[2K\rerror for %s: %v\n", dir, err) return nil } _, id, err := arch.Snapshot(cmd.newArchiveProgress(stat), target, parentSnapshotID) if err != nil { return err } cmd.global.Verbosef("snapshot %s saved\n", id.Str()) return nil }
func (cmd CmdFsck) Execute(args []string) error { if len(args) != 0 { return errors.New("fsck has no arguments") } if cmd.RemoveOrphaned && !cmd.Orphaned { cmd.Orphaned = true } s, err := cmd.global.OpenRepository() if err != nil { return err } err = s.LoadIndex() if err != nil { return err } if cmd.Snapshot != "" { id, err := restic.FindSnapshot(s, cmd.Snapshot) if err != nil { return fmt.Errorf("invalid id %q: %v", cmd.Snapshot, err) } err = fsckSnapshot(cmd, s, id) if err != nil { fmt.Fprintf(os.Stderr, "check for snapshot %v failed\n", id) } return err } if cmd.Orphaned { cmd.o_data = backend.NewIDSet() cmd.o_trees = backend.NewIDSet() } done := make(chan struct{}) defer close(done) var firstErr error for id := range s.List(backend.Snapshot, done) { err = fsckSnapshot(cmd, s, id) if err != nil { fmt.Fprintf(os.Stderr, "check for snapshot %v failed\n", id) firstErr = err } } if !cmd.Orphaned { return firstErr } debug.Log("restic.fsck", "starting orphaned check\n") cnt := make(map[pack.BlobType]*backend.IDSet) cnt[pack.Data] = cmd.o_data cnt[pack.Tree] = cmd.o_trees for blob := range s.Index().Each(done) { debug.Log("restic.fsck", "checking %v blob %v\n", blob.Type, blob.ID) err = cnt[blob.Type].Find(blob.ID) if err != nil { debug.Log("restic.fsck", " blob %v is orphaned\n", blob.ID) if !cmd.RemoveOrphaned { fmt.Printf("orphaned %v blob %v\n", blob.Type, blob.ID) continue } fmt.Printf("removing orphaned %v blob %v\n", blob.Type, blob.ID) // err := s.Remove(d.tpe, name) // if err != nil { // return err // } return errors.New("not implemented") } } return firstErr }