0

I am wirting a mongodb auto backup code but i am stuck in some error: 'mongodump' is not recognized as an internal or external command. can anyone help me out?

or is there another way to get auto backup with mongodb

exports.dbAutoBackUp = () => {

        let cmd =
        'mongodump --host ' +
        dbOptions.host +
        ' --port ' +
        dbOptions.port +
        ' --db ' +
        dbOptions.database +
        ' --username ' +
        dbOptions.user +
        ' --password ' +
        dbOptions.pass +
        ' --out ' +
        newBackupPath;

        exec(cmd, (error, stdout, stderr) => {
            console.log("Error : "+error)
            console.log("Error 1: "+stdout)
            console.log("Error 2: "+stderr)
        if (this.empty(error)) {
            // check for remove old backup after keeping # of days given in configuration.
            if (dbOptions.removeOldBackup == true) {
            if (fs.existsSync(oldBackupPath)) {
                exec('rm -rf ' + oldBackupPath, err => {
                    console.log(err);
                });
            }
            }
        }
        });
    }
};
2
  • What is your current MongoDB setup btw ? What version is installed and are you sure you have configured your PATH correctly to make mongodump util available globally on your system ? Commented Feb 3, 2020 at 11:12
  • Did you look at any node module out there? Commented Feb 4, 2020 at 8:13

1 Answer 1

0

The error is probably because you are not in the directory where you have mongodb executable. There are two ways to do it.

  1. change your directory to mongodb's installation path
  2. Add the mongodb executable to your environment variables

Path should be something like

{installation_directory}:\Program Files\MongoDB\Server\{version}\bin

For example C:\Program Files\MongoDB\Server\4.2\bin

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

2 Comments

can you explain more, like how to this because my mongo db database is running on docker contaienr and this code is running on another container,
sorry you didn't mention about docker. take a look here

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.