|
@@ -1,7 +1,5 @@
|
|
|
package com.qmth.distributed.print.business.backup;
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
-import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
|
|
|
import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
@@ -9,12 +7,12 @@ import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Date;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
@@ -32,27 +30,33 @@ public class MySQLDatabaseBackup {
|
|
|
@Resource
|
|
|
DictionaryConfig dictionaryConfig;
|
|
|
|
|
|
+ @Value("${db.host}")
|
|
|
+ String host;
|
|
|
+
|
|
|
+ @Value("${db.port}")
|
|
|
+ String port;
|
|
|
+
|
|
|
+ @Value("${db.name}")
|
|
|
+ String databaseName;
|
|
|
+
|
|
|
+ @Value("${db.username}")
|
|
|
+ String username;
|
|
|
+
|
|
|
+ @Value("${db.password}")
|
|
|
+ String password;
|
|
|
+
|
|
|
/**
|
|
|
* Java代码实现MySQL数据库导出
|
|
|
*
|
|
|
- * @param hostIP
|
|
|
- * @param userName
|
|
|
- * @param password
|
|
|
* @param fileName
|
|
|
- * @param databaseName
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public boolean exportDatabaseTool(String hostIP, String userName, String password, String fileName, String databaseName) throws Exception {
|
|
|
-// File saveFile = new File(savePath);
|
|
|
-// if (!saveFile.exists()) {// 如果目录不存在
|
|
|
-// saveFile.mkdirs();// 创建文件夹
|
|
|
-// }
|
|
|
-// if (!savePath.endsWith(File.separator)) {
|
|
|
-// savePath = savePath + File.separator;
|
|
|
-// }
|
|
|
-// File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
- File file = new File("/Users/king/Downloads/file-temp/db/backup" + File.separator + fileName);
|
|
|
+ public boolean exportDatabaseTool(String fileName) throws Exception {
|
|
|
+ Optional.ofNullable(fileName).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件名不能为空"));
|
|
|
+
|
|
|
+ File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
+// File file = new File("/Users/king/Downloads/file-temp/db/backup" + File.separator + fileName);
|
|
|
if (!file.exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
file.createNewFile();
|
|
@@ -62,7 +66,7 @@ public class MySQLDatabaseBackup {
|
|
|
BufferedReader bufferedReader = null;
|
|
|
try {
|
|
|
printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
|
|
|
- String execSql = "mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=" + StandardCharsets.UTF_8 + " " + databaseName;
|
|
|
+ String execSql = "mysqldump -h" + this.host + " -P" + this.port + " -u" + this.username + " -p" + this.password + " --set-charset=" + StandardCharsets.UTF_8 + " " + this.databaseName;
|
|
|
Process process = Runtime.getRuntime().exec(execSql);
|
|
|
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8);
|
|
|
bufferedReader = new BufferedReader(inputStreamReader);
|
|
@@ -86,31 +90,29 @@ public class MySQLDatabaseBackup {
|
|
|
/**
|
|
|
* 删除数据库数据
|
|
|
*
|
|
|
- * @param hostIP
|
|
|
- * @param userName
|
|
|
- * @param password
|
|
|
- * @param databaseName
|
|
|
* @param fileName
|
|
|
* @param schoolId
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public void deleteDbTool(String hostIP, String userName, String password, String databaseName, String fileName, Long schoolId) throws Exception {
|
|
|
-// InputStream inputStream = MySQLDatabaseBackup.class.getClassLoader().getResourceAsStream(fileName);
|
|
|
-// Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
|
|
|
-// File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
-// if (!file.exists()) {
|
|
|
-// file.getParentFile().mkdirs();
|
|
|
-// file.createNewFile();
|
|
|
-// }
|
|
|
-// FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
-
|
|
|
+ public void deleteDbTool(String fileName, Long schoolId) throws Exception {
|
|
|
+ Optional.ofNullable(fileName).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件名不能为空"));
|
|
|
Optional.ofNullable(schoolId).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("学校id不能为空"));
|
|
|
- File file = new File("/Users/king/Downloads/file-temp/db/delete" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_" + schoolId + "_delete.sql");
|
|
|
+
|
|
|
+ InputStream inputStream = MySQLDatabaseBackup.class.getClassLoader().getResourceAsStream(fileName);
|
|
|
+ Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
|
|
|
+ File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
if (!file.exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
file.createNewFile();
|
|
|
}
|
|
|
- FileUtils.copyInputStreamToFile(new FileInputStream(new File(fileName)), file);
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
+
|
|
|
+// File file = new File("/Users/king/Downloads/file-temp/db/delete" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_" + schoolId + "_delete.sql");
|
|
|
+// if (!file.exists()) {
|
|
|
+// file.getParentFile().mkdirs();
|
|
|
+// file.createNewFile();
|
|
|
+// }
|
|
|
+// FileUtils.copyInputStreamToFile(new FileInputStream(new File(fileName)), file);
|
|
|
|
|
|
ByteArrayOutputStream ou = new ByteArrayOutputStream();
|
|
|
IOUtils.copy(new FileInputStream(file), ou);
|
|
@@ -119,7 +121,7 @@ public class MySQLDatabaseBackup {
|
|
|
string = string.replaceAll("\\#\\{schoolId\\}", String.valueOf(schoolId));
|
|
|
}
|
|
|
IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
- String cmdarray[] = {"mysql -h" + hostIP + " -u" + userName + " -p" + password + " " + databaseName, "source " + file.getPath()};
|
|
|
+ String cmdarray[] = {"mysql -h" + this.host + " -P" + this.port + " -u" + this.username + " -p" + this.password + " " + this.databaseName, "source " + file.getPath()};
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
Process process = null;
|
|
|
try {
|
|
@@ -150,27 +152,27 @@ public class MySQLDatabaseBackup {
|
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
|
} finally {
|
|
|
if (process.waitFor() == 0 && Objects.nonNull(file)) {
|
|
|
- string = "用户" + "删除数据成功";
|
|
|
+ string = "删除数据成功";
|
|
|
IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- MySQLDatabaseBackup mySQLDatabaseBackup = new MySQLDatabaseBackup();
|
|
|
- try {
|
|
|
- String host = "127.0.0.1", userName = "root", password = "123456789", databaseName = "distributed-v3.2.0";
|
|
|
- Long schoolId = 290869043907264512L;
|
|
|
- if (mySQLDatabaseBackup.exportDatabaseTool(host, userName, password, DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_backup.sql", databaseName)) {
|
|
|
- System.out.println("数据库成功备份!!!");
|
|
|
-// deleteDbTool(host, userName, password, databaseName, SystemConstant.PRINT_DELETE_DATA_FILE_NAME, schoolId);
|
|
|
- mySQLDatabaseBackup.deleteDbTool(host, userName, password, databaseName, "/Users/king/git/distributed-print-service/distributed-print-business/src/main/resources/db/4、delete-data.sql", schoolId);
|
|
|
- System.out.println("删除数据成功!!!");
|
|
|
- } else {
|
|
|
- System.out.println("数据库备份失败!!!");
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+// public static void main(String[] args) {
|
|
|
+// MySQLDatabaseBackup mySQLDatabaseBackup = new MySQLDatabaseBackup();
|
|
|
+// try {
|
|
|
+// String host = "127.0.0.1", userName = "root", password = "123456789", databaseName = "distributed-v3.2.0";
|
|
|
+// Long schoolId = 290869043907264512L;
|
|
|
+// if (mySQLDatabaseBackup.exportDatabaseTool(this.host, userName, password, DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_backup.sql", databaseName)) {
|
|
|
+// System.out.println("数据库成功备份!!!");
|
|
|
+//// deleteDbTool(host, userName, password, databaseName, SystemConstant.PRINT_DELETE_DATA_FILE_NAME, schoolId);
|
|
|
+// mySQLDatabaseBackup.deleteDbTool(host, userName, password, databaseName, "/Users/king/git/distributed-print-service/distributed-print-business/src/main/resources/db/4、delete-data.sql", schoolId);
|
|
|
+// System.out.println("删除数据成功!!!");
|
|
|
+// } else {
|
|
|
+// System.out.println("数据库备份失败!!!");
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|