You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autorestic/internal/backend.go

72 lines
1.5 KiB

4 years ago
package internal
import (
"fmt"
)
type Backend struct {
Name string `mapstructure:"name"`
4 years ago
Type string `mapstructure:"type"`
Path string `mapstructure:"path"`
Key string `mapstructure:"key"`
Env map[string]string `mapstructure:"env"`
}
func GetBackend(name string) (Backend, bool) {
c := GetConfig()
for _, b := range c.Backends {
if b.Name == name {
return b, true
}
}
return Backend{}, false
}
4 years ago
func (b Backend) generateRepo() (string, error) {
switch b.Type {
case "local":
4 years ago
return GetPathRelativeToConfig(b.Path)
4 years ago
case "b2", "azure", "gs", "s3", "sftp", "rest":
return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
default:
return "", fmt.Errorf("backend type \"%s\" is invalid", b.Type)
}
}
4 years ago
func (b Backend) getEnv() (map[string]string, error) {
4 years ago
env := make(map[string]string)
env["RESTIC_PASSWORD"] = b.Key
repo, err := b.generateRepo()
env["RESTIC_REPOSITORY"] = repo
4 years ago
return env, err
4 years ago
}
func (b Backend) validate() error {
4 years ago
env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
4 years ago
// Check if already initialized
4 years ago
_, err = ExecuteResticCommand(options, "snapshots")
4 years ago
if err == nil {
return nil
} else {
// If not initialize
out, err := ExecuteResticCommand(options, "init")
fmt.Println(out)
return err
}
}
func (b Backend) Exec(args []string) error {
4 years ago
env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
4 years ago
out, err := ExecuteResticCommand(options, args...)
fmt.Println(out)
return err
}