Please follow the below steps to backup the MongoDB.
1) Install AWS CLI with the below command.
pip install awscli –upgrade –user
2) Configure AWS client with the below command.
aws configure
3) After running the above command it will ask for the below details which are related to IAM user. If you have not created IAM user, create and give the details.
AWS Access Key ID [None]: KEY
AWS Secret Access Key [None]: SECRET
Default region name [None]: region
Default output format [None]: json

4) Create a directory scripts wherever you wish and paste the below script in a file( ex:- under that directory. Before that make a directory db_backups and give that path in the script.
backup_name=~/db_backups-`date +%Y-%m-%d-%H%M`
mongodump –out $backup_name
tar czf $backup_name.tar.gz $backup_name
aws s3 cp $backup_name.tar.gz s3://my_bucket/db_backups/
rm -rf $backup_name
rm $backup_name.tar.gz

5) The last step is to configure cron to run the script every day, we should first open cron file:
crontab -e
6) Add something like below
0 0 * * * /bin/bash ~/bin/ >> ~/logs/db_backups.log 2>&1
7) The above line asks cron to run the script every day at midnight.
8) To test it, run the below command
/bin/bash ~/bin/

Also we can wait to the next day and verify in our Amazon Web Services account that the file was created.

Leave a Reply