2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2014-07-28 18:20:32 +00:00
|
|
|
"github.com/fd0/khepri"
|
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/restor from"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func errmsg(code int, format string, data ...interface{}) {
|
|
|
|
if len(format) > 0 && format[len(format)-1] != '\n' {
|
|
|
|
format += "\n"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, format, data...)
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2014-07-28 18:20:32 +00:00
|
|
|
type commandFunc func(*khepri.DirRepository, []string) error
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
var commands map[string]commandFunc
|
|
|
|
|
|
|
|
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-04-27 22:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
Opts.Repo = os.Getenv("KHEPRI_REPOSITORY")
|
|
|
|
if Opts.Repo == "" {
|
|
|
|
Opts.Repo = "khepri-backup"
|
|
|
|
}
|
|
|
|
args, err := flags.Parse(&Opts)
|
|
|
|
|
|
|
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
cmds := []string{}
|
|
|
|
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]
|
|
|
|
|
|
|
|
f, ok := commands[cmd]
|
|
|
|
if !ok {
|
|
|
|
errmsg(1, "unknown command: %q\n", cmd)
|
|
|
|
}
|
|
|
|
|
2014-07-28 18:20:32 +00:00
|
|
|
repo, err := khepri.NewDirRepository(Opts.Repo)
|
2014-04-27 22:00:15 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errmsg(1, "unable to create/open repo: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f(repo, args[1:])
|
|
|
|
if err != nil {
|
|
|
|
errmsg(1, "error executing command %q: %v", cmd, err)
|
|
|
|
}
|
|
|
|
}
|