Skip to content

Commit

Permalink
added the condition (params.BackupType.Type == "minio" || params.Back…
Browse files Browse the repository at this point in the history
…upType.Type == "s3") to streaming backups.

Rsync backup: added some conditions to create folder.
  • Loading branch information
LobbyLobster committed Jul 9, 2024
1 parent acb1eee commit 654eefe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
51 changes: 39 additions & 12 deletions backup/rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -77,5 +103,6 @@ func sendRsync(srcPath, dstPath string, target config.Target) error {
notify.SendAlarm(message, false)
itWorksNow(message, true)

lastDB = db
return nil
}

0 comments on commit 654eefe

Please sign in to comment.