mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
Add exclude filter to restorer and 'restore' command
This commit is contained in:
parent
7fd52f9f57
commit
c0337a2675
@ -1,14 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/restic/restic"
|
||||
"github.com/restic/restic/debug"
|
||||
"github.com/restic/restic/filter"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@ -23,14 +27,22 @@ func init() {
|
||||
}
|
||||
|
||||
func (cmd CmdRestore) Usage() string {
|
||||
return "snapshot-ID TARGETDIR [PATTERN]"
|
||||
return "snapshot-ID"
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
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()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -47,14 +59,11 @@ func (cmd CmdRestore) Execute(args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := restic.FindSnapshot(repo, args[0])
|
||||
id, err := restic.FindSnapshot(repo, snapshotIDString)
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
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 {
|
||||
pattern := args[2]
|
||||
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
|
||||
selectFilter := 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
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/restic/restic/debug"
|
||||
"github.com/restic/restic/filter"
|
||||
. "github.com/restic/restic/test"
|
||||
)
|
||||
|
||||
@ -68,9 +69,13 @@ func cmdList(t testing.TB, global GlobalOptions, tpe string) []backend.ID {
|
||||
return IDs
|
||||
}
|
||||
|
||||
func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID, args ...string) {
|
||||
cmd := &CmdRestore{global: &global}
|
||||
cmd.Execute(append([]string{snapshotID.String(), dir}, args...))
|
||||
func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) {
|
||||
cmdRestoreExcludes(t, global, dir, snapshotID, nil)
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -517,10 +522,10 @@ func TestRestoreFilter(t *testing.T) {
|
||||
|
||||
for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} {
|
||||
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 {
|
||||
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)
|
||||
} else {
|
||||
Assert(t, os.IsNotExist(err),
|
||||
@ -558,7 +563,7 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) {
|
||||
// restore with filter "*.ext", this should restore "file.ext", but
|
||||
// since the directories are ignored and only created because of
|
||||
// "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")
|
||||
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")
|
||||
|
||||
// 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")
|
||||
fi, err = os.Stat(f2)
|
||||
|
@ -18,8 +18,8 @@ type Restorer struct {
|
||||
repo *repository.Repository
|
||||
sn *Snapshot
|
||||
|
||||
Error func(dir string, node *Node, err error) error
|
||||
SelectForRestore func(item string, dstpath string, node *Node) bool
|
||||
Error func(dir string, node *Node, err error) error
|
||||
SelectFilter func(item string, dstpath string, node *Node) bool
|
||||
}
|
||||
|
||||
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) {
|
||||
r := &Restorer{
|
||||
repo: repo, Error: restorerAbortOnAllErrors,
|
||||
SelectForRestore: func(string, string, *Node) bool { return true },
|
||||
SelectFilter: func(string, string, *Node) bool { return true },
|
||||
}
|
||||
|
||||
var err error
|
||||
@ -48,7 +48,7 @@ func (res *Restorer) restoreTo(dst string, dir string, treeID backend.ID) error
|
||||
}
|
||||
|
||||
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)
|
||||
debug.Log("Restorer.restoreNodeTo", "SelectForRestore returned %v", selectedForRestore)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user