2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-23 05:12:10 +00:00

Add exclude filter to restorer and 'restore' command

This commit is contained in:
Alexander Neumann 2015-07-20 00:38:44 +02:00
parent 7fd52f9f57
commit c0337a2675
3 changed files with 45 additions and 47 deletions

View File

@ -1,14 +1,18 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"path/filepath"
"github.com/restic/restic" "github.com/restic/restic"
"github.com/restic/restic/debug" "github.com/restic/restic/debug"
"github.com/restic/restic/filter"
) )
type CmdRestore struct { type CmdRestore struct {
Exclude []string `short:"e" long:"exclude" description:"Exclude a pattern (can be specified multiple times)"`
Target string `short:"t" long:"target" description:"Directory to restore to"`
global *GlobalOptions global *GlobalOptions
} }
@ -23,14 +27,22 @@ func init() {
} }
func (cmd CmdRestore) Usage() string { func (cmd CmdRestore) Usage() string {
return "snapshot-ID TARGETDIR [PATTERN]" return "snapshot-ID"
} }
func (cmd CmdRestore) Execute(args []string) error { func (cmd CmdRestore) Execute(args []string) error {
if len(args) < 2 || len(args) > 3 { if len(args) != 1 {
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) 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)")
}
snapshotIDString := args[0]
debug.Log("restore", "restore %v to %v", snapshotIDString, cmd.Target)
repo, err := cmd.global.OpenRepository() repo, err := cmd.global.OpenRepository()
if err != nil { if err != nil {
return err return err
@ -47,14 +59,11 @@ func (cmd CmdRestore) Execute(args []string) error {
return err return err
} }
id, err := restic.FindSnapshot(repo, args[0]) id, err := restic.FindSnapshot(repo, snapshotIDString)
if err != nil { if err != nil {
cmd.global.Exitf(1, "invalid id %q: %v", args[0], err) cmd.global.Exitf(1, "invalid id %q: %v", snapshotIDString, err)
} }
target := args[1]
// create restorer
res, err := restic.NewRestorer(repo, id) res, err := restic.NewRestorer(repo, id)
if err != nil { if err != nil {
cmd.global.Exitf(2, "creating restorer failed: %v\n", err) cmd.global.Exitf(2, "creating restorer failed: %v\n", err)
@ -62,41 +71,25 @@ func (cmd CmdRestore) Execute(args []string) error {
res.Error = func(dir string, node *restic.Node, err error) error { res.Error = func(dir string, node *restic.Node, err error) error {
cmd.global.Warnf("error for %s: %+v\n", dir, err) 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 return err
} }
// TODO: a filter against the full path sucks as filepath.Match doesn't match selectFilter := func(item string, dstpath string, node *restic.Node) bool {
// directory separators on '*'. still, it's better than nothing. matched, err := filter.List(cmd.Exclude, item)
if len(args) > 2 { if err != nil {
pattern := args[2] cmd.global.Warnf("error for exclude pattern: %v", err)
cmd.global.Verbosef("filter pattern %q\n", pattern)
res.SelectForRestore = func(item string, dstpath string, node *restic.Node) bool {
matched, err := filepath.Match(pattern, node.Name)
if err != nil {
panic(err)
}
if !matched {
debug.Log("restic.restore", "item %v doesn't match pattern %q", item, pattern)
}
return matched
} }
return !matched
} }
cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), target) if len(cmd.Exclude) > 0 {
res.SelectFilter = selectFilter
}
err = res.RestoreTo(target) cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), cmd.Target)
err = res.RestoreTo(cmd.Target)
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,6 +17,7 @@ import (
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
"github.com/restic/restic/debug" "github.com/restic/restic/debug"
"github.com/restic/restic/filter"
. "github.com/restic/restic/test" . "github.com/restic/restic/test"
) )
@ -68,9 +69,13 @@ func cmdList(t testing.TB, global GlobalOptions, tpe string) []backend.ID {
return IDs return IDs
} }
func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID, args ...string) { func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) {
cmd := &CmdRestore{global: &global} cmdRestoreExcludes(t, global, dir, snapshotID, nil)
cmd.Execute(append([]string{snapshotID.String(), dir}, args...)) }
func cmdRestoreExcludes(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID, excludes []string) {
cmd := &CmdRestore{global: &global, Target: dir, Exclude: excludes}
OK(t, cmd.Execute([]string{snapshotID.String()}))
} }
func cmdCheck(t testing.TB, global GlobalOptions) { func cmdCheck(t testing.TB, global GlobalOptions) {
@ -517,10 +522,10 @@ func TestRestoreFilter(t *testing.T) {
for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} { for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} {
base := filepath.Join(env.base, fmt.Sprintf("restore%d", i+1)) base := filepath.Join(env.base, fmt.Sprintf("restore%d", i+1))
cmdRestore(t, global, base, snapshotID, pat) cmdRestoreExcludes(t, global, base, snapshotID, []string{pat})
for _, test := range testfiles { for _, test := range testfiles {
err := testFileSize(filepath.Join(base, "testdata", test.name), int64(test.size)) err := testFileSize(filepath.Join(base, "testdata", test.name), int64(test.size))
if ok, _ := filepath.Match(pat, filepath.Base(test.name)); ok { if ok, _ := filter.Match(pat, filepath.Base(test.name)); !ok {
OK(t, err) OK(t, err)
} else { } else {
Assert(t, os.IsNotExist(err), Assert(t, os.IsNotExist(err),
@ -558,7 +563,7 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) {
// restore with filter "*.ext", this should restore "file.ext", but // restore with filter "*.ext", this should restore "file.ext", but
// since the directories are ignored and only created because of // since the directories are ignored and only created because of
// "file.ext", no meta data should be restored for them. // "file.ext", no meta data should be restored for them.
cmdRestore(t, global, filepath.Join(env.base, "restore0"), snapshotID, "*.ext") cmdRestoreExcludes(t, global, filepath.Join(env.base, "restore0"), snapshotID, []string{"*.ext"})
f1 := filepath.Join(env.base, "restore0", "testdata", "subdir1", "subdir2") f1 := filepath.Join(env.base, "restore0", "testdata", "subdir1", "subdir2")
fi, err := os.Stat(f1) fi, err := os.Stat(f1)
@ -568,7 +573,7 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) {
"meta data of intermediate directory has been restore although it was ignored") "meta data of intermediate directory has been restore although it was ignored")
// restore with filter "*", this should restore meta data on everything. // restore with filter "*", this should restore meta data on everything.
cmdRestore(t, global, filepath.Join(env.base, "restore1"), snapshotID, "*") cmdRestoreExcludes(t, global, filepath.Join(env.base, "restore1"), snapshotID, []string{"*"})
f2 := filepath.Join(env.base, "restore1", "testdata", "subdir1", "subdir2") f2 := filepath.Join(env.base, "restore1", "testdata", "subdir1", "subdir2")
fi, err = os.Stat(f2) fi, err = os.Stat(f2)

View File

@ -18,8 +18,8 @@ type Restorer struct {
repo *repository.Repository repo *repository.Repository
sn *Snapshot sn *Snapshot
Error func(dir string, node *Node, err error) error Error func(dir string, node *Node, err error) error
SelectForRestore func(item string, dstpath string, node *Node) bool SelectFilter func(item string, dstpath string, node *Node) bool
} }
var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { return err } var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { return err }
@ -28,7 +28,7 @@ var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { r
func NewRestorer(repo *repository.Repository, id backend.ID) (*Restorer, error) { func NewRestorer(repo *repository.Repository, id backend.ID) (*Restorer, error) {
r := &Restorer{ r := &Restorer{
repo: repo, Error: restorerAbortOnAllErrors, repo: repo, Error: restorerAbortOnAllErrors,
SelectForRestore: func(string, string, *Node) bool { return true }, SelectFilter: func(string, string, *Node) bool { return true },
} }
var err error var err error
@ -48,7 +48,7 @@ func (res *Restorer) restoreTo(dst string, dir string, treeID backend.ID) error
} }
for _, node := range tree.Nodes { for _, node := range tree.Nodes {
selectedForRestore := res.SelectForRestore(filepath.Join(dir, node.Name), selectedForRestore := res.SelectFilter(filepath.Join(dir, node.Name),
filepath.Join(dst, dir, node.Name), node) filepath.Join(dst, dir, node.Name), node)
debug.Log("Restorer.restoreNodeTo", "SelectForRestore returned %v", selectedForRestore) debug.Log("Restorer.restoreNodeTo", "SelectForRestore returned %v", selectedForRestore)