feat: 添加网元配置文件备份的FTP配置管理功能

This commit is contained in:
TsMask
2025-04-11 11:04:39 +08:00
parent 8910008e92
commit ad81d04db1
8 changed files with 258 additions and 54 deletions

View File

@@ -3,18 +3,15 @@ package exportTable
import (
"encoding/json"
"fmt"
"path"
"path/filepath"
"strings"
"time"
"be.ems/src/framework/config"
"be.ems/src/framework/cron"
"be.ems/src/framework/database/db"
"be.ems/src/framework/i18n"
"be.ems/src/framework/logger"
"be.ems/src/framework/ssh"
"be.ems/src/framework/utils/crypto"
"be.ems/src/framework/utils/date"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/parse"
@@ -927,17 +924,9 @@ func (s BarProcessor) putFTP(localFilePath string) {
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfig.FindByKey("sys.exportTable")
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
if cfg.ConfigId > 0 {
// 解密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 {
if err := json.Unmarshal([]byte(cfg.ConfigValue), &cfgData); err != nil {
logger.Errorf("putFTP unmarshal error: %v", err)
return
}
@@ -967,7 +956,7 @@ func (s BarProcessor) putFTP(localFilePath string) {
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(localFilePath), filepath.Base(localFilePath))
remotePath := filepath.Join(cfgData.Dir, "/backup", filepath.Base(localFilePath))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
logger.Errorf("putFTP uploading error: %v", err)

View File

@@ -1,18 +1,22 @@
package ne_config_backup
import (
"encoding/json"
"fmt"
"path/filepath"
"be.ems/src/framework/cron"
"be.ems/src/framework/logger"
"be.ems/src/framework/ssh"
neModel "be.ems/src/modules/network_element/model"
neService "be.ems/src/modules/network_element/service"
systemService "be.ems/src/modules/system/service"
)
var NewProcessor = &NeConfigBackupProcessor{
neConfigBackupService: neService.NewNeConfigBackup,
neInfoService: neService.NewNeInfo,
sysConfigService: systemService.NewSysConfig,
count: 0,
}
@@ -20,6 +24,7 @@ var NewProcessor = &NeConfigBackupProcessor{
type NeConfigBackupProcessor struct {
neConfigBackupService *neService.NeConfigBackup // 网元配置文件备份记录服务
neInfoService *neService.NeInfo // 网元信息服务
sysConfigService *systemService.SysConfig // 参数配置服务
count int // 执行次数
}
@@ -50,9 +55,65 @@ func (s *NeConfigBackupProcessor) Execute(data any) (any, error) {
Path: zipFilePath,
CreateBy: "system",
}
s.neConfigBackupService.Insert(item)
result[neTypeAndId] = "ok"
rows := s.neConfigBackupService.Insert(item)
if rows <= 0 {
result[neTypeAndId] = "failed"
continue
}
result[neTypeAndId] = "success"
s.putFTP(zipFilePath) // 上传到FTP服务器
}
return result, nil
}
// putFTP 提交到服务器ssh
func (s NeConfigBackupProcessor) putFTP(localFilePath 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"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfig.FindByKeyDecryptValue("neData.exportTableFTP")
if cfg.ConfigId > 0 {
if err := json.Unmarshal([]byte(cfg.ConfigValue), &cfgData); err != nil {
logger.Errorf("putFTP unmarshal error: %v", err)
return
}
}
if !cfgData.Enable {
return
}
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, "/ne_config", filepath.Base(localFilePath))
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
logger.Errorf("putFTP uploading error: %v", err)
return
}
}