|
@@ -121,38 +121,56 @@ public class MySQLDatabaseBackup {
|
|
|
IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
String cmdarray[] = {"mysql -h" + hostIP + " -u" + userName + " -p" + password + " " + databaseName, "source " + file.getPath()};
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
- OutputStream os = null;
|
|
|
- OutputStreamWriter writer = null;
|
|
|
+ Process process = null;
|
|
|
try {
|
|
|
- Process process = runtime.exec(cmdarray[0]);//cmd之后执行数组的第一个条件进入数据库
|
|
|
+ process = runtime.exec(cmdarray[0]);//cmd之后执行数组的第一个条件进入数据库
|
|
|
//执行了第一条命令以后已经登录到mysql了
|
|
|
- os = process.getOutputStream();
|
|
|
- writer = new OutputStreamWriter(os);
|
|
|
- IOUtils.write(cmdarray[1], writer);
|
|
|
+ Process finalProcess = process;
|
|
|
+ new Thread(() -> {
|
|
|
+ OutputStream os = finalProcess.getOutputStream();
|
|
|
+ OutputStreamWriter writer = new OutputStreamWriter(os);
|
|
|
+ try {
|
|
|
+ IOUtils.write(cmdarray[1], writer);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (Objects.nonNull(writer)) {
|
|
|
+ writer.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(os)) {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
} finally {
|
|
|
- if (Objects.nonNull(writer)) {
|
|
|
- writer.close();
|
|
|
- }
|
|
|
- if (Objects.nonNull(os)) {
|
|
|
- os.close();
|
|
|
+ if (process.waitFor() == 0 && Objects.nonNull(file)) {
|
|
|
+ string = "用户" + "删除数据成功";
|
|
|
+ IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// public static void main(String[] args) {
|
|
|
-// try {
|
|
|
-// String host = "127.0.0.1", userName = "root", password = "123456789", databaseName = "distributed-v3.2.0";
|
|
|
-// Long schoolId = 290869043907264512L;
|
|
|
-// if (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);
|
|
|
-// 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(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();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|