6

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.