简体   繁体   中英

backup mysql database without losing portability with java

i am trying to add a "create backup" option on one of my desktop applications which i have developed by java. i have searched a lot about this and found some approaches to solve this problem, but all of them destroy a single important concept "portability" . many people suggest to use mysqldump.exe (in windows) to do this, but i guess i need to know the mysql installation folder in order to take this approach . the below is the recommended code which by the way failed to run properly (i dont know why, please tell me if you notice the reason)

private static String dbName = "shams";
private static String dbUserName = "root";
private static String dbPassword = "";

public static boolean backupDB(File file) {
    String path = file.getPath();
    if (!path.contains(".sql")) {
        file = new File(path +".sql");
    }

    String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + 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 true;
        } else {

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


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


    }

    return false;
}

and some suggest to simply copy the database to a certain location.

so.. what should i do to this in a neat way? any third party jar available?

--edit ---

the problem with the given code is that, when you dont have a password for a user (in this case root user) you should not mention the password in your command. the command should be like this : String executeCmd = "mysqldump -u " + dbUserName + " --add-drop-database -B " + dbName + " -r " + file.getPath();

and also you don't need to specify the mysql installation folder after all. the above code works as it is.

First, needing to know the location of the mysqldump executable does not necessarily mean a loss of portability - just make it a configuration parameter - either as a command-line option to the program, or in a configuration file.

Second, your issue with running the mysqldump command is likely related to your use of Runtime.exec(). The ProcessBuilder object gives you much more control. There are many questions/answers here on SO and all over the web about the proper use of ProcessBuilder - you will likely find some of them useful.

As far as I know, there is no all-java way to perform a mysqldump without actually running the mysqldump executable.

There are pure Java solutions to this problem; eg

(I haven't checked either of these to see if they do a decent job ... YMMV.)

However, I'd be wary of using a 3rd-party dump program, because of the risk that you might end up with a dump that doesn't restore properly.

Yes, you do need to know the path to the mysqldump program. But that can be dealt with using sensible defaults and/or an entry in a configuration file. Your "destroying portability" comment is making a mountain out of a molehill.


I cannot see anything obviously wrong with your code. I suggest that you capture the "standard error" and "standard output" from mysqldump and see what it says.

  • Maybe it is something like incorrect credentials or a non-existent output directory.
  • Maybe you got one of the options incorrect. (Is that really how you are supposed to use the -B option??)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM