package com.product.system.backup.service;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.FileWriter;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.nio.charset.StandardCharsets;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Properties;
|
import java.util.stream.Collectors;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipOutputStream;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.alibaba.druid.util.StringUtils;
|
import com.jcraft.jsch.Channel;
|
import com.jcraft.jsch.ChannelSftp;
|
import com.jcraft.jsch.JSch;
|
import com.jcraft.jsch.JSchException;
|
import com.jcraft.jsch.Session;
|
import com.jcraft.jsch.SftpException;
|
import com.jcraft.jsch.SftpProgressMonitor;
|
import com.product.core.config.Global;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.system.backup.entity.BackupLogger;
|
|
@Service("systemBackService")
|
public class SystemBackupService2 extends AbstractBaseService{
|
|
@Autowired
|
BaseDao baseDao;
|
|
// 备份配置
|
Properties config;
|
|
// 备份日志
|
BackupLogger log;
|
|
// 数字时间和数字日期(用做文件夹或者文件名)
|
String NUMBER_TIME; //例如(20250428091001)
|
String NUMBER_DATE; //例如(20240428)
|
String STANDARD_START_TIME; // 例如(2024-04-28 09:10:00)
|
String STANDARD_FINAL_TIME; // 例如(2024-04-28 09:10:00)
|
|
// 数字日期格式
|
SimpleDateFormat numberTimeFormat = new SimpleDateFormat("yyyyMMddHHmm00");
|
SimpleDateFormat numberDateFormat = new SimpleDateFormat("yyyyMMdd");
|
SimpleDateFormat standardTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
|
|
// 备份配置文件
|
private static final String CONFIG_FILE_PATH = "systemBackup.properties";
|
|
/**
|
* 系统备份判定
|
* 调用地点:定时任务配置功能(bean)
|
*/
|
public void systemBackupInit() {
|
// 获取系统时间
|
Date finalTime = new Date();
|
Date startTime = null;
|
// 获取系统备份周期(分钟)
|
int backupCycle = Integer.valueOf(Global.getSystemConfig("SYSTEM_BACK_CYCLE", "20"));
|
// 获取系统最大备份时间
|
StringBuilder sbSql = new StringBuilder();
|
sbSql.append("SELECT TIMESTAMPDIFF(MINUTE, end_time, ?) AS diff_minutes,end_time \n");
|
sbSql.append("FROM product_sys_backup_log \n");
|
sbSql.append("WHERE backup_status = 1 \n");
|
sbSql.append("ORDER BY end_time DESC \n");
|
sbSql.append("LIMIT 1 \n");
|
FieldSetEntity fseMaxLogTime = baseDao.getFieldSetEntityBySQL(sbSql.toString(), new Object[] {standardTimeFormat.format(finalTime)}, false);
|
if(fseMaxLogTime != null && !StringUtils.isEmpty(fseMaxLogTime.getString("diff_minutes"))) {
|
Integer diffMinutes = fseMaxLogTime.getInteger("diff_minutes");
|
if (diffMinutes < backupCycle) {
|
// 小于备份周期,不备份
|
return ;
|
}
|
startTime = fseMaxLogTime.getDate("end_time");
|
}
|
try {
|
// 执行备份
|
systemBackup(startTime, finalTime);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
FieldSetEntity fseBackLog = new FieldSetEntity("product_sys_backup_log");
|
fseBackLog.setValue("start_time", startTime);
|
fseBackLog.setValue("end_time", finalTime);
|
fseBackLog.setValue("backup_status", 1);
|
baseDao.saveFieldSetEntity(fseBackLog);
|
}
|
|
/**
|
* 系统备份入口
|
*
|
*/
|
public void systemBackup(Date startTime, Date finalTime){
|
|
// 初始日志文件
|
log = new BackupLogger();
|
// 在日志中记录操作系统信息,便于调试
|
log.writeInfo("【系统备份入口】操作系统: " + System.getProperty("os.name"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【系统备份入口】文件分隔符: " + File.separator, BackupLogger.INFO_TYPE);
|
log.writeInfo("【系统备份入口】系统备份开始..................", BackupLogger.INFO_TYPE);
|
NUMBER_TIME = numberTimeFormat.format(finalTime);
|
NUMBER_DATE = numberDateFormat.format(finalTime);
|
if(startTime == null) {
|
STANDARD_START_TIME = "2000-01-01 00:00:00";
|
} else {
|
STANDARD_START_TIME = standardTimeFormat.format(startTime);
|
}
|
STANDARD_FINAL_TIME = standardTimeFormat.format(finalTime);
|
log.writeInfo("【系统备份入口】系统日期:" + NUMBER_DATE, BackupLogger.INFO_TYPE);
|
log.writeInfo("【系统备份入口】系统时间:" + NUMBER_TIME, BackupLogger.INFO_TYPE);
|
log.writeInfo("【系统本分入口】系统备份开始时间:" + STANDARD_START_TIME, BackupLogger.INFO_TYPE);
|
log.writeInfo("【系统备份入口】系统备份截止时间:" + STANDARD_FINAL_TIME, BackupLogger.INFO_TYPE);
|
// 进入备份进程
|
backupProcess();
|
log.closeLogger();
|
}
|
|
/**
|
* 数据备份进程
|
*/
|
public void backupProcess() {
|
log.writeInfo("【系统备份进程】系统备份进程开始..................", BackupLogger.INFO_TYPE);
|
long start = System.currentTimeMillis();
|
boolean status= true;
|
// 第一步:初始配文件参数,当前目生成时间戳的文件夹
|
status = initSystemConfig();
|
if (!status)
|
return;
|
// 第二步:执行数据库备份
|
status = runExpDataBase();
|
if (!status)
|
return;
|
// 第三步:执行对数据库文件、工程代码、上传文件做压缩备份
|
status = zipDataBackup();
|
if (!status)
|
return;
|
// 第四步:上传压缩备份文件到FTP
|
status = sftpTransferService();
|
if (!status)
|
return;
|
// 第五步:清除数据
|
// clearKeepData();
|
|
long end = System.currentTimeMillis();
|
log.writeInfo("【系统备份进程】系统备份进程结束,耗时.................." + ((end - start) / 1000) + "秒", BackupLogger.INFO_TYPE);
|
System.out.println("系统备份耗时.................." + ((end - start) / 1000) + "秒");
|
}
|
|
/**
|
* 第一步:初始配置文件参数,当前目录生成时间戳的文件夹
|
*/
|
public boolean initSystemConfig() {
|
log.writeInfo("【初始配置文件】开始初始系统配置文件..................", BackupLogger.INFO_TYPE);
|
try (InputStream reader = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE_PATH)) {
|
if (reader == null) {
|
log.writeInfo("【初始配置文件】初始系统配置文件失败:" + CONFIG_FILE_PATH + "配置文件不存在", BackupLogger.ERROR_TYPE);
|
}
|
// 读取备份配置文件
|
config = new Properties();
|
config.load(reader);
|
// 获取系统文件路径
|
String systemFileFolder = Global.getSystemConfig("local.dir", "");
|
File newFile= new File(systemFileFolder);
|
// 系统附件存放目录
|
config.setProperty("DOCUMENT_ROOT", newFile.getAbsolutePath() + File.separator + "00000000-0000-0000-0000-000000000000");
|
// 数据库备份目录(数据库备份根目录+时间文件名+.sql)
|
config.setProperty("DATABASE_BACKUP", config.getProperty("DATABASE_ROOT") + File.separator + NUMBER_TIME + ".sql");
|
// 备份目标文件(ZIP备份根目录+时间文件名+.zip)
|
config.setProperty("ZIPFILE_BACKUP", config.getProperty("ZIPFILE_ROOT") + File.separator + NUMBER_TIME + ".zip");
|
// 创建ZIP文件根目录
|
File zipFileRoot = new File(config.getProperty("ZIPFILE_ROOT"));
|
if (!zipFileRoot.exists()) {
|
if (!zipFileRoot.mkdirs()) {
|
log.writeInfo("【初始配置文件】无法创建目录: " + zipFileRoot.getAbsolutePath(), BackupLogger.ERROR_TYPE);
|
return false;
|
}
|
}
|
log.writeInfo("【初始配置文件】DATABASE_ROOT=" + config.getProperty("DATABASE_ROOT"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】DATABASE_BACKUP=" + config.getProperty("DATABASE_BACKUP"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】DOCUMENT_ROOT=" + config.getProperty("DOCUMENT_ROOT"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】DOCUMENT_BACKUP=" + config.getProperty("DOCUMENT_BACKUP"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】ZIPFILE_ROOT=" + config.getProperty("ZIPFILE_ROOT"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】ZIPFILE_BACKUP=" + config.getProperty("ZIPFILE_BACKUP"), BackupLogger.INFO_TYPE);
|
log.writeInfo("【初始配置文件】初始系统配置文件结束.................", BackupLogger.INFO_TYPE);
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.writeInfo("【初始配置文件】系统初始配置文件失败," + new File("config.properties").getAbsolutePath() + "," + e.getMessage(),
|
BackupLogger.ERROR_TYPE);
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
* 第二步:备份数据库文件
|
*/
|
public boolean runExpDataBase() {
|
log.writeInfo("【备份数据库】开始备份数据库..................", BackupLogger.INFO_TYPE);
|
String databaseHost = config.getProperty("DATABASE_HOST");
|
String databasePort = config.getProperty("DATABASE_PORT");
|
String databaseName = config.getProperty("DATABASE_NAME");
|
String databaseUser = config.getProperty("DATABASE_USER");
|
String databasePwd = config.getProperty("DATABASE_PWD");
|
|
log.writeInfo("【备份数据库】数据库HOST:" + databaseHost, BackupLogger.INFO_TYPE);
|
log.writeInfo("【备份数据库】数据库PORT:" + databasePort, BackupLogger.INFO_TYPE);
|
|
File backupFile = new File(config.getProperty("DATABASE_BACKUP"));
|
|
// 确保备份文件的目录存在
|
if (!backupFile.getParentFile().exists()) {
|
backupFile.getParentFile().mkdirs();
|
}
|
log.writeInfo("【备份数据库】数据库备份文件完整目录:" + backupFile.getAbsolutePath(), BackupLogger.INFO_TYPE);
|
|
ProcessBuilder processBuilder = new ProcessBuilder(
|
"mysqldump",
|
"-h" + databaseHost,
|
"-P" + databasePort,
|
"-u" + databaseUser,
|
"-p" + databasePwd,
|
databaseName
|
);
|
|
try {
|
Process process = processBuilder.start();
|
// 读取mysqldump的输出并写入到备份文件
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
|
FileWriter writer = new FileWriter(backupFile)) {
|
|
String line;
|
while ((line = reader.readLine()) != null) {
|
writer.write(line + System.lineSeparator());
|
}
|
}
|
|
// 读取错误流并记录日志
|
try (BufferedReader errorReader = new BufferedReader(
|
new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {
|
String errorLine;
|
while ((errorLine = errorReader.readLine()) != null) {
|
log.writeInfo("【备份数据库】错误输出: " + errorLine, BackupLogger.ERROR_TYPE);
|
}
|
}
|
|
int exitCode = process.waitFor();
|
if (exitCode == 0) {
|
log.writeInfo("【备份数据库】数据库备份成功:" + backupFile.getAbsolutePath(), BackupLogger.INFO_TYPE);
|
// if (!"127.0.0.1".equals(databaseHost)) {
|
// 获取数据库备份文件
|
// return sftpTransferService(false);
|
// }
|
} else {
|
log.writeInfo("【备份数据库】数据库备份失败,退出码: " + exitCode, BackupLogger.ERROR_TYPE);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.writeInfo("【备份数据库】数据库备份失败:" + e.getMessage(), BackupLogger.ERROR_TYPE);
|
return false;
|
}
|
return true;
|
}
|
|
private static void addFileToZip(File file, String entryName, ZipOutputStream zos) throws IOException {
|
try (FileInputStream fis = new FileInputStream(file)) {
|
zos.putNextEntry(new ZipEntry(entryName));
|
byte[] buffer = new byte[1024];
|
int length;
|
while ((length = fis.read(buffer)) >= 0) {
|
zos.write(buffer, 0, length);
|
}
|
zos.closeEntry();
|
}
|
}
|
|
/**
|
* 压缩增量附件
|
* @param groupFolderFile 增量文件夹,文件名
|
* @param baseDir 附件根目录
|
* @param outputZipPath 压缩文件
|
* @throws IOException
|
*/
|
public static void compressExistingFiles(Map<Integer, List<String>> groupFolderFile, String baseDir, ZipOutputStream zos) throws IOException {
|
for (Map.Entry<Integer, List<String>> entry : groupFolderFile.entrySet()) {
|
int folderName = entry.getKey(); // 文件夹名(如 20250611)
|
List<String> files = entry.getValue(); // 该文件夹下的文件名列表
|
Path folderPath = Paths.get(baseDir).resolve(String.valueOf(folderName));
|
for (String fileName : files) {
|
Path filePath = folderPath.resolve(fileName);
|
// 检查文件是否存在
|
if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
|
// 计算 ZIP 内的相对路径(如 "20250611/file1.txt")
|
ZipEntry zipEntry = new ZipEntry(
|
String.valueOf(folderName) + "/" + fileName
|
);
|
zos.putNextEntry(zipEntry);
|
|
// 写入文件内容到 ZIP
|
Files.copy(filePath, zos);
|
zos.closeEntry();
|
} else {
|
System.err.println("文件不存在或不是文件: " + filePath);
|
}
|
}
|
}
|
}
|
|
public boolean zipDataBackup() {
|
log.writeInfo("【压缩备份文件】开始压缩备份文件..................", BackupLogger.INFO_TYPE);
|
String documentPath = config.getProperty("DOCUMENT_ROOT");
|
String databasePath = config.getProperty("DATABASE_BACKUP");
|
String zipFilePath = config.getProperty("ZIPFILE_BACKUP");
|
log.writeInfo("【压缩备份文件】附件存储目录路径:" + documentPath, BackupLogger.INFO_TYPE);
|
log.writeInfo("【压缩备份文件】数据库备份完整路径:" + databasePath, BackupLogger.INFO_TYPE);
|
log.writeInfo("【压缩备份文件】备份压缩文件完整路径:" + zipFilePath, BackupLogger.INFO_TYPE);
|
|
try (FileOutputStream fos = new FileOutputStream(zipFilePath);
|
ZipOutputStream zos = new ZipOutputStream(fos)){
|
StringBuilder sbSql = new StringBuilder();
|
sbSql.append("SELECT SUBSTRING_INDEX(attachment_url, '/', -1) id,attachment_title AS uuid,attachment_url \n");
|
sbSql.append("FROM product_sys_attachments \n");
|
sbSql.append("WHERE (created_utc_datetime > ? AND created_utc_datetime <= ?) \n");
|
sbSql.append("AND (updated_utc_datetime > ? AND updated_utc_datetime <= ?) \n");
|
DataTableEntity dtTable = baseDao.listTable(sbSql.toString(), new Object[] {STANDARD_START_TIME, STANDARD_FINAL_TIME, STANDARD_START_TIME, STANDARD_FINAL_TIME});
|
if(dtTable != null && dtTable.getRows() > 0) {
|
Map<Integer, List<String>> groupFolderFile = dtTable.getData().stream()
|
.collect(Collectors.groupingBy(
|
FieldSetEntity::getId,
|
Collectors.mapping(
|
FieldSetEntity::getUUID,
|
Collectors.toList()
|
)
|
));
|
log.writeInfo("【压缩备份文件】压缩系统增量附件", BackupLogger.INFO_TYPE);
|
compressExistingFiles(groupFolderFile, documentPath, zos);
|
}
|
|
log.writeInfo("【压缩备份文件】压缩数据库备份文件", BackupLogger.INFO_TYPE);
|
// 压缩SQL文件(放在指定路径下)
|
addFileToZip(new File(databasePath), "database/" + NUMBER_TIME + ".sql", zos);
|
log.writeInfo("【压缩备份文件】压缩完成", BackupLogger.INFO_TYPE);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
File file = new File(databasePath);
|
file.delete();
|
log.writeInfo("【压缩备份文件】清理数据库备份文件完成", BackupLogger.INFO_TYPE);
|
}
|
return false;
|
}
|
|
/**
|
* SFTP连接:通过SFTP将备份文件上传备用机 或 从mysql服务器上获取备份文件
|
* @param isUpload 是否上传
|
* @return
|
*/
|
public boolean sftpTransferService() {
|
String sftpTitle = "上传系统备份文件";;
|
String localFilePath = config.getProperty("ZIPFILE_BACKUP"); // 本地文件路径
|
String sftpFilePath = config.getProperty("UPLOAD_BACKUP_DIR") + NUMBER_TIME + ".zip"; // SFTP服务文件路径;
|
|
String host = config.getProperty("UPLOAD_SFTP_HOST"); // SFTP服务IP地址
|
String port = config.getProperty("UPLOAD_SFTP_PORT"); // SSH端口
|
String user = config.getProperty("UPLOAD_SFTP_USER"); // SFTP服务的用户名
|
String password = config.getProperty("UPLOAD_SFTP_PWD"); // SFTP服务的密码
|
log.writeInfo(String.format("【%s】开始%s..................", sftpTitle, sftpTitle), BackupLogger.INFO_TYPE);
|
log.writeInfo(String.format("【%s】SFTP服务地址:", sftpTitle) + host, BackupLogger.INFO_TYPE);
|
log.writeInfo(String.format("【%s】SFTP服务端口:", sftpTitle) + port, BackupLogger.INFO_TYPE);
|
log.writeInfo(String.format("【%s】SFTP服务存储目录:", sftpTitle) + sftpFilePath, BackupLogger.INFO_TYPE);
|
log.writeInfo(String.format("【%s】当前服务存储目录:", sftpTitle) + localFilePath, BackupLogger.INFO_TYPE);
|
if (StringUtils.isEmpty(host) || StringUtils.isEmpty(port) || StringUtils.isEmpty(user) || StringUtils.isEmpty(password)) {
|
log.writeInfo(String.format("【%s】SFTP信息未配置完整,不予连接传输文件", sftpTitle), BackupLogger.INFO_TYPE);
|
return true;
|
}
|
try {
|
JSch jsch = new JSch();
|
Session session = jsch.getSession(user, host, Integer.valueOf(port));
|
session.setPassword(password);
|
session.setConfig("StrictHostKeyChecking", "no"); // 仅限测试环境
|
session.connect(5000); // 设置连接超时时间
|
|
Channel channel = session.openChannel("sftp");
|
channel.connect(5000); // 设置通道超时时间
|
ChannelSftp sftpChannel = (ChannelSftp) channel;
|
|
// 确保目录存在(关键步骤)
|
String remoteDir = sftpFilePath.substring(0, sftpFilePath.lastIndexOf('/'));
|
if (!remoteDir.startsWith("/")) {
|
remoteDir = "/" + remoteDir;
|
}
|
// 创建对应目录文件夹
|
createRemoteDirectory(sftpChannel, remoteDir);
|
sftpChannel.put(localFilePath, sftpFilePath, new SftpProgressMonitor() {
|
public void init(int op, String src, String dest, long max) {
|
log.writeInfo("【上传系统备份文件】开始传输: " + src + " -> " + dest, BackupLogger.INFO_TYPE);
|
}
|
public boolean count(long count) { return true; }
|
public void end() { log.writeInfo("【上传系统备份文件】传输完成", BackupLogger.INFO_TYPE); }
|
});
|
sftpChannel.exit();
|
session.disconnect();
|
log.writeInfo(String.format("【%s】%s成功", sftpTitle, sftpTitle), BackupLogger.INFO_TYPE);
|
} catch (JSchException | SftpException e) {
|
e.printStackTrace();
|
log.writeInfo(String.format("【%s】%s失败:", sftpTitle, sftpTitle) + e.getMessage(), BackupLogger.ERROR_TYPE);
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
* SFTP递归创建远程目录
|
* @param sftpChannel
|
* @param remoteDir
|
* @throws SftpException
|
*/
|
private void createRemoteDirectory(ChannelSftp sftpChannel, String remoteDir) throws SftpException {
|
try {
|
sftpChannel.cd(remoteDir);
|
} catch (SftpException e) {
|
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
|
if (remoteDir.length() == 3 && remoteDir.charAt(1) == ':') {
|
// 跳过根目录
|
return;
|
}
|
int pos = remoteDir.lastIndexOf('/');
|
if (pos > 0) {
|
createRemoteDirectory(sftpChannel, remoteDir.substring(0, pos));
|
}
|
sftpChannel.mkdir(remoteDir);
|
log.writeInfo("【上传备份文件】创建目录成功: " + remoteDir, BackupLogger.INFO_TYPE);
|
} else {
|
throw e;
|
}
|
}
|
}
|
}
|