2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-13 06:12:22 +00:00
restic/cmd/restic/main.go

219 lines
4.9 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
2014-12-07 15:30:52 +00:00
"errors"
2014-04-27 22:00:15 +00:00
"fmt"
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
2014-11-15 18:04:32 +00:00
"golang.org/x/crypto/ssh/terminal"
2014-04-27 22:00:15 +00:00
"github.com/jessevdk/go-flags"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic/backend"
2015-03-28 10:50:23 +00:00
"github.com/restic/restic/backend/local"
"github.com/restic/restic/backend/sftp"
2015-01-14 21:08:48 +00:00
"github.com/restic/restic/debug"
"github.com/restic/restic/repository"
2014-04-27 22:00:15 +00:00
)
var version = "compiled manually"
2015-06-21 09:38:07 +00:00
var mainOpts struct {
Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restore from"`
CacheDir string ` long:"cache-dir" description:"Directory to use as a local cache"`
Quiet bool `short:"q" long:"quiet" default:"false" description:"Do not output comprehensive progress report"`
password string
2014-04-27 22:00:15 +00:00
}
2015-06-21 09:38:07 +00:00
var parser = flags.NewParser(&mainOpts, flags.Default)
2014-12-07 15:30:52 +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)
}
func readPassword(prompt string) string {
fmt.Fprint(os.Stderr, prompt)
2014-09-23 20:39:12 +00:00
pw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
errx(2, "unable to read password: %v", err)
}
fmt.Fprintln(os.Stderr)
2014-09-23 20:39:12 +00:00
return string(pw)
}
func disableProgress() bool {
2015-06-21 09:38:07 +00:00
if mainOpts.Quiet {
return true
}
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return true
}
return false
}
func silenceRequested() bool {
2015-06-21 09:38:07 +00:00
if mainOpts.Quiet {
return true
}
return false
}
func verbosePrintf(format string, args ...interface{}) {
if silenceRequested() {
return
}
fmt.Printf(format, args...)
}
2014-12-07 15:30:52 +00:00
type CmdInit struct{}
func (cmd CmdInit) Execute(args []string) error {
2015-06-21 09:38:07 +00:00
if mainOpts.Repo == "" {
2014-12-07 15:30:52 +00:00
return errors.New("Please specify repository location (-r)")
}
2015-06-21 09:38:07 +00:00
if mainOpts.password == "" {
pw := readPassword("enter password for new backend: ")
pw2 := readPassword("enter password again: ")
2014-10-04 17:20:15 +00:00
if pw != pw2 {
errx(1, "passwords do not match")
}
2015-06-21 09:38:07 +00:00
mainOpts.password = pw
2014-10-04 17:20:15 +00:00
}
2015-06-21 09:38:07 +00:00
be, err := create(mainOpts.Repo)
2014-10-04 17:20:15 +00:00
if err != nil {
2015-06-21 09:38:07 +00:00
fmt.Fprintf(os.Stderr, "creating backend at %s failed: %v\n", mainOpts.Repo, err)
2014-10-04 17:20:15 +00:00
os.Exit(1)
}
s := repository.New(be)
2015-06-21 09:38:07 +00:00
err = s.Init(mainOpts.password)
2014-10-04 17:20:15 +00:00
if err != nil {
2015-06-21 09:38:07 +00:00
fmt.Fprintf(os.Stderr, "creating key in backend at %s failed: %v\n", mainOpts.Repo, err)
2014-10-04 17:20:15 +00:00
os.Exit(1)
}
2015-06-21 09:38:07 +00:00
verbosePrintf("created restic backend %v at %s\n", s.Config.ID[:10], mainOpts.Repo)
verbosePrintf("\n")
verbosePrintf("Please note that knowledge of your password is required to access\n")
verbosePrintf("the repository. Losing your password means that your data is\n")
verbosePrintf("irrecoverably lost.\n")
2015-04-24 23:08:05 +00:00
2014-10-04 17:20:15 +00:00
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
2014-12-21 16:02:49 +00:00
func open(u string) (backend.Backend, error) {
2014-10-04 17:20:15 +00:00
url, err := url.Parse(u)
if err != nil {
return nil, err
}
if url.Scheme == "" {
2015-03-28 10:50:23 +00:00
return local.Open(url.Path)
2014-10-04 17:20:15 +00:00
}
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")
2015-03-28 10:50:23 +00:00
return sftp.Open(url.Path[1:], "ssh", args...)
2014-10-04 17:20:15 +00:00
}
// Create the backend specified by URI.
2014-12-21 16:02:49 +00:00
func create(u string) (backend.Backend, error) {
2014-10-04 17:20:15 +00:00
url, err := url.Parse(u)
if err != nil {
return nil, err
}
if url.Scheme == "" {
2015-03-28 10:50:23 +00:00
return local.Create(url.Path)
2014-10-04 17:20:15 +00:00
}
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")
2015-03-28 10:50:23 +00:00
return sftp.Create(url.Path[1:], "ssh", args...)
2014-10-04 17:20:15 +00:00
}
2015-05-09 21:59:58 +00:00
func OpenRepo() (*repository.Repository, error) {
2015-06-21 09:38:07 +00:00
if mainOpts.Repo == "" {
2015-04-26 12:46:15 +00:00
return nil, errors.New("Please specify repository location (-r)")
}
2015-06-21 09:38:07 +00:00
be, err := open(mainOpts.Repo)
2014-12-07 15:30:52 +00:00
if err != nil {
2015-04-26 12:46:15 +00:00
return nil, err
2014-12-07 15:30:52 +00:00
}
s := repository.New(be)
2014-12-21 16:02:49 +00:00
2015-06-21 09:38:07 +00:00
if mainOpts.password == "" {
mainOpts.password = readPassword("enter password for repository: ")
}
2015-06-21 09:38:07 +00:00
err = s.SearchKey(mainOpts.password)
2014-12-07 15:30:52 +00:00
if err != nil {
2015-04-26 12:46:15 +00:00
return nil, fmt.Errorf("unable to open repo: %v", err)
2014-12-07 15:30:52 +00:00
}
2014-12-21 17:10:19 +00:00
return s, nil
2014-12-07 15:30:52 +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-12-07 15:30:52 +00:00
_, err := parser.AddCommand("init",
"create repository",
"The init command creates a new repository",
&CmdInit{})
if err != nil {
panic(err)
}
2014-04-27 22:00:15 +00:00
}
func main() {
// defer profile.Start(profile.MemProfileRate(100000), profile.ProfilePath(".")).Stop()
2015-02-21 23:09:57 +00:00
// defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
2015-06-21 09:38:07 +00:00
mainOpts.Repo = os.Getenv("RESTIC_REPOSITORY")
mainOpts.password = os.Getenv("RESTIC_PASSWORD")
2014-04-27 22:00:15 +00:00
2015-01-14 21:08:48 +00:00
debug.Log("restic", "main %#v", os.Args)
2014-12-07 15:30:52 +00:00
_, err := parser.Parse()
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 err != nil {
2014-12-07 15:30:52 +00:00
os.Exit(1)
2014-04-27 22:00:15 +00:00
}
}