From c77ab3a4f44740bfed713af41055ebf5277452be Mon Sep 17 00:00:00 2001 From: Bekir Pehlivan Date: Wed, 17 Jul 2024 15:45:36 +0300 Subject: [PATCH] Changed the way I send webhook alarms --- backup/backup.go | 69 +++++++++++++++++++++++------------------ backup/mssql_linux.go | 8 ++--- backup/mssql_windows.go | 3 +- backup/mysql.go | 16 +++++++--- backup/postgresql.go | 3 +- backup/rsync.go | 45 +++++++++++++-------------- backup/s3.go | 21 +++++++------ backup/sftp.go | 33 ++++++++++---------- main.go | 3 +- notify/webhook.go | 19 ++++++++++++ 10 files changed, 126 insertions(+), 94 deletions(-) diff --git a/backup/backup.go b/backup/backup.go index 9b35f02..b10d83f 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -10,16 +10,16 @@ import ( "time" ) -var didItWork bool = true // this variable is for determining if the app should send a notification after a failed backup to inform that it works now -func itWorksNow(message string, worked bool) { - oldOnlyOnError := params.Notify.Webhook.OnlyOnError - if !didItWork && worked && oldOnlyOnError { - params.Notify.Webhook.OnlyOnError = false - notify.SendAlarm(message, false) - params.Notify.Webhook.OnlyOnError = oldOnlyOnError - } - didItWork = worked -} +// var didItWork bool = true // this variable is for determining if the app should send a notification after a failed backup to inform that it works now +// func tWorksNow(message string, worked bool) { +// oldOnlyOnError := params.Notify.Webhook.OnlyOnError +// if !didItWork && worked && oldOnlyOnError { +// params.Notify.Webhook.OnlyOnError = false +// // notify.SendAlarm(message, false) +// params.Notify.Webhook.OnlyOnError = oldOnlyOnError +// } +// didItWork = worked +// } func getDBList() (dbList []string) { switch params.Database { @@ -65,7 +65,7 @@ func dumpDB(db string, dst string) (string, string, error) { func Backup() { logger.Info("monodb-backup job started.") - notify.SendAlarm("Database backup started.", false) + // notify.SendAlarm("Database backup started.", false) streamable := (params.Database == "" || params.Database == "postgresql" || (params.Database == "mysql" && !params.BackupAsTables)) && params.ArchivePass == "" dateNow = rightNow{ @@ -107,11 +107,11 @@ func Backup() { if params.BackupAsTables && db != "mysql" { dumpPaths, names, err := dumpDBWithTables(db, dst) if err != nil { - notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) - itWorksNow("", false) + // notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) + // itWorksNow("", false) } else { logger.Info("Successfully backed up database:" + db + " with its tables separately, at " + params.BackupDestination + "/" + db) - notify.SendAlarm("Successfully backed up "+db+" at "+params.BackupDestination+"/"+db, false) + // notify.SendAlarm("Successfully backed up "+db+" at "+params.BackupDestination+"/"+db, false) for i, filePath := range dumpPaths { name := names[i] upload(name, db, filePath) @@ -121,11 +121,13 @@ func Backup() { } else { filePath, name, err := dumpDB(db, dst) if err != nil { - notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) - itWorksNow("", false) + // notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) + notify.FailedDBList = append(notify.FailedDBList, db+" - Error: "+err.Error()) + // itWorksNow("", false) } else { logger.Info("Successfully backed up database:" + db + " at " + filePath) - notify.SendAlarm("Successfully backed up "+db+" at "+filePath, false) + // notify.SendAlarm("Successfully backed up "+db+" at "+filePath, false) + notify.SuccessfulDBList = append(notify.SuccessfulDBList, db) upload(name, db, filePath) } @@ -175,8 +177,8 @@ func uploadWhileDumping(db string) { err := dumpAndUpload(db, pipeWriters) if err != nil { cancel() - notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) - itWorksNow("", false) + // notify.SendAlarm("Problem during backing up "+db+" - Error: "+err.Error(), true) + // itWorksNow("", false) } for _, writer := range pipeWriters { writer.Close() @@ -185,13 +187,15 @@ func uploadWhileDumping(db string) { uploadErr := <-channel if uploadErr != nil { logger.Error(strconv.Itoa(i+1) + ") " + db + " - " + "Couldn't upload to S3: " + uploaders[i].instance.Endpoint + " - Error: " + uploadErr.Error()) - notify.SendAlarm(strconv.Itoa(i+1)+") "+db+" - "+"Couldn't upload to S3: "+uploaders[i].instance.Endpoint+" - Error: "+uploadErr.Error(), true) - itWorksNow("", false) + // notify.SendAlarm(strconv.Itoa(i+1)+") "+db+" - "+"Couldn't upload to S3: "+uploaders[i].instance.Endpoint+" - Error: "+uploadErr.Error(), true) + notify.FailedDBList = append(notify.FailedDBList, db+" to "+uploaders[i].instance.Endpoint+" - Error: "+uploadErr.Error()) + // itWorksNow("", false) } else { - logger.Info(strconv.Itoa(i+1) + ") " + db + " - " + "Successfully uploaded to S3: " + uploaders[i].instance.Endpoint) message := strconv.Itoa(i+1) + ") " + db + " - " + "Successfully uploaded to S3: " + uploaders[i].instance.Endpoint - notify.SendAlarm(message, false) - itWorksNow(message, true) + logger.Info(message) + // notify.SendAlarm(message, false) + notify.SuccessfulDBList = append(notify.SuccessfulDBList, db+" to "+uploaders[i].instance.Endpoint) + // itWorksNow(message, true) } } } @@ -200,22 +204,25 @@ func upload(name, db, filePath string) { var err error switch params.BackupType.Type { case "s3", "minio": - err = uploadToS3(filePath, name, db) - if err != nil { - itWorksNow("", false) - } + uploadToS3(filePath, name, db) case "sftp": for _, target := range params.BackupType.Info[0].Targets { err = SendSFTP(filePath, name, db, target) if err != nil { - itWorksNow("", false) + // itWorksNow("", false) + notify.FailedDBList = append(notify.FailedDBList, db+" - "+name+" - Error: "+err.Error()) + } else { + notify.SuccessfulDBList = append(notify.SuccessfulDBList, db+" - "+name) } } case "rsync": for _, target := range params.BackupType.Info[0].Targets { - err = SendRsync(filePath, name, db, target) + message, err := SendRsync(filePath, name, db, target) if err != nil { - itWorksNow("", false) + // itWorksNow("", false) + notify.FailedDBList = append(notify.FailedDBList, db+" - "+message) + } else { + notify.SuccessfulDBList = append(notify.SuccessfulDBList, db+" - "+name) } } } diff --git a/backup/mssql_linux.go b/backup/mssql_linux.go index 0ea7ffb..149bd04 100644 --- a/backup/mssql_linux.go +++ b/backup/mssql_linux.go @@ -6,12 +6,12 @@ import ( "context" "database/sql" "fmt" - _ "github.com/denisenkom/go-mssqldb" "log" - "monodb-backup/notify" "os" "path/filepath" "strconv" + + _ "github.com/denisenkom/go-mssqldb" ) var mssqlDB *sql.DB @@ -20,13 +20,13 @@ func InitializeMSSQL() { var host, port, user, password string var err error - if params.Remote.IsRemote == true { + if params.Remote.IsRemote { host = params.Remote.Host port = params.Remote.Port user = params.Remote.User password = params.Remote.Password } else { - notify.SendAlarm("Remote should be enabled when backing up MSSQL databases.", true) + // notify.SendAlarm("Remote should be enabled when backing up MSSQL databases.", true) logger.Fatal("Remote should be enabled when backing up MSSQL databases.") return } diff --git a/backup/mssql_windows.go b/backup/mssql_windows.go index 49f528e..cf3c8d9 100644 --- a/backup/mssql_windows.go +++ b/backup/mssql_windows.go @@ -7,7 +7,6 @@ import ( "database/sql" "fmt" "log" - "monodb-backup/notify" "os" "path/filepath" "strconv" @@ -30,7 +29,7 @@ func InitializeMSSQL() { user = params.Remote.User password = params.Remote.Password } else { - notify.SendAlarm("Remote should be enabled when backing up MSSQL databases.", true) + // notify.SendAlarm("Remote should be enabled when backing up MSSQL databases.", true) logger.Fatal("Remote should be enabled when backing up MSSQL databases.") return } diff --git a/backup/mysql.go b/backup/mysql.go index 0c21c77..c137a27 100644 --- a/backup/mysql.go +++ b/backup/mysql.go @@ -23,7 +23,7 @@ func getMySQLList() []string { cmd := exec.Command("/usr/bin/mysql", mysqlArgs...) out, err := cmd.Output() if err != nil { - notify.SendAlarm("Couldn't get the list of databases - Error: "+string(out), true) + // notify.SendAlarm("Couldn't get the list of databases - Error: "+string(out), true) logger.Fatal("Couldn't get the list of databases - Error: " + string(out)) return nil } @@ -59,7 +59,7 @@ func getMySQLList() []string { // cmd.Stdout = &stdout // err := cmd.Run() // if err != nil { -// notify.SendAlarm("Couldn't get the list of databases - Error: "+stdout.String()+"\n"+stderr.String()+"\n"+err.Error(), true) +// // notify.SendAlarm("Couldn't get the list of databases - Error: "+stdout.String()+"\n"+stderr.String()+"\n"+err.Error(), true) // logger.Fatal("Couldn't get the list of databases - Error: " + stdout.String() + "\n" + stderr.String() + "\n" + err.Error()) // return make([]string, 0) // } @@ -215,12 +215,16 @@ func dumpDBWithTables(db, dst string) ([]string, []string, error) { var dumpPaths, names []string oldDst := dst if err := os.MkdirAll(dst, 0770); err != nil { - logger.Error("Couldn't create parent direectories at backup destination. dst: " + dst + " - Error: " + err.Error()) + message := "Couldn't create parent direectories at backup destination. dst: " + dst + " - Error: " + err.Error() + logger.Error(message) + notify.FailedDBList = append(notify.FailedDBList, db+" - "+message) return make([]string, 0), make([]string, 0), err } tableList, metaFile, err := getTableList(db, dst) if err != nil { - logger.Error("Couldn't get the list of tables. Error: " + err.Error()) + message := "Couldn't get the list of tables. Error: " + err.Error() + logger.Error(message) + notify.FailedDBList = append(notify.FailedDBList, db+" - "+message) return nil, nil, err } dumpPaths = append(dumpPaths, metaFile) @@ -231,7 +235,9 @@ func dumpDBWithTables(db, dst string) ([]string, []string, error) { } filePath, name, err := dumpTable(db, table, oldDst) //TODO + if err != nil { - logger.Error("Couldn't dump databases. Error: " + err.Error()) + message := "Couldn't dump databases. Error: " + err.Error() + logger.Error(message) + notify.FailedDBList = append(notify.FailedDBList, db+" - table: "+table+" - "+message) return nil, nil, err } dumpPaths = append(dumpPaths, filePath) diff --git a/backup/postgresql.go b/backup/postgresql.go index 5f0ed08..0b8ab96 100644 --- a/backup/postgresql.go +++ b/backup/postgresql.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "monodb-backup/config" - "monodb-backup/notify" "os" "os/exec" "path/filepath" @@ -30,7 +29,7 @@ func getPSQLList() []string { cmd.Stderr = &stderr out, err := cmd.Output() if err != nil { - notify.SendAlarm("Couldn't get the list of databases - Error: "+string(out), true) + // notify.SendAlarm("Couldn't get the list of databases - Error: "+string(out), true) logger.Fatal("Couldn't get the list of databases - Error: " + string(out) + "\nError: " + stderr.String()) return nil } diff --git a/backup/rsync.go b/backup/rsync.go index 8f09d39..f4f009b 100644 --- a/backup/rsync.go +++ b/backup/rsync.go @@ -3,7 +3,6 @@ package backup import ( "bytes" "monodb-backup/config" - "monodb-backup/notify" "os/exec" "strings" ) @@ -11,7 +10,7 @@ import ( var lastDB string var folderCreated bool -func SendRsync(srcPath, dstPath, db string, target config.Target) error { +func SendRsync(srcPath, dstPath, db string, target config.Target) (string, error) { var dst string if target.Path != "" { @@ -20,9 +19,9 @@ func SendRsync(srcPath, dstPath, db string, target config.Target) error { dst = nameWithPath(dstPath) } - err := sendRsync(srcPath, dst, db, target) + message, err := sendRsync(srcPath, dst, db, target) if err != nil { - return err + return message, err } if params.Rotation.Enabled { @@ -35,16 +34,13 @@ func SendRsync(srcPath, dstPath, db string, target config.Target) error { dstPath = target.Path + "/" + dstPath } if shouldRotate { - err = sendRsync(srcPath, dstPath, db, target) - if err != nil { - return err - } + return sendRsync(srcPath, dstPath, db, target) } } - return nil + return "", nil } -func sendRsync(srcPath, dstPath, db string, target config.Target) error { +func sendRsync(srcPath, dstPath, db string, target config.Target) (string, error) { var stderr1, stderr2, stdout bytes.Buffer logger.Info("rsync transfer started.\n Source: " + srcPath + " - Destination: " + target.Host + ":" + dstPath) @@ -66,10 +62,11 @@ func sendRsync(srcPath, dstPath, db string, target config.Target) error { err := cmdMkdir.Run() if err != nil { cmdMkdir.Stderr = &stderr1 - notify.SendAlarm("Couldn't create folder "+newPath+" to upload backups at"+target.Host+":"+dstPath+"\nError: "+err.Error()+" "+stderr1.String(), true) - logger.Error("Couldn't create folder " + newPath + " to upload backups at" + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr1.String()) + message := "Couldn't create folder " + newPath + " to upload backups at" + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr1.String() + // notify.SendAlarm(message, true) + logger.Error(message) lastDB = db - return err + return message, err } } else { if lastDB != db && !folderCreated { @@ -77,10 +74,11 @@ func sendRsync(srcPath, dstPath, db string, target config.Target) error { err := cmdMkdir.Run() if err != nil { cmdMkdir.Stderr = &stderr1 - notify.SendAlarm("Couldn't create folder "+newPath+" to upload backups at"+target.Host+":"+dstPath+"\nError: "+err.Error()+" "+stderr1.String(), true) - logger.Error("Couldn't create folder " + newPath + " to upload backups at" + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr1.String()) + message := "Couldn't create folder " + newPath + " to upload backups at" + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr1.String() + // notify.SendAlarm(message, true) + logger.Error(message) lastDB = db - return err + return message, err } folderCreated = true } @@ -92,17 +90,18 @@ func sendRsync(srcPath, dstPath, db string, target config.Target) error { err := cmdRsync.Run() if err != nil { - notify.SendAlarm("Couldn't send "+srcPath+" to "+target.Host+":"+dstPath+"\nError: "+err.Error()+" "+stderr2.String()+" Stdout: "+stdout.String(), true) - logger.Error("Couldn't send " + srcPath + " to " + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr2.String() + " Stdout: " + stdout.String()) + message := "Couldn't send " + srcPath + " to " + target.Host + ":" + dstPath + "\nError: " + err.Error() + " " + stderr2.String() + " Stdout: " + stdout.String() + // notify.SendAlarm(message, true) + logger.Error(message) lastDB = db - return err + return message, err } - logger.Info("Successfully uploaded " + srcPath + " to " + target.Host + ":" + dstPath) message := "Successfully uploaded " + srcPath + " to " + target.Host + ":" + dstPath - notify.SendAlarm(message, false) - itWorksNow(message, true) + logger.Info(message) + // notify.SendAlarm(message, false) + // itWorksNow(message, true) lastDB = db - return nil + return "", nil } diff --git a/backup/s3.go b/backup/s3.go index f64b637..a737a9e 100644 --- a/backup/s3.go +++ b/backup/s3.go @@ -96,7 +96,7 @@ func InitializeS3Session() { } sess, err := session.NewSessionWithOptions(options) if err != nil { - notify.SendAlarm("Couldn't initialize S3 session. Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't initialize S3 session. Error: "+err.Error(), true) logger.Fatal(err) return } @@ -111,7 +111,7 @@ func uploadFileToS3(ctx context.Context, src, dst, db string, reader io.Reader, file, err := os.Open(src) if err != nil { logger.Error("Couldn't open file " + src + " to read - Error: " + err.Error()) - notify.SendAlarm("Couldn't open file "+src+" to read - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't open file "+src+" to read - Error: "+err.Error(), true) return err } defer file.Close() @@ -128,13 +128,13 @@ func uploadFileToS3(ctx context.Context, src, dst, db string, reader io.Reader, }) if err != nil { logger.Error("Couldn't upload " + src + " to S3\nBucket: " + bucketName + " path: " + dst + "\n Error: " + err.Error()) - notify.SendAlarm("Couldn't upload "+src+" to S3\nBucket: "+bucketName+" path: "+dst+"\n Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload "+src+" to S3\nBucket: "+bucketName+" path: "+dst+"\n Error: "+err.Error(), true) return err } - logger.Info("Successfully uploaded " + src + " to S3\nBucket: " + bucketName + " path: " + dst) message := "Successfully uploaded " + src + " to S3\nBucket: " + bucketName + " path: " + dst - notify.SendAlarm(message, false) - itWorksNow(message, true) + logger.Info(message) + // notify.SendAlarm(message, false) + // itWorksNow(message, true) if params.Rotation.Enabled { if db == "mysql" { db = db + "_users" @@ -155,11 +155,11 @@ func uploadFileToS3(ctx context.Context, src, dst, db string, reader io.Reader, }) if err != nil { logger.Error("Couldn't create copy of " + src + " for rotation\nBucket: " + bucketName + " path: " + name + "\n Error: " + err.Error()) - notify.SendAlarm("Couldn't create copy of "+src+" for rotation\nBucket: "+bucketName+" path: "+name+"\n Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't create copy of "+src+" for rotation\nBucket: "+bucketName+" path: "+name+"\n Error: "+err.Error(), true) return err } logger.Info("Successfully created a copy of " + src + " for rotation\nBucket: " + bucketName + " path: " + name) - notify.SendAlarm("Successfully created a copy of "+src+" for rotation\nBucket: "+bucketName+" path: "+name, false) + // notify.SendAlarm("Successfully created a copy of "+src+" for rotation\nBucket: "+bucketName+" path: "+name, false) } } return nil @@ -174,7 +174,10 @@ func uploadToS3(src, dst, db string) error { } err := uploadFileToS3(ctx, src, dst, db, nil, &s3Instance) if err != nil { - return err + notify.FailedDBList = append(notify.FailedDBList, db+" - "+src+" - "+err.Error()) + // itWorksNow("", false) + } else { + notify.SuccessfulDBList = append(notify.SuccessfulDBList, db+" - "+src) } } return nil diff --git a/backup/sftp.go b/backup/sftp.go index 1e6c49e..af96b0b 100644 --- a/backup/sftp.go +++ b/backup/sftp.go @@ -2,7 +2,6 @@ package backup import ( "monodb-backup/config" - "monodb-backup/notify" "net" "os" "strings" @@ -18,7 +17,7 @@ func SendSFTP(srcPath, dstPath, db string, target config.Target) error { sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) if err != nil { logger.Error("Couldn't get environment variable SSH_AUTH_SOCK - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get environment variable SSH_AUTH_SOCK - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get environment variable SSH_AUTH_SOCK - Error: "+err.Error(), true) return err } @@ -27,51 +26,51 @@ func SendSFTP(srcPath, dstPath, db string, target config.Target) error { signers, err := sockAgent.Signers() if err != nil { logger.Error("Couldn't get signers for ssh keys - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get signers for ssh keys - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get signers for ssh keys - Error: "+err.Error(), true) return err } auths := []ssh.AuthMethod{ssh.PublicKeys(signers...)} - config := &ssh.ClientConfig{ + sshConfig := &ssh.ClientConfig{ User: target.User, Auth: auths, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } - client, _ := ssh.Dial("tcp", target.Host+":"+target.Port, config) + client, _ := ssh.Dial("tcp", target.Host+":"+target.Port, sshConfig) defer func() { err = client.Close() if err != nil { logger.Error("Couldn't close SSH client - Error: " + err.Error()) - notify.SendAlarm("Couldn't close SSH client - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't close SSH client - Error: "+err.Error(), true) } }() sftpCli, err := sftp.NewClient(client) if err != nil { logger.Error("Couldn't create an SFTP client - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create an SFTP client - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create an SFTP client - Error: "+err.Error(), true) return err } defer func() { err = sftpCli.Close() if err != nil { logger.Error("Couldn't close SFTP client - Error: " + err.Error()) - notify.SendAlarm("Couldn't close SFTP client - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't close SFTP client - Error: "+err.Error(), true) } }() src, err := os.Open(srcPath) if err != nil { logger.Error("Couldn't open source file " + srcPath + " for copying - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't open source file "+srcPath+" for copying - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't open source file "+srcPath+" for copying - Error: "+err.Error(), true) return err } defer func() { err = src.Close() if err != nil { logger.Error("Couldn't close source file: " + srcPath + " - Error: " + err.Error()) - notify.SendAlarm("Couldn't close source file: "+srcPath+" - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't close source file: "+srcPath+" - Error: "+err.Error(), true) } }() @@ -106,32 +105,32 @@ func sendOverSFTP(srcPath, dstPath string, src *os.File, target config.Target, s err := sftpCli.MkdirAll(newPath) if err != nil { logger.Error("Couldn't create folders " + newPath + " - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create folders "+newPath+" - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create folders "+newPath+" - Error: "+err.Error(), true) return err } dst, err := sftpCli.Create(dstPath) if err != nil { logger.Error("Couldn't create file " + dstPath + " - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create file "+dstPath+" - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create file "+dstPath+" - Error: "+err.Error(), true) return err } defer func() { err = dst.Close() if err != nil { logger.Error("Couldn't close destination file: " + dstPath + " - Error: " + err.Error()) - notify.SendAlarm("Couldn't close destination file: "+dstPath+" - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't close destination file: "+dstPath+" - Error: "+err.Error(), true) } }() logger.Info("Created destination file " + dstPath + " Now starting copying") if _, err := dst.ReadFrom(src); err != nil { logger.Error("Couldn't read from file " + srcPath + " to write at " + dstPath + " - Error: " + err.Error()) - notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't read from file "+srcPath+" to write at "+dstPath+" - Error: "+err.Error(), true) + // notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't read from file "+srcPath+" to write at "+dstPath+" - Error: "+err.Error(), true) return err } - logger.Info("Successfully copied " + srcPath + " to " + target.Host + ":" + dstPath) message := "Successfully copied " + srcPath + " to " + target.Host + ":" + dstPath - notify.SendAlarm(message, false) - itWorksNow(message, true) + logger.Info(message) + // notify.SendAlarm(message, false) + // itWorksNow(message, true) return nil } diff --git a/main.go b/main.go index 6a9babb..3e0f736 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func main() { initBackup() } logger.Info("monodb-backup job finished.") - notify.SendAlarm("monodb-backup job finished.", false) + // notify.SendAlarm("monodb-backup job finished.", false) } func initBackup() { @@ -58,4 +58,5 @@ func initBackup() { backup.InitializeS3Session() } backup.Backup() + notify.SendSingleEntityAlarm() } diff --git a/notify/webhook.go b/notify/webhook.go index a802500..00f8e92 100644 --- a/notify/webhook.go +++ b/notify/webhook.go @@ -6,11 +6,30 @@ import ( "monodb-backup/clog" "monodb-backup/config" "net/http" + "strings" ) var webhookStruct *config.Webhook = &config.Parameters.Notify.Webhook var logger *clog.CustomLogger = &clog.Logger +var FailedDBList []string +var SuccessfulDBList []string + +func SendSingleEntityAlarm() { + if !webhookStruct.Enabled { + return + } + + if len(FailedDBList) != 0 { + SendAlarm("Failed to backup the following databases:\n- "+strings.Join(FailedDBList, "\n- "), true) + webhookStruct.OnlyOnError = false + } + if len(SuccessfulDBList) != 0 { + SendAlarm("Successfully backed up the following databases:\n- "+strings.Join(SuccessfulDBList, "\n- "), false) + } + return +} + func SendAlarm(message string, isError bool) { if !webhookStruct.Enabled || (webhookStruct.OnlyOnError && !isError) { return