Merge pull request #117 from cupcakearmy/specific-location

specific location
This commit is contained in:
Nicco 2021-10-28 17:30:51 +02:00 committed by GitHub
commit 3e80e6d18e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"strings"
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/colors" "github.com/cupcakearmy/autorestic/internal/colors"
@ -22,8 +23,13 @@ var backupCmd = &cobra.Command{
CheckErr(err) CheckErr(err)
errors := 0 errors := 0
for _, name := range selected { for _, name := range selected {
location, _ := internal.GetLocation(name) var splitted = strings.Split(name, "@")
errs := location.Backup(false) var specificBackend = ""
if len(splitted) > 1 {
specificBackend = splitted[1]
}
location, _ := internal.GetLocation(splitted[0])
errs := location.Backup(false, specificBackend)
for err := range errs { for err := range errs {
colors.Error.Println(err) colors.Error.Println(err)
errors++ errors++

View File

@ -185,20 +185,18 @@ func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
selected, _ = cmd.Flags().GetStringSlice("location") selected, _ = cmd.Flags().GetStringSlice("location")
} }
for _, s := range selected { for _, s := range selected {
found := false var splitted = strings.Split(s, "@")
for _, l := range list { for _, l := range list {
if l == s { if l == splitted[0] {
found = true goto found
break
} }
} }
if !found {
if backends { if backends {
return nil, fmt.Errorf("invalid backend \"%s\"", s) return nil, fmt.Errorf("invalid backend \"%s\"", s)
} else { } else {
return nil, fmt.Errorf("invalid location \"%s\"", s) return nil, fmt.Errorf("invalid location \"%s\"", s)
} }
} found:
} }
if len(selected) == 0 { if len(selected) == 0 {

View File

@ -125,8 +125,9 @@ func (l Location) getPath() (string, error) {
return "", fmt.Errorf("could not get path for location \"%s\"", l.name) return "", fmt.Errorf("could not get path for location \"%s\"", l.name)
} }
func (l Location) Backup(cron bool) []error { func (l Location) Backup(cron bool, specificBackend string) []error {
var errors []error var errors []error
var backends []string
colors.PrimaryPrint(" Backing up location \"%s\" ", l.name) colors.PrimaryPrint(" Backing up location \"%s\" ", l.name)
t := l.getType() t := l.getType()
options := ExecuteOptions{ options := ExecuteOptions{
@ -155,7 +156,20 @@ func (l Location) Backup(cron bool) []error {
} }
// Backup // Backup
for i, to := range l.To { if specificBackend == "" {
backends = l.To
} else {
for _, b := range l.To {
if b == specificBackend {
backends = []string{b}
goto backup
}
}
errors = append(errors, fmt.Errorf("backup location \"%s\" has no backend \"%s\"", l.name, specificBackend))
return errors
}
backup:
for i, to := range backends {
backend, _ := GetBackend(to) backend, _ := GetBackend(to)
colors.Secondary.Printf("Backend: %s\n", backend.name) colors.Secondary.Printf("Backend: %s\n", backend.name)
env, err := backend.getEnv() env, err := backend.getEnv()
@ -338,7 +352,7 @@ func (l Location) RunCron() error {
now := time.Now() now := time.Now()
if now.After(next) { if now.After(next) {
lock.SetCron(l.name, now.Unix()) lock.SetCron(l.name, now.Unix())
l.Backup(true) l.Backup(true, "")
} else { } else {
if !CRON_LEAN { if !CRON_LEAN {
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name) colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name)