如何使用NodeJs使用MongoDump-uri备份MongoAtlasM0集群



如何使用mongodump URI备份mongoAtlas M0集群。这个URI在终端中运行得很好,我得到了gzip文件。使用spawn,我调用这个URI,它返回错误代码。是否有任何方法可以使用节点cron定期备份M0集群。

const { spawn } = require('child_process');
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
backupMongoDbData();
});
async function backupMongoDbData{
const child = await spawn('mongodump', [
"--uri mongodb+srv://DBusername:DBpassword@DBcluster/databaseName",//this is CommandLine tool for Mongodump
`--archive=./BackupData`,
'--gzip',
]);

child.stdout.on('data', (data) => {
console.log('stdout:n', data);
});
child.stderr.on('data', (data) => {
console.log('stderr:n', Buffer.from(data).toString());
});
child.on('error', (error) => {
console.log('error:n', error);
});
child.on('exit', (code, signal) => {
if (code) console.log('Process exit with code:', code);
else if (signal) console.log('Process killed with signal:', signal);
else console.log('Backup is successfull ');
});
}

尝试在"--uri"后面添加"="。像这样:

function backupMongoDbData(){

const-child=spawn('mongdump'[&quot--uri="+uri,//这是Mongodump的命令行工具CCD_ 1,'-gzip’,]);

最新更新