|
@@ -1,21 +1,65 @@
|
|
|
-import com.jjt.common.enums.SyncType;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class Test {
|
|
|
- public static void main(String[] args) {
|
|
|
- System.err.println(SyncType.mysql.toString());
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ List<String> commands = new ArrayList<>();
|
|
|
+ commands.add("/usr/bin/sh");
|
|
|
+ commands.add("mongo-full-bak.sh");
|
|
|
+ commands.add("localhost");
|
|
|
+ commands.add("27017");
|
|
|
+ commands.add("/mnt/tt");
|
|
|
+
|
|
|
+ exec(commands, "/mnt/");
|
|
|
+ }
|
|
|
|
|
|
- String s = "/usr/bin/mongodump --host localhost --port 27017 -d local -c oplog.rs -q '{\"ts\":{\"$gt\":{\"$timestamp\":{\"t\":1687157753,\"i\":1}}},\"op\":{\"$ne\":\"d\"},\"op\":{\"$ne\":\"n\"}}' -o /mnt/tt";
|
|
|
- System.err.println(s);
|
|
|
+ /**
|
|
|
+ * 执行shell脚本
|
|
|
+ *
|
|
|
+ * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
|
|
|
+ * @param dir 脚本路径
|
|
|
+ * @throws Exception 异常说明
|
|
|
+ */
|
|
|
+ public static void exec(List<String> commands, String dir) throws Exception {
|
|
|
+ System.err.println("要执行命令了哟:{}" + Arrays.toString(commands.toArray()));
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(commands);
|
|
|
+ processBuilder.directory(new File(dir));
|
|
|
+ Process process = processBuilder.start();
|
|
|
+
|
|
|
+ print(process);
|
|
|
+
|
|
|
+ int code = process.waitFor();
|
|
|
+ System.err.println("命令执行状态:{}" + code);
|
|
|
+ if (code != 0) {
|
|
|
+ throw new Exception("执行命令出错啦!");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private static void print(Process process) {
|
|
|
+ // 获取子进程的输入流
|
|
|
+ InputStream inputStream = process.getInputStream();
|
|
|
+ // 获取子进程的错误流
|
|
|
+ InputStream errorStream = process.getErrorStream();
|
|
|
+ // 获取子进程的输出流
|
|
|
+ OutputStream outputStream = process.getOutputStream();
|
|
|
+ try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);
|
|
|
+ BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
+ BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) {
|
|
|
+ pw.println("exit");
|
|
|
|
|
|
-// try {
|
|
|
-// String cmd = "/usr/bin/mongodump --host 127.0.0.1 --port 27017 --oplog --out /mnt/bak";
|
|
|
-// Process process = Runtime.getRuntime().exec(cmd);
|
|
|
-// } catch (IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// throw new RuntimeException(e);
|
|
|
-// }
|
|
|
+ String line;
|
|
|
+ System.err.println("子进程输入流:");
|
|
|
+ while ((line = readInput.readLine()) != null) {
|
|
|
+ System.err.println(line);
|
|
|
+ }
|
|
|
+ System.err.println("子进程错误流:");
|
|
|
+ while ((line = readError.readLine()) != null) {
|
|
|
+ System.err.println(line);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("出错啦:{}" + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|