diff --git a/README.md b/README.md index bb7e527..caddd6b 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,11 @@ are only as many as this config variable. Default: `12` +### `BACKUP_COMPLETION_WEBHOOK_URL` + +If provided, the tool will do an empty GET request to this URL to indicate it successfully completed the backup job. +You can use this with other tools to monitor if backups are completed as they should. + ## Building from source This project uses go modules, so you'll need at least go 1.11 to compile it. diff --git a/config.go b/config.go index 37ea58c..ea32445 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type conf struct { fullCurrentBackupPath string Schedule string MaxBackups int + CompletionWebhookURL string } var ( @@ -25,9 +26,10 @@ var ( ) const ( - envBackupFolder = `BACKUP_FOLDER` - envSchedule = `BACKUP_SCHEDULE` - envMax = `BACKUP_MAX` + envBackupFolder = `BACKUP_FOLDER` + envSchedule = `BACKUP_SCHEDULE` + envMax = `BACKUP_MAX` + envCompletionWebhookURL = `BACKUP_COMPLETION_WEBHOOK_URL` ) func init() { @@ -60,6 +62,11 @@ func init() { config.MaxBackups = int(maxBackups) } + webhookURL, has := os.LookupEnv(envCompletionWebhookURL) + if has { + config.CompletionWebhookURL = webhookURL + } + updateFullBackupPath() } diff --git a/main.go b/main.go index dbebb09..cb022ee 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,11 @@ func main() { log.Fatalf("Could not dump databases: %s", err) } + err = callWebhook() + if err != nil { + log.Fatalf("Could not call completion webhook: %s", err) + } + log.Println("Done.") }) if err != nil { diff --git a/webhook.go b/webhook.go new file mode 100644 index 0000000..c957c4e --- /dev/null +++ b/webhook.go @@ -0,0 +1,26 @@ +package main + +import ( + "bytes" + "fmt" + "net/http" +) + +func callWebhook() error { + if config.CompletionWebhookURL == "" { + return nil + } + + res, err := http.Get(config.CompletionWebhookURL) + if err != nil { + return err + } + + if res.StatusCode > 399 { + buf := bytes.Buffer{} + _, _ = buf.ReadFrom(res.Body) + return fmt.Errorf("recived an error status code while calling the webhook: %d, message: %s", res.StatusCode, buf.String()) + } + + return nil +}