This project provides a Node.js script to backup your MongoDB database to a .gzip
file daily using node-cron
.
- Node.js and npm installed on your machine.
- MongoDB Database Tools installed. MongoDB Database Tools download page (specifically
mongodump
andmongorestore
). - A MongoDB Atlas cluster or any MongoDB instance.
-
Clone the repository:
git clone https://github.com/mian-ali/mongodb_dump_nodejs.git cd mongodb_dump_nodejs
-
Install the required packages:
npm install
-
Update
backup.js
with your MongoDB details:const { spawn } = require('child_process'); const path = require('path'); const cron = require('node-cron'); const DB_NAME = '<DB_NAME>'; // Replace with your database name const ARCHIVE_PATH = path.join(__dirname, 'backup', `${DB_NAME}.gzip`); const MONGO_URI = '<Atlas Cluster URI>'; // Replace with your MongoDB connection string // Scheduling the backup every day at midnight cron.schedule('0 0 * * *', () => backupMongoDB()); function backupMongoDB() { const child = spawn('mongodump', [ `--uri=${MONGO_URI}`, `--archive=${ARCHIVE_PATH}`, '--gzip', ]); child.stdout.on('data', (data) => { console.log('stdout:\n', data.toString()); }); child.stderr.on('data', (data) => { console.log('stderr:\n', 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 successful 🎉'); }); }
-
Start the script using
nodemon
:npm run dev
This will start the
backup.js
script and schedule a backup of your MongoDB database daily at midnight.
To restore the MongoDB backup from the .gzip
file, use the following steps:
-
Ensure MongoDB tools are installed:
Make sure you have
mongorestore
available on your machine. -
Run the
mongorestore
command:mongorestore --gzip --archive=path/to/your/backup/chat-app.gzip --nsFrom="chat-app.*" --nsTo="new-chat-app.*" --uri="mongodb+srv://<cred>@<your connection string>/chat-app"
Replace
path/to/your/backup/chat-app.gzip
with the actual path to your backup file, andnew-chat-app
with the database name where you want to restore the data.
mongodb_dump_nodejs/
|── backup
├── backup.js
├── package.json
└── README.md