pull/44/head
cupcakearmy 4 years ago
parent 6e25b90915
commit 19e75c1dad
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9

@ -33,10 +33,9 @@ var execCmd = &cobra.Command{
defer lock.Unlock() defer lock.Unlock()
config := internal.GetConfig() config := internal.GetConfig()
if err := config.CheckConfig(); err != nil { err = config.CheckConfig()
panic(err) CheckErr(err)
}
{
selected, err := internal.GetAllOrSelected(cmd, true) selected, err := internal.GetAllOrSelected(cmd, true)
CheckErr(err) CheckErr(err)
for _, name := range selected { for _, name := range selected {
@ -44,7 +43,6 @@ var execCmd = &cobra.Command{
backend, _ := internal.GetBackend(name) backend, _ := internal.GetBackend(name)
backend.Exec(args) backend.Exec(args)
} }
}
}, },
} }

@ -31,10 +31,8 @@ var forgetCmd = &cobra.Command{
defer lock.Unlock() defer lock.Unlock()
config := internal.GetConfig() config := internal.GetConfig()
if err := config.CheckConfig(); err != nil { CheckErr(config.CheckConfig())
panic(err)
}
{
selected, err := internal.GetAllOrSelected(cmd, false) selected, err := internal.GetAllOrSelected(cmd, false)
CheckErr(err) CheckErr(err)
prune, _ := cmd.Flags().GetBool("prune") prune, _ := cmd.Flags().GetBool("prune")
@ -44,7 +42,6 @@ var forgetCmd = &cobra.Command{
err := location.Forget(prune, dry) err := location.Forget(prune, dry)
CheckErr(err) CheckErr(err)
} }
}
}, },
} }

