Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created June 17, 2014 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbengfort/e05e116b811e2229193b to your computer and use it in GitHub Desktop.
Save bbengfort/e05e116b811e2229193b to your computer and use it in GitHub Desktop.
Backup mongo to s3
#!/bin/bash
# Dumps the Mongo Database and exports it to S3
# Make sure to run this script in the background! (nohup)
WORKING_DIRECTORY="/mnt/vol-604be929/tmp/data"
if [ ! -d "$WORKING_DIRECTORY" ]; then
mkdir -p $WORKING_DIRECTORY
fi
cd $WORKING_DIRECTORY
# Force file synchronization and lock writes
#mongo admin --eval "printjson(db.fsyncLock())"
MONGODUMP="/usr/bin/mongodump"
MONGO_HOST="localhost" # replace with server IP
MONGO_PORT="27017" # replace with server port
# Other settings
TIMESTAMP=`date +%Y%m%dT%H%M%S`
S3_BUCKET="cobrainabsolem"
S3_PATH="datadumps/kerkes"
LOG_PATH="/mnt/vol-604be929/mongodb/logs/mongodump.log"
DUMP_NAME="mongodb-$HOSTNAME-$TIMESTAMP.dump"
# Create the backup
$MONGODUMP --host $MONGO_HOST --port $MONGO_PORT 2>&1 >> $LOG_PATH
# Add timestamp to backup
mv dump $DUMP_NAME
tar czf $DUMP_NAME.tgz $DUMP_NAME
# Upload to s3
aws s3 mv $DUMP_NAME.tgz s3://$S3_BUCKET/$S3_PATH/$DUMP_NAME.tgz
# Delete the old backup
rm -rf $DUMP_NAME
@edelans
Copy link

edelans commented Jun 20, 2014

Hi, thanks for the script, may I ask you how dump is defined ?? (l31).

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