mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
unlock on error and named arrays for config
This commit is contained in:
parent
8a1fe41825
commit
6e25b90915
18
ROADMAP.md
Normal file
18
ROADMAP.md
Normal 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
|
@ -28,25 +28,21 @@ var backupCmd = &cobra.Command{
|
||||
Use: "backup",
|
||||
Short: "Create backups for given locations",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config := internal.GetConfig()
|
||||
{
|
||||
err := config.CheckConfig()
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
{
|
||||
err := lock.Lock()
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
defer lock.Unlock()
|
||||
{
|
||||
|
||||
config := internal.GetConfig()
|
||||
err = config.CheckConfig()
|
||||
CheckErr(err)
|
||||
|
||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
for _, name := range selected {
|
||||
location := config.Locations[name]
|
||||
location, _ := internal.GetLocation(name)
|
||||
fmt.Printf("Backing up: `%s`", name)
|
||||
location.Backup()
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
12
cmd/check.go
12
cmd/check.go
@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -29,11 +30,16 @@ var checkCmd = &cobra.Command{
|
||||
Short: "Check if everything is setup",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
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()
|
||||
err := config.CheckConfig()
|
||||
cobra.CheckErr(err)
|
||||
err = config.CheckConfig()
|
||||
CheckErr(err)
|
||||
fmt.Println("Everyting is fine.")
|
||||
},
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -26,8 +27,12 @@ var cronCmd = &cobra.Command{
|
||||
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.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := internal.RunCron()
|
||||
cobra.CheckErr(err)
|
||||
err := lock.Lock()
|
||||
CheckErr(err)
|
||||
defer lock.Unlock()
|
||||
|
||||
err = internal.RunCron()
|
||||
CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -27,16 +28,20 @@ var execCmd = &cobra.Command{
|
||||
Use: "exec",
|
||||
Short: "Execute arbitrary native restic commands for given backends",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := lock.Lock()
|
||||
CheckErr(err)
|
||||
defer lock.Unlock()
|
||||
|
||||
config := internal.GetConfig()
|
||||
if err := config.CheckConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{
|
||||
selected, err := internal.GetAllOrSelected(cmd, true)
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
for _, name := range selected {
|
||||
fmt.Println(name)
|
||||
backend := config.Backends[name]
|
||||
backend, _ := internal.GetBackend(name)
|
||||
backend.Exec(args)
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -25,19 +26,23 @@ var forgetCmd = &cobra.Command{
|
||||
Use: "forget",
|
||||
Short: "Forget and optionally prune snapshots according the specified policies",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := lock.Lock()
|
||||
CheckErr(err)
|
||||
defer lock.Unlock()
|
||||
|
||||
config := internal.GetConfig()
|
||||
if err := config.CheckConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{
|
||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
prune, _ := cmd.Flags().GetBool("prune")
|
||||
dry, _ := cmd.Flags().GetBool("dry-run")
|
||||
for _, name := range selected {
|
||||
location := config.Locations[name]
|
||||
location, _ := internal.GetLocation(name)
|
||||
err := location.Forget(prune, dry)
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -25,7 +25,7 @@ var installCmd = &cobra.Command{
|
||||
Short: "Install restic if missing",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := bins.InstallRestic()
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -27,17 +28,20 @@ var restoreCmd = &cobra.Command{
|
||||
Use: "restore",
|
||||
Short: "Restore backup for location",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config := internal.GetConfig()
|
||||
err := lock.Lock()
|
||||
CheckErr(err)
|
||||
defer lock.Unlock()
|
||||
|
||||
location, _ := cmd.Flags().GetString("location")
|
||||
l, ok := config.Locations[location]
|
||||
l, ok := internal.GetLocation(location)
|
||||
if !ok {
|
||||
cobra.CheckErr(fmt.Errorf("invalid location \"%s\"", location))
|
||||
CheckErr(fmt.Errorf("invalid location \"%s\"", location))
|
||||
}
|
||||
target, _ := cmd.Flags().GetString("to")
|
||||
from, _ := cmd.Flags().GetString("from")
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
err := l.Restore(target, from, force)
|
||||
cobra.CheckErr(err)
|
||||
err = l.Restore(target, from, force)
|
||||
CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
|
13
cmd/root.go
13
cmd/root.go
@ -20,12 +20,21 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"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
|
||||
|
||||
// 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.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
cobra.CheckErr(rootCmd.Execute())
|
||||
CheckErr(rootCmd.Execute())
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -63,7 +72,7 @@ func initConfig() {
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := homedir.Dir()
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath(home)
|
||||
|
@ -26,7 +26,7 @@ var upgradeCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
noRestic, _ := cmd.Flags().GetBool("no-restic")
|
||||
err := bins.Upgrade(!noRestic)
|
||||
cobra.CheckErr(err)
|
||||
CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
|
22
install.sh
Executable file
22
install.sh
Executable 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"
|
@ -5,12 +5,23 @@ import (
|
||||
)
|
||||
|
||||
type Backend struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Type string `mapstructure:"type"`
|
||||
Path string `mapstructure:"path"`
|
||||
Key string `mapstructure:"key"`
|
||||
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) {
|
||||
switch b.Type {
|
||||
case "local":
|
||||
|
@ -15,8 +15,8 @@ import (
|
||||
const VERSION = "1.0.0"
|
||||
|
||||
type Config struct {
|
||||
Locations map[string]Location `mapstructure:"locations"`
|
||||
Backends map[string]Backend `mapstructure:"backends"`
|
||||
Locations []Location `mapstructure:"locations"`
|
||||
Backends []Backend `mapstructure:"backends"`
|
||||
}
|
||||
|
||||
var once sync.Once
|
||||
@ -65,12 +65,12 @@ func (c Config) CheckConfig() error {
|
||||
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
|
||||
var list []string
|
||||
if backends {
|
||||
for key := range config.Backends {
|
||||
list = append(list, key)
|
||||
for _, b := range config.Backends {
|
||||
list = append(list, b.Name)
|
||||
}
|
||||
} else {
|
||||
for key := range config.Locations {
|
||||
list = append(list, key)
|
||||
for _, l := range config.Locations {
|
||||
list = append(list, l.Name)
|
||||
}
|
||||
}
|
||||
all, _ := cmd.Flags().GetBool("all")
|
||||
|
@ -21,6 +21,7 @@ type Hooks struct {
|
||||
type Options map[string]map[string][]string
|
||||
|
||||
type Location struct {
|
||||
Name string `mapstructure:"name"`
|
||||
From string `mapstructure:"from"`
|
||||
To []string `mapstructure:"to"`
|
||||
Hooks Hooks `mapstructure:"hooks"`
|
||||
@ -28,10 +29,20 @@ type Location struct {
|
||||
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 {
|
||||
// Check if backends are all valid
|
||||
for _, to := range l.To {
|
||||
_, ok := c.Backends[to]
|
||||
_, ok := GetBackend(to)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid backend `%s`", to)
|
||||
}
|
||||
@ -60,10 +71,9 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
|
||||
}
|
||||
|
||||
func (l Location) Backup() error {
|
||||
c := GetConfig()
|
||||
from := GetPathRelativeToConfig(l.From)
|
||||
for _, to := range l.To {
|
||||
backend := c.Backends[to]
|
||||
backend, _ := GetBackend(to)
|
||||
options := ExecuteOptions{
|
||||
Command: "bash",
|
||||
Envs: backend.getEnv(),
|
||||
@ -92,10 +102,9 @@ func (l Location) Backup() error {
|
||||
}
|
||||
|
||||
func (l Location) Forget(prune bool, dry bool) error {
|
||||
c := GetConfig()
|
||||
from := GetPathRelativeToConfig(l.From)
|
||||
for _, to := range l.To {
|
||||
backend := c.Backends[to]
|
||||
backend, _ := GetBackend(to)
|
||||
options := ExecuteOptions{
|
||||
Envs: backend.getEnv(),
|
||||
Dir: from,
|
||||
@ -159,8 +168,7 @@ func (l Location) Restore(to, from string, force bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
c := GetConfig()
|
||||
backend := c.Backends[from]
|
||||
backend, _ := GetBackend(from)
|
||||
err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"})
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user