main
黄海 7 months ago
parent 3866ea8c71
commit 5543ef804c

@ -1,6 +1,8 @@
package com.dsideal.YunXiaoTools.Controller; package com.dsideal.YunXiaoTools.Controller;
import com.dsideal.YunXiaoTools.Service.MysqlBackupService; import com.dsideal.YunXiaoTools.Service.MysqlBackupService;
import com.dsideal.YunXiaoTools.Service.MysqlRestoreService;
import com.dsideal.YunXiaoTools.Utils.CommonUtil;
import com.jfinal.aop.Before; import com.jfinal.aop.Before;
import com.jfinal.core.Controller; import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.GET; import com.jfinal.ext.interceptor.GET;
@ -22,4 +24,20 @@ public class DataBaseController extends Controller {
kv.set("url", url); kv.set("url", url);
renderJson(kv); renderJson(kv);
} }
/**
* mysql
*/
public void restoreMysql() {
//获取最后一次上传的数据库文件
String key = CommonUtil.getLastDataBaseZip();
//还原数据库
MysqlRestoreService mr = new MysqlRestoreService();
int total = mr.restore(mr.jdbcUrl, mr.username, mr.password, key);
Kv kv = Kv.create();
kv.set("success", true);
kv.set("message", "数据库还原成功完成,共还原数据表:" + total + "个。");
kv.set("total", total);
renderJson(kv);
}
} }

@ -15,9 +15,9 @@ import java.util.zip.*;
public class MysqlRestoreService { public class MysqlRestoreService {
private final String jdbcUrl; public final String jdbcUrl;
private final String username; public final String username;
private final String password; public final String password;
public MysqlRestoreService() { public MysqlRestoreService() {
jdbcUrl = PropKit.get("write.jdbcUrl"); jdbcUrl = PropKit.get("write.jdbcUrl");
@ -30,10 +30,11 @@ public class MysqlRestoreService {
* *
* @param obsKey OBS * @param obsKey OBS
*/ */
public void restore(String jdbcUrl, String username, String password, String obsKey) { public int restore(String jdbcUrl, String username, String password, String obsKey) {
String tempDir = System.getProperty("java.io.tmpdir"); String tempDir = System.getProperty("java.io.tmpdir");
String sqlFile = null; String sqlFile = null;
String zipFile = tempDir + obsKey.split("/")[2]; String zipFile = tempDir + obsKey.split("/")[2];
int total;
try { try {
// 1. 从OBS下载ZIP文件 // 1. 从OBS下载ZIP文件
downloadFromObs(obsKey, tempDir); downloadFromObs(obsKey, tempDir);
@ -52,7 +53,7 @@ public class MysqlRestoreService {
conn.setAutoCommit(false); // 开启事务 conn.setAutoCommit(false); // 开启事务
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
int total = sqlStatements.size(); total = sqlStatements.size();
int current = 0; int current = 0;
for (String sql : sqlStatements) { for (String sql : sqlStatements) {
@ -93,6 +94,7 @@ public class MysqlRestoreService {
new File(sqlFile).delete(); new File(sqlFile).delete();
} }
} }
return total;
} }
/** /**
@ -190,7 +192,6 @@ public class MysqlRestoreService {
PropKit.use("application.properties"); PropKit.use("application.properties");
//获取最后一次上传的数据库文件 //获取最后一次上传的数据库文件
String key = CommonUtil.getLastDataBaseZip(); String key = CommonUtil.getLastDataBaseZip();
//还原数据库 //还原数据库
MysqlRestoreService mr = new MysqlRestoreService(); MysqlRestoreService mr = new MysqlRestoreService();
mr.restore(mr.jdbcUrl, mr.username, mr.password, key); mr.restore(mr.jdbcUrl, mr.username, mr.password, key);

Loading…
Cancel
Save