Skip to content

Commit

Permalink
added rsync
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Mar 21, 2024
1 parent 4cc69af commit 94a1ca9
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
5 changes: 5 additions & 0 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func Backup() {
SendSFTP(filePath, name, db, target)
}
}
if params.Rsync.Enabled {
for _, target := range params.Rsync.Targets {
SendRsync(filePath, name, db, target)
}
}
}
if params.RemoveLocal {
err = os.Remove(filePath)
Expand Down
41 changes: 41 additions & 0 deletions backup/rsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package backup

import (
"bytes"
"monodb-backup/config"
"monodb-backup/notify"
"os/exec"
"strings"
)

func SendRsync(srcPath, dstPath, db string, target config.Target) {
var stderr1 bytes.Buffer
var stderr2 bytes.Buffer
var stdout bytes.Buffer
logger.Info("rsync transfer started.\n Source: " + srcPath + " - Destination: " + target.Host + ":" + dstPath)
dstPath = target.Path + "/" + nameWithPath(dstPath)
fullPath := strings.Split(dstPath, "/")
newPath := "/"
for i := 0; i < len(fullPath)-1; i++ {
newPath = newPath + "/" + fullPath[i]
}
cmdMkdir := exec.Command("ssh", 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
}
cmdRsync := exec.Command("/usr/bin/rsync", target.Flags, srcPath, target.User+"@"+target.Host+":"+dstPath)
cmdRsync.Stderr = &stderr2
cmdRsync.Stdout = &stdout
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())
return
}
logger.Info("Successfully uploaded " + srcPath + " to " + target.Host + ":" + dstPath)
notify.SendAlarm("Successfully uploaded "+srcPath+" to "+target.Host+":"+dstPath, false)
}
15 changes: 3 additions & 12 deletions backup/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"monodb-backup/notify"
"net"
"os"
"os/user"
"strings"

"github.com/pkg/sftp"
Expand All @@ -16,18 +15,10 @@ import (
func SendSFTP(srcPath, dstPath, db string, target config.Target) {
dstPath = target.Path + "/" + nameWithPath(dstPath)
logger.Info("SFTP transfer started.\n Source: " + srcPath + " - Destination: " + target.Host + ":" + dstPath)

currentUser, err := user.Current()
if err != nil {
logger.Error("Couldn't get current user's id to get SSH socket" + err.Error())
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get current user's id to get SSH socket - Error: "+err.Error(), true)
return
}

sock, err := net.Dial("unix", "/run/user/"+currentUser.Uid+"/keyring/ssh")
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
logger.Error("Couldn't connect to SSH socket - Error: " + err.Error())
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't connect to SSH socket - Error: "+err.Error(), true)
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)
return
}

Expand Down
13 changes: 13 additions & 0 deletions config/config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ sftp:
host: ssh.example2.com
path: /var/backups
port: 22
rsync:
enabled: false
targets:
- user: username
flags: "-a"
host: ssh.example.com
path: /var/backups
port: 22
- user: username2
flags: "-a"
host: ssh.example2.com
path: /var/backups
port: 22
notify:
email:
enabled: false
Expand Down
15 changes: 9 additions & 6 deletions config/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Params struct {
Enabled bool
Targets []Target
}
Rsync struct {
Enabled bool
Targets []Target
}
Log LoggerParams
Fqdn string
}
Expand All @@ -59,10 +63,11 @@ type MinIO struct {
}

type Target struct {
User string
Host string
Port string
Path string
User string
Flags string
Host string
Port string
Path string
}

type S3FS struct {
Expand Down Expand Up @@ -141,6 +146,4 @@ func ParseParams(configFile *string) {
}

Parameters.Fqdn, _ = os.Hostname()

return
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kr/fs v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down

0 comments on commit 94a1ca9

Please sign in to comment.