2015-06-21 13:02:56 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-04 11:16:55 +02:00
|
|
|
"context"
|
2017-07-23 14:21:03 +02:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2016-09-17 12:36:05 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2015-06-21 13:02:56 +02:00
|
|
|
)
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
var cmdInit = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "initialize a new repository",
|
|
|
|
Long: `
|
|
|
|
The "init" command initializes a new repository.
|
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runInit(globalOptions, args)
|
|
|
|
},
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdInit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInit(gopts GlobalOptions, args []string) error {
|
|
|
|
if gopts.Repo == "" {
|
2016-09-01 22:17:37 +02:00
|
|
|
return errors.Fatal("Please specify repository location (-r)")
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 15:33:52 +01:00
|
|
|
be, err := create(gopts.Repo, gopts.extended)
|
2015-06-26 13:58:43 -04:00
|
|
|
if err != nil {
|
2016-09-17 12:36:05 +02:00
|
|
|
return errors.Fatalf("create backend at %s failed: %v\n", gopts.Repo, err)
|
2015-06-26 13:58:43 -04:00
|
|
|
}
|
|
|
|
|
2017-07-24 23:15:31 +02:00
|
|
|
gopts.password, err = ReadPasswordTwice(gopts,
|
|
|
|
"enter password for new backend: ",
|
|
|
|
"enter password again: ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:14:06 +01:00
|
|
|
s := repository.New(be)
|
2016-03-06 12:34:23 +01:00
|
|
|
|
2017-06-04 11:16:55 +02:00
|
|
|
err = s.Init(context.TODO(), gopts.password)
|
2015-06-21 13:02:56 +02:00
|
|
|
if err != nil {
|
2016-09-17 12:36:05 +02:00
|
|
|
return errors.Fatalf("create key in backend at %s failed: %v\n", gopts.Repo, err)
|
2015-06-21 13:02:56 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
Verbosef("created restic backend %v at %s\n", s.Config().ID[:10], gopts.Repo)
|
|
|
|
Verbosef("\n")
|
|
|
|
Verbosef("Please note that knowledge of your password is required to access\n")
|
|
|
|
Verbosef("the repository. Losing your password means that your data is\n")
|
|
|
|
Verbosef("irrecoverably lost.\n")
|
2015-06-21 13:02:56 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|