A Shell Script to Backup MongoDB Database

A Shell Script to Backup MongoDB Database

Shell Script to Backup MongoDB Database

Creating a shell script for backing up a MongoDB database can automate the process and save time. Below is a simple shell script that uses mongodump to back up a MongoDB database and store the backup in a specific directory.

1. Create the Shell Script

  1. Open a terminal and create a new script file. You can name it backup_mongo.sh:

    touch backup_mongo.sh
  2. Open the script file in your preferred text editor (e.g., nano):

    nano backup_mongo.sh
  3. Add the following content to the script:

#!/bin/bash # Set the MongoDB connection details (replace with your details) MONGO_HOST="127.0.0.1" # MongoDB Host (default is localhost) MONGO_PORT="27017" # MongoDB Port (default is 27017) MONGO_DB="blogDB" # MongoDB Database Name to back up BACKUP_DIR="/path/to/backup" # Backup directory (modify to your desired location) DATE=$(date +"%Y%m%d_%H%M%S") # Timestamp for unique backup folder # Create a backup folder with timestamp mkdir -p "$BACKUP_DIR/$DATE" # Perform the backup using mongodump echo "Starting backup of $MONGO_DB database..." mongodump --host "$MONGO_HOST" --port "$MONGO_PORT" --db "$MONGO_DB" --out "$BACKUP_DIR/$DATE" # Check if mongodump was successful if [ $? -eq 0 ]; then echo "✅ Backup completed successfully! Backup is stored in $BACKUP_DIR/$DATE" else echo "❌ Backup failed!" fi

4. Explanation of the Script

  • MONGO_HOST: The MongoDB server's hostname or IP address (default is 127.0.0.1 for local MongoDB).
  • MONGO_PORT: The port MongoDB is listening on (default is 27017).
  • MONGO_DB: The name of the database you want to back up (e.g., blogDB).
  • BACKUP_DIR: The directory where backups will be stored. Modify it to your desired path.
  • DATE: A timestamp to make each backup folder unique.
  • mongodump: The command to create the backup, which includes the database name and backup location.
  • The script checks if mongodump was successful by inspecting the exit status ($?). If the command returns 0, it means success.

5. Make the Script Executable

After saving the file, make the script executable by running the following command:

chmod +x backup_mongo.sh

6. Run the Script

Now, you can run the script to back up your MongoDB database:

./backup_mongo.sh

7. Verify the Backup

Check the BACKUP_DIR directory for the backup files. The backup should be stored in a folder with the current date and time in the directory you specified ($BACKUP_DIR/$DATE).

8. Automate the Backup (Optional)

To automate the backup process, you can schedule the script to run at specific intervals using cron jobs.

  1. Open the crontab editor:

    crontab -e
  2. Add a cron job to run the script every day at midnight (or adjust the timing as needed):

    0 0 * * * /path/to/backup_mongo.sh

This will execute the backup script every day at 12:00 AM.

9. Conclusion

  • Created a shell script to back up a MongoDB database automatically.
  • Scheduled backups using cron for automation.
  • The backup is stored with a timestamp, ensuring unique backup folders for each run.

Would you like more help on scheduling backups or any specific customizations for your script? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close