Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
backup mongodb without pain using nodejs

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 ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!