autorestic/internal/location.go

303 lines
6.2 KiB
Go
Raw Normal View History

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"
"github.com/robfig/cron"
2021-04-08 23:55:10 +00:00
)
2021-04-15 19:55:35 +00:00
type LocationType string
const (
TypeLocal LocationType = "local"
TypeVolume LocationType = "volume"
VolumePrefix string = "volume:"
)
2021-04-08 23:55:10 +00:00
type HookArray = []string
type Hooks struct {
Before HookArray `yaml:"before"`
After HookArray `yaml:"after"`
2021-04-08 23:55:10 +00:00
}
type Options map[string]map[string][]string
type Location struct {
name string `yaml:",omitempty"`
From string `yaml:"from,omitempty"`
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
}
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 22:02:35 +00:00
func (l Location) validate(c *Config) error {
if l.From == "" {
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
}
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 {
_, ok := GetBackend(to)
2021-04-08 23:55:10 +00:00
if !ok {
return fmt.Errorf("invalid backend `%s`", to)
}
}
return nil
}
func (l Location) getOptions(key string) []string {
var options []string
saved := l.Options[key]
for k, values := range saved {
for _, value := range values {
options = append(options, fmt.Sprintf("--%s", k), value)
}
}
return options
}
func ExecuteHooks(commands []string, options ExecuteOptions) error {
2021-04-12 08:55:57 +00:00
if len(commands) == 0 {
return nil
}
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 VERBOSE {
colors.Faint.Println(out)
2021-04-08 23:55:10 +00:00
}
2021-04-12 14:15:17 +00:00
if err != nil {
2021-04-11 16:17:21 +00:00
return err
}
}
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-04-15 19:55:35 +00:00
func (l Location) getType() LocationType {
if strings.HasPrefix(l.From, VolumePrefix) {
return TypeVolume
}
return TypeLocal
}
func (l Location) getVolumeName() string {
return strings.TrimPrefix(l.From, VolumePrefix)
}
func (l Location) getPath() (string, error) {
t := l.getType()
switch t {
case TypeLocal:
if path, err := GetPathRelativeToConfig(l.From); err != nil {
return "", err
} else {
return path, nil
}
case TypeVolume:
2021-04-16 20:02:25 +00:00
return "/volume/" + l.name + "/" + l.getVolumeName(), nil
2021-04-15 19:55:35 +00:00
}
2021-04-16 20:02:25 +00:00
return "", fmt.Errorf("could not get path for location \"%s\"", l.name)
2021-04-15 19:55:35 +00:00
}
2021-04-20 18:49:09 +00:00
func (l Location) Backup(cron bool) error {
2021-04-16 20:02:25 +00:00
colors.PrimaryPrint(" Backing up location \"%s\" ", l.name)
2021-04-15 19:55:35 +00:00
t := l.getType()
2021-04-12 08:55:57 +00:00
options := ExecuteOptions{
Command: "bash",
}
2021-04-15 19:55:35 +00:00
if t == TypeLocal {
dir, _ := GetPathRelativeToConfig(l.From)
options.Dir = dir
}
// Hooks
2021-04-12 08:55:57 +00:00
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
2021-04-15 19:55:35 +00:00
return err
2021-04-12 08:55:57 +00:00
}
2021-04-15 19:55:35 +00:00
// Backup
2021-04-12 08:55:57 +00:00
for _, to := range l.To {
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-04-08 23:55:10 +00:00
return nil
}
2021-04-15 19:55:35 +00:00
2021-04-11 16:17:21 +00:00
flags := l.getOptions("backup")
cmd := []string{"backup"}
cmd = append(cmd, flags...)
2021-04-20 18:49:09 +00:00
if cron {
cmd = append(cmd, "--tag", "cron")
}
2021-04-11 16:17:21 +00:00
cmd = append(cmd, ".")
2021-04-15 19:55:35 +00:00
backupOptions := ExecuteOptions{
Dir: options.Dir,
Envs: env,
}
var out string
switch t {
case TypeLocal:
out, err = ExecuteResticCommand(backupOptions, cmd...)
if VERBOSE {
colors.Faint.Println(out)
}
case TypeVolume:
err = backend.ExecDocker(l, cmd)
2021-04-12 14:15:17 +00:00
}
2021-04-15 19:55:35 +00:00
2021-04-11 16:17:21 +00:00
if err != nil {
return err
2021-04-08 23:55:10 +00:00
}
2021-04-12 08:55:57 +00:00
}
2021-04-15 19:55:35 +00:00
// After hooks
2021-04-12 08:55:57 +00:00
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
2021-04-15 19:55:35 +00:00
return err
2021-04-12 08:55:57 +00:00
}
2021-04-12 14:15:17 +00:00
colors.Success.Println("Done")
2021-04-15 19:55:35 +00:00
return nil
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
2021-04-15 19:55:35 +00:00
path, err := l.getPath()
2021-04-12 14:15:17 +00:00
if err != nil {
return err
}
2021-04-15 19:55:35 +00:00
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-04-11 11:04:11 +00:00
flags := l.getOptions("forget")
2021-04-15 19:55:35 +00:00
cmd := []string{"forget", "--path", path}
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-04-11 11:04:11 +00:00
cmd = append(cmd, flags...)
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
}
func (l Location) Restore(to, from string, force bool) error {
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-04-16 20:02:25 +00:00
colors.PrimaryPrint("Restoring location \"%s\"", l.name)
2021-04-11 12:03:38 +00:00
backend, _ := GetBackend(from)
2021-04-15 19:55:35 +00:00
path, err := l.getPath()
2021-04-11 16:17:21 +00:00
if err != nil {
return nil
}
2021-04-15 19:55:35 +00:00
colors.Secondary.Println("Restoring lastest snapshot")
colors.Body.Printf("%s → %s.\n", from, path)
switch l.getType() {
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
}
}
}
err = backend.Exec([]string{"restore", "--target", to, "--path", path, "latest"})
case TypeVolume:
err = backend.ExecDocker(l, []string{"restore", "--target", ".", "--path", path, "latest"})
}
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-04-20 18:49:09 +00:00
l.Backup(true)
2021-04-11 13:02:27 +00:00
} else {
2021-04-16 20:02:25 +00:00
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name)
2021-04-11 13:02:27 +00:00
}
return nil
}