restic/cmd/khepri/main.go

78 lines
1.5 KiB
Go
Raw Normal View History

2014-04-28 00:00:15 +02:00
package main
import (
"fmt"
2014-08-05 23:13:07 +02:00
"log"
2014-04-28 00:00:15 +02:00
"os"
2014-07-28 20:20:32 +02:00
"github.com/fd0/khepri"
2014-04-28 00:00:15 +02: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-28 00:00:15 +02:00
}
2014-08-03 15:16:56 +02:00
func errx(code int, format string, data ...interface{}) {
2014-04-28 00:00:15 +02:00
if len(format) > 0 && format[len(format)-1] != '\n' {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, data...)
os.Exit(code)
}
2014-08-04 20:47:04 +02:00
type commandFunc func(*khepri.Repository, []string) error
2014-04-28 00:00:15 +02:00
var commands map[string]commandFunc
func init() {
commands = make(map[string]commandFunc)
commands["backup"] = commandBackup
commands["restore"] = commandRestore
2014-08-01 22:20:28 +02:00
commands["list"] = commandList
2014-08-04 22:55:54 +02:00
commands["snapshots"] = commandSnapshots
2014-08-04 23:25:58 +02:00
commands["fsck"] = commandFsck
2014-08-11 22:47:24 +02:00
commands["dump"] = commandDump
2014-04-28 00:00:15 +02:00
}
func main() {
2014-08-05 23:13:07 +02:00
log.SetOutput(os.Stdout)
2014-04-28 00:00:15 +02:00
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)
}
cmd := args[0]
if cmd == "init" {
err = commandInit(Opts.Repo)
if err != nil {
errx(1, "error executing command %q: %v", cmd, err)
2014-04-28 00:00:15 +02:00
}
return
}
2014-04-28 00:00:15 +02:00
f, ok := commands[cmd]
if !ok {
2014-08-03 15:16:56 +02:00
errx(1, "unknown command: %q\n", cmd)
2014-04-28 00:00:15 +02:00
}
2014-08-04 20:47:04 +02:00
repo, err := khepri.NewRepository(Opts.Repo)
2014-04-28 00:00:15 +02:00
if err != nil {
errx(1, "unable to open repo: %v", err)
2014-04-28 00:00:15 +02:00
}
err = f(repo, args[1:])
if err != nil {
2014-08-03 15:16:56 +02:00
errx(1, "error executing command %q: %v", cmd, err)
2014-04-28 00:00:15 +02:00
}
}