@ -25,7 +25,7 @@ func GetBackend(name string) (Backend, bool) {
func (b Backend) generateRepo() (string, error) { func (b Backend) generateRepo() (string, error) {
switch b.Type { switch b.Type {
case "local": case "local":
return GetPathRelativeToConfig(b.Path), nil return GetPathRelativeToConfig(b.Path)
case "b2", "azure", "gs", "s3", "sftp", "rest": case "b2", "azure", "gs", "s3", "sftp", "rest":
return fmt.Sprintf("%s:%s", b.Type, b.Path), nil return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
default: default:
@ -33,21 +33,22 @@ func (b Backend) generateRepo() (string, error) {
} }
} }
func (b Backend) getEnv() map[string]string { func (b Backend) getEnv() (map[string]string, error) {
env := make(map[string]string) env := make(map[string]string)
env["RESTIC_PASSWORD"] = b.Key env["RESTIC_PASSWORD"] = b.Key
repo, err := b.generateRepo() repo, err := b.generateRepo()
if err != nil {
panic(err)
}
env["RESTIC_REPOSITORY"] = repo env["RESTIC_REPOSITORY"] = repo
return env return env, err
} }
func (b Backend) validate() error { func (b Backend) validate() error {
options := ExecuteOptions{Envs: b.getEnv()} env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
// Check if already initialized // Check if already initialized
_, err := ExecuteResticCommand(options, "snapshots") _, err = ExecuteResticCommand(options, "snapshots")
if err == nil { if err == nil {
return nil return nil
} else { } else {
@ -59,7 +60,11 @@ func (b Backend) validate() error {
} }
func (b Backend) Exec(args []string) error { func (b Backend) Exec(args []string) error {
options := ExecuteOptions{Envs: b.getEnv()} env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
out, err := ExecuteResticCommand(options, args...) out, err := ExecuteResticCommand(options, args...)
fmt.Println(out) fmt.Println(out)
return err return err

@ -34,29 +34,26 @@ func GetConfig() *Config {
return config return config
} }
func GetPathRelativeToConfig(p string) string { func GetPathRelativeToConfig(p string) (string, error) {
if path.IsAbs(p) { if path.IsAbs(p) {
return p return p, nil
} else if strings.HasPrefix(p, "~") { } else if strings.HasPrefix(p, "~") {
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { return path.Join(home, strings.TrimPrefix(p, "~")), err
panic(err)
}
return path.Join(home, strings.TrimPrefix(p, "~"))
} else { } else {
return path.Join(path.Dir(viper.ConfigFileUsed()), p) return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil
} }
} }
func (c Config) CheckConfig() error { func (c Config) CheckConfig() error {
for name, backend := range c.Backends { for _, backend := range c.Backends {
if err := backend.validate(); err != nil { if err := backend.validate(); err != nil {
return fmt.Errorf("backend \"%s\": %s", name, err) return fmt.Errorf("backend \"%s\": %s", backend.Name, err)
} }
} }
for name, location := range c.Locations { for _, location := range c.Locations {
if err := location.validate(c); err != nil { if err := location.validate(c); err != nil {
return fmt.Errorf("location \"%s\": %s", name, err) return fmt.Errorf("location \"%s\": %s", location.Name, err)
} }
} }
return nil return nil

@ -70,20 +70,35 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
return nil return nil
} }
func (l Location) Backup() error { func (l Location) forEachBackend(fn func(ExecuteOptions) error) error {
from := GetPathRelativeToConfig(l.From) from, err := GetPathRelativeToConfig(l.From)
if err != nil {
return err
}
for _, to := range l.To { for _, to := range l.To {
backend, _ := GetBackend(to) backend, _ := GetBackend(to)
env, err := backend.getEnv()
if err != nil {
return nil
}
options := ExecuteOptions{ options := ExecuteOptions{
Command: "bash", Command: "bash",
Envs: backend.getEnv(), Envs: env,
Dir: from, Dir: from,
} }
if err := fn(options); err != nil {
return err
}
}
return nil
}
func (l Location) Backup() error {
return l.forEachBackend(func(options ExecuteOptions) error {
if err := ExecuteHooks(l.Hooks.Before, options); err != nil { if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
return nil return nil
} }
{
flags := l.getOptions("backup") flags := l.getOptions("backup")
cmd := []string{"backup"} cmd := []string{"backup"}
cmd = append(cmd, flags...) cmd = append(cmd, flags...)
@ -93,24 +108,18 @@ func (l Location) Backup() error {
if err != nil { if err != nil {
return err return err
} }
}
if err := ExecuteHooks(l.Hooks.After, options); err != nil { if err := ExecuteHooks(l.Hooks.After, options); err != nil {
return nil return nil
} }
}
return nil return nil
})
} }
func (l Location) Forget(prune bool, dry bool) error { func (l Location) Forget(prune bool, dry bool) error {
from := GetPathRelativeToConfig(l.From) return l.forEachBackend(func(options ExecuteOptions) error {
for _, to := range l.To {
backend, _ := GetBackend(to)
options := ExecuteOptions{
Envs: backend.getEnv(),
Dir: from,
}
flags := l.getOptions("forget") flags := l.getOptions("forget")
cmd := []string{"forget", "--path", from} cmd := []string{"forget", "--path", options.Dir}
if prune { if prune {
cmd = append(cmd, "--prune") cmd = append(cmd, "--prune")
} }
@ -123,8 +132,8 @@ func (l Location) Forget(prune bool, dry bool) error {
if err != nil { if err != nil {
return err return err
} }
}
return nil return nil
})
} }
func (l Location) hasBackend(backend string) bool { func (l Location) hasBackend(backend string) bool {
@ -169,7 +178,11 @@ func (l Location) Restore(to, from string, force bool) error {
} }
backend, _ := GetBackend(from) backend, _ := GetBackend(from)
err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"}) resolved, err := GetPathRelativeToConfig(l.From)
if err != nil {
return nil
}
err = backend.Exec([]string{"restore", "--target", to, "--path", resolved, "latest"})
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save