node.js - 如何使用节点 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);
});
}
}
}
});
}
};
解决方案
该错误可能是因为您不在具有 mongodb 可执行文件的目录中。有两种方法可以做到这一点。
- 把你的目录改成mongodb的安装路径
- 将 mongodb 可执行文件添加到您的环境变量中
路径应该类似于
{installation_directory}:\Program Files\MongoDB\Server\{version}\bin
例如
C:\Program Files\MongoDB\Server\4.2\bin
推荐阅读
- java - STOMP 在后端 Java Springboot 上接收空值
- java - 您可以使用增强的 for 每个循环将元素添加到数组中吗?
- html - 角度绑定到选择标签表 - 如何获取值?
- reactjs - 在 React 中从另一个文件导入 html 作为 jsx
- java - Java 异常:java.version 字符串的长度低于 2:10
- c# - 将 PropertyGroup 添加到 csProj C#
- javascript - 如何从文档和窗口中删除 JavaScript 触摸事件处理程序?
- python - 如何实时获取 QLineEdit 文本光标位置
- pandas - value_counts 并按组归一化
- sql - 返回 FieldValue 其中 FieldName 等于 Value 并且 ID 等于 Number