autorestic/internal/backend.go

117 lines
2.6 KiB
Go
Raw Normal View History

2021-04-08 23:55:10 +00:00
package internal
import (
2021-04-11 22:02:35 +00:00
"crypto/rand"
"encoding/base64"
2021-04-08 23:55:10 +00:00
"fmt"
2021-04-11 22:02:35 +00:00
"strings"
2021-04-12 14:15:17 +00:00
"github.com/cupcakearmy/autorestic/internal/colors"
2021-04-11 22:02:35 +00:00
"github.com/spf13/viper"
2021-04-08 23:55:10 +00:00
)
type Backend struct {
Name string `mapstructure:"name"`
2021-04-08 23:55:10 +00:00
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
}
2021-04-08 23:55:10 +00:00
func (b Backend) generateRepo() (string, error) {
switch b.Type {
case "local":
2021-04-11 16:17:21 +00:00
return GetPathRelativeToConfig(b.Path)
2021-04-08 23:55:10 +00:00
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)
}
}
2021-04-11 16:17:21 +00:00
func (b Backend) getEnv() (map[string]string, error) {
2021-04-08 23:55:10 +00:00
env := make(map[string]string)
env["RESTIC_PASSWORD"] = b.Key
repo, err := b.generateRepo()
env["RESTIC_REPOSITORY"] = repo
2021-04-11 16:17:21 +00:00
return env, err
2021-04-08 23:55:10 +00:00
}
2021-04-11 22:02:35 +00:00
func generateRandomKey() string {
b := make([]byte, 64)
rand.Read(b)
key := base64.StdEncoding.EncodeToString(b)
key = strings.ReplaceAll(key, "=", "")
key = strings.ReplaceAll(key, "+", "")
key = strings.ReplaceAll(key, "/", "")
return key
}
2021-04-08 23:55:10 +00:00
func (b Backend) validate() error {
2021-04-11 22:02:35 +00:00
if b.Name == "" {
return fmt.Errorf(`Backend has no "name"`)
}
if b.Type == "" {
return fmt.Errorf(`Backend "%s" has no "type"`, b.Name)
}
if b.Path == "" {
return fmt.Errorf(`Backend "%s" has no "path"`, b.Name)
}
if b.Key == "" {
key := generateRandomKey()
b.Key = key
c := GetConfig()
for i, backend := range c.Backends {
if backend.Name == b.Name {
c.Backends[i].Key = key
break
}
}
file := viper.ConfigFileUsed()
if err := CopyFile(file, file+".old"); err != nil {
return err
}
2021-04-12 14:15:17 +00:00
colors.Secondary.Println("Saved a backup copy of your file next the the original.")
2021-04-11 22:02:35 +00:00
viper.Set("backends", c.Backends)
viper.WriteConfig()
}
2021-04-11 16:17:21 +00:00
env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
2021-04-08 23:55:10 +00:00
// Check if already initialized
2021-04-11 16:17:21 +00:00
_, err = ExecuteResticCommand(options, "snapshots")
2021-04-08 23:55:10 +00:00
if err == nil {
return nil
} else {
// If not initialize
out, err := ExecuteResticCommand(options, "init")
2021-04-12 14:15:17 +00:00
colors.Faint.Println(out)
2021-04-08 23:55:10 +00:00
return err
}
}
func (b Backend) Exec(args []string) error {
2021-04-11 16:17:21 +00:00
env, err := b.getEnv()
if err != nil {
return err
}
options := ExecuteOptions{Envs: env}
2021-04-08 23:55:10 +00:00
out, err := ExecuteResticCommand(options, args...)
2021-04-12 14:15:17 +00:00
if VERBOSE {
colors.Faint.Println(out)
}
2021-04-08 23:55:10 +00:00
return err
}