2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-05 04:47:51 +00:00
restic/cmd/khepri/cmd_restore.go

138 lines
2.3 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
"errors"
"io"
2014-08-04 20:46:14 +00:00
"log"
2014-04-27 22:00:15 +00:00
"os"
"path"
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-27 22:00:15 +00:00
)
2014-08-04 18:47:04 +00:00
func restore_file(repo *khepri.Repository, node khepri.Node, target string) error {
2014-08-05 21:13:07 +00:00
log.Printf(" restore file %q\n", target)
2014-04-27 22:00:15 +00:00
2014-08-03 13:16:56 +00:00
rd, err := repo.Get(khepri.TYPE_BLOB, node.Content)
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if err != nil {
return err
}
_, err = io.Copy(f, rd)
if err != nil {
return err
}
err = f.Chmod(node.Mode)
if err != nil {
return err
}
err = f.Chown(int(node.User), int(node.Group))
if err != nil {
return err
}
err = os.Chtimes(target, node.AccessTime, node.ModTime)
if err != nil {
return err
}
return nil
}
2014-08-04 18:47:04 +00:00
func restore_dir(repo *khepri.Repository, id khepri.ID, target string) error {
2014-08-05 21:13:07 +00:00
log.Printf(" restore dir %q\n", target)
2014-08-04 20:46:14 +00:00
rd, err := repo.Get(khepri.TYPE_BLOB, id)
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
2014-07-28 18:20:32 +00:00
t := khepri.NewTree()
2014-04-27 22:00:15 +00:00
err = t.Restore(rd)
if err != nil {
return err
}
for _, node := range t.Nodes {
name := path.Base(node.Name)
if name == "." || name == ".." {
return errors.New("invalid path")
}
nodepath := path.Join(target, name)
if node.Mode.IsDir() {
err = os.MkdirAll(nodepath, 0700)
if err != nil {
return err
}
err = os.Chmod(nodepath, node.Mode)
if err != nil {
return err
}
err = os.Chown(nodepath, int(node.User), int(node.Group))
if err != nil {
return err
}
err = os.Chtimes(nodepath, node.AccessTime, node.ModTime)
if err != nil {
return err
}
err = restore_dir(repo, node.Content, nodepath)
if err != nil {
return err
}
} else {
err = restore_file(repo, node, nodepath)
if err != nil {
return err
}
}
}
return nil
}
2014-08-04 18:47:04 +00:00
func commandRestore(repo *khepri.Repository, args []string) error {
2014-04-27 22:00:15 +00:00
if len(args) != 2 {
return errors.New("usage: restore ID dir")
}
2014-07-28 18:20:32 +00:00
id, err := khepri.ParseID(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]
err = os.MkdirAll(target, 0700)
if err != nil {
return err
}
2014-08-04 20:46:14 +00:00
sn, err := khepri.LoadSnapshot(repo, id)
if err != nil {
log.Fatalf("error loading snapshot %s", id)
}
err = restore_dir(repo, sn.TreeID, target)
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
2014-08-05 21:13:07 +00:00
log.Printf("%q restored to %q\n", id, target)
2014-04-27 22:00:15 +00:00
return nil
}