2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-09-23 20:39:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-04-27 22:00:15 +00:00
|
|
|
|
2014-07-28 18:20:32 +00:00
|
|
|
"github.com/fd0/khepri"
|
2014-09-23 20:39:12 +00:00
|
|
|
"github.com/fd0/khepri/backend"
|
2014-04-27 22:00:15 +00:00
|
|
|
)
|
|
|
|
|
2014-11-30 21:39:58 +00:00
|
|
|
func init() {
|
|
|
|
commands["restore"] = commandRestore
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func commandRestore(be backend.Server, key *khepri.Key, args []string) error {
|
2014-04-27 22:00:15 +00:00
|
|
|
if len(args) != 2 {
|
|
|
|
return errors.New("usage: restore ID dir")
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
id, err := backend.FindSnapshot(be, args[0])
|
2014-04-27 22:00:15 +00:00
|
|
|
if err != nil {
|
2014-08-03 13:16:56 +00:00
|
|
|
errx(1, "invalid id %q: %v", args[0], err)
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target := args[1]
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
// create restorer
|
|
|
|
res, err := khepri.NewRestorer(be, key, id)
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "creating restorer failed: %v\n", err)
|
|
|
|
os.Exit(2)
|
2014-08-04 20:46:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
res.Error = func(dir string, node *khepri.Node, err error) error {
|
|
|
|
fmt.Fprintf(os.Stderr, "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
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
fmt.Printf("restoring %s to %s\n", res.Snapshot(), target)
|
|
|
|
|
|
|
|
err = res.RestoreTo(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|