1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-12-20 18:41:21 +00:00

Send notifications only on some commands

Also fixed some linter errors
This commit is contained in:
Knut Ahlers 2016-05-23 11:54:45 +02:00
parent a227b52dd4
commit 6c841d29f4
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 58 additions and 33 deletions

5
Godeps/Godeps.json generated
View file

@ -3,6 +3,11 @@
"GoVersion": "go1.6",
"GodepVersion": "v62",
"Deps": [
{
"ImportPath": "github.com/Luzifer/go_helpers/str",
"Comment": "v1.3.0",
"Rev": "6abbbafaada02b63dd8f9e185921fd8b3c35b6c2"
},
{
"ImportPath": "github.com/Luzifer/go_helpers/which",
"Comment": "v1.3.0",

View file

@ -12,6 +12,23 @@ import (
"gopkg.in/yaml.v2"
)
const (
commandBackup = "backup"
commandCleanup = "cleanup"
commandList = "list-current-files"
commandRestore = "restore"
commandStatus = "status"
commandVerify = "verify"
commandRemove = "__remove_old"
)
var (
notifyCommands = []string{
commandBackup,
commandRemove,
}
)
type configFile struct {
RootPath string `yaml:"root" valid:"required"`
Hostname string `yaml:"hostname"`
@ -128,18 +145,18 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine []
)
switch command {
case "backup":
case commandBackup:
option = ""
root = c.RootPath
dest = c.Destination
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
case "cleanup":
case commandCleanup:
option = command
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case "list-current-files":
case commandList:
option = command
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case "restore":
case commandRestore:
addTime = true
option = command
root = c.Destination
@ -156,15 +173,15 @@ func (c *configFile) GenerateCommand(argv []string, time string) (commandLine []
}
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, restoreFile)
case "status":
case commandStatus:
option = "collection-status"
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case "verify":
case commandVerify:
option = command
root = c.Destination
dest = c.RootPath
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
case "__remove_old":
case commandRemove:
commandLine, env, err = c.generateRemoveCommand()
default:
err = fmt.Errorf("Did not understand command '%s', please see 'help' for details what to do.", command)

23
main.go
View file

@ -114,29 +114,18 @@ func main() {
defer lock.Unlock()
if err := execute(config, argv[1:]); err != nil {
if err := config.Notify(false, fmt.Errorf("Could not create backup: %s", err)); err != nil {
logf("[ERR] Error sending notifications: %s", err)
} else {
logf("[INF] Notifications sent")
}
return
}
if config.Cleanup.Type != "none" {
logf("++++ Starting removal of old backups")
if err := execute(config, []string{"__remove_old"}); err != nil {
if err := config.Notify(false, fmt.Errorf("Could not cleanup backup: %s", err)); err != nil {
logf("[ERR] Error sending notifications: %s", err)
} else {
logf("[INF] Notifications sent")
}
if err := execute(config, []string{commandRemove}); err != nil {
return
}
}
if err := config.Notify(true, nil); err != nil {
if err := config.Notify(argv[1], true, nil); err != nil {
logf("[ERR] Error sending notifications: %s", err)
} else {
logf("[INF] Notifications sent")
@ -186,6 +175,14 @@ func execute(config *configFile, argv []string) error {
logf("[INF] Execution of duplicity command was successful.")
}
if err != nil {
if nErr := config.Notify(argv[0], false, fmt.Errorf("Could not create backup: %s", err)); nErr != nil {
logf("[ERR] Error sending notifications: %s", nErr)
} else {
logf("[INF] Notifications sent")
}
}
return err
}

View file

@ -5,9 +5,15 @@ import (
"encoding/json"
"fmt"
"net/http"
"github.com/Luzifer/go_helpers/str"
)
func (c *configFile) Notify(success bool, err error) error {
func (c *configFile) Notify(command string, success bool, err error) error {
if !str.StringInSlice(command, notifyCommands) {
return nil
}
errs := []error{}
for _, n := range []func(bool, error) error{
@ -21,7 +27,8 @@ func (c *configFile) Notify(success bool, err error) error {
if len(errs) == 0 {
return nil
} else {
}
estr := ""
for _, e := range errs {
if e == nil {
@ -32,7 +39,6 @@ func (c *configFile) Notify(success bool, err error) error {
}
return fmt.Errorf("%d notifiers failed:%s", len(errs), estr)
}
}
type mondashResult struct {
Title string `json:"title"`
@ -64,7 +70,7 @@ func (c *configFile) notifyMonDash(success bool, err error) error {
}
buf := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(buf).Encode(monitoringResult); err != nil {
if err = json.NewEncoder(buf).Encode(monitoringResult); err != nil {
return err
}
@ -114,7 +120,7 @@ func (c *configFile) notifySlack(success bool, err error) error {
}
buf := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(buf).Encode(sr); err != nil {
if err = json.NewEncoder(buf).Encode(sr); err != nil {
return err
}