mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-12-22 08:16:25 +00:00
add failure and success hooks
This commit is contained in:
parent
aebaf0a225
commit
e05386b0b5
@ -17,15 +17,15 @@ var backupCmd = &cobra.Command{
|
|||||||
CheckErr(err)
|
CheckErr(err)
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
CheckErr(internal.CheckConfig())
|
internal.GetConfig()
|
||||||
|
|
||||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||||
CheckErr(err)
|
CheckErr(err)
|
||||||
errors := 0
|
errors := 0
|
||||||
for _, name := range selected {
|
for _, name := range selected {
|
||||||
location, _ := internal.GetLocation(name)
|
location, _ := internal.GetLocation(name)
|
||||||
err := location.Backup(false)
|
errs := location.Backup(false)
|
||||||
if err != nil {
|
for err := range errs {
|
||||||
colors.Error.Println(err)
|
colors.Error.Println(err)
|
||||||
errors++
|
errors++
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,14 @@
|
|||||||
|
|
||||||
If you want to perform some commands before and/or after a backup, you can use hooks.
|
If you want to perform some commands before and/or after a backup, you can use hooks.
|
||||||
|
|
||||||
They consist of a list of `before`/`after` commands that will be executed in the same directory as the target `from`.
|
They consist of a list of commands that will be executed in the same directory as the target `from`.
|
||||||
|
|
||||||
|
The following hooks groups are supported, none are required:
|
||||||
|
|
||||||
|
- `before`
|
||||||
|
- `after`
|
||||||
|
- `failure`
|
||||||
|
- `success`
|
||||||
|
|
||||||
```yml | .autorestic.yml
|
```yml | .autorestic.yml
|
||||||
locations:
|
locations:
|
||||||
@ -11,10 +18,25 @@ locations:
|
|||||||
to: my-backend
|
to: my-backend
|
||||||
hooks:
|
hooks:
|
||||||
before:
|
before:
|
||||||
- echo "Hello"
|
- echo "One"
|
||||||
- echo "Human"
|
- echo "Two"
|
||||||
|
- echo "Three"
|
||||||
after:
|
after:
|
||||||
- echo "kthxbye"
|
- echo "Byte"
|
||||||
|
failure:
|
||||||
|
- echo "Something went wrong"
|
||||||
|
success:
|
||||||
|
- echo "Well done!"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Flowchart
|
||||||
|
|
||||||
|
1. `before` hook
|
||||||
|
2. Run backup
|
||||||
|
3. `after` hook
|
||||||
|
4. - `success` hook if no errors were found
|
||||||
|
- `failure` hook if at least error was encountered
|
||||||
|
|
||||||
|
If the `before` hook encounters errors the backup and `after` hooks will be skipped and only the `failed` hooks will run.
|
||||||
|
|
||||||
> :ToCPrevNext
|
> :ToCPrevNext
|
||||||
|
@ -75,21 +75,22 @@ func (c *Config) Describe() {
|
|||||||
colors.PrintDescription("Cron", l.Cron)
|
colors.PrintDescription("Cron", l.Cron)
|
||||||
}
|
}
|
||||||
|
|
||||||
after, before := len(l.Hooks.After), len(l.Hooks.Before)
|
tmp = ""
|
||||||
if after+before > 0 {
|
hooks := map[string][]string{
|
||||||
tmp = ""
|
"Before": l.Hooks.Before,
|
||||||
if before > 0 {
|
"After": l.Hooks.After,
|
||||||
tmp += "\tBefore"
|
"Failure": l.Hooks.Failure,
|
||||||
for _, cmd := range l.Hooks.Before {
|
"Success": l.Hooks.Success,
|
||||||
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
|
}
|
||||||
}
|
for hook, commands := range hooks {
|
||||||
}
|
if len(commands) > 0 {
|
||||||
if after > 0 {
|
tmp += "\n\t" + hook
|
||||||
tmp += "\n\tAfter"
|
for _, cmd := range commands {
|
||||||
for _, cmd := range l.Hooks.After {
|
|
||||||
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
|
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if tmp != "" {
|
||||||
colors.PrintDescription("Hooks", tmp)
|
colors.PrintDescription("Hooks", tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,8 +24,10 @@ const (
|
|||||||
type HookArray = []string
|
type HookArray = []string
|
||||||
|
|
||||||
type Hooks struct {
|
type Hooks struct {
|
||||||
Before HookArray `yaml:"before"`
|
Before HookArray `yaml:"before,omitempty"`
|
||||||
After HookArray `yaml:"after"`
|
After HookArray `yaml:"after,omitempty"`
|
||||||
|
Success HookArray `yaml:"success,omitempty"`
|
||||||
|
Failure HookArray `yaml:"failure,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Options map[string]map[string][]string
|
type Options map[string]map[string][]string
|
||||||
@ -133,7 +135,8 @@ 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) []error {
|
||||||
|
var errors []error
|
||||||
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{
|
||||||
@ -147,7 +150,8 @@ func (l Location) Backup(cron bool) error {
|
|||||||
|
|
||||||
// Hooks
|
// Hooks
|
||||||
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
|
||||||
return err
|
errors = append(errors, err)
|
||||||
|
goto after
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backup
|
// Backup
|
||||||
@ -156,7 +160,8 @@ func (l Location) Backup(cron bool) error {
|
|||||||
colors.Secondary.Printf("Backend: %s\n", backend.name)
|
colors.Secondary.Printf("Backend: %s\n", backend.name)
|
||||||
env, err := backend.getEnv()
|
env, err := backend.getEnv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
errors = append(errors, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := l.getOptions("backup")
|
flags := l.getOptions("backup")
|
||||||
@ -181,7 +186,8 @@ func (l Location) Backup(cron bool) error {
|
|||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
colors.Error.Println(out)
|
colors.Error.Println(out)
|
||||||
return err
|
errors = append(errors, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if VERBOSE {
|
if VERBOSE {
|
||||||
colors.Faint.Println(out)
|
colors.Faint.Println(out)
|
||||||
@ -190,10 +196,22 @@ func (l Location) Backup(cron bool) error {
|
|||||||
|
|
||||||
// After hooks
|
// After hooks
|
||||||
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
||||||
return err
|
errors = append(errors, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
after:
|
||||||
|
var commands []string
|
||||||
|
if len(errors) > 0 {
|
||||||
|
commands = l.Hooks.Failure
|
||||||
|
} else {
|
||||||
|
commands = l.Hooks.Success
|
||||||
|
}
|
||||||
|
if err := ExecuteHooks(commands, options); err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
}
|
||||||
|
|
||||||
colors.Success.Println("Done")
|
colors.Success.Println("Done")
|
||||||
return nil
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Location) Forget(prune bool, dry bool) error {
|
func (l Location) Forget(prune bool, dry bool) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user