无法使用ProcessBuilder()备份mysql数据库

时间:2019-04-04 13:20:02

标签: java mysql processbuilder

我已经使用ProcessBuilder()编写了一个备份mysql数据库的代码,因此在这里我无法始终完成过程,我总是将过程完成代码设为6,预期为0

Please someone help for this scenario 

public static boolean backupDataWithDatabase(String dumpExePath, String user, String password, String database,
        String backupPath) {

    boolean status = false;
    try {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("cmd.exe", "/c");
        Process runtimeProcess = processBuilder.start();

        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String filename = "backup(with_DB)-" + database + "-(" + dateFormat.format(date) + ").sql";

        String batchCommand = dumpExePath + " -u" + user + "  -p" + password + " " + database + " >" + backupPath
                + "" + filename + "";

        System.out.println(batchCommand);

        Runtime runtime = Runtime.getRuntime();
        runtimeProcess = runtime.exec(new String[] { "cmd.exe", "/c", batchCommand });

        int processComplete = runtimeProcess.waitFor();

        if (processComplete == 0) {
            status = true;
            System.out.println("Backup created successfully for DB : " + database);
        } else {
            status = false;
            System.out.println("Unable to create backup for DB : " + database);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return status;
}

0 个答案:

没有答案