定时任务备份数据库
1.场景
- MySql数据库跟服务不在同一个服务器上
- 数据库备份工具没有配置到系统路径
2.方案
将备份工具放入项目classpath
路径下,通过读取该路径下备份工具进行备份。
1.在idea中执行时,可以直接通过读取该路径下备份工具进行备份
2.打包成jar后,jar相当于一个压缩包,压缩包里的文件是无法直接进行操作的,因此我们需要先从jar里把工具取出来,然后再进行备份
3.实现
1.将备份工具放入classpath
路径下
链接:https://pan.baidu.com/s/1Za3G9c5HqgW6I2C_Qx2Y-Q?pwd=ltn1
提取码:ltn1
2.代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @Author Qring
* @Date 2022/9/24 9:30
* @Version 1.0
* @Description 定时备份数据库
*/
@Configuration
@EnableScheduling
@Slf4j
public class TestTask {
@Value("${backup.dump.util:mysqldump/5.7/mysqldump}")
private String dumpUtil;
@Value("${backup.dump.path}")
private String dumpPath;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Scheduled(cron = "0/5 * * * * ?")
private void configureTasks() {
checkDumpUtil();
URI uri = URI.create(url.substring(5));
String host = uri.getHost();
String dataBaseName = uri.getPath().substring(1);
String filename = System.currentTimeMillis() + "_" + "dump.sql";
StringBuilder backupSql = new StringBuilder();
backupSql.append(dumpUtil).append(" --opt").append(" -h ").append(host).append(" --user=").append(username).append(" --password=").append(password);
backupSql.append(" --result-file=").append(dumpPath).append(filename).append(" --default-character-set=utf8 ").append(dataBaseName);
log.info("backup sql: {} ", backupSql);
try {
Process exec = Runtime.getRuntime().exec(backupSql.toString());
// 如果需要控制执行时间, 可调用 public boolean waitFor(long timeout, TimeUnit unit)
int code = exec.waitFor();
if (code == 0) {
log.info("数据库备份成功,保存路径:" + dumpPath);
} else {
log.info("数据库备份失败, 错误码: {}", code);
}
} catch (Exception e) {
log.error("数据库备份失败!", e);
}
}
private void checkDumpUtil() {
String os = System.getProperty("os.name").toLowerCase();
// 简单判断下操作系统, 如果是spring中定时任务执行的类是单例的,
// 需要加上!backupBinPath.contains(".exe")避免每次执行都拼接后缀
if (os.startsWith("windows") && !dumpUtil.contains(".exe")) {
dumpUtil += ".exe";
}
// 判断该路径下是否已有备份工具
File file = Paths.get(dumpUtil).toFile();
if (!file.exists()) {
// 获取备份工具数据流
// TestTask.class 是当前类, 任意一个项目中的类也可
InputStream inputStream = TestTask.class.getClassLoader().getResourceAsStream(dumpUtil);
if (inputStream != null) {
try {
File t = new File(dumpUtil);
// 如果文件目录不存在, 先创建目录
if (t.getParentFile() != null && !t.getParentFile().exists()) {
boolean mkdirs = t.getParentFile().mkdirs();
log.info("备份工具目录创建结果: {}", mkdirs);
}
// 将备份工具提取出来
Files.copy(inputStream, Paths.get(dumpUtil));
} catch (IOException e) {
throw new RuntimeException("备份工具不存在!", e);
}
} else {
log.info("备份工具不存在!!!");
}
}
}
}