mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
fixes
This commit is contained in:
parent
6e25b90915
commit
19e75c1dad
20
cmd/exec.go
20
cmd/exec.go
@ -33,17 +33,15 @@ var execCmd = &cobra.Command{
|
||||
defer lock.Unlock()
|
||||
|
||||
config := internal.GetConfig()
|
||||
if err := config.CheckConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{
|
||||
selected, err := internal.GetAllOrSelected(cmd, true)
|
||||
CheckErr(err)
|
||||
for _, name := range selected {
|
||||
fmt.Println(name)
|
||||
backend, _ := internal.GetBackend(name)
|
||||
backend.Exec(args)
|
||||
}
|
||||
err = config.CheckConfig()
|
||||
CheckErr(err)
|
||||
|
||||
selected, err := internal.GetAllOrSelected(cmd, true)
|
||||
CheckErr(err)
|
||||
for _, name := range selected {
|
||||
fmt.Println(name)
|
||||
backend, _ := internal.GetBackend(name)
|
||||
backend.Exec(args)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -31,19 +31,16 @@ var forgetCmd = &cobra.Command{
|
||||
defer lock.Unlock()
|
||||
|
||||
config := internal.GetConfig()
|
||||
if err := config.CheckConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{
|
||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||
CheckErr(config.CheckConfig())
|
||||
|
||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||
CheckErr(err)
|
||||
prune, _ := cmd.Flags().GetBool("prune")
|
||||
dry, _ := cmd.Flags().GetBool("dry-run")
|
||||
for _, name := range selected {
|
||||
location, _ := internal.GetLocation(name)
|
||||
err := location.Forget(prune, dry)
|
||||
CheckErr(err)
|
||||
prune, _ := cmd.Flags().GetBool("prune")
|
||||
dry, _ := cmd.Flags().GetBool("dry-run")
|
||||
for _, name := range selected {
|
||||
location, _ := internal.GetLocation(name)
|
||||
err := location.Forget(prune, dry)
|
||||
CheckErr(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ func GetBackend(name string) (Backend, bool) {
|
||||
func (b Backend) generateRepo() (string, error) {
|
||||
switch b.Type {
|
||||
case "local":
|
||||
return GetPathRelativeToConfig(b.Path), nil
|
||||
return GetPathRelativeToConfig(b.Path)
|
||||
case "b2", "azure", "gs", "s3", "sftp", "rest":
|
||||
return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
|
||||
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["RESTIC_PASSWORD"] = b.Key
|
||||
repo, err := b.generateRepo()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
env["RESTIC_REPOSITORY"] = repo
|
||||
return env
|
||||
return env, err
|
||||
}
|
||||
|
||||
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
|
||||
_, err := ExecuteResticCommand(options, "snapshots")
|
||||
_, err = ExecuteResticCommand(options, "snapshots")
|
||||
if err == nil {
|
||||
return nil
|
||||
} else {
|
||||
@ -59,7 +60,11 @@ func (b Backend) validate() 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...)
|
||||
fmt.Println(out)
|
||||
return err
|
||||
|
@ -34,29 +34,26 @@ func GetConfig() *Config {
|
||||
return config
|
||||
}
|
||||
|
||||
func GetPathRelativeToConfig(p string) string {
|
||||
func GetPathRelativeToConfig(p string) (string, error) {
|
||||
if path.IsAbs(p) {
|
||||
return p
|
||||
return p, nil
|
||||
} else if strings.HasPrefix(p, "~") {
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return path.Join(home, strings.TrimPrefix(p, "~"))
|
||||
return path.Join(home, strings.TrimPrefix(p, "~")), err
|
||||
} else {
|
||||
return path.Join(path.Dir(viper.ConfigFileUsed()), p)
|
||||
return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) CheckConfig() error {
|
||||
for name, backend := range c.Backends {
|
||||
for _, backend := range c.Backends {
|
||||
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 {
|
||||
return fmt.Errorf("location \"%s\": %s", name, err)
|
||||
return fmt.Errorf("location \"%s\": %s", location.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -70,47 +70,56 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Location) Backup() error {
|
||||
from := GetPathRelativeToConfig(l.From)
|
||||
func (l Location) forEachBackend(fn func(ExecuteOptions) error) error {
|
||||
from, err := GetPathRelativeToConfig(l.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, to := range l.To {
|
||||
backend, _ := GetBackend(to)
|
||||
env, err := backend.getEnv()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
options := ExecuteOptions{
|
||||
Command: "bash",
|
||||
Envs: backend.getEnv(),
|
||||
Envs: env,
|
||||
Dir: from,
|
||||
}
|
||||
|
||||
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
||||
return nil
|
||||
}
|
||||
{
|
||||
flags := l.getOptions("backup")
|
||||
cmd := []string{"backup"}
|
||||
cmd = append(cmd, flags...)
|
||||
cmd = append(cmd, ".")
|
||||
out, err := ExecuteResticCommand(options, cmd...)
|
||||
fmt.Println(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
||||
return nil
|
||||
if err := fn(options); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Location) Forget(prune bool, dry bool) error {
|
||||
from := GetPathRelativeToConfig(l.From)
|
||||
for _, to := range l.To {
|
||||
backend, _ := GetBackend(to)
|
||||
options := ExecuteOptions{
|
||||
Envs: backend.getEnv(),
|
||||
Dir: from,
|
||||
func (l Location) Backup() error {
|
||||
return l.forEachBackend(func(options ExecuteOptions) error {
|
||||
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
flags := l.getOptions("backup")
|
||||
cmd := []string{"backup"}
|
||||
cmd = append(cmd, flags...)
|
||||
cmd = append(cmd, ".")
|
||||
out, err := ExecuteResticCommand(options, cmd...)
|
||||
fmt.Println(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (l Location) Forget(prune bool, dry bool) error {
|
||||
return l.forEachBackend(func(options ExecuteOptions) error {
|
||||
flags := l.getOptions("forget")
|
||||
cmd := []string{"forget", "--path", from}
|
||||
cmd := []string{"forget", "--path", options.Dir}
|
||||
if prune {
|
||||
cmd = append(cmd, "--prune")
|
||||
}
|
||||
@ -123,8 +132,8 @@ func (l Location) Forget(prune bool, dry bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (l Location) hasBackend(backend string) bool {
|
||||
@ -169,7 +178,11 @@ func (l Location) Restore(to, from string, force bool) error {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user