BackUp and restore db with MongoDB java driver
Asked Answered
T

1

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.

Thuthucydides answered 19/7, 2016 at 11:22 Comment(0)
H
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.

Hoxsie answered 20/7, 2016 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.