chore: fix infinity calling Fatal, add a backup reference

This commit is contained in:
Jonas Kaninda
2024-10-10 04:14:42 +02:00
parent df490af7b6
commit e5dd7e76ce
9 changed files with 133 additions and 59 deletions

View File

@@ -12,20 +12,23 @@ type MailConfig struct {
SkipTls bool
}
type NotificationData struct {
File string
BackupSize int64
Database string
StartTime string
EndTime string
Storage string
BackupLocation string
File string
BackupSize int64
Database string
StartTime string
EndTime string
Storage string
BackupLocation string
BackupReference string
}
type ErrorMessage struct {
Database string
EndTime string
Error string
Database string
EndTime string
Error string
BackupReference string
}
// loadMailConfig gets mail environment variables and returns MailConfig
func loadMailConfig() *MailConfig {
return &MailConfig{
MailHost: os.Getenv("MAIL_HOST"),
@@ -39,4 +42,18 @@ func loadMailConfig() *MailConfig {
}
// TimeFormat returns the format of the time
func TimeFormat() string {
format := os.Getenv("TIME_FORMAT")
if format == "" {
return "2006-01-02 at 15:04:05"
}
return format
}
func backupReference() string {
return os.Getenv("BACKUP_REFERENCE")
}
const templatePath = "/config/templates"

View File

@@ -6,9 +6,9 @@
**/
package utils
const RestoreExample = "mysql-bkup restore --dbname database --file db_20231219_022941.sql.gz\n" +
const RestoreExample = "restore --dbname database --file db_20231219_022941.sql.gz\n" +
"restore --dbname database --storage s3 --path /custom-path --file db_20231219_022941.sql.gz"
const BackupExample = "mysql-bkup backup --dbname database --disable-compression\n" +
const BackupExample = "backup --dbname database --disable-compression\n" +
"backup --dbname database --storage s3 --path /custom-path --disable-compression"
const MainExample = "mysql-bkup backup --dbname database --disable-compression\n" +

View File

@@ -31,8 +31,8 @@ func parseTemplate[T any](data T, fileName string) (string, error) {
return buf.String(), nil
}
func SendEmail(subject, body string) {
Info("Start sending email....")
func SendEmail(subject, body string) error {
Info("Start sending email notification....")
config := loadMailConfig()
emails := strings.Split(config.MailTo, ",")
m := mail.NewMessage()
@@ -44,14 +44,16 @@ func SendEmail(subject, body string) {
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.SkipTls}
if err := d.DialAndSend(m); err != nil {
Fatal("Error could not send email : %v", err)
Error("Error could not send email : %v", err)
return err
}
Info("Email has been sent")
Info("Email notification has been sent")
return nil
}
func sendMessage(msg string) {
func sendMessage(msg string) error {
Info("Sending notification... ")
Info("Sending Telegram notification... ")
chatId := os.Getenv("TG_CHAT_ID")
body, _ := json.Marshal(map[string]string{
"chat_id": chatId,
@@ -67,18 +69,21 @@ func sendMessage(msg string) {
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
panic(err)
return err
}
code := response.StatusCode
if code == 200 {
Info("Notification has been sent")
Info("Telegram notification has been sent")
return nil
} else {
body, _ := ioutil.ReadAll(response.Body)
Fatal("Error could not send message, error: %s", string(body))
Error("Error could not send message, error: %s", string(body))
return fmt.Errorf("error could not send message %s", string(body))
}
}
func NotifySuccess(notificationData *NotificationData) {
notificationData.BackupReference = backupReference()
var vars = []string{
"TG_TOKEN",
"TG_CHAT_ID",
@@ -99,17 +104,23 @@ func NotifySuccess(notificationData *NotificationData) {
if err != nil {
Error("Could not parse email template: %v", err)
}
SendEmail(fmt.Sprintf("✅ Database Backup Notification %s", notificationData.Database), body)
err = SendEmail(fmt.Sprintf("✅ Database Backup Notification %s", notificationData.Database), body)
if err != nil {
Error("Could not send email: %v", err)
}
}
//Telegram notification
err = CheckEnvVars(vars)
if err == nil {
message, err := parseTemplate(*notificationData, "telegram.template")
if err != nil {
Error("Could not parse email template: %v", err)
Error("Could not parse telegram template: %v", err)
}
sendMessage(message)
err = sendMessage(message)
if err != nil {
Error("Could not send Telegram message: %v", err)
}
}
}
func NotifyError(error string) {
@@ -130,26 +141,35 @@ func NotifyError(error string) {
err := CheckEnvVars(mailVars)
if err == nil {
body, err := parseTemplate(ErrorMessage{
Error: error,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
Error: error,
EndTime: time.Now().Format(TimeFormat()),
BackupReference: os.Getenv("BACKUP_REFERENCE"),
}, "email-error.template")
if err != nil {
Error("Could not parse email template: %v", err)
Error("Could not parse error template: %v", err)
}
err = SendEmail(fmt.Sprintf("🔴 Urgent: Database Backup Failure Notification"), body)
if err != nil {
Error("Could not send email: %v", err)
}
SendEmail(fmt.Sprintf("🔴 Urgent: Database Backup Failure Notification"), body)
}
//Telegram notification
err = CheckEnvVars(vars)
if err == nil {
message, err := parseTemplate(ErrorMessage{
Error: error,
EndTime: time.Now().Format("2006-01-02 15:04:05"),
Error: error,
EndTime: time.Now().Format(TimeFormat()),
BackupReference: os.Getenv("BACKUP_REFERENCE"),
}, "telegram-error.template")
if err != nil {
Error("Could not parse email template: %v", err)
Error("Could not parse error template: %v", err)
}
sendMessage(message)
err = sendMessage(message)
if err != nil {
Error("Could not send telegram message: %v", err)
}
}
}