mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-09-06 10:30:39 +00:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
4d9a2b828e | |||
|
b830667264 | ||
|
83eeb847ac | ||
|
a89ba5a40a | ||
|
6990bf6adc | ||
|
2f407cf211 |
17
cmd/exec.go
17
cmd/exec.go
@@ -1,6 +1,8 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal"
|
"github.com/cupcakearmy/autorestic/internal"
|
||||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||||
@@ -18,10 +20,23 @@ var execCmd = &cobra.Command{
|
|||||||
|
|
||||||
selected, err := internal.GetAllOrSelected(cmd, true)
|
selected, err := internal.GetAllOrSelected(cmd, true)
|
||||||
CheckErr(err)
|
CheckErr(err)
|
||||||
|
|
||||||
|
var errors []error
|
||||||
for _, name := range selected {
|
for _, name := range selected {
|
||||||
colors.PrimaryPrint(" Executing on \"%s\" ", name)
|
colors.PrimaryPrint(" Executing on \"%s\" ", name)
|
||||||
backend, _ := internal.GetBackend(name)
|
backend, _ := internal.GetBackend(name)
|
||||||
backend.Exec(args)
|
err := backend.Exec(args)
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errors) > 0 {
|
||||||
|
for _, err := range errors {
|
||||||
|
colors.Error.Printf("%s\n\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckErr(fmt.Errorf("%d errors were found", len(errors)))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@@ -41,6 +41,7 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().BoolVar(&flags.CI, "ci", false, "CI mode disabled interactive mode and colors and enables verbosity")
|
rootCmd.PersistentFlags().BoolVar(&flags.CI, "ci", false, "CI mode disabled interactive mode and colors and enables verbosity")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode")
|
rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode")
|
||||||
rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary")
|
rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image")
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -21,7 +21,7 @@ locations:
|
|||||||
keep-weekly: 1 # keep 1 last weekly snapshots
|
keep-weekly: 1 # keep 1 last weekly snapshots
|
||||||
keep-monthly: 12 # keep 12 last monthly snapshots
|
keep-monthly: 12 # keep 12 last monthly snapshots
|
||||||
keep-yearly: 7 # keep 7 last yearly snapshots
|
keep-yearly: 7 # keep 7 last yearly snapshots
|
||||||
keep-within: '2w' # keep snapshots from the last 2 weeks
|
keep-within: '14d' # keep snapshots from the last 14 days
|
||||||
```
|
```
|
||||||
|
|
||||||
## Globally
|
## Globally
|
||||||
|
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||||
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BackendRest struct {
|
type BackendRest struct {
|
||||||
@@ -121,13 +122,17 @@ func (b Backend) validate() error {
|
|||||||
}
|
}
|
||||||
options := ExecuteOptions{Envs: env, Silent: true}
|
options := ExecuteOptions{Envs: env, Silent: true}
|
||||||
// Check if already initialized
|
// Check if already initialized
|
||||||
_, _, err = ExecuteResticCommand(options, "check")
|
cmd := []string{"check"}
|
||||||
|
cmd = append(cmd, combineBackendOptions("check", b)...)
|
||||||
|
_, _, err = ExecuteResticCommand(options, cmd...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
// If not initialize
|
// If not initialize
|
||||||
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
colors.Body.Printf("Initializing backend \"%s\"...\n", b.name)
|
||||||
_, _, err := ExecuteResticCommand(options, "init")
|
cmd := []string{"init"}
|
||||||
|
cmd = append(cmd, combineBackendOptions("init", b)...)
|
||||||
|
_, _, err := ExecuteResticCommand(options, cmd...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,7 +165,6 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
|
|||||||
args = append([]string{"restic"}, args...)
|
args = append([]string{"restic"}, args...)
|
||||||
docker := []string{
|
docker := []string{
|
||||||
"run", "--rm",
|
"run", "--rm",
|
||||||
"--pull", "always",
|
|
||||||
"--entrypoint", "ash",
|
"--entrypoint", "ash",
|
||||||
"--workdir", dir,
|
"--workdir", dir,
|
||||||
"--volume", volume + ":" + dir,
|
"--volume", volume + ":" + dir,
|
||||||
@@ -194,6 +198,7 @@ func (b Backend) ExecDocker(l Location, args []string) (int, string, error) {
|
|||||||
for key, value := range env {
|
for key, value := range env {
|
||||||
docker = append(docker, "--env", key+"="+value)
|
docker = append(docker, "--env", key+"="+value)
|
||||||
}
|
}
|
||||||
docker = append(docker, "cupcakearmy/autorestic:"+VERSION, "-c", strings.Join(args, " "))
|
|
||||||
|
docker = append(docker, flags.DOCKER_IMAGE, "-c", strings.Join(args, " "))
|
||||||
return ExecuteCommand(options, docker...)
|
return ExecuteCommand(options, docker...)
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,7 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "1.7.2"
|
const VERSION = "1.7.3"
|
||||||
|
|
||||||
type OptionMap map[string][]interface{}
|
type OptionMap map[string][]interface{}
|
||||||
type Options map[string]OptionMap
|
type Options map[string]OptionMap
|
||||||
@@ -303,7 +303,17 @@ func getOptions(options Options, keys []string) []string {
|
|||||||
return selected
|
return selected
|
||||||
}
|
}
|
||||||
|
|
||||||
func combineOptions(key string, l Location, b Backend) []string {
|
func combineBackendOptions(key string, b Backend) []string {
|
||||||
|
// Priority: backend > global
|
||||||
|
var options []string
|
||||||
|
gFlags := getOptions(GetConfig().Global, []string{key})
|
||||||
|
bFlags := getOptions(b.Options, []string{"all", key})
|
||||||
|
options = append(options, gFlags...)
|
||||||
|
options = append(options, bFlags...)
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
func combineAllOptions(key string, l Location, b Backend) []string {
|
||||||
// Priority: location > backend > global
|
// Priority: location > backend > global
|
||||||
var options []string
|
var options []string
|
||||||
gFlags := getOptions(GetConfig().Global, []string{key})
|
gFlags := getOptions(GetConfig().Global, []string{key})
|
||||||
|
@@ -5,4 +5,5 @@ var (
|
|||||||
VERBOSE bool = false
|
VERBOSE bool = false
|
||||||
CRON_LEAN bool = false
|
CRON_LEAN bool = false
|
||||||
RESTIC_BIN string
|
RESTIC_BIN string
|
||||||
|
DOCKER_IMAGE string
|
||||||
)
|
)
|
||||||
|
@@ -216,7 +216,7 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := []string{"backup"}
|
cmd := []string{"backup"}
|
||||||
cmd = append(cmd, combineOptions("backup", l, backend)...)
|
cmd = append(cmd, combineAllOptions("backup", l, backend)...)
|
||||||
if cron {
|
if cron {
|
||||||
cmd = append(cmd, "--tag", buildTag("cron"))
|
cmd = append(cmd, "--tag", buildTag("cron"))
|
||||||
}
|
}
|
||||||
@@ -320,7 +320,12 @@ after:
|
|||||||
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)
|
||||||
|
|
||||||
for _, to := range l.To {
|
backendsToForget := l.To
|
||||||
|
for _, copyBackends := range l.CopyOption {
|
||||||
|
backendsToForget = append(backendsToForget, copyBackends...)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, to := range backendsToForget {
|
||||||
backend, _ := GetBackend(to)
|
backend, _ := GetBackend(to)
|
||||||
colors.Secondary.Printf("For backend \"%s\"\n", backend.name)
|
colors.Secondary.Printf("For backend \"%s\"\n", backend.name)
|
||||||
env, err := backend.getEnv()
|
env, err := backend.getEnv()
|
||||||
@@ -337,7 +342,7 @@ func (l Location) Forget(prune bool, dry bool) error {
|
|||||||
if dry {
|
if dry {
|
||||||
cmd = append(cmd, "--dry-run")
|
cmd = append(cmd, "--dry-run")
|
||||||
}
|
}
|
||||||
cmd = append(cmd, combineOptions("forget", l, backend)...)
|
cmd = append(cmd, combineAllOptions("forget", l, backend)...)
|
||||||
_, _, err = ExecuteResticCommand(options, cmd...)
|
_, _, err = ExecuteResticCommand(options, cmd...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Reference in New Issue
Block a user