Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dump Mongo DB and move it to an S3 bucket.
# This will create a full db dump and store it to s3 bucket my-db-backup-bucket in folder full-db-backup
# You can modify all variables as you wish
# Here we are making dump to /data/database-backup/ make sure that the directory is there.
# Here we're not keeping dumps locally.
echo "Enter DB name: "
read db
echo "removing current backups from /data/database-backup/"
rm -rf /data/database-backup/*
echo "Creating db dump to /data/database-backup/"
mongodump --db $db -o /data/database-backup/$db-$(date +%F:%R)
status=$(echo "$?")
if [ $status != 0 ]; then
echo "Something Wrong!!"
echo "All set"
echo "Moving backup to the s3 bucket mongo-db-backup-bucket"
cmd="aws s3 sync /data/database-backup/ s3://my-db-backup-bucket/full-db-backup --profile user2"
nohup $cmd &
echo "Done!"

This comment has been minimized.

Copy link
Owner Author

@ashokarun ashokarun commented Nov 3, 2018


This comment has been minimized.

Copy link

@julienbriault julienbriault commented Mar 2, 2020

You can replace:

echo "Enter DB name: "
read db


read -p "Enter DB name: " db

It's nicer and in case you have a script with several times questions to ask, it stays cleaner that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment