Did you know that MongoDB databases have a built-in backup mechanism that is accessible via shell or the mongod process? The mongod process automatically takes a snapshot every time a database transitions to another state. These different states are: starting, stopping, upgrading, and recovering after a crash. However, these snapshots won’t be enough in case of catastrophic failures such as disk corruption or natural disaster. To protect your valuable data from such threats, it is advisable to implement an automated backup strategy for your MongoDB databases.
In this article, we will discuss how to create automated backups for your MongoDB databases using a simple Shell script.
Shell Script for MongoDB Backup
The shell script for MongoDB database backup is available on Github. You can use the below link to get access of the shell script.
https://github.com/tecrahul/shell-scripts/blob/master/backup-mongo.sh
Alternatively, You can copy the below script and save it on your Linux system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/bin/bash ###################################################################### ## ## MongoDB Database Backup Script ## Written By: Rahul Kumar ## URL: https://tecadmin.net/shell-script-backup-mongodb-database/ ## Update 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 is protected with a username password. # Set AUTH_ENABLED to 1 # and add MONGO_USER and MONGO_PASSWD values correctly AUTH_ENABLED=0 MONGO_USER='' MONGO_PASSWD='' # Set DATABASE_NAMES to "ALL" to backup all databases. # or specify databases names separated with space to backup # specific databases only. DATABASE_NAMES='ALL' #DATABASE_NAMES='mydb db2 newdb' ## Number of days to keep a local 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 choose to backup all databases" mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/ else echo "Running backup for 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 above script in a file with .sh
extension. I want to save all the backups under /the backup directory. So placed the shell script in the same directory. Then set the execute permission on the script.
chmod +x /backup/backup-mongo.sh
Run the shell script as below:
bash /backup/backup-mongo.sh
Schedule MongoDB Backup Script
You can easily schedule this script under crontab to backup databases regularly. To edit the crontab, run crontab -e
command and append the below code:
1 2 | ## Backup database daily at 02:00 AM 0 2 * * * /backup/mongo-backup.sh |
Wrap Up
In this tutorial, we have discussed a shell script that helps to backup MongoDB databases manually. Also, you can schedule scripts to backup databases on regular basis.
2 Comments
Deletion is not working. Help appreciated. Thanks
very nice tuto