Skip to content

Commit 0f07de1

Browse files
committed
feat: Add Telegram notification
1 parent a7f7e57 commit 0f07de1

20 files changed

+218
-32
lines changed

cmd/backup.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package cmd /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package cmd
28

39
import (

cmd/migrate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package cmd /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package cmd
28

39
import (

cmd/root.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Package cmd /*
2-
/*
3-
Copyright © 2024 Jonas Kaninda
4-
*/
1+
// Package cmd /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
57
package cmd
68

79
import (

cmd/version.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Package cmd /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package cmd
28

3-
/*
4-
Copyright © 2024 Jonas Kaninda
5-
*/
6-
79
import (
810
"fmt"
911
"github.com/spf13/cobra"

docs/reference/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ Backup, restore and migrate targets, schedule and retention are configured using
6262
| TARGET_DB_NAME | Optional, required for database migration | Target database name |
6363
| TARGET_DB_USERNAME | Optional, required for database migration | Target database username |
6464
| TARGET_DB_PASSWORD | Optional, required for database migration | Target database password |
65-
65+
| TG_TOKEN | Optional, required for Telegram notification | Telegram token |
66+
| TG_CHAT_ID | Optional, required for Telegram notification | Telegram Chat ID |
6667
---
6768
## Run in Scheduled mode
6869

main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package main
2-
3-
//main
1+
// Package main /
42
/*****
5-
* MySQL Backup & Restore
6-
* @author Jonas Kaninda
7-
* @license MIT License <https://opensource.org/licenses/MIT>
8-
* @link https://github.com/jkaninda/mysql-bkup
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
96
**/
7+
package main
8+
109
import "github.com/jkaninda/mysql-bkup/cmd"
1110

1211
func main() {

pkg/backup.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Package pkg /*
2-
/*
3-
Copyright © 2024 Jonas Kaninda
4-
*/
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
57
package pkg
68

79
import (
@@ -17,7 +19,7 @@ import (
1719
)
1820

1921
func StartBackup(cmd *cobra.Command) {
20-
_, _ = cmd.Flags().GetString("operation")
22+
utils.Welcome()
2123
//Set env
2224
utils.SetEnv("STORAGE_PATH", storagePath)
2325
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
@@ -193,6 +195,8 @@ func localBackup(db *dbConfig, backupFileName string, disableCompression bool, p
193195
}
194196
utils.Info("Backup name is %s", finalFileName)
195197
moveToBackup(finalFileName, storagePath)
198+
//Send notification
199+
utils.NotifySuccess(finalFileName)
196200
//Delete old backup
197201
if prune {
198202
deleteOldBackup(backupRetention)
@@ -234,6 +238,8 @@ func s3Backup(db *dbConfig, backupFileName string, disableCompression bool, prun
234238
}
235239
}
236240
utils.Done("Uploading backup archive to remote storage S3 ... done ")
241+
//Send notification
242+
utils.NotifySuccess(finalFileName)
237243
//Delete temp
238244
deleteTemp()
239245
}
@@ -267,6 +273,8 @@ func sshBackup(db *dbConfig, backupFileName, remotePath string, disableCompressi
267273
}
268274

269275
utils.Done("Uploading backup archive to remote storage ... done ")
276+
//Send notification
277+
utils.NotifySuccess(finalFileName)
270278
//Delete temp
271279
deleteTemp()
272280
}

pkg/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (

pkg/encrypt.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (

pkg/helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (

pkg/migrate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (
@@ -8,6 +14,7 @@ import (
814
)
915

1016
func StartMigration(cmd *cobra.Command) {
17+
utils.Welcome()
1118
utils.Info("Starting database migration...")
1219
//Get DB config
1320
dbConf = getDbConfig(cmd)

pkg/restore.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (
@@ -10,7 +16,7 @@ import (
1016
)
1117

1218
func StartRestore(cmd *cobra.Command) {
13-
19+
utils.Welcome()
1420
//Set env
1521
utils.SetEnv("STORAGE_PATH", storagePath)
1622

pkg/scp.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
import (

pkg/scripts.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

3-
// Package pkg /*
4-
/*
5-
Copyright © 2024 Jonas Kaninda
6-
*/
79
import (
810
"fmt"
911
"github.com/jkaninda/mysql-bkup/utils"

pkg/var.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package pkg /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package pkg
28

39
const cronLogFile = "/var/log/mysql-bkup.log"

utils/constant.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package utils /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package utils
28

39
const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +

utils/logger.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package utils /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package utils
28

39
import (
@@ -49,8 +55,13 @@ func Fatal(msg string, args ...any) {
4955
formattedMessage := fmt.Sprintf(msg, args...)
5056
if len(args) == 0 {
5157
fmt.Printf("%s ERROR: %s\n", currentTime, msg)
58+
NotifyError(msg)
5259
} else {
5360
fmt.Printf("%s ERROR: %s\n", currentTime, formattedMessage)
61+
NotifyError(formattedMessage)
62+
5463
}
64+
5565
os.Exit(1)
66+
os.Kill.Signal()
5667
}

utils/notification.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
10+
)
11+
12+
func sendMessage(msg string) {
13+
14+
Info("Sending notification... ")
15+
chatId := os.Getenv("TG_CHAT_ID")
16+
body, _ := json.Marshal(map[string]string{
17+
"chat_id": chatId,
18+
"text": msg,
19+
})
20+
url := fmt.Sprintf("%s/sendMessage", getTgUrl())
21+
// Create an HTTP post request
22+
request, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
23+
if err != nil {
24+
panic(err)
25+
}
26+
request.Header.Add("Content-Type", "application/json")
27+
client := &http.Client{}
28+
response, err := client.Do(request)
29+
if err != nil {
30+
panic(err)
31+
}
32+
code := response.StatusCode
33+
if code == 200 {
34+
Info("Notification has been sent")
35+
} else {
36+
body, _ := ioutil.ReadAll(response.Body)
37+
Error("Message not sent, error: %s", string(body))
38+
}
39+
40+
}
41+
func NotifySuccess(fileName string) {
42+
var vars = []string{
43+
"TG_TOKEN",
44+
"TG_CHAT_ID",
45+
}
46+
47+
//Telegram notification
48+
err := CheckEnvVars(vars)
49+
if err == nil {
50+
message := "PostgreSQL Backup \n" +
51+
"Database has been backed up \n" +
52+
"Backup name is " + fileName
53+
sendMessage(message)
54+
}
55+
}
56+
func NotifyError(error string) {
57+
var vars = []string{
58+
"TG_TOKEN",
59+
"TG_CHAT_ID",
60+
}
61+
62+
//Telegram notification
63+
err := CheckEnvVars(vars)
64+
if err == nil {
65+
message := "PostgreSQL Backup \n" +
66+
"An error occurred during database backup \n" +
67+
"Error: " + error
68+
sendMessage(message)
69+
}
70+
}
71+
72+
func getTgUrl() string {
73+
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TG_TOKEN"))
74+
75+
}

utils/s3.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Package utils /
2+
/*****
3+
@author Jonas Kaninda
4+
@license MIT License <https://opensource.org/licenses/MIT>
5+
@Copyright © 2024 Jonas Kaninda
6+
**/
17
package utils
28

39
import (

0 commit comments

Comments
 (0)