restic/cmd/khepri/cmd_restore.go

178 lines
3.4 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
"errors"
2014-08-11 20:47:24 +00:00
"fmt"
2014-04-27 22:00:15 +00:00
"io"
2014-08-04 20:46:14 +00:00
"log"
2014-04-27 22:00:15 +00:00
"os"
2014-08-11 20:47:24 +00:00
"path/filepath"
"syscall"
2014-04-27 22:00:15 +00:00
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-27 22:00:15 +00:00
)
2014-08-11 20:47:24 +00:00
func restore_file(repo *khepri.Repository, node *khepri.Node, path string) (err error) {
switch node.Type {
case "file":
// TODO: handle hard links
rd, err := repo.Get(khepri.TYPE_BLOB, node.Content)
if err != nil {
return err
}
2014-04-27 22:00:15 +00:00
2014-08-11 20:47:24 +00:00
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if err != nil {
return err
}
2014-04-27 22:00:15 +00:00
2014-08-11 20:47:24 +00:00
_, err = io.Copy(f, rd)
if err != nil {
return err
}
2014-04-27 22:00:15 +00:00
2014-08-11 20:47:24 +00:00
case "symlink":
err = os.Symlink(node.LinkTarget, path)
if err != nil {
return err
}
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return err
}
f, err := os.OpenFile(path, khepri.O_PATH|syscall.O_NOFOLLOW, 0600)
defer f.Close()
if err != nil {
return err
}
var utimes = []syscall.Timeval{
syscall.NsecToTimeval(node.AccessTime.UnixNano()),
syscall.NsecToTimeval(node.ModTime.UnixNano()),
}
err = syscall.Futimes(int(f.Fd()), utimes)
if err != nil {
return err
}
return nil
case "dev":
err = syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
if err != nil {
return err
}
case "chardev":
err = syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device))
if err != nil {
return err
}
case "fifo":
err = syscall.Mkfifo(path, 0600)
if err != nil {
return err
}
case "socket":
// nothing to do, we do not restore sockets
default:
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
2014-04-27 22:00:15 +00:00
}
2014-08-11 20:47:24 +00:00
err = os.Chmod(path, node.Mode)
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
2014-08-11 20:47:24 +00:00
err = os.Chown(path, int(node.UID), int(node.GID))
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
2014-08-11 20:47:24 +00:00
err = os.Chtimes(path, node.AccessTime, node.ModTime)
2014-04-27 22:00:15 +00:00
if err != nil {
return err
}
return nil
}
2014-08-11 20:47:24 +00:00
func restore_subtree(repo *khepri.Repository, tree *khepri.Tree, path string) {
fmt.Printf("restore_subtree(%s)\n", path)
2014-04-27 22:00:15 +00:00
2014-08-11 20:47:24 +00:00
for _, node := range tree.Nodes {
nodepath := filepath.Join(path, node.Name)
// fmt.Printf("%s:%s\n", node.Type, nodepath)
2014-04-27 22:00:15 +00:00
2014-08-11 20:47:24 +00:00
if node.Type == "dir" {
err := os.Mkdir(nodepath, 0700)
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-11 20:47:24 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
2014-04-27 22:00:15 +00:00
}
err = os.Chmod(nodepath, node.Mode)
if err != nil {
2014-08-11 20:47:24 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
2014-04-27 22:00:15 +00:00
}
2014-08-11 20:47:24 +00:00
err = os.Chown(nodepath, int(node.UID), int(node.GID))
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-11 20:47:24 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
2014-04-27 22:00:15 +00:00
}
2014-08-11 20:47:24 +00:00
restore_subtree(repo, node.Tree, filepath.Join(path, node.Name))
2014-04-27 22:00:15 +00:00
2014-08-06 18:09:51 +00:00
err = os.Chtimes(nodepath, node.AccessTime, node.ModTime)
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-11 20:47:24 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
2014-04-27 22:00:15 +00:00
}
2014-08-06 18:09:51 +00:00
2014-04-27 22:00:15 +00:00
} else {
2014-08-11 20:47:24 +00:00
err := restore_file(repo, node, nodepath)
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-11 20:47:24 +00:00
fmt.Fprintf(os.Stderr, "%s\n", err)
continue
2014-04-27 22:00:15 +00:00
}
}
}
}
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)
}
2014-08-11 20:47:24 +00:00
tree, err := khepri.NewTreeFromRepo(repo, sn.Content)
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-11 20:47:24 +00:00
log.Fatalf("error loading tree %s", sn.Content)
2014-04-27 22:00:15 +00:00
}
2014-08-11 20:47:24 +00:00
restore_subtree(repo, tree, target)
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
}