Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Apr 26, 2024
1 parent 98fa782 commit b772b6b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
7 changes: 6 additions & 1 deletion backup/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"monodb-backup/notify"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -73,6 +74,7 @@ func uploadFileToMinio(src, dst, db string) {
notify.SendAlarm("Successfully uploaded file "+src+" to MinIO\nBucket: "+bucketName+" path: "+dst, false)

if params.Rotation.Enabled {
var oldDB string
if params.BackupAsTables {
var dbWithTable string
path := strings.Split(dst, "/")
Expand All @@ -84,11 +86,14 @@ func uploadFileToMinio(src, dst, db string) {
} else {
dbWithTable += path[0]
}

oldDB = db
db = dbWithTable
}
shouldRotate, name := rotate(db)
if shouldRotate {
if params.BackupAsTables {
name = filepath.Dir(name) + "/" + oldDB + "/" + filepath.Base(name)
}
source := minio.CopySrcOptions{
Bucket: bucketName,
Object: dst,
Expand Down
5 changes: 5 additions & 0 deletions backup/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ func mysqlDump(db, name, dst string, encrypted bool, mysqlArgs []string) (string
cmd2 = exec.Command("gzip")
cmd2.Stdin = stdout

if err := os.MkdirAll(filepath.Dir(dumpPath), 0770); err != nil {
logger.Error("Couldn't create parent direectories at backup destination. dst: " + dst + " - Error: " + err.Error())
return "", "", err
}

f, err := os.Create(dumpPath)
if err != nil {
logger.Error("Couldn't back up " + db + " - Error: " + err.Error())
Expand Down
10 changes: 0 additions & 10 deletions backup/rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
func SendRsync(srcPath, dstPath string, target config.Target) {
var dst string

// Construct the full destination path
if target.Path != "" {
dst = target.Path + "/" + nameWithPath(dstPath)
} else {
Expand All @@ -35,45 +34,36 @@ func SendRsync(srcPath, dstPath string, target config.Target) {
}

func sendRsync(srcPath, dstPath string, target config.Target) error {
// Create buffers to store stderr and stdout from commands
var stderr1, stderr2, stdout bytes.Buffer

// Log the start of the rsync transfer
logger.Info("rsync transfer started.\n Source: " + srcPath + " - Destination: " + target.Host + ":" + dstPath)

// Split the destination path to get the containing folder path
fullPath := strings.Split(dstPath, "/")
newPath := fullPath[0]
for i := 1; i < len(fullPath)-1; i++ {
newPath = newPath + "/" + fullPath[i]
}

// Create the destination folder on the remote host if it doesn't exist
cmdMkdir := exec.Command("ssh", "-o", "HostKeyAlgorithms=+ssh-rsa", "-o", "PubKeyAcceptedKeyTypes=+ssh-rsa", target.Host, "mkdir -p "+newPath)
err := cmdMkdir.Run()
if err != nil {
// If creating the folder fails, capture the error and send notifications
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
}

// Construct the rsync command to transfer the file
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

// Execute the rsync transfer
err = cmdRsync.Run()
if err != nil {
// If the transfer fails, capture the error and send notifications
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())
return err
}

// If the transfer is successful, log and send a notification
logger.Info("Successfully uploaded " + srcPath + " to " + target.Host + ":" + dstPath)
notify.SendAlarm("Successfully uploaded "+srcPath+" to "+target.Host+":"+dstPath, false)

Expand Down

0 comments on commit b772b6b

Please sign in to comment.