forked from monobilisim/pgsql-backup
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganized sftp and added rotation to it
- Loading branch information
1 parent
2f2ce39
commit d2b85d8
Showing
9 changed files
with
179 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,128 @@ | ||
package backup | ||
|
||
import ( | ||
"monodb-backup/config" | ||
"monodb-backup/notify" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
"github.com/pkg/sftp" | ||
"golang.org/x/crypto/ssh" | ||
"golang.org/x/crypto/ssh/agent" | ||
) | ||
|
||
func SendSFTP(srcPath, dstPath, user, target, port string) error { | ||
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")) | ||
if err != nil { | ||
return err | ||
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 | ||
} | ||
|
||
sockAgent := agent.NewClient(sock) | ||
|
||
signers, err := sockAgent.Signers() | ||
if err != nil { | ||
return err | ||
logger.Error("Couldn't get signers for ssh keys - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't get signers for ssh keys - Error: "+err.Error(), true) | ||
return | ||
} | ||
auths := []ssh.AuthMethod{ssh.PublicKeys(signers...)} | ||
|
||
config := &ssh.ClientConfig{ | ||
User: user, | ||
User: target.User, | ||
Auth: auths, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
} | ||
|
||
client, _ := ssh.Dial("tcp", target+":"+port, config) | ||
client, _ := ssh.Dial("tcp", target.Host+":"+target.Port, config) | ||
defer func() { | ||
err = client.Close() | ||
if err != nil { | ||
// TODO | ||
logger.Error("Couldn't close SSH client - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't close SSH client - Error: "+err.Error(), true) | ||
} | ||
}() | ||
|
||
sftpCli, err := sftp.NewClient(client) | ||
if err != nil { | ||
return err | ||
logger.Error("Couldn't create an SFTP client - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create an SFTP client - Error: "+err.Error(), true) | ||
return | ||
} | ||
defer func() { | ||
err = sftpCli.Close() | ||
if err != nil { | ||
// TODO | ||
logger.Error("Couldn't close SFTP client - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't close SFTP client - Error: "+err.Error(), true) | ||
} | ||
}() | ||
|
||
src, err := os.Open(srcPath) | ||
if err != nil { | ||
return err | ||
logger.Error("Couldn't open source file " + srcPath + " for copying - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't open source file "+srcPath+" for copying - Error: "+err.Error(), true) | ||
return | ||
} | ||
defer func() { | ||
err = src.Close() | ||
if err != nil { | ||
// TODO | ||
logger.Error("Couldn't close source file: " + srcPath + " - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't close source file: "+srcPath+" - Error: "+err.Error(), true) | ||
} | ||
}() | ||
|
||
sendOverSFTp(srcPath, dstPath, src, target, sftpCli) | ||
|
||
if params.Rotation.Enabled { | ||
shouldRotate, newDst := rotate(db) | ||
if shouldRotate { | ||
extension := strings.Split(dstPath, ".") | ||
for i := 1; i < len(extension); i++ { | ||
newDst = newDst + "." + extension[i] | ||
} | ||
newDst = target.Path + "/" + newDst | ||
sendOverSFTp(srcPath, newDst, src, target, sftpCli) | ||
} | ||
} | ||
|
||
} | ||
|
||
func sendOverSFTp(srcPath, dstPath string, src *os.File, target config.Target, sftpCli *sftp.Client) { | ||
fullPath := strings.Split(dstPath, "/") | ||
newPath := "/" | ||
for i := 0; i < len(fullPath)-1; i++ { | ||
newPath = newPath + "/" + fullPath[i] | ||
} | ||
err := sftpCli.MkdirAll(newPath) | ||
if err != nil { | ||
logger.Error("Couldn't create folders " + newPath + " - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create folders "+newPath+" - Error: "+err.Error(), true) | ||
return | ||
} | ||
dst, err := sftpCli.Create(dstPath) | ||
if err != nil { | ||
return err | ||
logger.Error("Couldn't create file " + dstPath + " - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't create file "+dstPath+" - Error: "+err.Error(), true) | ||
return | ||
} | ||
defer func() { | ||
err = dst.Close() | ||
if err != nil { | ||
// TODO | ||
logger.Error("Couldn't close destination file: " + dstPath + " - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't close destination file: "+dstPath+" - Error: "+err.Error(), true) | ||
} | ||
}() | ||
logger.Info("Created destination file " + dstPath + " Now starting copying") | ||
|
||
if _, err := dst.ReadFrom(src); err != nil { | ||
return err | ||
logger.Error("Couldn't read from file " + srcPath + " to write at " + dstPath + " - Error: " + err.Error()) | ||
notify.SendAlarm("Couldn't upload backup "+srcPath+" to "+target.Host+":"+dstPath+"\nCouldn't read from file "+srcPath+" to write at "+dstPath+" - Error: "+err.Error(), true) | ||
return | ||
} | ||
return nil | ||
logger.Info("Successfully copied " + srcPath + " to " + target.Host + ":" + dstPath) | ||
notify.SendAlarm("Successfully copied "+srcPath+" to "+target.Host+":"+dstPath, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.