Skip to content

Instantly share code, notes, and snippets.

@MacHu-GWU
Created November 5, 2017 02:56
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 MacHu-GWU/d0b860f0daf6e77185f7326e708473b7 to your computer and use it in GitHub Desktop.
Save MacHu-GWU/d0b860f0daf6e77185f7326e708473b7 to your computer and use it in GitHub Desktop.
A bash script backup your mongodb data and store archive to AWS S3.
#!/usr/bin/env bash
DATABASE="Database name here"
HOST="Host here"
PORT=27017
USERNAME="username"
PASSWORD="password"
BUCKET_NAME="www.wbh-doc.com"
STORAGE_PREFIX="FileHost/MongoBackup" # No Slash at Begin and End!
# Logic
S3_DST="s3://${BUCKET_NAME}/${STORAGE_PREFIX}/${HOST}/${DATABASE}/${ARCHIVE_NAME}"
CURRENT_DATETIME="`date +%Y-%m-%dT%H\.%M\.%S%z`"
ARCHIVE_NAME="${CURRENT_DATETIME}.gz"
mongodump --db ${DATABASE} --host ${HOST} --port ${PORT} --username ${USERNAME} --password ${PASSWORD} --gzip --archive=${ARCHIVE_NAME}
aws s3 cp ${ARCHIVE_NAME} ${S3_DST}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment