首页 > 解决方案 > 如何使用节点 js 进行 Mongodb 备份

问题描述

我正在编写一个 mongodb 自动备份代码,但我遇到了一些错误:“mongodump”不被识别为内部或外部命令。谁能帮我吗?

还是有另一种方法可以使用 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);
                });
            }
            }
        }
        });
    }
};

标签: node.jsmongodbexpressdatabase-backups

解决方案


该错误可能是因为您不在具有 mongodb 可执行文件的目录中。有两种方法可以做到这一点。

  1. 把你的目录改成mongodb的安装路径
  2. 将 mongodb 可执行文件添加到您的环境变量中

路径应该类似于

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

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


推荐阅读