MongoDB Database Backup Bash Script

This simple Linux BASH script creates a MongoDB database dump to a file, the script can be used later for some cronjobs.

MongoDB Database Backup Bash Script

mongodb-garanet


GitHub https://github.com/garanet/MongoDB-bkp-script.git

  • Create a folder / backup.
  • Place the following script named ‘mongo_db_bkp.sh’ in the newly created folder.

#!/bin/bash
# www.garanet.net
# MySQL Full Backup Bash Script
# https://github.com/garanet/MongoDB-bkp-script.git

# Define variables
m_host="localhost"
m_user="root"
m_password="secret"
m_database="nameMongoDB"
m_dump="/backup/mongo-dump"

# Backup Database.
/usr/bin/mongodump -h ${m_host} -u ${m_user} -p ${m_password} -d ${m_database} -o ${m_dump}; 
# Compress dump.
tar cvfz /backup/${m_dump}.tar.gz ${m_dump}/; 
# Delete old.
rm -rf ${m_dump};

  • Check that the script owner is root and change the permissions to 744.
  • As root, run the script with the command:: # sh mongo_db_bkp.sh.