2021-04-08 23:55:10 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-11 12:03:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-04-15 19:55:35 +00:00
|
|
|
"strings"
|
2021-04-11 13:02:27 +00:00
|
|
|
"time"
|
|
|
|
|
2021-04-12 08:55:57 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2021-04-11 13:02:27 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/lock"
|
2021-10-26 13:57:40 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/metadata"
|
2021-04-11 13:02:27 +00:00
|
|
|
"github.com/robfig/cron"
|
2021-04-08 23:55:10 +00:00
|
|
|
)
|
|
|
|
|
2021-04-15 19:55:35 +00:00
|
|
|
type LocationType string
|
|
|
|
|
|
|
|
const (
|
2021-10-31 21:33:02 +00:00
|
|
|
TypeLocal LocationType = "local"
|
|
|
|
TypeVolume LocationType = "volume"
|
2021-04-15 19:55:35 +00:00
|
|
|
)
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
type HookArray = []string
|
|
|
|
|
|
|
|
type Hooks struct {
|
2021-10-31 21:33:02 +00:00
|
|
|
Dir string `yaml:"dir"`
|
2021-05-06 13:55:32 +00:00
|
|
|
Before HookArray `yaml:"before,omitempty"`
|
|
|
|
After HookArray `yaml:"after,omitempty"`
|
|
|
|
Success HookArray `yaml:"success,omitempty"`
|
|
|
|
Failure HookArray `yaml:"failure,omitempty"`
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Location struct {
|
2021-04-23 11:11:15 +00:00
|
|
|
name string `yaml:",omitempty"`
|
2021-10-30 11:01:31 +00:00
|
|
|
From []string `yaml:"from,omitempty"`
|
2021-10-31 21:33:02 +00:00
|
|
|
Type string `yaml:"type,omitempty"`
|
2021-04-23 11:11:15 +00:00
|
|
|
To []string `yaml:"to,omitempty"`
|
|
|
|
Hooks Hooks `yaml:"hooks,omitempty"`
|
|
|
|
Cron string `yaml:"cron,omitempty"`
|
|
|
|
Options Options `yaml:"options,omitempty"`
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 15:02:34 +00:00
|
|
|
func GetLocation(name string) (Location, bool) {
|
2021-04-16 20:02:25 +00:00
|
|
|
l, ok := GetConfig().Locations[name]
|
|
|
|
l.name = name
|
|
|
|
return l, ok
|
2021-04-11 15:02:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 11:51:04 +00:00
|
|
|
func (l Location) validate() error {
|
2021-10-31 21:33:02 +00:00
|
|
|
if len(l.From) == 0 {
|
2021-04-16 20:02:25 +00:00
|
|
|
return fmt.Errorf(`Location "%s" is missing "from" key`, l.name)
|
2021-04-11 22:02:35 +00:00
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
t, err := l.getType()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch t {
|
|
|
|
case TypeLocal:
|
|
|
|
for _, path := range l.From {
|
|
|
|
if from, err := GetPathRelativeToConfig(path); err != nil {
|
2021-05-01 20:54:52 +00:00
|
|
|
return err
|
|
|
|
} else {
|
2021-10-31 21:33:02 +00:00
|
|
|
if stat, err := os.Stat(from); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
if !stat.IsDir() {
|
|
|
|
return fmt.Errorf("\"%s\" is not valid directory for location \"%s\"", from, l.name)
|
|
|
|
}
|
2021-05-01 20:54:52 +00:00
|
|
|
}
|
2021-04-24 10:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
case TypeVolume:
|
|
|
|
if len(l.From) > 1 {
|
|
|
|
return fmt.Errorf(`location "%s" has more than one docker volume`, l.name)
|
|
|
|
}
|
2021-04-24 10:40:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:02:35 +00:00
|
|
|
if len(l.To) == 0 {
|
2021-04-16 20:02:25 +00:00
|
|
|
return fmt.Errorf(`Location "%s" has no "to" targets`, l.name)
|
2021-04-11 22:02:35 +00:00
|
|
|
}
|
2021-04-08 23:55:10 +00:00
|
|
|
// Check if backends are all valid
|
|
|
|
for _, to := range l.To {
|
2021-04-11 15:02:34 +00:00
|
|
|
_, ok := GetBackend(to)
|
2021-04-08 23:55:10 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid backend `%s`", to)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:33:02 +00:00
|
|
|
func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error {
|
2021-04-12 08:55:57 +00:00
|
|
|
if len(commands) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
if l.Hooks.Dir != "" {
|
|
|
|
if dir, err := GetPathRelativeToConfig(l.Hooks.Dir); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
options.Dir = dir
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Secondary.Println("\nRunning hooks")
|
2021-04-08 23:55:10 +00:00
|
|
|
for _, command := range commands {
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Body.Println("> " + command)
|
2021-04-08 23:55:10 +00:00
|
|
|
out, err := ExecuteCommand(options, "-c", command)
|
2021-04-12 14:15:17 +00:00
|
|
|
if err != nil {
|
2021-04-24 10:40:33 +00:00
|
|
|
colors.Error.Println(out)
|
2021-04-11 16:17:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-24 10:40:33 +00:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
2021-04-11 16:17:21 +00:00
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Body.Println("")
|
2021-04-11 16:17:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-08 23:55:10 +00:00
|
|
|
|
2021-10-31 21:33:02 +00:00
|
|
|
func (l Location) getType() (LocationType, error) {
|
|
|
|
t := strings.ToLower(l.Type)
|
|
|
|
if t == "" || t == "local" {
|
|
|
|
return TypeLocal, nil
|
|
|
|
} else if t == "volume" {
|
|
|
|
return TypeVolume, nil
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
return "", fmt.Errorf("invalid location type \"%s\"", l.Type)
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:48:03 +00:00
|
|
|
func buildTag(parts ...string) string {
|
2021-10-31 21:33:02 +00:00
|
|
|
parts = append([]string{"ar"}, parts...)
|
|
|
|
return strings.Join(parts, ":")
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:48:03 +00:00
|
|
|
func (l Location) getLocationTags() string {
|
|
|
|
return buildTag("location", l.name)
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:29:32 +00:00
|
|
|
func (l Location) Backup(cron bool, specificBackend string) []error {
|
2021-05-06 13:55:32 +00:00
|
|
|
var errors []error
|
2021-10-28 15:29:32 +00:00
|
|
|
var backends []string
|
2021-04-16 20:02:25 +00:00
|
|
|
colors.PrimaryPrint(" Backing up location \"%s\" ", l.name)
|
2021-10-31 21:33:02 +00:00
|
|
|
t, err := l.getType()
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
cwd, _ := GetPathRelativeToConfig(".")
|
2021-04-12 08:55:57 +00:00
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "bash",
|
2021-10-31 21:33:02 +00:00
|
|
|
Dir: cwd,
|
2021-10-26 13:57:40 +00:00
|
|
|
Envs: map[string]string{
|
|
|
|
"AUTORESTIC_LOCATION": l.name,
|
|
|
|
},
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
|
2021-07-12 17:10:36 +00:00
|
|
|
if err := l.validate(); err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
goto after
|
|
|
|
}
|
|
|
|
|
2021-04-15 19:55:35 +00:00
|
|
|
// Hooks
|
2021-10-31 21:33:02 +00:00
|
|
|
if err := l.ExecuteHooks(l.Hooks.Before, options); err != nil {
|
2021-05-06 13:55:32 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
goto after
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
|
|
|
|
// Backup
|
2021-10-28 15:29:32 +00:00
|
|
|
if specificBackend == "" {
|
|
|
|
backends = l.To
|
|
|
|
} else {
|
2021-10-29 16:35:21 +00:00
|
|
|
if l.hasBackend(specificBackend) {
|
|
|
|
backends = []string{specificBackend}
|
|
|
|
} else {
|
|
|
|
errors = append(errors, fmt.Errorf("backup location \"%s\" has no backend \"%s\"", l.name, specificBackend))
|
|
|
|
return errors
|
2021-10-28 15:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, to := range backends {
|
2021-04-12 08:55:57 +00:00
|
|
|
backend, _ := GetBackend(to)
|
2021-04-16 20:02:25 +00:00
|
|
|
colors.Secondary.Printf("Backend: %s\n", backend.name)
|
2021-04-12 08:55:57 +00:00
|
|
|
env, err := backend.getEnv()
|
|
|
|
if err != nil {
|
2021-05-06 13:55:32 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
|
2021-04-11 16:17:21 +00:00
|
|
|
cmd := []string{"backup"}
|
2021-10-31 22:07:12 +00:00
|
|
|
cmd = append(cmd, combineOptions("backup", l, backend)...)
|
2021-04-20 18:49:09 +00:00
|
|
|
if cron {
|
2021-11-07 10:48:03 +00:00
|
|
|
cmd = append(cmd, "--tag", buildTag("cron"))
|
2021-04-20 18:49:09 +00:00
|
|
|
}
|
2021-11-07 10:48:03 +00:00
|
|
|
cmd = append(cmd, "--tag", l.getLocationTags())
|
2021-04-15 19:55:35 +00:00
|
|
|
backupOptions := ExecuteOptions{
|
|
|
|
Envs: env,
|
|
|
|
}
|
|
|
|
|
|
|
|
var out string
|
|
|
|
switch t {
|
|
|
|
case TypeLocal:
|
2021-10-31 21:33:02 +00:00
|
|
|
for _, from := range l.From {
|
|
|
|
path, err := GetPathRelativeToConfig(from)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
goto after
|
|
|
|
}
|
|
|
|
cmd = append(cmd, path)
|
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
out, err = ExecuteResticCommand(backupOptions, cmd...)
|
|
|
|
case TypeVolume:
|
2021-10-31 21:33:02 +00:00
|
|
|
ok := CheckIfVolumeExists(l.From[0])
|
|
|
|
if !ok {
|
|
|
|
errors = append(errors, fmt.Errorf("volume \"%s\" does not exist", l.From[0]))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "/data")
|
2021-04-23 21:53:57 +00:00
|
|
|
out, err = backend.ExecDocker(l, cmd)
|
2021-04-12 14:15:17 +00:00
|
|
|
}
|
2021-04-11 16:17:21 +00:00
|
|
|
if err != nil {
|
2021-12-06 11:13:18 +00:00
|
|
|
colors.Error.Println(out)
|
|
|
|
errors = append(errors, fmt.Errorf("%s@%s:\n%s%s", l.name, backend.name, out, err))
|
2021-05-06 13:55:32 +00:00
|
|
|
continue
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2021-10-26 13:57:40 +00:00
|
|
|
|
|
|
|
md := metadata.ExtractMetadataFromBackupLog(out)
|
|
|
|
mdEnv := metadata.MakeEnvFromMetadata(&md)
|
|
|
|
for k, v := range mdEnv {
|
|
|
|
options.Envs[k+"_"+fmt.Sprint(i)] = v
|
|
|
|
options.Envs[k+"_"+strings.ToUpper(backend.name)] = v
|
|
|
|
}
|
2021-04-23 21:53:57 +00:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-04-15 19:55:35 +00:00
|
|
|
|
|
|
|
// After hooks
|
2021-10-31 21:33:02 +00:00
|
|
|
if err := l.ExecuteHooks(l.Hooks.After, options); err != nil {
|
2021-05-06 13:55:32 +00:00
|
|
|
errors = append(errors, err)
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-05-06 13:55:32 +00:00
|
|
|
|
|
|
|
after:
|
|
|
|
var commands []string
|
|
|
|
if len(errors) > 0 {
|
|
|
|
commands = l.Hooks.Failure
|
|
|
|
} else {
|
|
|
|
commands = l.Hooks.Success
|
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
if err := l.ExecuteHooks(commands, options); err != nil {
|
2021-05-06 13:55:32 +00:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:33:02 +00:00
|
|
|
if len(errors) == 0 {
|
|
|
|
colors.Success.Println("Done")
|
|
|
|
}
|
2021-05-06 13:55:32 +00:00
|
|
|
return errors
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2021-04-11 11:04:11 +00:00
|
|
|
|
2021-04-11 12:22:46 +00:00
|
|
|
func (l Location) Forget(prune bool, dry bool) error {
|
2021-04-16 20:02:25 +00:00
|
|
|
colors.PrimaryPrint("Forgetting for location \"%s\"", l.name)
|
2021-04-12 14:15:17 +00:00
|
|
|
|
|
|
|
for _, to := range l.To {
|
|
|
|
backend, _ := GetBackend(to)
|
2021-04-16 20:02:25 +00:00
|
|
|
colors.Secondary.Printf("For backend \"%s\"\n", backend.name)
|
2021-04-12 14:15:17 +00:00
|
|
|
env, err := backend.getEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{
|
2021-04-15 19:55:35 +00:00
|
|
|
Envs: env,
|
2021-04-12 14:15:17 +00:00
|
|
|
}
|
2021-11-07 10:48:03 +00:00
|
|
|
cmd := []string{"forget", "--tag", l.getLocationTags()}
|
2021-04-11 11:04:11 +00:00
|
|
|
if prune {
|
|
|
|
cmd = append(cmd, "--prune")
|
|
|
|
}
|
2021-04-11 12:22:46 +00:00
|
|
|
if dry {
|
|
|
|
cmd = append(cmd, "--dry-run")
|
|
|
|
}
|
2021-10-31 22:07:12 +00:00
|
|
|
cmd = append(cmd, combineOptions("forget", l, backend)...)
|
2021-04-11 11:04:11 +00:00
|
|
|
out, err := ExecuteResticCommand(options, cmd...)
|
2021-04-12 14:15:17 +00:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
2021-04-11 11:04:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
}
|
|
|
|
colors.Success.Println("Done")
|
|
|
|
return nil
|
2021-04-11 11:04:11 +00:00
|
|
|
}
|
2021-04-11 12:03:38 +00:00
|
|
|
|
|
|
|
func (l Location) hasBackend(backend string) bool {
|
|
|
|
for _, b := range l.To {
|
|
|
|
if b == backend {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:33:02 +00:00
|
|
|
func (l Location) Restore(to, from string, force bool, snapshot string) error {
|
2021-04-11 12:03:38 +00:00
|
|
|
if from == "" {
|
|
|
|
from = l.To[0]
|
|
|
|
} else if !l.hasBackend(from) {
|
|
|
|
return fmt.Errorf("invalid backend: \"%s\"", from)
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err := filepath.Abs(to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:33:02 +00:00
|
|
|
if snapshot == "" {
|
|
|
|
snapshot = "latest"
|
|
|
|
}
|
|
|
|
|
|
|
|
colors.PrimaryPrint("Restoring location \"%s\"", l.name)
|
2021-04-11 15:02:34 +00:00
|
|
|
backend, _ := GetBackend(from)
|
2021-10-31 21:33:02 +00:00
|
|
|
colors.Secondary.Printf("Restoring %s@%s → %s\n", snapshot, backend.name, to)
|
|
|
|
|
|
|
|
t, err := l.getType()
|
2021-04-11 16:17:21 +00:00
|
|
|
if err != nil {
|
2021-10-31 21:33:02 +00:00
|
|
|
return err
|
2021-04-11 16:17:21 +00:00
|
|
|
}
|
2021-10-31 21:33:02 +00:00
|
|
|
switch t {
|
2021-04-15 19:55:35 +00:00
|
|
|
case TypeLocal:
|
|
|
|
// Check if target is empty
|
|
|
|
if !force {
|
|
|
|
notEmptyError := fmt.Errorf("target %s is not empty", to)
|
|
|
|
_, err = os.Stat(to)
|
|
|
|
if err == nil {
|
|
|
|
files, err := ioutil.ReadDir(to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(files) > 0 {
|
|
|
|
return notEmptyError
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 10:48:03 +00:00
|
|
|
err = backend.Exec([]string{"restore", "--target", to, "--tag", l.getLocationTags(), snapshot})
|
2021-04-15 19:55:35 +00:00
|
|
|
case TypeVolume:
|
2021-11-07 10:48:03 +00:00
|
|
|
_, err = backend.ExecDocker(l, []string{"restore", "--target", "/", "--tag", l.getLocationTags(), snapshot})
|
2021-04-15 19:55:35 +00:00
|
|
|
}
|
2021-04-11 12:03:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Success.Println("Done")
|
2021-04-11 12:03:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-11 13:02:27 +00:00
|
|
|
|
|
|
|
func (l Location) RunCron() error {
|
|
|
|
if l.Cron == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule, err := cron.ParseStandard(l.Cron)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-16 20:02:25 +00:00
|
|
|
last := time.Unix(lock.GetCron(l.name), 0)
|
2021-04-11 22:17:29 +00:00
|
|
|
next := schedule.Next(last)
|
2021-04-11 13:02:27 +00:00
|
|
|
now := time.Now()
|
|
|
|
if now.After(next) {
|
2021-04-16 20:02:25 +00:00
|
|
|
lock.SetCron(l.name, now.Unix())
|
2021-10-28 15:29:32 +00:00
|
|
|
l.Backup(true, "")
|
2021-04-11 13:02:27 +00:00
|
|
|
} else {
|
2021-04-28 08:54:07 +00:00
|
|
|
if !CRON_LEAN {
|
|
|
|
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name)
|
|
|
|
}
|
2021-04-11 13:02:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|