Merge pull request #81 from jkaninda/docs

refactor: add Telegram env in Dockerfile, move telegram notification …
This commit is contained in:
Jonas Kaninda
2024-09-11 04:37:50 +02:00
committed by GitHub
6 changed files with 80 additions and 85 deletions

View File

@@ -38,6 +38,8 @@ ENV TARGET_DB_PASSWORD=""
ARG DEBIAN_FRONTEND=noninteractive
ENV VERSION="v1.2.5"
ENV BACKUP_CRON_EXPRESSION=""
ENV TG_TOKEN=""
ENV TG_CHAT_ID=""
ARG WORKDIR="/config"
ARG BACKUPDIR="/backup"
ARG BACKUP_TMP_DIR="/tmp/backup"

View File

@@ -19,7 +19,7 @@ import (
)
func StartBackup(cmd *cobra.Command) {
utils.Welcome()
intro()
//Set env
utils.SetEnv("STORAGE_PATH", storagePath)
utils.GetEnv(cmd, "period", "BACKUP_CRON_EXPRESSION")
@@ -116,6 +116,13 @@ func scheduledMode(db *dbConfig, storage string) {
fmt.Println(line.Text)
}
}
func intro() {
fmt.Println()
fmt.Println("**********************************")
fmt.Println(" MySQL Backup ")
fmt.Println(" @Copyright © 2024 jkaninda ")
fmt.Println("***********************************")
}
// BackupDatabase backup database
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {

View File

@@ -14,7 +14,7 @@ import (
)
func StartMigration(cmd *cobra.Command) {
utils.Welcome()
intro()
utils.Info("Starting database migration...")
//Get DB config
dbConf = getDbConfig(cmd)

View File

@@ -16,7 +16,7 @@ import (
)
func StartRestore(cmd *cobra.Command) {
utils.Welcome()
intro()
//Set env
utils.SetEnv("STORAGE_PATH", storagePath)

View File

@@ -1,75 +0,0 @@
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"))
}

View File

@@ -7,10 +7,14 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"io"
"io/fs"
"io/ioutil"
"net/http"
"os"
"strconv"
)
@@ -153,13 +157,6 @@ func CheckEnvVars(vars []string) error {
return nil
}
func Welcome() {
fmt.Println()
fmt.Println("**********************************")
fmt.Println(" MySQL Backup ")
fmt.Println(" @Copyright © 2024 jkaninda ")
fmt.Println("***********************************")
}
// MakeDir create directory
func MakeDir(dirPath string) error {
@@ -189,3 +186,67 @@ func GetIntEnv(envName string) int {
}
return ret
}
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"))
}