2
2
mirror of https://github.com/octoleo/restic.git synced 2024-09-24 20:49:01 +00:00
restic/cmd/khepri/main.go

112 lines
2.2 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
"fmt"
2014-08-05 21:13:07 +00:00
"log"
2014-04-27 22:00:15 +00:00
"os"
2014-09-23 20:39:12 +00:00
"sort"
"strings"
"code.google.com/p/go.crypto/ssh/terminal"
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
"github.com/jessevdk/go-flags"
)
var Opts struct {
Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"`
2014-04-27 22:00:15 +00:00
}
2014-08-03 13:16:56 +00:00
func errx(code int, format string, data ...interface{}) {
2014-04-27 22:00:15 +00:00
if len(format) > 0 && format[len(format)-1] != '\n' {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, data...)
os.Exit(code)
}
2014-09-23 20:39:12 +00:00
type commandFunc func(backend.Server, *khepri.Key, []string) error
2014-04-27 22:00:15 +00:00
var commands map[string]commandFunc
2014-09-23 20:39:12 +00:00
func read_password(prompt string) string {
p := os.Getenv("KHEPRI_PASSWORD")
if p != "" {
return p
}
fmt.Print(prompt)
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
errx(2, "unable to read password: %v", err)
}
fmt.Println()
return string(pw)
}
2014-04-27 22:00:15 +00:00
func init() {
commands = make(map[string]commandFunc)
commands["backup"] = commandBackup
commands["restore"] = commandRestore
2014-08-01 20:20:28 +00:00
commands["list"] = commandList
2014-08-04 20:55:54 +00:00
commands["snapshots"] = commandSnapshots
2014-04-27 22:00:15 +00:00
}
func main() {
2014-08-05 21:13:07 +00:00
log.SetOutput(os.Stdout)
2014-04-27 22:00:15 +00:00
Opts.Repo = os.Getenv("KHEPRI_REPOSITORY")
if Opts.Repo == "" {
Opts.Repo = "khepri-backup"
}
2014-09-23 20:39:12 +00:00
args, err := flags.Parse(&Opts)
2014-04-27 22:00:15 +00:00
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
os.Exit(0)
}
2014-09-23 20:39:12 +00:00
if len(args) == 0 {
cmds := []string{"init"}
for k := range commands {
cmds = append(cmds, k)
}
sort.Strings(cmds)
fmt.Printf("nothing to do, available commands: [%v]\n", strings.Join(cmds, "|"))
os.Exit(0)
}
cmd := args[0]
if cmd == "init" {
err = commandInit(Opts.Repo)
if err != nil {
errx(1, "error executing command %q: %v", cmd, err)
2014-04-27 22:00:15 +00:00
}
return
}
2014-04-27 22:00:15 +00:00
f, ok := commands[cmd]
if !ok {
2014-08-03 13:16:56 +00:00
errx(1, "unknown command: %q\n", cmd)
2014-04-27 22:00:15 +00:00
}
2014-09-23 20:39:12 +00:00
// read_password("enter password: ")
repo, err := backend.OpenLocal(Opts.Repo)
2014-04-27 22:00:15 +00:00
if err != nil {
errx(1, "unable to open repo: %v", err)
2014-04-27 22:00:15 +00:00
}
2014-09-23 20:39:12 +00:00
key, err := khepri.SearchKey(repo, read_password("Enter Password for Repository: "))
if err != nil {
errx(2, "unable to open repo: %v", err)
}
err = f(repo, key, args[1:])
2014-04-27 22:00:15 +00:00
if err != nil {
2014-08-03 13:16:56 +00:00
errx(1, "error executing command %q: %v", cmd, err)
2014-04-27 22:00:15 +00:00
}
}