This is my module for creating a mongoDB back up with nodejs server:
const root = require('./root');
const { spawn } = require('child_process');
const config = require('../config.json');
setTimeout(() => {
backupMongoDB();
}, 1000);
function backupMongoDB() {
const DB_NAME = 'pors_db';
const DATE = getTodayDate();
const ARCHIVE_PATH = `${root}/db_backup/${DB_NAME}-${DATE}.gzip`;
const child = spawn('mongodump', [
`--db=${DB_NAME}`,
`--archive=${ARCHIVE_PATH}`,
'--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..');
});
function getTodayDate() {
const date = new Date();
const dd = String(date.getDate()).padStart(2, '0');
const mm = String(date.getMonth() + 1).padStart(2, '0');
const yyyy = date.getFullYear();
return `${yyyy}-${mm}-${dd}`;
}
}
module.exports = backupMongoDB;
This gives me an error:
error creating intents to dump. error for getting collections for database pors_db : unauthorized command listCollections requires authentication.
I tried to connect via this options but it returns the error that it's not a function:
const child = spawn('mongodump', [
`--db=${DB_NAME}`,
`--uri=${config.MONGODB_URI_SERVER}` // this is what I addded
`--archive=${ARCHIVE_PATH}`,
'--gzip',
]);
How can I backup my db without pain ?