| | |
| | | |
| | | 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.InputStreamReader; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | 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.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; |
| | | import com.product.system.backup.util.SystemBackupUtil; |
| | | |
| | | /** |
| | | * 系统备份 |
| | | * 数据库备份:每天执行代码备份 |
| | | * 附件备份:每天执行代码备份当天附件 |
| | | * 遗留问题:附件被修改,不能锁定非当天被修改的附件 |
| | | */ |
| | | @Service("systemBackService1") |
| | | public class SystemBackupService { |
| | | @Service("systemBackService") |
| | | public class SystemBackupService extends AbstractBaseService{ |
| | | |
| | | @Autowired |
| | | BaseDao baseDao; |
| | | |
| | | // 备份配置 |
| | | Properties config; |
| | |
| | | // 数字时间和数字日期(用做文件夹或者文件名) |
| | | 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("yyyyMMddHHmmss"); |
| | | 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(){ |
| | | // 初始日志文件 |
| | | 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(new Date()); |
| | | NUMBER_DATE = numberDateFormat.format(new Date()); |
| | | log.writeInfo("【系统备份入口】系统日期:" + NUMBER_DATE, BackupLogger.INFO_TYPE); |
| | | log.writeInfo("【系统备份入口】系统时间:" + NUMBER_TIME, BackupLogger.INFO_TYPE); |
| | | // 进入备份进程 |
| | | backupProcess(); |
| | | log.closeLogger(); |
| | | public void systemBackupInit() { |
| | | // 获取系统时间 |
| | | Date finalTime = new Date(); |
| | | Date startTime = null; |
| | | // 获取系统最大备份时间 |
| | | StringBuilder sbSql = new StringBuilder(); |
| | | sbSql.append("SELECT 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[] {}, false); |
| | | if(fseMaxLogTime != null && !StringUtils.isEmpty(fseMaxLogTime.getString("end_time"))) { |
| | | startTime = fseMaxLogTime.getDate("end_time"); |
| | | } |
| | | // 初始备份日志数据 |
| | | FieldSetEntity fseBackLog = new FieldSetEntity("product_sys_backup_log"); |
| | | fseBackLog.setValue("start_time", startTime); |
| | | fseBackLog.setValue("end_time", finalTime); |
| | | |
| | | // 执行备份 |
| | | boolean flag = backupProcess(startTime, finalTime); |
| | | if (flag) { |
| | | fseBackLog.setValue("backup_status", 1); |
| | | } else { |
| | | fseBackLog.setValue("backup_status", 0); |
| | | } |
| | | // 保存日志 |
| | | baseDao.saveFieldSetEntity(fseBackLog); |
| | | } |
| | | |
| | | /** |
| | | * 数据备份进程 |
| | | * 数据备份进程 |
| | | * @param startTime 备份开始时间 |
| | | * @param finalTime 备份截止时间 |
| | | * @return |
| | | */ |
| | | public void backupProcess() { |
| | | public boolean backupProcess(Date startTime, Date finalTime) { |
| | | // 初始日志文件 |
| | | log = new BackupLogger(); |
| | | log.writeInfo("【系统备份进程】系统备份进程开始..................", BackupLogger.INFO_TYPE); |
| | | long start = System.currentTimeMillis(); |
| | | boolean status= true; |
| | | // 第一步:初始配文件参数,当前目生成时间戳的文件夹 |
| | | status = initSystemConfig(); |
| | | // 第一步:初始备份参数 |
| | | initBackupParam(startTime, finalTime); |
| | | // 第二步:初始配文件参数,当前目生成时间戳的文件夹 |
| | | boolean status = initSystemConfig(); |
| | | if (!status) |
| | | return; |
| | | // 第二步:执行数据库备份 |
| | | return false; |
| | | // 第三步:执行数据库备份 |
| | | status = runExpDataBase(); |
| | | if (!status) |
| | | return; |
| | | // 第三步:执行对数据库文件、工程代码、上传文件做压缩备份 |
| | | return false; |
| | | // 第四步:执行对数据库文件、工程代码、上传文件做压缩备份 |
| | | status = zipDataBackup(); |
| | | if (!status) |
| | | return; |
| | | // 第四步:上传压缩备份文件到FTP |
| | | status = sftpTransferService(true); |
| | | // uploadBackupMachine2(); |
| | | return false; |
| | | // 第五步:上传压缩备份文件到FTP |
| | | status = sftpTransferService(); |
| | | if (!status) |
| | | return; |
| | | // 第五步:清除数据 |
| | | return false; |
| | | // 第六步:清除数据 |
| | | // clearKeepData(); |
| | | |
| | | long end = System.currentTimeMillis(); |
| | | log.writeInfo("【系统备份进程】系统备份进程结束,耗时.................." + ((end - start) / 1000) + "秒", BackupLogger.INFO_TYPE); |
| | | System.out.println("系统备份耗时.................." + ((end - start) / 1000) + "秒"); |
| | | // 关闭日志 |
| | | log.closeLogger(); |
| | | return status; |
| | | } |
| | | |
| | | /** |
| | | * 第一步:初始备份参数 |
| | | * @param startTime 备份开始时间 |
| | | * @param finalTime 备份截止时间 |
| | | * @return |
| | | */ |
| | | public boolean initBackupParam(Date startTime, Date finalTime){ |
| | | // 在日志中记录操作系统信息,便于调试 |
| | | 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); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 第一步:初始配置文件参数,当前目录生成时间戳的文件夹 |
| | | * 第二步:初始配置文件参数,当前目录生成时间戳的文件夹 |
| | | */ |
| | | 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); |
| | | return false; |
| | | } |
| | | // 读取备份配置文件 |
| | | config = new Properties(); |
| | |
| | | // 获取系统文件路径 |
| | | String systemFileFolder = Global.getSystemConfig("local.dir", ""); |
| | | File newFile= new File(systemFileFolder); |
| | | // 系统附件存放目录(当天附件) |
| | | config.setProperty("DOCUMENT_ROOT", newFile.getAbsolutePath() + File.separator + "00000000-0000-0000-0000-000000000000" + File.separator + NUMBER_DATE); |
| | | // 数据库备份目录(数据库备份根目录+时间文件名+.sql) |
| | | // 系统附件存放目录(../product-server-web/attachment/upload/) |
| | | config.setProperty("DOCUMENT_ROOT", newFile.getAbsolutePath()); |
| | | // 数据库备份目录(数据库备份根目录+时间文件名+.sql,例如:E://backup/database/20250101090000.sql) |
| | | config.setProperty("DATABASE_BACKUP", config.getProperty("DATABASE_ROOT") + File.separator + NUMBER_TIME + ".sql"); |
| | | // 备份目标文件(ZIP备份根目录+时间文件名+.zip) |
| | | // 备份ZIP文件(ZIP备份根目录+时间文件名+.zip,例如:E://backup/20250101090000.zip) |
| | | config.setProperty("ZIPFILE_BACKUP", config.getProperty("ZIPFILE_ROOT") + File.separator + NUMBER_TIME + ".zip"); |
| | | // 创建ZIP文件根目录 |
| | | File zipFileRoot = new File(config.getProperty("ZIPFILE_ROOT")); |
| | |
| | | return false; |
| | | } |
| | | } |
| | | log.writeInfo("【初始配置文件】DOCUMENT_ROOT=" + config.getProperty("DOCUMENT_ROOT"), BackupLogger.INFO_TYPE); |
| | | 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); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 第二步:备份数据库文件 |
| | | * 第三步:备份数据库文件 |
| | | */ |
| | | public boolean runExpDataBase() { |
| | | log.writeInfo("【备份数据库】开始备份数据库..................", BackupLogger.INFO_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); |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.writeInfo("【备份数据库】数据库备份失败:" + e.getMessage(), BackupLogger.ERROR_TYPE); |
| | | backupFile.delete(); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | private static void addFolderToZip(File folder, String parentPath, ZipOutputStream zos) throws IOException { |
| | | File[] files = folder.listFiles(); |
| | | if (files != null) { |
| | | for (File file : files) { |
| | | if (file.isDirectory()) { |
| | | // 递归处理子目录,保持路径结构 |
| | | addFolderToZip(file, parentPath + file.getName() + "/", zos); |
| | | } else { |
| | | // 添加文件到ZIP,保持路径结构 |
| | | addFileToZip(file, parentPath + file.getName(), zos); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 第三步:压缩备份文件 |
| | | */ |
| | | public boolean zipDataBackup() { |
| | | log.writeInfo("【压缩备份文件】开始压缩备份文件..................", BackupLogger.INFO_TYPE); |
| | | /** |
| | | * 第四步:执行对数据库文件、工程代码、上传文件做压缩备份 |
| | | * @return |
| | | */ |
| | | 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)) { |
| | | |
| | | log.writeInfo("【压缩备份文件】压缩系统附件", BackupLogger.INFO_TYPE); |
| | | // 压缩文件夹(保留完整路径结构) |
| | | // 注意:这里我们传递了根目录名称作为初始parentPath |
| | | addFolderToZip(new File(documentPath), NUMBER_DATE + "/", zos); |
| | | |
| | | log.writeInfo("【压缩备份文件】压缩数据库备份文件", BackupLogger.INFO_TYPE); |
| | | // 压缩SQL文件(放在指定路径下) |
| | | addFileToZip(new File(databasePath), "database/" + NUMBER_TIME + ".sql", zos); |
| | | |
| | | |
| | | try (FileOutputStream fos = new FileOutputStream(zipFilePath); |
| | | ZipOutputStream zos = new ZipOutputStream(fos)){ |
| | | StringBuilder sbSql = new StringBuilder(); |
| | | sbSql.append("SELECT SUBSTRING_INDEX(attachment_url, '/', -1) folder_name,attachment_title AS file_name,attachment_url AS folder_path \n"); |
| | | sbSql.append("FROM product_sys_attachments \n"); |
| | | sbSql.append("WHERE (created_utc_datetime > ? AND created_utc_datetime <= ?) \n"); |
| | | sbSql.append("OR (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<String, List<String>> groupFolderFile = new HashMap<>(); |
| | | for(int i = 0; i < dtTable.getRows(); i++) { |
| | | String folderName = dtTable.getFieldSetEntity(i).getString("folder_path"); |
| | | String fileName = dtTable.getFieldSetEntity(i).getString("file_name"); |
| | | if (groupFolderFile.containsKey(folderName)) { |
| | | groupFolderFile.get(folderName).add(fileName); |
| | | } else { |
| | | groupFolderFile.put(folderName, new ArrayList<String>(Arrays.asList(fileName))); |
| | | } |
| | | } |
| | | log.writeInfo("【压缩备份文件】压缩系统增量附件", BackupLogger.INFO_TYPE); |
| | | SystemBackupUtil.compressExistingFiles(groupFolderFile, documentPath, zos); |
| | | } |
| | | |
| | | log.writeInfo("【压缩备份文件】压缩数据库备份文件", BackupLogger.INFO_TYPE); |
| | | SystemBackupUtil.addFileToZip(new File(databasePath), "database/" + NUMBER_TIME + ".sql", zos); |
| | | log.writeInfo("【压缩备份文件】压缩完成", BackupLogger.INFO_TYPE); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return true; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | log.writeInfo("【压缩备份文件】压缩失败:" + e.getMessage(), BackupLogger.ERROR_TYPE); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | } finally { |
| | | File file = new File(databasePath); |
| | | file.delete(); |
| | | log.writeInfo("【压缩备份文件】清理数据库备份文件完成", BackupLogger.INFO_TYPE); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * SFTP连接:通过SFTP将备份文件上传备用机 或 从mysql服务器上获取备份文件 |
| | | * 第五步:SFTP连接:通过SFTP将备份文件上传备用机 或 从mysql服务器上获取备份文件 |
| | | * @param isUpload 是否上传 |
| | | * @return |
| | | */ |
| | | public boolean sftpTransferService(boolean isUpload) { |
| | | String sftpTitle = null; |
| | | String localFilePath = null; |
| | | String sftpFilePath = null; |
| | | if(isUpload) { |
| | | sftpTitle = "上传系统备份文件"; |
| | | localFilePath = config.getProperty("ZIPFILE_BACKUP"); // 本地文件路径 |
| | | sftpFilePath = config.getProperty("UPLOAD_BACKUP_DIR") + NUMBER_TIME + ".zip"; // SFTP服务文件路径 |
| | | } else { |
| | | // sftpTitle = "获取数据库备份文件"; |
| | | // localFilePath = config.getProperty("DATABASE_BACKUP"); // 本地文件路径 |
| | | // sftpFilePath = config.getProperty("DATABASE_BACKUP"); // SFTP服务文件路径 |
| | | } |
| | | public boolean sftpTransferService() { |
| | | 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); |
| | | log.writeInfo("【上传系统备份文件】开始上传系统备份文件..................", BackupLogger.INFO_TYPE); |
| | | log.writeInfo("【上传系统备份文件】SFTP服务地址:" + host, BackupLogger.INFO_TYPE); |
| | | log.writeInfo("【上传系统备份文件】SFTP服务端口:" + port, BackupLogger.INFO_TYPE); |
| | | log.writeInfo("【上传系统备份文件】SFTP服务存储目录:" + sftpFilePath, BackupLogger.INFO_TYPE); |
| | | log.writeInfo("【上传系统备份文件】当前服务存储目录:" + 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); |
| | | log.writeInfo("【上传系统备份文件】SFTP信息未配置完整,不予连接传输文件", BackupLogger.INFO_TYPE); |
| | | return true; |
| | | } |
| | | try { |
| | |
| | | if (!remoteDir.startsWith("/")) { |
| | | remoteDir = "/" + remoteDir; |
| | | } |
| | | if(isUpload) { |
| | | // 创建对应目录文件夹 |
| | | 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); } |
| | | }); |
| | | } else { |
| | | // 创建对应目录文件夹 |
| | | File fileDirectory = new File(remoteDir); |
| | | if (!fileDirectory.exists()) { |
| | | fileDirectory.mkdirs(); |
| | | // 创建对应目录文件夹 |
| | | 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); |
| | | } |
| | | sftpChannel.get(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); } |
| | | }); |
| | | } |
| | | 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); |
| | | log.writeInfo("【上传系统备份文件】上传系统备份文件成功", BackupLogger.INFO_TYPE); |
| | | } catch (JSchException | SftpException e) { |
| | | e.printStackTrace(); |
| | | log.writeInfo(String.format("【%s】%s失败:", sftpTitle, sftpTitle) + e.getMessage(), BackupLogger.ERROR_TYPE); |
| | | log.writeInfo("【上传系统备份文件】上传系统备份文件失败:" + e.getMessage(), BackupLogger.ERROR_TYPE); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 递归创建远程目录 |
| | | * SFTP递归创建远程目录 |
| | | * @param sftpChannel |
| | | * @param remoteDir |
| | | * @throws SftpException |