feat: 网元配置文件备份文件到本地打zip压缩包

This commit is contained in:
TsMask
2024-07-23 16:57:31 +08:00
parent d09edf9960
commit 831b2e2eac
2 changed files with 51 additions and 0 deletions

View File

@@ -21,4 +21,7 @@ type INeConfigBackup interface {
// DeleteByIds 批量删除信息
DeleteByIds(ids []string) (int64, error)
// NeConfigFileToLocal 网元备份文件到本地
NeConfigFileToLocal(neInfo model.NeInfo) (string, error)
}

View File

@@ -2,7 +2,13 @@ package service
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"be.ems/src/framework/utils/date"
"be.ems/src/framework/utils/file"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
)
@@ -65,3 +71,45 @@ func (r *NeConfigBackupImpl) DeleteByIds(ids []string) (int64, error) {
// 删除信息失败!
return 0, fmt.Errorf("delete fail")
}
// NeConfigFileToLocal 网元备份文件到本地
func (r *NeConfigBackupImpl) NeConfigFileToLocal(neInfo model.NeInfo) (string, error) {
// 网元主机的SSH客户端
sshClient, err := NewNeInfoImpl.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", fmt.Errorf("ne info ssh client err")
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
return "", fmt.Errorf("ne info sftp client err")
}
defer sftpClient.Close()
neTypeLower := strings.ToLower(neInfo.NeType)
// 网管本地路径
omcPath := "/usr/local/etc/omc/ne_config"
if runtime.GOOS == "windows" {
omcPath = fmt.Sprintf("C:%s", omcPath)
}
localDirPath := fmt.Sprintf("%s/%s/%s/backup/tmp", omcPath, neTypeLower, neInfo.NeId)
// 网元配置文件先复制到临时目录目录
nePath := fmt.Sprintf("/usr/local/etc/%s/*.yaml", neTypeLower)
neDirTemp := fmt.Sprintf("/tmp/omc/ne_config/%s/%s", neTypeLower, neInfo.NeId)
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo cp -rf %s %s", neDirTemp, nePath, neDirTemp))
// 复制到本地
if err = sftpClient.CopyDirRemoteToLocal(neDirTemp, localDirPath); err != nil {
return "", fmt.Errorf("copy config err")
}
// 压缩zip文件名
zipFileName := fmt.Sprintf("%s-%s-etc-%s.zip", neTypeLower, neInfo.NeId, date.ParseDateToStr(time.Now(), date.YYYYMMDDHHMMSS))
zipFilePath := fmt.Sprintf("%s/%s/%s/backup/%s", omcPath, neTypeLower, neInfo.NeId, zipFileName)
if err := file.CompressZipByDir(zipFilePath, localDirPath); err != nil {
return "", fmt.Errorf("compress zip err")
}
_ = os.RemoveAll(localDirPath) // 删除本地临时目录
sshClient.RunCMD(fmt.Sprintf("sudo rm -rf %s", neDirTemp)) // 删除临时目录
return zipFilePath, nil
}