|
|
@ -5,6 +5,7 @@ import (
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
|
|
@ -12,6 +13,14 @@ import (
|
|
|
|
"github.com/robfig/cron"
|
|
|
|
"github.com/robfig/cron"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type LocationType string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
TypeLocal LocationType = "local"
|
|
|
|
|
|
|
|
TypeVolume LocationType = "volume"
|
|
|
|
|
|
|
|
VolumePrefix string = "volume:"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type HookArray = []string
|
|
|
|
type HookArray = []string
|
|
|
|
|
|
|
|
|
|
|
|
type Hooks struct {
|
|
|
|
type Hooks struct {
|
|
|
@ -90,19 +99,50 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
return "/volume/" + l.Name + "/" + l.getVolumeName(), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("Could not get path for location \"%s\"", l.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l Location) Backup() error {
|
|
|
|
func (l Location) Backup() error {
|
|
|
|
colors.PrimaryPrint(" Backing up location \"%s\" ", l.Name)
|
|
|
|
colors.PrimaryPrint(" Backing up location \"%s\" ", l.Name)
|
|
|
|
from, err := GetPathRelativeToConfig(l.From)
|
|
|
|
t := l.getType()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
options := ExecuteOptions{
|
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "bash",
|
|
|
|
Command: "bash",
|
|
|
|
Dir: from,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if t == TypeLocal {
|
|
|
|
|
|
|
|
dir, _ := GetPathRelativeToConfig(l.From)
|
|
|
|
|
|
|
|
options.Dir = dir
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
|
|
|
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
|
|
|
return nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Backup
|
|
|
|
for _, to := range l.To {
|
|
|
|
for _, to := range l.To {
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
colors.Secondary.Printf("Backend: %s\n", backend.Name)
|
|
|
|
colors.Secondary.Printf("Backend: %s\n", backend.Name)
|
|
|
@ -110,37 +150,49 @@ func (l Location) Backup() error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{
|
|
|
|
|
|
|
|
Command: "restic",
|
|
|
|
|
|
|
|
Dir: from,
|
|
|
|
|
|
|
|
Envs: env,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := l.getOptions("backup")
|
|
|
|
flags := l.getOptions("backup")
|
|
|
|
cmd := []string{"backup"}
|
|
|
|
cmd := []string{"backup"}
|
|
|
|
cmd = append(cmd, flags...)
|
|
|
|
cmd = append(cmd, flags...)
|
|
|
|
cmd = append(cmd, ".")
|
|
|
|
cmd = append(cmd, ".")
|
|
|
|
out, err := ExecuteResticCommand(options, cmd...)
|
|
|
|
backupOptions := ExecuteOptions{
|
|
|
|
|
|
|
|
Dir: options.Dir,
|
|
|
|
|
|
|
|
Envs: env,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var out string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
|
|
|
|
case TypeLocal:
|
|
|
|
|
|
|
|
out, err = ExecuteResticCommand(backupOptions, cmd...)
|
|
|
|
if VERBOSE {
|
|
|
|
if VERBOSE {
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
colors.Faint.Println(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case TypeVolume:
|
|
|
|
|
|
|
|
err = backend.ExecDocker(l, cmd)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// After hooks
|
|
|
|
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
|
|
|
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
|
|
|
return nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
colors.Success.Println("Done")
|
|
|
|
colors.Success.Println("Done")
|
|
|
|
return err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l Location) Forget(prune bool, dry bool) error {
|
|
|
|
func (l Location) Forget(prune bool, dry bool) error {
|
|
|
|
colors.PrimaryPrint("Forgetting for location \"%s\"", l.Name)
|
|
|
|
colors.PrimaryPrint("Forgetting for location \"%s\"", l.Name)
|
|
|
|
|
|
|
|
|
|
|
|
from, err := GetPathRelativeToConfig(l.From)
|
|
|
|
path, err := l.getPath()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, to := range l.To {
|
|
|
|
for _, to := range l.To {
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
backend, _ := GetBackend(to)
|
|
|
|
colors.Secondary.Printf("For backend \"%s\"\n", backend.Name)
|
|
|
|
colors.Secondary.Printf("For backend \"%s\"\n", backend.Name)
|
|
|
@ -149,12 +201,10 @@ func (l Location) Forget(prune bool, dry bool) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
options := ExecuteOptions{
|
|
|
|
options := ExecuteOptions{
|
|
|
|
Command: "bash",
|
|
|
|
|
|
|
|
Envs: env,
|
|
|
|
Envs: env,
|
|
|
|
Dir: from,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flags := l.getOptions("forget")
|
|
|
|
flags := l.getOptions("forget")
|
|
|
|
cmd := []string{"forget", "--path", options.Dir}
|
|
|
|
cmd := []string{"forget", "--path", path}
|
|
|
|
if prune {
|
|
|
|
if prune {
|
|
|
|
cmd = append(cmd, "--prune")
|
|
|
|
cmd = append(cmd, "--prune")
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -195,9 +245,16 @@ func (l Location) Restore(to, from string, force bool) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
colors.PrimaryPrint("Restoring location \"%s\"", l.Name)
|
|
|
|
colors.PrimaryPrint("Restoring location \"%s\"", l.Name)
|
|
|
|
colors.Secondary.Println("Restoring lastest snapshot")
|
|
|
|
|
|
|
|
colors.Body.Printf("%s → %s.\n", from, to)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend, _ := GetBackend(from)
|
|
|
|
|
|
|
|
path, err := l.getPath()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
colors.Secondary.Println("Restoring lastest snapshot")
|
|
|
|
|
|
|
|
colors.Body.Printf("%s → %s.\n", from, path)
|
|
|
|
|
|
|
|
switch l.getType() {
|
|
|
|
|
|
|
|
case TypeLocal:
|
|
|
|
// Check if target is empty
|
|
|
|
// Check if target is empty
|
|
|
|
if !force {
|
|
|
|
if !force {
|
|
|
|
notEmptyError := fmt.Errorf("target %s is not empty", to)
|
|
|
|
notEmptyError := fmt.Errorf("target %s is not empty", to)
|
|
|
@ -216,13 +273,10 @@ func (l Location) Restore(to, from string, force bool) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = backend.Exec([]string{"restore", "--target", to, "--path", path, "latest"})
|
|
|
|
backend, _ := GetBackend(from)
|
|
|
|
case TypeVolume:
|
|
|
|
resolved, err := GetPathRelativeToConfig(l.From)
|
|
|
|
err = backend.ExecDocker(l, []string{"restore", "--target", ".", "--path", path, "latest"})
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = backend.Exec([]string{"restore", "--target", to, "--path", resolved, "latest"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|