1.从nacos注册中心.yml文件中获取数据库信息
package com.datago.serve.common.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
/**
* 类描述:TODO
*
* @author HBO
* @date 2022-12-07 09:34
**/
@Configuration
public class JdbcUtils {
@Value("${spring.datasource.dynamic.datasource.master.url}")
public String url;
@Value("${spring.datasource.dynamic.datasource.master.username}")
public String username;
@Value("${spring.datasource.dynamic.datasource.master.password}")
public String password;
public HashMap<String, String> getDBInfo() {
String[] split = url.split(":");
String host = String.format("%s:%s:%s", split[0], split[1], split[2]);
String[] portSplit = split[3].split("/");
String port = portSplit[0];
String[] databaseSplit = portSplit[1].split("\\?");
String dbName = databaseSplit[0];
HashMap<String, String> result = new HashMap<>();
result.put("url",url);
result.put("host",host);
result.put("port",port);
result.put("dbName",dbName);
result.put("userName",username);
result.put("passWord",password);
return result;
}
}
2.数据库定时备份代码
package com.datago.serve.task;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.datago.serve.common.util.JdbcUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Map;
/**
* 类描述:TODO
* 数据库定时备份
*
* @author HBO
* @date 2022-12-07 09:25
**/
@Component
@EnableScheduling
@Slf4j
public class DatabaseBackups {
@Autowired
private JdbcUtils jdbcUtils;
@Value("${file.databaseBackups}")
private String resourcePath;
/**
* 定时备份数据库信息 每天23点执行一次
*/
@Scheduled(cron = "0 0 23 * * ?")
public void backUpDataBase() {
log.info("======执行定时器:定时备份数据库=======");
String backUpPath = resourcePath + "/sql/" + Date.valueOf(LocalDate.now());
File backUpFile = new File(backUpPath);
if (!backUpFile.exists()) {
backUpFile.mkdirs();
}
File dataFile = new File(backUpPath + "/idis" + System.currentTimeMillis() + ".sql");
//拼接cmd命令
StringBuffer sb = new StringBuffer();
Map<String, String> dbInfo = jdbcUtils.getDBInfo();
sb.append("mysqldump");
sb.append(" -u" + dbInfo.get("userName"));
sb.append(" --default-character-set=utf8 ");//编码格式
sb.append(" -p" + dbInfo.get("passWord"));
sb.append(" " + dbInfo.get("dbName") + " > ");
sb.append(dataFile);
log.info("======数据库备份cmd命令为:" + sb.toString() + "=======");
try {
Process exec = Runtime.getRuntime().exec("cmd /c" + sb.toString());
if (exec.waitFor() == 0) {
log.info("======数据库备份成功,路径为:" + dataFile + "=======");
}
} catch (Exception e) {
log.info("======数据库备份失败,异常为:" + e.getMessage() + "=======");
}
}
/**
* 定时删除数据库备份文件,只保留最近一个星期 每天凌晨执行一次
*/
@Scheduled(cron = "0 0 0 * * ?")
public void deleteBackUpDataBase() {
log.info("======执行定时器:定时删除备份数据库文件=======");
String backUpPath = resourcePath + "/sql";
File backUpFile = new File(backUpPath);
if (backUpFile.exists()) {
File[] files = backUpFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
Date date1 = Date.valueOf(file.getName());
Date date2 = Date.valueOf(LocalDate.now());
long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
if (betweenDay > 7) {
File[] subFiles = file.listFiles();
for (File subFile : subFiles) {
subFile.delete();
}
file.delete();
}
}
}
}
}
}