Did you know that MongoDB databases have a built-in way to make backups using the shell or the mongod process? The mongod process automatically takes a snapshot (a quick copy) when the database starts, stops, upgrades, or recovers from a crash. But these snapshots are not enough in case something bad happens, like disk damage or a natural disaster. To keep your data safe, it is a good idea to set up an automatic backup system for your MongoDB databases.
In this article, we will explain how to create automatic backups for your MongoDB databases using a simple Shell script.
Shell Script for MongoDB Backup
The shell script for backing up MongoDB databases is available on GitHub. You can use the link below to access the script.
https://github.com/tecrahul/shell-scripts/blob/master/backup-mongo.sh
You can also copy the script below and save it on your Linux system.
#!/bin/bash
######################################################################
## MongoDB Database Backup Script
## Written By: Rahul Kumar
## URL: https://tecadmin.net/shell-script-backup-mongodb-database/
## Updated on: June 20, 2020
######################################################################
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y"`
######################################################################
DB_BACKUP_PATH='/backup/mongo'
MONGO_HOST='localhost'
MONGO_PORT='27017'
# If MongoDB needs a username and password to access
# Set AUTH_ENABLED to 1
# and correctly add MONGO_USER and MONGO_PASSWD values
AUTH_ENABLED=0
MONGO_USER=''
MONGO_PASSWD=''
# To backup all databases, set DATABASE_NAMES to "ALL"
# Or, to backup only some databases, list their names separated by spaces
DATABASE_NAMES='ALL'
#DATABASE_NAMES='mydb db2 newdb'
## Number of days to keep a backup copy
BACKUP_RETAIN_DAYS=30
######################################################################
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
AUTH_PARAM=""
if [ ${AUTH_ENABLED} -eq 1 ]; then
AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
fi
if [ ${DATABASE_NAMES} = "ALL" ]; then
echo "You have chosen to backup all databases"
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
else
echo "Backing up selected databases"
for DB_NAME in ${DATABASE_NAMES}
do
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
done
fi
######## Remove backups older than {BACKUP_RETAIN_DAYS} days ########
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
######################### End of script ##############################
Run Script Manually
Save the script above in a file with a .sh
extension. If you want to save backups in a specific directory, place the script there. Then, give the script permission to run.
chmod +x /backup/backup-mongo.sh
Run the shell script with this command:
bash /backup/backup-mongo.sh
Schedule MongoDB Backup Script
You can set up this script to run automatically at a regular time using crontab. To edit crontab, use the crontab -e
command and add the following line:
## Backup database daily at 02:00 AM
0 2 * * * /backup/mongo-backup.sh
Wrap Up
In this tutorial, we talked about a shell script that helps you manually backup MongoDB databases. You can also schedule the script to run backups automatically on a regular basis.
2 Comments
Deletion is not working. Help appreciated. Thanks
very nice tuto