From 654eefee8a97a6a9d670b7beae6c0b4705ea8f3e Mon Sep 17 00:00:00 2001 From: Bekir Pehlivan Date: Tue, 9 Jul 2024 13:33:15 +0300 Subject: [PATCH] added the condition (params.BackupType.Type == "minio" || params.BackupType.Type == "s3") to streaming backups. Rsync backup: added some conditions to create folder. --- backup/backup.go | 4 ++-- backup/rsync.go | 51 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/backup/backup.go b/backup/backup.go index 8d0873a..9b35f02 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -96,7 +96,7 @@ func Backup() { params.Databases = tmpDatabases } - if streamable { + if streamable && (params.BackupType.Type == "minio" || params.BackupType.Type == "s3") { for _, db := range params.Databases { uploadWhileDumping(db) } @@ -213,7 +213,7 @@ func upload(name, db, filePath string) { } case "rsync": for _, target := range params.BackupType.Info[0].Targets { - err = SendRsync(filePath, name, target) + err = SendRsync(filePath, name, db, target) if err != nil { itWorksNow("", false) } diff --git a/backup/rsync.go b/backup/rsync.go index ff37611..8f09d39 100644 --- a/backup/rsync.go +++ b/backup/rsync.go @@ -8,7 +8,10 @@ import ( "strings" ) -func SendRsync(srcPath, dstPath string, target config.Target) error { +var lastDB string +var folderCreated bool + +func SendRsync(srcPath, dstPath, db string, target config.Target) error { var dst string if target.Path != "" { @@ -17,7 +20,7 @@ func SendRsync(srcPath, dstPath string, target config.Target) error { dst = nameWithPath(dstPath) } - err := sendRsync(srcPath, dst, target) + err := sendRsync(srcPath, dst, db, target) if err != nil { return err } @@ -32,7 +35,7 @@ func SendRsync(srcPath, dstPath string, target config.Target) error { dstPath = target.Path + "/" + dstPath } if shouldRotate { - err = sendRsync(srcPath, dstPath, target) + err = sendRsync(srcPath, dstPath, db, target) if err != nil { return err } @@ -41,7 +44,7 @@ func SendRsync(srcPath, dstPath string, target config.Target) error { return nil } -func sendRsync(srcPath, dstPath string, target config.Target) error { +func sendRsync(srcPath, dstPath, db string, target config.Target) error { var stderr1, stderr2, stdout bytes.Buffer logger.Info("rsync transfer started.\n Source: " + srcPath + " - Destination: " + target.Host + ":" + dstPath) @@ -52,23 +55,46 @@ func sendRsync(srcPath, dstPath string, target config.Target) error { newPath = newPath + "/" + fullPath[i] } - cmdMkdir := exec.Command("ssh", "-o", "HostKeyAlgorithms=+ssh-rsa", "-o", "PubKeyAcceptedKeyTypes=+ssh-rsa", target.Host, "mkdir -p "+newPath) - 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()) - return err + // We need folderCreated in case if the folder creation failed at the first table + // So even if the folder creation failed at the first table, it will be created this time + if lastDB != db && folderCreated { + folderCreated = false + } + + if !params.BackupAsTables { + cmdMkdir := exec.Command("ssh", "-o", "HostKeyAlgorithms=+ssh-rsa", "-o", "PubKeyAcceptedKeyTypes=+ssh-rsa", target.Host, "mkdir -p "+newPath) + 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()) + lastDB = db + return err + } + } else { + if lastDB != db && !folderCreated { + cmdMkdir := exec.Command("ssh", "-o", "HostKeyAlgorithms=+ssh-rsa", "-o", "PubKeyAcceptedKeyTypes=+ssh-rsa", target.Host, "mkdir -p "+newPath) + 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()) + lastDB = db + return err + } + folderCreated = true + } } cmdRsync := exec.Command("/usr/bin/rsync", target.Flags, "-e", "ssh -o HostKeyAlgorithms=+ssh-rsa -o PubKeyAcceptedKeyTypes=+ssh-rsa", srcPath, target.User+"@"+target.Host+":"+dstPath) cmdRsync.Stderr = &stderr2 cmdRsync.Stdout = &stdout - err = cmdRsync.Run() + 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()) + lastDB = db return err } @@ -77,5 +103,6 @@ func sendRsync(srcPath, dstPath string, target config.Target) error { notify.SendAlarm(message, false) itWorksNow(message, true) + lastDB = db return nil }