Tuesday, October 6, 2020

How To Create MongoDump Backup Script in Linux

 How To Create MongoDump Backup Script in Linux

MongoDump is a utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances; i.e. can export data from standalone, replica set, and sharded cluster deployments.


1) Create backup stoerd directory and move on it.

$ mkdir /home/ubuntu/mongo_database_backup

$ cd    /home/ubuntu/mongo_database_backup


2) Create backup script file 

$ sudo vim mongodump_backup.sh

  • press i button (for insert mode )
  • and add below yellow mark lines
#!/bin/bash

mongodump --username testuser --password 123456 --db testdb

zip -r /home/ubuntu/mongo_database_backup/database__`date '+\%d\%m\%y_%H:%M:%S'`.zip dump

rm -rf dump

  •  after add , save and quit the file




3) after add yellow mark lines then Save and quit the file press ( shift+:wq! ) and enter

4) Give excutive permission to the mongodump_backup.sh file

$ sudo chmod +x mongodump_backup.sh file


5) Set cron job for database backup script file

$ crontab -e



  • add below yellow mark line in crontab -e (cron will run every 10 min) Note: adjust cron time acording to you
  • */10 * * * * /home/ubuntu/mongo_database_backup/mongodump_backup.sh
  • after added save and quit the file press ( shit+:wq ) enter


6) Now check the database backup after 10 min (as you mentioned time on crontab) 

$ cd /home/ubuntu/mongo_database_backup
$ ls


No comments:

Post a Comment

testing