unlock on error and named arrays for config

This commit is contained in:
cupcakearmy 2021-04-11 17:02:34 +02:00
parent 8a1fe41825
commit 6e25b90915
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
14 changed files with 138 additions and 49 deletions

18
ROADMAP.md Normal file
View File

@ -0,0 +1,18 @@
# Roadmap
## Todo
- implement commands
## Packages
- https://github.com/fatih/color
- https://github.com/manifoldco/promptui
- https://github.com/AlecAivazis/survey
## To ask
- union types for options config
- utility library
- keys of map
- includes array

View File

@ -28,24 +28,20 @@ var backupCmd = &cobra.Command{
Use: "backup", Use: "backup",
Short: "Create backups for given locations", Short: "Create backups for given locations",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig() err := lock.Lock()
{ CheckErr(err)
err := config.CheckConfig()
cobra.CheckErr(err)
}
{
err := lock.Lock()
cobra.CheckErr(err)
}
defer lock.Unlock() defer lock.Unlock()
{
selected, err := internal.GetAllOrSelected(cmd, false) config := internal.GetConfig()
cobra.CheckErr(err) err = config.CheckConfig()
for _, name := range selected { CheckErr(err)
location := config.Locations[name]
fmt.Printf("Backing up: `%s`", name) selected, err := internal.GetAllOrSelected(cmd, false)
location.Backup() CheckErr(err)
} for _, name := range selected {
location, _ := internal.GetLocation(name)
fmt.Printf("Backing up: `%s`", name)
location.Backup()
} }
}, },
} }

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -29,11 +30,16 @@ var checkCmd = &cobra.Command{
Short: "Check if everything is setup", Short: "Check if everything is setup",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if !internal.CheckIfResticIsCallable() { if !internal.CheckIfResticIsCallable() {
cobra.CheckErr(errors.New("restic is not callable. Install: https://restic.readthedocs.io/en/stable/020_installation.html")) CheckErr(errors.New("restic is not callable. Install: https://restic.readthedocs.io/en/stable/020_installation.html"))
} }
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig() config := internal.GetConfig()
err := config.CheckConfig() err = config.CheckConfig()
cobra.CheckErr(err) CheckErr(err)
fmt.Println("Everyting is fine.") fmt.Println("Everyting is fine.")
}, },
} }

View File

