mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
Compare commits
4 Commits
0a94869fc6
...
4b7de2c221
Author | SHA1 | Date | |
---|---|---|---|
|
4b7de2c221 | ||
|
41e4e4a5f3 | ||
|
51a8298f2a | ||
|
f6aa45fd6c |
@ -42,6 +42,7 @@ func init() {
|
||||
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.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image")
|
||||
rootCmd.PersistentFlags().StringVar(&flags.LOCKFILE_PATH, "lockfile-path", "", "specify a custom path for the lockfile (defaults to .autorestic.lock.yml next to the loaded autorestic config file)")
|
||||
cobra.OnInitialize(initConfig)
|
||||
}
|
||||
|
||||
|
@ -34,3 +34,11 @@ With `--restic-bin` you can specify to run a specific restic binary. This can be
|
||||
```bash
|
||||
autorestic --restic-bin /some/path/to/my/custom/restic/binary
|
||||
```
|
||||
|
||||
## `--lockfile-path`
|
||||
|
||||
Specify the path for the lockfile used by `autorestic`. If omitted, this will default to `.autorestic.lock.yml` next to the loaded config file.
|
||||
|
||||
```bash
|
||||
autorestic --lockfile-path /path/to/my/.autorestic.lock.yml
|
||||
```
|
||||
|
@ -1,12 +1,22 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func RunCron() error {
|
||||
c := GetConfig()
|
||||
var errs []error
|
||||
for name, l := range c.Locations {
|
||||
l.name = name
|
||||
if err := l.RunCron(); err != nil {
|
||||
return err
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("Encountered errors during cron process:\n%w", errors.Join(errs...))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package flags
|
||||
|
||||
var (
|
||||
CI bool = false
|
||||
VERBOSE bool = false
|
||||
CRON_LEAN bool = false
|
||||
RESTIC_BIN string
|
||||
DOCKER_IMAGE string
|
||||
CI bool = false
|
||||
VERBOSE bool = false
|
||||
CRON_LEAN bool = false
|
||||
RESTIC_BIN string
|
||||
DOCKER_IMAGE string
|
||||
LOCKFILE_PATH string
|
||||
)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -446,7 +447,10 @@ func (l Location) RunCron() error {
|
||||
now := time.Now()
|
||||
if now.After(next) {
|
||||
lock.SetCron(l.name, now.Unix())
|
||||
l.Backup(true, "")
|
||||
errs := l.Backup(true, "")
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("Failed to backup location \"%s\":\n%w", l.name, errors.Join(errs...))
|
||||
}
|
||||
} else {
|
||||
if !flags.CRON_LEAN {
|
||||
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name)
|
||||
|
@ -18,18 +18,28 @@ const (
|
||||
RUNNING = "running"
|
||||
)
|
||||
|
||||
// getLockfilePath returns the path to the lockfile. If flags.LOCKFILE_PATH is
|
||||
// set, its value is used, otherwise the path is generated relative to the
|
||||
// config file.
|
||||
func getLockfilePath() string {
|
||||
if flags.LOCKFILE_PATH != "" {
|
||||
return flags.LOCKFILE_PATH
|
||||
} else {
|
||||
p := viper.ConfigFileUsed()
|
||||
if p == "" {
|
||||
colors.Error.Println("cannot lock before reading config location")
|
||||
os.Exit(1)
|
||||
}
|
||||
return path.Join(path.Dir(p), ".autorestic.lock.yml")
|
||||
}
|
||||
}
|
||||
|
||||
func getLock() *viper.Viper {
|
||||
if lock == nil {
|
||||
|
||||
once.Do(func() {
|
||||
lock = viper.New()
|
||||
lock.SetDefault("running", false)
|
||||
p := viper.ConfigFileUsed()
|
||||
if p == "" {
|
||||
colors.Error.Println("cannot lock before reading config location")
|
||||
os.Exit(1)
|
||||
}
|
||||
file = path.Join(path.Dir(p), ".autorestic.lock.yml")
|
||||
file = getLockfilePath()
|
||||
if !flags.CRON_LEAN {
|
||||
colors.Faint.Println("Using lock:\t", file)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal/flags"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@ -28,6 +29,37 @@ func setup(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetLockfilePath(t *testing.T) {
|
||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
|
||||
flags.LOCKFILE_PATH = "/path/to/my/autorestic.lock.yml"
|
||||
defer func() { flags.LOCKFILE_PATH = "" }()
|
||||
|
||||
p := getLockfilePath()
|
||||
|
||||
if p != "/path/to/my/autorestic.lock.yml" {
|
||||
t.Errorf("got %v, want %v", p, "/path/to/my/autorestic.lock.yml")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("when flags.LOCKFILE_PATH is set", func(t *testing.T) {
|
||||
d, err := os.MkdirTemp("", testDirectory)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating temp dir: %v", err)
|
||||
return
|
||||
}
|
||||
viper.SetConfigFile(d + "/.autorestic.yml")
|
||||
defer viper.Reset()
|
||||
|
||||
flags.LOCKFILE_PATH = ""
|
||||
|
||||
p := getLockfilePath()
|
||||
|
||||
if p != d+"/.autorestic.lock.yml" {
|
||||
t.Errorf("got %v, want %v", p, d+"/.autorestic.lock.yml")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLock(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user