Skip to content

Instantly share code, notes, and snippets.

@ashokarun
Last active July 31, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashokarun/83db353f82c7637a46f3b040a4aa70f5 to your computer and use it in GitHub Desktop.
Save ashokarun/83db353f82c7637a46f3b040a4aa70f5 to your computer and use it in GitHub Desktop.
Dump Mongo DB and move it to an S3 bucket.
#!/bin/bash
# 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!!"
else
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!"
fi
@ashokarun
Copy link
Author

@juhnny5
Copy link

juhnny5 commented Mar 2, 2020

You can replace:

echo "Enter DB name: "
read db

By:

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.

@ashokarun
Copy link
Author

You can replace:

echo "Enter DB name: "
read db

By:

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.

Thanks

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