@ -17,6 +17,7 @@ package cmd
import ( import (
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -26,8 +27,12 @@ var cronCmd = &cobra.Command{
Short: "Run cron job for automated backups", Short: "Run cron job for automated backups",
Long: `Intended to be mainly triggered by an automated system like systemd or crontab. For each location checks if a cron backup is due and runs it.`, Long: `Intended to be mainly triggered by an automated system like systemd or crontab. For each location checks if a cron backup is due and runs it.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := internal.RunCron() err := lock.Lock()
cobra.CheckErr(err) CheckErr(err)
defer lock.Unlock()
err = internal.RunCron()
CheckErr(err)
}, },
} }

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,16 +28,20 @@ var execCmd = &cobra.Command{
Use: "exec", Use: "exec",
Short: "Execute arbitrary native restic commands for given backends", Short: "Execute arbitrary native restic commands for given backends",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig() config := internal.GetConfig()
if err := config.CheckConfig(); err != nil { if err := config.CheckConfig(); err != nil {
panic(err) panic(err)
} }
{ {
selected, err := internal.GetAllOrSelected(cmd, true) selected, err := internal.GetAllOrSelected(cmd, true)
cobra.CheckErr(err) CheckErr(err)
for _, name := range selected { for _, name := range selected {
fmt.Println(name) fmt.Println(name)
backend := config.Backends[name] backend, _ := internal.GetBackend(name)
backend.Exec(args) backend.Exec(args)
} }
} }

View File

@ -17,6 +17,7 @@ package cmd
import ( import (
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -25,19 +26,23 @@ var forgetCmd = &cobra.Command{
Use: "forget", Use: "forget",
Short: "Forget and optionally prune snapshots according the specified policies", Short: "Forget and optionally prune snapshots according the specified policies",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig() config := internal.GetConfig()
if err := config.CheckConfig(); err != nil { if err := config.CheckConfig(); err != nil {
panic(err) panic(err)
} }
{ {
selected, err := internal.GetAllOrSelected(cmd, false) selected, err := internal.GetAllOrSelected(cmd, false)
cobra.CheckErr(err) CheckErr(err)
prune, _ := cmd.Flags().GetBool("prune") prune, _ := cmd.Flags().GetBool("prune")
dry, _ := cmd.Flags().GetBool("dry-run") dry, _ := cmd.Flags().GetBool("dry-run")
for _, name := range selected { for _, name := range selected {
location := config.Locations[name] location, _ := internal.GetLocation(name)
err := location.Forget(prune, dry) err := location.Forget(prune, dry)
cobra.CheckErr(err) CheckErr(err)
} }
} }
}, },

View File

@ -25,7 +25,7 @@ var installCmd = &cobra.Command{
Short: "Install restic if missing", Short: "Install restic if missing",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := bins.InstallRestic() err := bins.InstallRestic()
cobra.CheckErr(err) CheckErr(err)
}, },
} }

View File

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,17 +28,20 @@ var restoreCmd = &cobra.Command{
Use: "restore", Use: "restore",
Short: "Restore backup for location", Short: "Restore backup for location",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig() err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
location, _ := cmd.Flags().GetString("location") location, _ := cmd.Flags().GetString("location")
l, ok := config.Locations[location] l, ok := internal.GetLocation(location)
if !ok { if !ok {
cobra.CheckErr(fmt.Errorf("invalid location \"%s\"", location)) CheckErr(fmt.Errorf("invalid location \"%s\"", location))
} }
target, _ := cmd.Flags().GetString("to") target, _ := cmd.Flags().GetString("to")
from, _ := cmd.Flags().GetString("from") from, _ := cmd.Flags().GetString("from")
force, _ := cmd.Flags().GetBool("force") force, _ := cmd.Flags().GetBool("force")
err := l.Restore(target, from, force) err = l.Restore(target, from, force)
cobra.CheckErr(err) CheckErr(err)
}, },
} }

View File

@ -20,12 +20,21 @@ import (
"os" "os"
"github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra" "github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func CheckErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
lock.Unlock()
os.Exit(1)
}
}
var cfgFile string var cfgFile string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
@ -38,7 +47,7 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
cobra.CheckErr(rootCmd.Execute()) CheckErr(rootCmd.Execute())
} }
func init() { func init() {
@ -63,7 +72,7 @@ func initConfig() {
} else { } else {
// Find home directory. // Find home directory.
home, err := homedir.Dir() home, err := homedir.Dir()
cobra.CheckErr(err) CheckErr(err)
viper.AddConfigPath(".") viper.AddConfigPath(".")
viper.AddConfigPath(home) viper.AddConfigPath(home)

View File

@ -26,7 +26,7 @@ var upgradeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic") noRestic, _ := cmd.Flags().GetBool("no-restic")
err := bins.Upgrade(!noRestic) err := bins.Upgrade(!noRestic)
cobra.CheckErr(err) CheckErr(err)
}, },
} }

22
install.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
OUT_FILE=/usr/local/bin/autorestic
if [[ "$OSTYPE" == "linux-gnu" ]]; then
TYPE=linux
elif [[ "$OSTYPE" == "darwin"* ]]; then
TYPE=macos
else
echo "Unsupported OS"
exit 1
fi
curl -s https://api.github.com/repos/cupcakearmy/autorestic/releases/latest \
| grep "browser_download_url.*_${TYPE}" \
| cut -d : -f 2,3 \
| tr -d \" \
| wget -O ${OUT_FILE} -i -
chmod +x ${OUT_FILE}
autorestic install
echo "Succefsully installed autorestic"

View File

@ -5,12 +5,23 @@ import (
) )
type Backend struct { type Backend struct {
Name string `mapstructure:"name"`
Type string `mapstructure:"type"` Type string `mapstructure:"type"`
Path string `mapstructure:"path"` Path string `mapstructure:"path"`
Key string `mapstructure:"key"` Key string `mapstructure:"key"`
Env map[string]string `mapstructure:"env"` Env map[string]string `mapstructure:"env"`
} }
func GetBackend(name string) (Backend, bool) {
c := GetConfig()
for _, b := range c.Backends {
if b.Name == name {
return b, true
}
}
return Backend{}, false
}
func (b Backend) generateRepo() (string, error) { func (b Backend) generateRepo() (string, error) {
switch b.Type { switch b.Type {
case "local": case "local":

View File

@ -15,8 +15,8 @@ import (
const VERSION = "1.0.0" const VERSION = "1.0.0"
type Config struct { type Config struct {
Locations map[string]Location `mapstructure:"locations"` Locations []Location `mapstructure:"locations"`
Backends map[string]Backend `mapstructure:"backends"` Backends []Backend `mapstructure:"backends"`
} }
var once sync.Once var once sync.Once
@ -65,12 +65,12 @@ func (c Config) CheckConfig() error {
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) { func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
var list []string var list []string
if backends { if backends {
for key := range config.Backends { for _, b := range config.Backends {
list = append(list, key) list = append(list, b.Name)
} }
} else { } else {
for key := range config.Locations { for _, l := range config.Locations {
list = append(list, key) list = append(list, l.Name)
} }
} }
all, _ := cmd.Flags().GetBool("all") all, _ := cmd.Flags().GetBool("all")

View File

@ -21,6 +21,7 @@ type Hooks struct {
type Options map[string]map[string][]string type Options map[string]map[string][]string
type Location struct { type Location struct {
Name string `mapstructure:"name"`
From string `mapstructure:"from"` From string `mapstructure:"from"`
To []string `mapstructure:"to"` To []string `mapstructure:"to"`
Hooks Hooks `mapstructure:"hooks"` Hooks Hooks `mapstructure:"hooks"`
@ -28,10 +29,20 @@ type Location struct {
Options Options `mapstructure:"options"` Options Options `mapstructure:"options"`
} }
func GetLocation(name string) (Location, bool) {
c := GetConfig()
for _, b := range c.Locations {
if b.Name == name {
return b, true
}
}
return Location{}, false
}
func (l Location) validate(c Config) error { func (l Location) validate(c Config) error {
// Check if backends are all valid // Check if backends are all valid
for _, to := range l.To { for _, to := range l.To {
_, ok := c.Backends[to] _, ok := GetBackend(to)
if !ok { if !ok {
return fmt.Errorf("invalid backend `%s`", to) return fmt.Errorf("invalid backend `%s`", to)
} }
@ -60,10 +71,9 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
} }
func (l Location) Backup() error { func (l Location) Backup() error {
c := GetConfig()
from := GetPathRelativeToConfig(l.From) from := GetPathRelativeToConfig(l.From)
for _, to := range l.To { for _, to := range l.To {
backend := c.Backends[to] backend, _ := GetBackend(to)
options := ExecuteOptions{ options := ExecuteOptions{
Command: "bash", Command: "bash",
Envs: backend.getEnv(), Envs: backend.getEnv(),
@ -92,10 +102,9 @@ func (l Location) Backup() error {
} }
func (l Location) Forget(prune bool, dry bool) error { func (l Location) Forget(prune bool, dry bool) error {
c := GetConfig()
from := GetPathRelativeToConfig(l.From) from := GetPathRelativeToConfig(l.From)
for _, to := range l.To { for _, to := range l.To {
backend := c.Backends[to] backend, _ := GetBackend(to)
options := ExecuteOptions{ options := ExecuteOptions{
Envs: backend.getEnv(), Envs: backend.getEnv(),
Dir: from, Dir: from,
@ -159,8 +168,7 @@ func (l Location) Restore(to, from string, force bool) error {
} }
} }
c := GetConfig() backend, _ := GetBackend(from)
backend := c.Backends[from]
err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"}) err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"})
if err != nil { if err != nil {
return err return err