简体   繁体   English

如何使用 node js 进行 Mongodb 备份

[英]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.我正在编写 mongodb 自动备份代码,但我遇到了一些错误:“mongodump”未被识别为内部或外部命令。 can anyone help me out?谁能帮我吗?

or is there another way to get auto backup with mongodb还是有另一种方法可以使用 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.该错误可能是因为您不在具有 mongodb 可执行文件的目录中。 There are two ways to do it.有两种方法可以做到。

  1. change your directory to mongodb's installation path将您的目录更改为 mongodb 的安装路径
  2. Add the mongodb executable to your environment variables将 mongodb 可执行文件添加到您的环境变量中

Path should be something like路径应该是这样的

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM