2021-04-09 01:55:10 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2021-04-12 00:02:35 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2021-04-09 01:55:10 +02:00
|
|
|
"fmt"
|
2021-04-23 13:11:15 +02:00
|
|
|
"net/url"
|
2021-04-15 21:55:35 +02:00
|
|
|
"os"
|
2021-04-12 00:02:35 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-04-12 16:15:17 +02:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2021-04-09 01:55:10 +02:00
|
|
|
)
|
|
|
|
|
2021-04-23 13:11:15 +02:00
|
|
|
type BackendRest struct {
|
|
|
|
User string `yaml:"user,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:55:10 +02:00
|
|
|
type Backend struct {
|
2021-05-17 22:34:14 +02:00
|
|
|
name string
|
|
|
|
Type string `yaml:"type,omitempty"`
|
|
|
|
Path string `yaml:"path,omitempty"`
|
|
|
|
Key string `yaml:"key,omitempty"`
|
|
|
|
Env map[string]string `yaml:"env,omitempty"`
|
|
|
|
Rest BackendRest `yaml:"rest,omitempty"`
|
|
|
|
Options Options `yaml:"options,omitempty"`
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:02:34 +02:00
|
|
|
func GetBackend(name string) (Backend, bool) {
|
2021-04-16 22:02:25 +02:00
|
|
|
b, ok := GetConfig().Backends[name]
|
|
|
|
b.name = name
|
|
|
|
return b, ok
|
2021-04-11 17:02:34 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 01:55:10 +02:00
|
|
|
func (b Backend) generateRepo() (string, error) {
|
|
|
|
switch b.Type {
|
|
|
|
case "local":
|
2021-04-11 18:17:21 +02:00
|
|
|
return GetPathRelativeToConfig(b.Path)
|
2021-04-23 13:11:15 +02:00
|
|
|
case "rest":
|
|
|
|
parsed, err := url.Parse(b.Path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if b.Rest.User != "" {
|
|
|
|
if b.Rest.Password == "" {
|
|
|
|
parsed.User = url.User(b.Rest.User)
|
|
|
|
} else {
|
|
|
|
parsed.User = url.UserPassword(b.Rest.User, b.Rest.Password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s", b.Type, parsed.String()), nil
|
2021-04-24 16:40:53 +02:00
|
|
|
case "b2", "azure", "gs", "s3", "sftp", "rclone":
|
2021-04-09 01:55:10 +02:00
|
|
|
return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("backend type \"%s\" is invalid", b.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 18:17:21 +02:00
|
|
|
func (b Backend) getEnv() (map[string]string, error) {
|
2021-04-09 01:55:10 +02:00
|
|
|
env := make(map[string]string)
|
|
|
|
env["RESTIC_PASSWORD"] = b.Key
|
|
|
|
repo, err := b.generateRepo()
|
|
|
|
env["RESTIC_REPOSITORY"] = repo
|
2021-04-17 13:04:40 +02:00
|
|
|
for key, value := range b.Env {
|
|
|
|
env[strings.ToUpper(key)] = value
|
|
|
|
}
|
2021-04-11 18:17:21 +02:00
|
|
|
return env, err
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 00:02:35 +02: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-09 01:55:10 +02:00
|
|
|
func (b Backend) validate() error {
|
2021-04-12 00:02:35 +02:00
|
|
|
if b.Type == "" {
|
2021-04-16 22:02:25 +02:00
|
|
|
return fmt.Errorf(`Backend "%s" has no "type"`, b.name)
|
2021-04-12 00:02:35 +02:00
|
|
|
}
|
|
|
|
if b.Path == "" {
|
2021-04-16 22:02:25 +02:00
|
|
|
return fmt.Errorf(`Backend "%s" has no "path"`, b.name)
|
2021-04-12 00:02:35 +02:00
|
|
|
}
|
|
|
|
if b.Key == "" {
|
|
|
|
key := generateRandomKey()
|
|
|
|
b.Key = key
|
|
|
|
c := GetConfig()
|
2021-04-16 22:02:25 +02:00
|
|
|
tmp := c.Backends[b.name]
|
|
|
|
tmp.Key = key
|
|
|
|
c.Backends[b.name] = tmp
|
2021-04-23 13:11:15 +02:00
|
|
|
if err := c.SaveConfig(); err != nil {
|
2021-04-12 00:02:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-04-11 18:17:21 +02:00
|
|
|
env, err := b.getEnv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{Envs: env}
|
2021-04-09 01:55:10 +02:00
|
|
|
// Check if already initialized
|
2021-04-11 18:17:21 +02:00
|
|
|
_, err = ExecuteResticCommand(options, "snapshots")
|
2021-04-09 01:55:10 +02:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// If not initialize
|
2021-04-17 13:04:40 +02:00
|
|
|
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
2021-04-09 01:55:10 +02:00
|
|
|
out, err := ExecuteResticCommand(options, "init")
|
2021-04-17 13:04:40 +02:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
2021-04-09 01:55:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Backend) Exec(args []string) error {
|
2021-04-11 18:17:21 +02:00
|
|
|
env, err := b.getEnv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{Envs: env}
|
2021-04-09 01:55:10 +02:00
|
|
|
out, err := ExecuteResticCommand(options, args...)
|
2021-04-23 23:53:57 +02:00
|
|
|
if err != nil {
|
|
|
|
colors.Error.Println(out)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-12 16:15:17 +02:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
2021-04-23 23:53:57 +02:00
|
|
|
return nil
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
2021-04-15 21:55:35 +02:00
|
|
|
|
2021-04-23 23:53:57 +02:00
|
|
|
func (b Backend) ExecDocker(l Location, args []string) (string, error) {
|
2021-04-15 21:55:35 +02:00
|
|
|
env, err := b.getEnv()
|
|
|
|
if err != nil {
|
2021-04-23 23:53:57 +02:00
|
|
|
return "", err
|
2021-04-15 21:55:35 +02:00
|
|
|
}
|
|
|
|
volume := l.getVolumeName()
|
|
|
|
path, _ := l.getPath()
|
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "docker",
|
|
|
|
Envs: env,
|
|
|
|
}
|
|
|
|
docker := []string{
|
|
|
|
"run", "--rm",
|
|
|
|
"--entrypoint", "ash",
|
|
|
|
"--workdir", path,
|
|
|
|
"--volume", volume + ":" + path,
|
|
|
|
}
|
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
|
|
|
docker = append(docker, "--hostname", hostname)
|
|
|
|
}
|
|
|
|
if b.Type == "local" {
|
|
|
|
actual := env["RESTIC_REPOSITORY"]
|
|
|
|
docker = append(docker, "--volume", actual+":"+"/repo")
|
|
|
|
env["RESTIC_REPOSITORY"] = "/repo"
|
|
|
|
}
|
|
|
|
for key, value := range env {
|
|
|
|
docker = append(docker, "--env", key+"="+value)
|
|
|
|
}
|
|
|
|
docker = append(docker, "restic/restic", "-c", "restic "+strings.Join(args, " "))
|
|
|
|
out, err := ExecuteCommand(options, docker...)
|
2021-04-23 23:53:57 +02:00
|
|
|
return out, err
|
2021-04-15 21:55:35 +02:00
|
|
|
}
|