120 lines
4.9 KiB
120 lines
4.9 KiB
package com.zc.business.controller;
|
|
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
import com.zc.business.enums.UniversalEnum;
|
|
import com.zc.business.utils.DcMYSQLUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* 数据库定时备份任务
|
|
* 在backup文件夹中备份最近七日的数据库文件, 备份文件夹 与当前程序同一目录
|
|
*/
|
|
@Component
|
|
@Slf4j
|
|
public class DcMYSQLJob {
|
|
//private final Environment environment;
|
|
|
|
private static final String IP = UniversalEnum.IP.getValue();
|
|
|
|
private static final String PORT = UniversalEnum.MYSQL_PORT.getValue();
|
|
|
|
private static final String USER = UniversalEnum.MYSQL_USER_NAME.getValue();
|
|
|
|
private static final String PASSWORD = UniversalEnum.MYSQL_PASSWORD.getValue();
|
|
|
|
//public DcMYSQLJob(Environment environment) {
|
|
// this.environment = environment;
|
|
//}
|
|
|
|
@Scheduled(cron = "0 0 22 * * ?")
|
|
//@Scheduled(cron = "0 10 * * * ?")
|
|
public void backupAthena() throws IOException {
|
|
//String url = "jdbc:mysql://10.0.111.11:3306/athena?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
|
|
|
|
extracted(UniversalEnum.ATHENA.getValue(), "athena");
|
|
}
|
|
|
|
@Scheduled(cron = "0 30 22 * * ?")
|
|
//@Scheduled(cron = "0 20 * * * ?")
|
|
public void backupJiHeDC() throws IOException {
|
|
// 数据库配置信息
|
|
//String url = "jdbc:mysql://10.0.111.11:3306/jihe-dc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
|
|
|
|
extracted(UniversalEnum.JIHE_DC.getValue(), "jihe-dc");
|
|
}
|
|
|
|
@Scheduled(cron = "0 0 23 * * ?")
|
|
//@Scheduled(cron = "0 30 * * * ?")
|
|
public void backupJiHeDCPro() throws IOException {
|
|
// 数据库配置信息
|
|
//String url = "jdbc:mysql://10.0.111.11:3306/jihe-dc-pro?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
|
|
|
|
extracted(UniversalEnum.JIHE_DC_PRO.getValue(), "jihe-dc-pro");
|
|
}
|
|
|
|
private static void extracted(String url, String nameOfTheDatabase) throws IOException {
|
|
LocalDateTime now = LocalDateTime.now();
|
|
log.info("*******************时间:【{}】, 系统开启定时任务数据库备份*******************", now);
|
|
|
|
// 第三个 :号下标
|
|
int subStrIndex = url.indexOf(UniversalEnum.IN_THE_COLON.getValue(), url.indexOf(UniversalEnum.IN_THE_COLON.getValue(), url.indexOf(UniversalEnum.IN_THE_COLON.getValue()) + UniversalEnum.ONE.getNumber()) + UniversalEnum.ONE.getNumber());
|
|
|
|
// IP
|
|
String host = url.substring(url.indexOf(UniversalEnum.DOUBLE_SLASH.getValue()) + UniversalEnum.TWO.getNumber(), subStrIndex);
|
|
|
|
// 端口
|
|
String subStr2 = url.substring(subStrIndex);
|
|
|
|
String port = subStr2.substring(UniversalEnum.ONE.getNumber(), subStr2.indexOf(UniversalEnum.SLASH.getValue()));
|
|
|
|
// 数据库名
|
|
String dataBaseName = subStr2.substring(subStr2.indexOf(UniversalEnum.SLASH.name()) + UniversalEnum.ONE.getNumber(), subStr2.indexOf(UniversalEnum.QUESTION_MARK.getValue()));
|
|
|
|
// 环境
|
|
String os = System.getProperties().getProperty("os.name");
|
|
|
|
log.info("备份环境信息:【{}】, 用户名:【{}】,密码:【{}】, 地址:【{}】, 端口:【{}】,数据库:【{}】", os, url, PASSWORD, host, port, dataBaseName);
|
|
|
|
|
|
LocalDate localDate = LocalDate.now();
|
|
|
|
String filePath = RuoYiConfig.getDownloadMysqlPath();
|
|
|
|
Date date = new Date();
|
|
|
|
String format = new SimpleDateFormat(UniversalEnum.ARCHIVE_TIME_FORMAT_ALL.getValue()).format(date).replace(UniversalEnum.BLANK_SPACE.getValue(), UniversalEnum.EMPTY_STRING.getValue());
|
|
|
|
String fileName = nameOfTheDatabase + UniversalEnum.UNDERLINE.getValue() + format + ".sql";
|
|
|
|
File file = new File(filePath, fileName);
|
|
|
|
file.getParentFile().mkdirs();
|
|
|
|
file.createNewFile();
|
|
|
|
// 备份今天数据库
|
|
DcMYSQLUtil.backup(IP, PORT, USER, PASSWORD, dataBaseName, file);
|
|
|
|
|
|
// 删除七天前数据库备份文件 LocalDate
|
|
LocalDate before = localDate.minusDays(UniversalEnum.SEVEN.getNumber());
|
|
String fileBeforeName = nameOfTheDatabase + UniversalEnum.SHORT_BAR.getValue() + before + ".sql";
|
|
File fileBefore = new File(filePath, fileBeforeName);
|
|
if (fileBefore.exists()) {
|
|
fileBefore.delete();
|
|
}
|
|
log.info("*******************时间:【{}】, 系统结束定时任务数据库备份*******************", now);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|