2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-28 21:02:47 +00:00
restic/cmd/khepri/main.go

198 lines
4.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-10-04 17:20:15 +00:00
"net/url"
2014-04-27 22:00:15 +00:00
"os"
2014-11-16 21:50:20 +00:00
"runtime"
2014-09-23 20:39:12 +00:00
"sort"
"strings"
2014-11-15 18:04:32 +00:00
"golang.org/x/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 version = "compiled manually"
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
2014-11-30 21:34:21 +00:00
var commands = make(map[string]commandFunc)
2014-04-27 22:00:15 +00:00
2014-11-25 22:07:00 +00:00
func readPassword(env string, prompt string) string {
if env != "" {
p := os.Getenv(env)
if p != "" {
return p
}
2014-09-23 20:39:12 +00:00
}
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-10-04 17:20:15 +00:00
func commandInit(repo string) error {
2014-11-25 22:07:00 +00:00
pw := readPassword("KHEPRI_PASSWORD", "enter password for new backend: ")
pw2 := readPassword("KHEPRI_PASSWORD", "enter password again: ")
2014-10-04 17:20:15 +00:00
if pw != pw2 {
errx(1, "passwords do not match")
}
be, err := create(repo)
if err != nil {
fmt.Fprintf(os.Stderr, "creating backend at %s failed: %v\n", repo, err)
os.Exit(1)
}
_, err = khepri.CreateKey(be, pw)
if err != nil {
fmt.Fprintf(os.Stderr, "creating key in backend at %s failed: %v\n", repo, err)
os.Exit(1)
}
fmt.Printf("created khepri backend at %s\n", be.Location())
return nil
}
// Open the backend specified by URI.
// Valid formats are:
// * /foo/bar -> local repository at /foo/bar
// * sftp://user@host/foo/bar -> remote sftp repository on host for user at path foo/bar
// * sftp://host//tmp/backup -> remote sftp repository on host at path /tmp/backup
func open(u string) (backend.Server, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
}
if url.Scheme == "" {
return backend.OpenLocal(url.Path)
}
args := []string{url.Host}
if url.User != nil && url.User.Username() != "" {
args = append(args, "-l")
args = append(args, url.User.Username())
}
args = append(args, "-s")
args = append(args, "sftp")
return backend.OpenSFTP(url.Path[1:], "ssh", args...)
2014-10-04 17:20:15 +00:00
}
// Create the backend specified by URI.
func create(u string) (backend.Server, error) {
url, err := url.Parse(u)
if err != nil {
return nil, err
}
if url.Scheme == "" {
return backend.CreateLocal(url.Path)
}
args := []string{url.Host}
if url.User != nil && url.User.Username() != "" {
args = append(args, "-l")
args = append(args, url.User.Username())
}
args = append(args, "-s")
args = append(args, "sftp")
return backend.CreateSFTP(url.Path[1:], "ssh", args...)
2014-10-04 17:20:15 +00:00
}
2014-04-27 22:00:15 +00:00
func init() {
2014-11-16 21:50:20 +00:00
// set GOMAXPROCS to number of CPUs
runtime.GOMAXPROCS(runtime.NumCPU())
2014-04-27 22:00:15 +00:00
}
func main() {
// defer profile.Start(profile.MemProfileRate(100000), profile.ProfilePath(".")).Stop()
2014-08-05 21:13:07 +00:00
log.SetOutput(os.Stdout)
opts.Repo = os.Getenv("KHEPRI_REPOSITORY")
2014-04-27 22:00:15 +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-11-23 22:19:47 +00:00
if opts.Repo == "" {
fmt.Fprintf(os.Stderr, "no repository specified, use -r or KHEPRI_REPOSITORY variable\n")
os.Exit(1)
}
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]
switch cmd {
case "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
case "version":
fmt.Printf("%v\n", version)
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 := open(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-11-25 22:07:00 +00:00
key, err := khepri.SearchKey(repo, readPassword("KHEPRI_PASSWORD", "Enter Password for Repository: "))
2014-09-23 20:39:12 +00:00
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
}
khepri.PoolAlloc()
2014-04-27 22:00:15 +00:00
}