Skip to content

Commit

Permalink
feat: Add Telegram notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaninda committed Sep 10, 2024
1 parent 90fd464 commit 13ea619
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 32 deletions.
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
6 changes: 6 additions & 0 deletions cmd/restore.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
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ENV TARGET_DB_NAME=""
ENV TARGET_DB_USERNAME=""
ENV TARGET_DB_PASSWORD=""
ARG DEBIAN_FRONTEND=noninteractive
ENV VERSION="v1.2.4"
ENV VERSION="v1.2.5"
ENV BACKUP_CRON_EXPRESSION=""
ARG WORKDIR="/config"
ARG BACKUPDIR="/backup"
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +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/pg-bkup
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
package main

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

func main() {
Expand Down
17 changes: 13 additions & 4 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,6 +19,7 @@ import (
)

func StartBackup(cmd *cobra.Command) {
utils.Welcome()
//Set env
utils.SetEnv("STORAGE_PATH", storagePath)
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
Expand Down Expand Up @@ -198,6 +201,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 @@ -240,6 +245,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 @@ -273,6 +280,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
10 changes: 10 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 All @@ -23,6 +29,10 @@ type targetDbConfig struct {
targetDbPassword string
targetDbName string
}
type TgConfig struct {
Token string
ChatId string
}

func getDbConfig(cmd *cobra.Command) *dbConfig {
//Set env
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
8 changes: 7 additions & 1 deletion 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 Expand Up @@ -125,7 +131,7 @@ func testDatabaseConnection(db *dbConfig) {
// Run the command and capture any errors
err = cmd.Run()
if err != nil {
utils.Error("Error running psql command: %v\nOutput: %s\n", err, out.String())
utils.Fatal("Error running psql command: %v\nOutput: %s\n", err, out.String())
return
}
utils.Info("Successfully connected to %s database", db.dbName)
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

// Package pkg /*
/*
Copyright © 2024 Jonas Kaninda
*/
// Package pkg /
/*****
@author Jonas Kaninda
@license MIT License <https://opensource.org/licenses/MIT>
@Copyright © 2024 Jonas Kaninda
**/
import (
"fmt"
"github.com/jkaninda/pg-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/pg-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 = "pg-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
Expand Down
10 changes: 10 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 @@ -51,9 +57,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

0 comments on commit 13ea619

Please sign in to comment.