This commit is contained in:
cupcakearmy 2021-04-11 18:17:21 +02:00
parent 6e25b90915
commit 19e75c1dad
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
5 changed files with 84 additions and 74 deletions

View File

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

View File

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

View File

@ -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

View File

@ -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

View File

@ -70,20 +70,35 @@ 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 := 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 {
return nil
}
{
flags := l.getOptions("backup")
cmd := []string{"backup"}
cmd = append(cmd, flags...)
@ -93,24 +108,18 @@ func (l Location) Backup() error {
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 {
from := GetPathRelativeToConfig(l.From)
for _, to := range l.To {
backend, _ := GetBackend(to)
options := ExecuteOptions{
Envs: backend.getEnv(),
Dir: from,
}
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
})
}
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
}