Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Telegram notification #78

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package cmd /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package cmd

import (
Expand Down
6 changes: 6 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package cmd /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package cmd

import (
Expand Down
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package cmd /*
/*
Copyright © 2024 Jonas Kaninda
*/
// Package cmd /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package cmd

import (
Expand Down
10 changes: 6 additions & 4 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Package cmd /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package cmd

/*
Copyright © 2024 Jonas Kaninda
*/

import (
"fmt"
"github.com/spf13/cobra"
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Backup, restore and migrate targets, schedule and retention are configured using
| TARGET_DB_NAME | Optional, required for database migration | Target database name |
| TARGET_DB_USERNAME | Optional, required for database migration | Target database username |
| TARGET_DB_PASSWORD | Optional, required for database migration | Target database password |

| TG_TOKEN | Optional, required for Telegram notification | Telegram token |
| TG_CHAT_ID | Optional, required for Telegram notification | Telegram Chat ID |
---
## Run in Scheduled mode

Expand Down
13 changes: 6 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

//main
// Package main /
/*****
* MySQL Backup & Restore
* @author Jonas Kaninda
* @license MIT License <https://opensource.org/licenses/MIT>
* @link https://github.com/jkaninda/mysql-bkup
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package main

import "github.com/jkaninda/mysql-bkup/cmd"

func main() {
Expand Down
18 changes: 13 additions & 5 deletions pkg/backup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Package pkg /*
/*
Copyright © 2024 Jonas Kaninda
*/
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand All @@ -17,7 +19,7 @@ import (
)

func StartBackup(cmd *cobra.Command) {
_, _ = cmd.Flags().GetString("operation")
utils.Welcome()
//Set env
utils.SetEnv("STORAGE_PATH", storagePath)
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
Expand Down Expand Up @@ -193,6 +195,8 @@ func localBackup(db *dbConfig, backupFileName string, disableCompression bool, p
}
utils.Info("Backup name is %s", finalFileName)
moveToBackup(finalFileName, storagePath)
//Send notification
utils.NotifySuccess(finalFileName)
//Delete old backup
if prune {
deleteOldBackup(backupRetention)
Expand Down Expand Up @@ -234,6 +238,8 @@ func s3Backup(db *dbConfig, backupFileName string, disableCompression bool, prun
}
}
utils.Done("Uploading backup archive to remote storage S3 ... done ")
//Send notification
utils.NotifySuccess(finalFileName)
//Delete temp
deleteTemp()
}
Expand Down Expand Up @@ -267,6 +273,8 @@ func sshBackup(db *dbConfig, backupFileName, remotePath string, disableCompressi
}

utils.Done("Uploading backup archive to remote storage ... done ")
//Send notification
utils.NotifySuccess(finalFileName)
//Delete temp
deleteTemp()
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand Down
6 changes: 6 additions & 0 deletions pkg/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand Down
6 changes: 6 additions & 0 deletions pkg/helper.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand Down
7 changes: 7 additions & 0 deletions pkg/migrate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand All @@ -8,6 +14,7 @@ import (
)

func StartMigration(cmd *cobra.Command) {
utils.Welcome()
utils.Info("Starting database migration...")
//Get DB config
dbConf = getDbConfig(cmd)
Expand Down
8 changes: 7 additions & 1 deletion pkg/restore.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand All @@ -10,7 +16,7 @@ import (
)

func StartRestore(cmd *cobra.Command) {

utils.Welcome()
//Set env
utils.SetEnv("STORAGE_PATH", storagePath)

Expand Down
6 changes: 6 additions & 0 deletions pkg/scp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

import (
Expand Down
10 changes: 6 additions & 4 deletions pkg/scripts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

// Package pkg /*
/*
Copyright © 2024 Jonas Kaninda
*/
import (
"fmt"
"github.com/jkaninda/mysql-bkup/utils"
Expand Down
6 changes: 6 additions & 0 deletions pkg/var.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package pkg

const cronLogFile = "/var/log/mysql-bkup.log"
Expand Down
6 changes: 6 additions & 0 deletions utils/constant.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package utils /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package utils

const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
Expand Down
11 changes: 11 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package utils /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package utils

import (
Expand Down Expand Up @@ -49,8 +55,13 @@ func Fatal(msg string, args ...any) {
formattedMessage := fmt.Sprintf(msg, args...)
if len(args) == 0 {
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
NotifyError(msg)
} else {
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
NotifyError(formattedMessage)

}

os.Exit(1)
os.Kill.Signal()
}
75 changes: 75 additions & 0 deletions utils/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package utils

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)

func sendMessage(msg string) {

Info("Sending notification... ")
chatId := os.Getenv("TG_CHAT_ID")
body, _ := json.Marshal(map[string]string{
"chat_id": chatId,
"text": msg,
})
url := fmt.Sprintf("%s/sendMessage", getTgUrl())
// Create an HTTP post request
request, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
panic(err)
}
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
panic(err)
}
code := response.StatusCode
if code == 200 {
Info("Notification has been sent")
} else {
body, _ := ioutil.ReadAll(response.Body)
Error("Message not sent, error: %s", string(body))
}

}
func NotifySuccess(fileName string) {
var vars = []string{
"TG_TOKEN",
"TG_CHAT_ID",
}

//Telegram notification
err := CheckEnvVars(vars)
if err == nil {
message := "PostgreSQL Backup \n" +
"Database has been backed up \n" +
"Backup name is " + fileName
sendMessage(message)
}
}
func NotifyError(error string) {
var vars = []string{
"TG_TOKEN",
"TG_CHAT_ID",
}

//Telegram notification
err := CheckEnvVars(vars)
if err == nil {
message := "PostgreSQL Backup \n" +
"An error occurred during database backup \n" +
"Error: " + error
sendMessage(message)
}
}

func getTgUrl() string {
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TG_TOKEN"))

}
6 changes: 6 additions & 0 deletions utils/s3.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Package utils /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package utils

import (
Expand Down
Loading
Loading