Search code examples
javamongodbmongo-java-driver

BackUp and restore db with MongoDB java driver


Is there any possibility in MongoDB java driver to make backUp and restore DB?

My solution (just execute command) for now:

public void makeBackUp(String path) {
    try {
        Runtime.getRuntime().exec("mongodump --out " + path);
    } catch (IOException ex) {
        Logger.getLogger(MongoDB.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public void restore(String backUpPath) {
    try {
        Runtime.getRuntime().exec("mongorestore " + backUpPath);
    } catch (IOException ex) {
        Logger.getLogger(MongoDB.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Thanks in advance.


Solution

  • The short answer is NO as at now. These commands can be invoked only from command line. You might consider to fetch all the data from all collections but its expected to be slow. You can read the discussion around this here.

    [UPDATE]

    However, you can invoke the mongodump and mongorestore commands from the command line in java. This means that you have to ensure that both commands are installed on the machine your code runs on.