feat: 调度任务-日志备份文件同步FTP功能
This commit is contained in:
@@ -6,11 +6,19 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"be.ems/lib/dborm"
|
"be.ems/lib/dborm"
|
||||||
"be.ems/lib/log"
|
"be.ems/lib/log"
|
||||||
|
"be.ems/src/framework/config"
|
||||||
"be.ems/src/framework/cron"
|
"be.ems/src/framework/cron"
|
||||||
|
"be.ems/src/framework/logger"
|
||||||
|
"be.ems/src/framework/utils/crypto"
|
||||||
|
"be.ems/src/framework/utils/ssh"
|
||||||
|
systemService "be.ems/src/modules/system/service"
|
||||||
|
"github.com/jlaffaye/ftp"
|
||||||
)
|
)
|
||||||
|
|
||||||
var NewProcessor = &BarProcessor{
|
var NewProcessor = &BarProcessor{
|
||||||
@@ -94,6 +102,9 @@ func (s *BarProcessor) Execute(data any) (any, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// put ftp
|
||||||
|
s.putFTP(params.FilePath, filepath.Base(filePath))
|
||||||
|
|
||||||
// 返回结果,用于记录执行结果
|
// 返回结果,用于记录执行结果
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"msg": "sucess",
|
"msg": "sucess",
|
||||||
@@ -158,3 +169,94 @@ func (s *BarProcessor) exportData(query, filePath string) (int64, error) {
|
|||||||
|
|
||||||
return affected, nil
|
return affected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s BarProcessor) putFTP(filePath, fileName string) {
|
||||||
|
// 获取配置
|
||||||
|
var cfgData struct {
|
||||||
|
Password string `json:"password" `
|
||||||
|
Username string `json:"username" binding:"required"`
|
||||||
|
ToIp string `json:"toIp" binding:"required"`
|
||||||
|
ToPort int64 `json:"toPort" binding:"required"`
|
||||||
|
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
|
||||||
|
Dir string `json:"dir" binding:"required"`
|
||||||
|
}
|
||||||
|
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
|
||||||
|
if cfg.ConfigID != "" {
|
||||||
|
// 解密body
|
||||||
|
appKey := config.Get("aes.appKey").(string)
|
||||||
|
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP decrypt error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal([]byte(bodyDe), &cfgData)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP unmarshal error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
localFilePath := filepath.Join(filePath, fileName)
|
||||||
|
|
||||||
|
if cfgData.Protocol == "ssh" {
|
||||||
|
connSSH := ssh.ConnSSH{
|
||||||
|
User: cfgData.Username,
|
||||||
|
Password: cfgData.Password,
|
||||||
|
Addr: cfgData.ToIp,
|
||||||
|
Port: cfgData.ToPort,
|
||||||
|
AuthMode: "0",
|
||||||
|
}
|
||||||
|
sshClient, err := connSSH.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP ssh error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sshClient.Close()
|
||||||
|
// 网元主机的SSH客户端进行文件传输
|
||||||
|
sftpClient, err := sshClient.NewClientSFTP()
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP sftp error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sftpClient.Close()
|
||||||
|
// 远程文件
|
||||||
|
remotePath := filepath.Join(cfgData.Dir, path.Base(filePath), fileName)
|
||||||
|
// 复制到远程
|
||||||
|
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
|
||||||
|
logger.Errorf("putFTP uploading error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfgData.Protocol == "ftp" {
|
||||||
|
// 连接到 FTP 服务器
|
||||||
|
addr := fmt.Sprintf("%s:%d", cfgData.ToIp, cfgData.ToPort)
|
||||||
|
ftpComm, err := ftp.Dial(addr, ftp.DialWithTimeout(15*time.Second))
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP ftp error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 登录到 FTP 服务器
|
||||||
|
err = ftpComm.Login(cfgData.Username, cfgData.Password)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP login error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ftpComm.Quit()
|
||||||
|
// 打开本地文件
|
||||||
|
file, err := os.Open(localFilePath)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP open error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
// 远程文件
|
||||||
|
remotePath := filepath.Join(cfgData.Dir, path.Base(filePath), fileName)
|
||||||
|
// 上传文件到 FTP 服务器
|
||||||
|
err = ftpComm.Stor(remotePath, file)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("putFTP uploading error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user