简体   繁体   中英

How to do Mongodb Backup with node js

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);
                });
            }
            }
        }
        });
    }
};

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

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