2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-01-01 15:17:33 +00:00
|
|
|
"syscall"
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2014-11-16 11:05:54 +00:00
|
|
|
"github.com/juju/arrar"
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2015-04-26 12:46:15 +00:00
|
|
|
"github.com/restic/restic/server"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Restorer struct {
|
2015-04-26 12:46:15 +00:00
|
|
|
s *server.Server
|
2014-12-21 17:10:19 +00:00
|
|
|
sn *Snapshot
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
Error func(dir string, node *Node, err error) error
|
2015-01-01 14:20:21 +00:00
|
|
|
Filter func(item string, dstpath string, node *Node) bool
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRestorer creates a restorer preloaded with the content from the snapshot snid.
|
2015-04-26 12:46:15 +00:00
|
|
|
func NewRestorer(s *server.Server, snid backend.ID) (*Restorer, error) {
|
2014-12-21 17:10:19 +00:00
|
|
|
r := &Restorer{s: s}
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
r.sn, err = LoadSnapshot(s, snid)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2014-11-21 20:21:44 +00:00
|
|
|
return nil, arrar.Annotate(err, "load snapshot for restorer")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// abort on all errors
|
|
|
|
r.Error = func(string, *Node, error) error { return err }
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
func (res *Restorer) to(dst string, dir string, treeBlob server.Blob) error {
|
2015-03-28 14:07:08 +00:00
|
|
|
tree, err := LoadTree(res.s, treeBlob)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2015-01-10 22:40:10 +00:00
|
|
|
return res.Error(dir, nil, arrar.Annotate(err, "LoadTree"))
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
for _, node := range tree.Nodes {
|
2015-01-01 14:20:21 +00:00
|
|
|
dstpath := filepath.Join(dst, dir, node.Name)
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2015-01-01 14:25:40 +00:00
|
|
|
if res.Filter == nil ||
|
2015-03-02 13:48:47 +00:00
|
|
|
res.Filter(filepath.Join(dir, node.Name), dstpath, node) {
|
2015-02-03 20:04:51 +00:00
|
|
|
err := CreateNodeAt(node, tree.Map, res.s, dstpath)
|
2015-01-01 15:17:33 +00:00
|
|
|
|
|
|
|
// Did it fail because of ENOENT?
|
|
|
|
if arrar.Check(err, func(err error) bool {
|
|
|
|
if pe, ok := err.(*os.PathError); ok {
|
|
|
|
errn, ok := pe.Err.(syscall.Errno)
|
|
|
|
return ok && errn == syscall.ENOENT
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}) {
|
|
|
|
// Create parent directories and retry
|
|
|
|
err = os.MkdirAll(filepath.Dir(dstpath), 0700)
|
|
|
|
if err == nil || err == os.ErrExist {
|
2015-02-03 20:04:51 +00:00
|
|
|
err = CreateNodeAt(node, tree.Map, res.s, dstpath)
|
2015-01-01 15:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2015-01-01 14:20:21 +00:00
|
|
|
err = res.Error(dstpath, node, arrar.Annotate(err, "create node"))
|
2015-01-01 13:50:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Type == "dir" {
|
|
|
|
if node.Subtree == nil {
|
2015-01-10 22:40:10 +00:00
|
|
|
return errors.New(fmt.Sprintf("Dir without subtree in tree %s", treeBlob))
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 14:20:21 +00:00
|
|
|
subp := filepath.Join(dir, node.Name)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
subtreeBlob, err := tree.Map.FindID(node.Subtree)
|
|
|
|
if err != nil {
|
|
|
|
err = res.Error(subp, node, arrar.Annotate(err, "lookup subtree"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.to(dst, subp, subtreeBlob)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2015-01-01 14:20:21 +00:00
|
|
|
err = res.Error(subp, node, arrar.Annotate(err, "restore subtree"))
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTo creates the directories and files in the snapshot below dir.
|
|
|
|
// Before an item is created, res.Filter is called.
|
|
|
|
func (res *Restorer) RestoreTo(dir string) error {
|
2015-01-01 14:20:21 +00:00
|
|
|
return res.to(dir, "", res.sn.Tree)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (res *Restorer) Snapshot() *Snapshot {
|
|
|
|
return res.sn
|
|
|
|
}
|