|
|
|
@ -1,41 +1,52 @@
|
|
|
|
|
package com.dsideal.YunXiaoTools;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.util.ZipUtil;
|
|
|
|
|
import com.dsideal.YunXiaoTools.Utils.ObsUtil;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
|
|
public class MySQLBackup {
|
|
|
|
|
|
|
|
|
|
//数据库连接串
|
|
|
|
|
public static String jdbcUrl;
|
|
|
|
|
//数据库用户名
|
|
|
|
|
public static String user;
|
|
|
|
|
//数据库密码
|
|
|
|
|
public static String password;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行MySQL备份
|
|
|
|
|
*/
|
|
|
|
|
public String backup() {
|
|
|
|
|
PropKit.clear();
|
|
|
|
|
PropKit.use("application.properties");
|
|
|
|
|
jdbcUrl = PropKit.get("read.jdbcUrl");
|
|
|
|
|
user = PropKit.get("read.user");
|
|
|
|
|
password = PropKit.get("read.password");
|
|
|
|
|
// 获取配置
|
|
|
|
|
String host = getHostFromJdbcUrl(jdbcUrl);
|
|
|
|
|
String port = getPortFromJdbcUrl(jdbcUrl);
|
|
|
|
|
String database = getDatabaseFromJdbcUrl(jdbcUrl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成备份文件名
|
|
|
|
|
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
|
|
|
|
|
String backupFileName = database + "_" + timestamp + ".sql";
|
|
|
|
|
|
|
|
|
|
String backupFileName = timestamp + ".sql";
|
|
|
|
|
// 创建临时目录
|
|
|
|
|
String tempDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
String backupPath = tempDir + File.separator + backupFileName;
|
|
|
|
|
String backupPath = tempDir + backupFileName;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 获取mysqldump路径
|
|
|
|
|
String mysqldumpPath = getMySQLDumpPath();
|
|
|
|
|
|
|
|
|
|
// 构建命令(不包含密码)
|
|
|
|
|
ProcessBuilder pb = new ProcessBuilder(
|
|
|
|
|
mysqldumpPath,
|
|
|
|
@ -56,10 +67,8 @@ public class MySQLBackup {
|
|
|
|
|
"-r",
|
|
|
|
|
backupPath
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 设置环境变量传递密码
|
|
|
|
|
pb.environment().put("MYSQL_PWD", password);
|
|
|
|
|
|
|
|
|
|
// 执行备份命令
|
|
|
|
|
Process process = pb.start();
|
|
|
|
|
|
|
|
|
@ -72,16 +81,14 @@ public class MySQLBackup {
|
|
|
|
|
System.out.println("备份进度: " + line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 等待命令执行完成
|
|
|
|
|
int exitCode = process.waitFor();
|
|
|
|
|
if (exitCode == 0) {
|
|
|
|
|
System.out.println("数据库备份成功: " + backupPath);
|
|
|
|
|
return backupPath;
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("数据库备份失败,错误信息:\n" + errorOutput.toString());
|
|
|
|
|
throw new RuntimeException("数据库备份失败,错误信息:\n" + errorOutput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("备份过程出错: " + e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
@ -138,18 +145,27 @@ public class MySQLBackup {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
PropKit.use("application.properties");
|
|
|
|
|
jdbcUrl = PropKit.get("read.jdbcUrl");
|
|
|
|
|
user = PropKit.get("read.user");
|
|
|
|
|
password = PropKit.get("read.password");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
MySQLBackup backup = new MySQLBackup();
|
|
|
|
|
String backupFile = backup.backup();
|
|
|
|
|
System.out.println("备份文件已生成: " + backupFile);
|
|
|
|
|
|
|
|
|
|
// 如果需要,可以进一步将文件上传到OBS
|
|
|
|
|
// ... 上传代码 ...
|
|
|
|
|
|
|
|
|
|
String sourceFile = backup.backup();
|
|
|
|
|
|
|
|
|
|
//压缩成ZIP文件
|
|
|
|
|
String zipFile = sourceFile.replace(".sql", ".zip");
|
|
|
|
|
ZipUtil.zip(sourceFile, zipFile);
|
|
|
|
|
|
|
|
|
|
//文件名称
|
|
|
|
|
String fileName = new File(zipFile).getName();
|
|
|
|
|
// 上传到华为云OBS
|
|
|
|
|
String key = "HuangHai/YunXiao/" + fileName;
|
|
|
|
|
ObsUtil.uploadToObs(zipFile, key);
|
|
|
|
|
|
|
|
|
|
//清理此路径下旧的文件
|
|
|
|
|
String url = "https://dsideal.obs.cn-north-1.myhuaweicloud.com/" + key;
|
|
|
|
|
System.out.println(url);
|
|
|
|
|
//清理
|
|
|
|
|
FileUtil.del(sourceFile);
|
|
|
|
|
FileUtil.del(zipFile);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
System.err.println("备份失败: " + e.getMessage());
|
|
|
|
|