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-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
|
|
|
)
|
|
|
|
|
|
|
|
type HookArray = []string
|
|
|
|
|
|
|
|
type Hooks struct {
|
|
|
|
Before HookArray `mapstructure:"before"`
|
|
|
|
After HookArray `mapstructure:"after"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options map[string]map[string][]string
|
|
|
|
|
|
|
|
type Location struct {
|
2021-04-11 15:02:34 +00:00
|
|
|
Name string `mapstructure:"name"`
|
2021-04-08 23:55:10 +00:00
|
|
|
From string `mapstructure:"from"`
|
|
|
|
To []string `mapstructure:"to"`
|
|
|
|
Hooks Hooks `mapstructure:"hooks"`
|
|
|
|
Cron string `mapstructure:"cron"`
|
|
|
|
Options Options `mapstructure:"options"`
|
|
|
|
}
|
|
|
|
|
2021-04-11 15:02:34 +00:00
|
|
|
func GetLocation(name string) (Location, bool) {
|
|
|
|
c := GetConfig()
|
|
|
|
for _, b := range c.Locations {
|
|
|
|
if b.Name == name {
|
|
|
|
return b, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Location{}, false
|
|
|
|
}
|
|
|
|
|
2021-04-11 22:02:35 +00:00
|
|
|
func (l Location) validate(c *Config) error {
|
|
|
|
if l.Name == "" {
|
|
|
|
return fmt.Errorf(`Location is missing name`)
|
|
|
|
}
|
|
|
|
if l.From == "" {
|
|
|
|
return fmt.Errorf(`Location "%s" is missing "from" key`, l.Name)
|
|
|
|
}
|
|
|
|
if len(l.To) == 0 {
|
|
|
|
return fmt.Errorf(`Location "%s" has no "to" targets`, l.Name)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11 16:17:21 +00:00
|
|
|
func (l Location) Backup() error {
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.PrimaryPrint(" Backing up location \"%s\" ", l.Name)
|
2021-04-12 08:55:57 +00:00
|
|
|
from, err := GetPathRelativeToConfig(l.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "bash",
|
|
|
|
Dir: from,
|
|
|
|
}
|
|
|
|
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, to := range l.To {
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
colors.Secondary.Printf("Backend: %s\n", backend.Name)
|
|
|
|
env, err := backend.getEnv()
|
|
|
|
if err != nil {
|
2021-04-08 23:55:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-12 08:55:57 +00:00
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "restic",
|
|
|
|
Dir: from,
|
|
|
|
Envs: env,
|
|
|
|
}
|
2021-04-11 16:17:21 +00:00
|
|
|
flags := l.getOptions("backup")
|
|
|
|
cmd := []string{"backup"}
|
|
|
|
cmd = append(cmd, flags...)
|
|
|
|
cmd = append(cmd, ".")
|
|
|
|
out, err := ExecuteResticCommand(options, cmd...)
|
2021-04-12 14:15:17 +00:00
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
2021-04-11 16:17:21 +00:00
|
|
|
return nil
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Success.Println("Done")
|
2021-04-12 08:55:57 +00:00
|
|
|
return err
|
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-12 14:15:17 +00:00
|
|
|
colors.PrimaryPrint("Forgetting for location \"%s\"", l.Name)
|
|
|
|
|
|
|
|
from, err := GetPathRelativeToConfig(l.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, to := range l.To {
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
colors.Secondary.Printf("For backend \"%s\"\n", backend.Name)
|
|
|
|
env, err := backend.getEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "bash",
|
|
|
|
Envs: env,
|
|
|
|
Dir: from,
|
|
|
|
}
|
2021-04-11 11:04:11 +00:00
|
|
|
flags := l.getOptions("forget")
|
2021-04-11 16:17:21 +00:00
|
|
|
cmd := []string{"forget", "--path", options.Dir}
|
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-12 14:15:17 +00:00
|
|
|
colors.PrimaryPrint("Restoring location \"%s\"", l.Name)
|
|
|
|
colors.Secondary.Println("Restoring lastest snapshot")
|
|
|
|
colors.Body.Printf("%s → %s.\n", from, to)
|
2021-04-11 12:03:38 +00:00
|
|
|
|
|
|
|
// 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-04-11 15:02:34 +00:00
|
|
|
backend, _ := GetBackend(from)
|
2021-04-11 16:17:21 +00:00
|
|
|
resolved, err := GetPathRelativeToConfig(l.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = backend.Exec([]string{"restore", "--target", to, "--path", resolved, "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-11 22:17:29 +00:00
|
|
|
last := time.Unix(lock.GetCron(l.Name), 0)
|
|
|
|
next := schedule.Next(last)
|
2021-04-11 13:02:27 +00:00
|
|
|
now := time.Now()
|
|
|
|
if now.After(next) {
|
2021-04-11 22:17:29 +00:00
|
|
|
lock.SetCron(l.Name, now.Unix())
|
|
|
|
l.Backup()
|
2021-04-11 13:02:27 +00:00
|
|
|
} else {
|
2021-04-12 14:15:17 +00:00
|
|
|
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.Name)
|
2021-04-11 13:02:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|