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-23 11:11:15 +00:00
|
|
|
"net/url"
|
2021-04-15 19:55:35 +00:00
|
|
|
"os"
|
2021-04-11 22:02:35 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-04-12 14:15:17 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2022-09-13 13:04:12 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
2021-04-08 23:55:10 +00:00
|
|
|
)
|
|
|
|
|
2021-04-23 11:11:15 +00:00
|
|
|
type BackendRest struct {
|
2022-04-14 09:51:32 +00:00
|
|
|
User string `mapstructure:"user,omitempty"`
|
|
|
|
Password string `mapstructure:"password,omitempty"`
|
2021-04-23 11:11:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
type Backend struct {
|
2021-05-17 20:34:14 +00:00
|
|
|
name string
|
2022-04-14 09:51:32 +00:00
|
|
|
Type string `mapstructure:"type,omitempty"`
|
|
|
|
Path string `mapstructure:"path,omitempty"`
|
|
|
|
Key string `mapstructure:"key,omitempty"`
|
|
|
|
Env map[string]string `mapstructure:"env,omitempty"`
|
|
|
|
Rest BackendRest `mapstructure:"rest,omitempty"`
|
|
|
|
Options Options `mapstructure:"options,omitempty"`
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 15:02:34 +00:00
|
|
|
func GetBackend(name string) (Backend, bool) {
|
2021-04-16 20:02:25 +00:00
|
|
|
b, ok := GetConfig().Backends[name]
|
|
|
|
b.name = name
|
|
|
|
return b, ok
|
2021-04-11 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
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-23 11:11:15 +00:00
|
|
|
case "rest":
|
2023-03-02 14:00:39 +00:00
|
|
|
parsed, err := url.Parse(os.ExpandEnv(b.Path))
|
2021-04-23 11:11:15 +00:00
|
|
|
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 14:40:53 +00:00
|
|
|
case "b2", "azure", "gs", "s3", "sftp", "rclone":
|
2021-04-08 23:55:10 +00: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 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)
|
2021-10-30 11:48:44 +00:00
|
|
|
// Key
|
2021-10-25 15:36:06 +00:00
|
|
|
if b.Key != "" {
|
|
|
|
env["RESTIC_PASSWORD"] = b.Key
|
|
|
|
}
|
2021-10-30 11:48:44 +00:00
|
|
|
|
|
|
|
// From config file
|
2021-04-08 23:55:10 +00:00
|
|
|
repo, err := b.generateRepo()
|
|
|
|
env["RESTIC_REPOSITORY"] = repo
|
2021-04-17 11:04:40 +00:00
|
|
|
for key, value := range b.Env {
|
|
|
|
env[strings.ToUpper(key)] = value
|
|
|
|
}
|
2021-10-30 11:48:44 +00:00
|
|
|
|
|
|
|
// From Envfile and passed as env
|
|
|
|
var prefix = "AUTORESTIC_" + strings.ToUpper(b.name) + "_"
|
|
|
|
for _, variable := range os.Environ() {
|
|
|
|
var splitted = strings.SplitN(variable, "=", 2)
|
|
|
|
if strings.HasPrefix(splitted[0], prefix) {
|
|
|
|
env[strings.TrimPrefix(splitted[0], prefix)] = splitted[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.Type == "" {
|
2021-04-16 20:02:25 +00:00
|
|
|
return fmt.Errorf(`Backend "%s" has no "type"`, b.name)
|
2021-04-11 22:02:35 +00:00
|
|
|
}
|
|
|
|
if b.Path == "" {
|
2021-04-16 20:02:25 +00:00
|
|
|
return fmt.Errorf(`Backend "%s" has no "path"`, b.name)
|
2021-04-11 22:02:35 +00:00
|
|
|
}
|
|
|
|
if b.Key == "" {
|
2021-10-25 15:36:06 +00:00
|
|
|
// Check if key is set in environment
|
2021-10-30 11:48:44 +00:00
|
|
|
env, _ := b.getEnv()
|
|
|
|
if _, found := env["RESTIC_PASSWORD"]; !found {
|
|
|
|
// No key set in config file or env => generate random key and save file
|
2021-10-25 15:36:06 +00:00
|
|
|
key := generateRandomKey()
|
|
|
|
b.Key = key
|
|
|
|
c := GetConfig()
|
|
|
|
tmp := c.Backends[b.name]
|
|
|
|
tmp.Key = key
|
|
|
|
c.Backends[b.name] = tmp
|
|
|
|
if err := c.SaveConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-11 22:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 16:17:21 +00:00
|
|
|
env, err := b.getEnv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-26 22:48:52 +00:00
|
|
|
options := ExecuteOptions{Envs: env, Silent: true}
|
2021-04-08 23:55:10 +00:00
|
|
|
// Check if already initialized
|
2022-09-13 13:15:02 +00:00
|
|
|
cmd := []string{"check"}
|
|
|
|
cmd = append(cmd, combineBackendOptions("check", b)...)
|
|
|
|
_, _, err = ExecuteResticCommand(options, cmd...)
|
2021-04-08 23:55:10 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// If not initialize
|
2021-04-17 11:04:40 +00:00
|
|
|
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
2022-09-13 13:15:02 +00:00
|
|
|
cmd := []string{"init"}
|
|
|
|
cmd = append(cmd, combineBackendOptions("init", b)...)
|
|
|
|
_, _, err := ExecuteResticCommand(options, cmd...)
|
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}
|
2022-11-07 07:09:45 +00:00
|
|
|
args = append(args, combineBackendOptions("exec", b)...)
|
2022-04-14 09:51:32 +00:00
|
|
|
_, out, err := ExecuteResticCommand(options, args...)
|
2021-04-23 21:53:57 +00:00
|
|
|
if err != nil {
|
|
|
|
colors.Error.Println(out)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
|
2022-04-14 09:51:32 +00:00
|
|
|
func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
|
2021-04-15 19:55:35 +00:00
|
|
|
env, err := b.getEnv()
|
|
|
|
if err != nil {
|
2022-04-14 09:51:32 +00:00
|
|
|
return -1, "", err
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
2021-10-31 21:32:55 +00:00
|
|
|
volume := l.From[0]
|
2021-04-15 19:55:35 +00:00
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "docker",
|
|
|
|
Envs: env,
|
|
|
|
}
|
2021-10-31 21:32:55 +00:00
|
|
|
dir := "/data"
|
2021-10-31 23:16:54 +00:00
|
|
|
args = append([]string{"restic"}, args...)
|
2021-04-15 19:55:35 +00:00
|
|
|
docker := []string{
|
|
|
|
"run", "--rm",
|
|
|
|
"--entrypoint", "ash",
|
2021-10-31 21:32:55 +00:00
|
|
|
"--workdir", dir,
|
|
|
|
"--volume", volume + ":" + dir,
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
2021-10-31 21:32:55 +00:00
|
|
|
// Use of docker host, not the container host
|
2021-04-15 19:55:35 +00:00
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
|
|
|
docker = append(docker, "--hostname", hostname)
|
|
|
|
}
|
2021-10-31 21:32:55 +00:00
|
|
|
switch b.Type {
|
|
|
|
case "local":
|
2021-04-15 19:55:35 +00:00
|
|
|
actual := env["RESTIC_REPOSITORY"]
|
|
|
|
docker = append(docker, "--volume", actual+":"+"/repo")
|
|
|
|
env["RESTIC_REPOSITORY"] = "/repo"
|
2021-10-31 21:32:55 +00:00
|
|
|
case "b2":
|
|
|
|
case "s3":
|
2021-10-31 23:19:32 +00:00
|
|
|
case "azure":
|
|
|
|
case "gs":
|
2021-10-31 21:32:55 +00:00
|
|
|
// No additional setup needed
|
2021-10-31 23:16:54 +00:00
|
|
|
case "rclone":
|
|
|
|
// Read host rclone config and mount it into the container
|
2022-04-14 09:51:32 +00:00
|
|
|
code, configFile, err := ExecuteCommand(ExecuteOptions{Command: "rclone"}, "config", "file")
|
2021-10-31 23:16:54 +00:00
|
|
|
if err != nil {
|
2022-04-14 09:51:32 +00:00
|
|
|
return code, "", err
|
2021-10-31 23:16:54 +00:00
|
|
|
}
|
|
|
|
splitted := strings.Split(strings.TrimSpace(configFile), "\n")
|
|
|
|
configFilePath := splitted[len(splitted)-1]
|
|
|
|
docker = append(docker, "--volume", configFilePath+":"+"/root/.config/rclone/rclone.conf:ro")
|
2021-10-31 21:32:55 +00:00
|
|
|
default:
|
2022-04-14 09:51:32 +00:00
|
|
|
return -1, "", fmt.Errorf("Backend type \"%s\" is not supported as volume endpoint", b.Type)
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
|
|
|
for key, value := range env {
|
|
|
|
docker = append(docker, "--env", key+"="+value)
|
|
|
|
}
|
2022-09-13 13:04:12 +00:00
|
|
|
|
|
|
|
docker = append(docker, flags.DOCKER_IMAGE, "-c", strings.Join(args, " "))
|
2022-04-14 09:51:32 +00:00
|
|
|
return ExecuteCommand(options, docker...)
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|