diff --git a/backup/sftp.go b/backup/sftp.go index cfd7273..d849704 100644 --- a/backup/sftp.go +++ b/backup/sftp.go @@ -5,6 +5,7 @@ import ( "monodb-backup/notify" "net" "os" + "os/user" "strings" "github.com/pkg/sftp" @@ -15,10 +16,18 @@ 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) - sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) + + 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") 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) + 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) return }