Bladeren bron

处理完成Mysql还原

wukai 2 jaren geleden
bovenliggende
commit
b32abca135

+ 50 - 5
sync-common/src/main/java/com/jjt/common/utils/LinuxCommand.java

@@ -22,9 +22,28 @@ public class LinuxCommand {
     public static void exec(String cmd) throws Exception {
         Process process = Runtime.getRuntime().exec(cmd);
         print(process);
-        log.info("{}命令,执行状态{}", cmd, process);
 
-        if (process.waitFor() != 0) {
+        int code = process.waitFor();
+        log.info("{}命令,执行状态{}", cmd, code);
+
+        if (code != 0) {
+            throw new Exception("执行命令出错啦!" + cmd);
+        }
+    }
+
+    /**
+     * 批量执行linux命令
+     *
+     * @param cmd
+     */
+    public static void exec(String[] cmd) throws Exception {
+        Process process = Runtime.getRuntime().exec(cmd);
+        print(process);
+
+        int code = process.waitFor();
+        log.info("{}命令,执行状态{}", cmd, code);
+
+        if (code != 0) {
             throw new Exception("执行命令出错啦!" + cmd);
         }
     }
@@ -58,7 +77,7 @@ public class LinuxCommand {
     /**
      * 执行mysql导出命令
      *
-     * @param commands 命令
+     * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
      * @param path     导出文件路径
      * @throws Exception 异常说明
      */
@@ -70,10 +89,36 @@ public class LinuxCommand {
 
         print(process);
 
-        log.info("mysql备份执行状态:{}", process.waitFor());
-        if (process.waitFor() != 0) {
+        int code = process.waitFor();
+        log.info("mysql备份执行状态:{}", code);
+        if (code != 0) {
+            throw new Exception("执行mysql备份命令出错啦!");
+        }
+    }
+
+    /**
+     * 执行mysql导出命令
+     *
+     * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
+     * @throws Exception 异常说明
+     */
+    public static void mysqlImport(List<String> commands) throws Exception {
+        ProcessBuilder processBuilder = new ProcessBuilder(commands);
+        Process process = processBuilder.start();
+
+        print(process);
+
+        int code = process.waitFor();
+        log.info("mysql备份执行状态:{}", code);
+        if (code != 0) {
             throw new Exception("执行mysql备份命令出错啦!");
         }
+    }
+
 
+    public static void main(String[] args) throws Exception {
+        String[] cmds = {"ipconfig", "/c", "clear"};
+        String cmd = "ipconfig";
+        LinuxCommand.exec(cmds);
     }
 }

+ 18 - 4
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -26,8 +26,10 @@ import java.nio.file.CopyOption;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.List;
 
 /**
  * 数据同步Service业务层处理
@@ -157,18 +159,30 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
 
         JSONObject jsonObject = JSONObject.parseObject(params);
 
-        String hostname = jsonObject.getString("host");
+        String host = jsonObject.getString("host");
         int port = jsonObject.getIntValue("port");
 
         String user = jsonObject.getString("user");
         String pass = jsonObject.getString("pass");
         String db = jsonObject.getString("db");
+        List<String> commands = new ArrayList<>();
+        commands.add("/usr/bin/mysql");
+        commands.add("-u" + user);
+        commands.add("-h");
+        commands.add(host);
+        commands.add("-P");
+        commands.add(port + "");
+        commands.add("-p" + pass);
+        commands.add(db);
+        commands.add("-e");
+        commands.add("source " + zipFile.getPath());
+
         //组装导入命令
-        String cmd = String.format("/usr/bin/mysql -u%s -p%s %s < %s", user, pass, db, zipFile.getPath());
         try {
-            LinuxCommand.exec(cmd);
+            LinuxCommand.mysqlImport(commands);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            log.error("导入mysql文件出错啦:{}", e.getMessage());
+            e.printStackTrace();
         }
     }