Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mian-ali/mongodb_dump_nodejs
This project provides a Node.js script to scheduled backup your MongoDB database to a .gzip file daily using node-cron.
https://github.com/mian-ali/mongodb_dump_nodejs
databases-dump mongodb mongodb-backup mongodump
Last synced: about 9 hours ago
JSON representation
This project provides a Node.js script to scheduled backup your MongoDB database to a .gzip file daily using node-cron.
- Host: GitHub
- URL: https://github.com/mian-ali/mongodb_dump_nodejs
- Owner: mian-ali
- License: mit
- Created: 2024-07-23T15:40:34.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2024-07-26T17:37:29.000Z (3 months ago)
- Last Synced: 2024-08-24T02:16:10.734Z (3 months ago)
- Topics: databases-dump, mongodb, mongodb-backup, mongodump
- Language: JavaScript
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MongoDB Backup Script with Node.js
This project provides a Node.js script to backup your MongoDB database to a `.gzip` file daily using `node-cron`.
## Prerequisites
- Node.js and npm installed on your machine.
- MongoDB Database Tools installed. [MongoDB Database Tools download page](https://www.mongodb.com/try/download/database-tools) (specifically `mongodump` and `mongorestore`).
- A MongoDB Atlas cluster or any MongoDB instance.## Installation
1. **Clone the repository:**
```bash
git clone https://github.com/mian-ali/mongodb_dump_nodejs.git
cd mongodb_dump_nodejs
```2. **Install the required packages:**
```bash
npm install
```## Configuration
1. **Update `backup.js` with your MongoDB details:**
```javascript
const { spawn } = require('child_process');
const path = require('path');
const cron = require('node-cron');const DB_NAME = ''; // Replace with your database name
const ARCHIVE_PATH = path.join(__dirname, 'backup', `${DB_NAME}.gzip`);
const MONGO_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 🎉');
});
}
```## Running the Backup Script
1. **Start the script using `nodemon`:**
```bash
npm run dev
```This will start the `backup.js` script and schedule a backup of your MongoDB database daily at midnight.
## Restoring the Backup
To restore the MongoDB backup from the `.gzip` file, use the following steps:
1. **Ensure MongoDB tools are installed:**
Make sure you have `mongorestore` available on your machine.
2. **Run the `mongorestore` command:**
```bash
mongorestore --gzip --archive=path/to/your/backup/chat-app.gzip --nsFrom="chat-app.*" --nsTo="new-chat-app.*" --uri="mongodb+srv://@/chat-app"
```Replace `path/to/your/backup/chat-app.gzip` with the actual path to your backup file, and `new-chat-app` with the database name where you want to restore the data.
## Project Structure
```plaintext
mongodb_dump_nodejs/
|── backup
├── backup.js
├── package.json
└── README.md