2

This is the code :

public static File backupDB() throws IOException {

    File file = null;

    file = new File("BKUP_DB_"
            +   Calendar.getInstance()).replace("/", "-") + ".sql");

    String executeCmd = "mysqldump -u " + "dbUserName" + " -p" + "dbPassword"
    + " " + "dbName" + " > " +file.getPath();



    Process runtimeProcess;
    try {

        runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        if (processComplete == 0) {
            System.out.println("Backup created successfully");
            runtimeProcess.destroy();
            return file;
        } else {

            System.out.println("Could not create the backup");
        }

    } catch (Exception ex) {
        ex.printStackTrace();

    }

    return file;
}

i have tried using the full path of mysqldump like /usr/bin/mysqldum but nothing seems to be working, i am suspecting permission issues about linux. do you have to be root to be able to create a mysql backup? if not, what is the problem with this code ? if yes what is the problem with linux :D? how can i fix this problem. thank you.

4

1 回答 1

12

元字符 ">" 由 shell 实现;没有 shell 参与运行程序,Runtime.exec()因此最后两个参数mysqldump是垃圾。Runtime.exec()使用;的数组参数形式 传递“/bin/sh”作为第一个参数,“-c”作为第二个参数,你的命令行作为第三个参数;这样,shell 元字符将由 /bin/sh 解释。

于 2013-06-10T11:00:38.640 回答