diff --git a/cmd/backup.go b/cmd/backup.go index 7f85cc2..ad19ba0 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -1,3 +1,9 @@ +// Package cmd / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package cmd import ( diff --git a/cmd/migrate.go b/cmd/migrate.go index cc8c1d4..0504f42 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -1,3 +1,9 @@ +// Package cmd / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package cmd import ( diff --git a/cmd/root.go b/cmd/root.go index 2380c2c..aa509da 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,9 @@ -// Package cmd /* -/* -Copyright © 2024 Jonas Kaninda -*/ +// Package cmd / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package cmd import ( diff --git a/cmd/version.go b/cmd/version.go index c1f19c1..ff9c06d 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,9 +1,11 @@ +// Package cmd / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package cmd -/* -Copyright © 2024 Jonas Kaninda -*/ - import ( "fmt" "github.com/spf13/cobra" diff --git a/docs/reference/index.md b/docs/reference/index.md index be21b22..efa683e 100644 --- a/docs/reference/index.md +++ b/docs/reference/index.md @@ -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 diff --git a/main.go b/main.go index c0f1b7b..0ad2398 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,11 @@ -package main - -//main +// Package main / /***** -* MySQL Backup & Restore -* @author Jonas Kaninda -* @license MIT License -* @link https://github.com/jkaninda/mysql-bkup +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda **/ +package main + import "github.com/jkaninda/mysql-bkup/cmd" func main() { diff --git a/pkg/backup.go b/pkg/backup.go index 9bba739..939fb84 100644 --- a/pkg/backup.go +++ b/pkg/backup.go @@ -1,7 +1,9 @@ -// Package pkg /* -/* -Copyright © 2024 Jonas Kaninda -*/ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( @@ -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") @@ -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) @@ -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() } @@ -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() } diff --git a/pkg/config.go b/pkg/config.go index 721a23a..af0f10f 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( diff --git a/pkg/encrypt.go b/pkg/encrypt.go index 01ee5ad..d096321 100644 --- a/pkg/encrypt.go +++ b/pkg/encrypt.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( diff --git a/pkg/helper.go b/pkg/helper.go index a2c00e3..a87b328 100644 --- a/pkg/helper.go +++ b/pkg/helper.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( diff --git a/pkg/migrate.go b/pkg/migrate.go index 26453e6..1d41b3c 100644 --- a/pkg/migrate.go +++ b/pkg/migrate.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( @@ -8,6 +14,7 @@ import ( ) func StartMigration(cmd *cobra.Command) { + utils.Welcome() utils.Info("Starting database migration...") //Get DB config dbConf = getDbConfig(cmd) diff --git a/pkg/restore.go b/pkg/restore.go index 6745579..e927c16 100644 --- a/pkg/restore.go +++ b/pkg/restore.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( @@ -10,7 +16,7 @@ import ( ) func StartRestore(cmd *cobra.Command) { - + utils.Welcome() //Set env utils.SetEnv("STORAGE_PATH", storagePath) diff --git a/pkg/scp.go b/pkg/scp.go index f2890f7..3833082 100644 --- a/pkg/scp.go +++ b/pkg/scp.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg import ( diff --git a/pkg/scripts.go b/pkg/scripts.go index 4e25a22..4390cf0 100644 --- a/pkg/scripts.go +++ b/pkg/scripts.go @@ -1,9 +1,11 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg -// Package pkg /* -/* -Copyright © 2024 Jonas Kaninda -*/ import ( "fmt" "github.com/jkaninda/mysql-bkup/utils" diff --git a/pkg/var.go b/pkg/var.go index b5f8bda..837c8ec 100644 --- a/pkg/var.go +++ b/pkg/var.go @@ -1,3 +1,9 @@ +// Package pkg / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package pkg const cronLogFile = "/var/log/mysql-bkup.log" diff --git a/utils/constant.go b/utils/constant.go index a7375ab..84421e5 100644 --- a/utils/constant.go +++ b/utils/constant.go @@ -1,3 +1,9 @@ +// Package utils / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package utils const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" + diff --git a/utils/logger.go b/utils/logger.go index 7059e3b..cd84a3e 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -1,3 +1,9 @@ +// Package utils / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package utils import ( @@ -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() } diff --git a/utils/notification.go b/utils/notification.go new file mode 100644 index 0000000..3b9591e --- /dev/null +++ b/utils/notification.go @@ -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")) + +} diff --git a/utils/s3.go b/utils/s3.go index a72c986..a6dfab6 100644 --- a/utils/s3.go +++ b/utils/s3.go @@ -1,3 +1,9 @@ +// Package utils / +/***** +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda +**/ package utils import ( diff --git a/utils/utils.go b/utils/utils.go index 6b839c4..9cd4fe5 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,17 +1,18 @@ -package utils - +// Package utils / /***** -* MySQL Backup & Restore -* @author Jonas Kaninda -* @license MIT License -* @link https://github.com/jkaninda/mysql-bkup +@author Jonas Kaninda +@license MIT License +@Copyright © 2024 Jonas Kaninda **/ +package utils + import ( "fmt" "github.com/spf13/cobra" "io" "io/fs" "os" + "strconv" ) func FileExists(filename string) bool { @@ -152,6 +153,13 @@ 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 { @@ -170,3 +178,14 @@ func MakeDirAll(dirPath string) error { } return nil } +func GetIntEnv(envName string) int { + val := os.Getenv(envName) + if val == "" { + return 0 + } + ret, err := strconv.Atoi(val) + if err != nil { + Error("Error: %v", err) + } + return ret +}