Last active
February 4, 2016 07:14
-
-
Save riyadhalnur/92f05811e30d8208dcd6 to your computer and use it in GitHub Desktop.
Take a snapshot of your MongoDB using shell script and upload to S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MONGO_DATABASE="" | |
APP="" | |
TIMESTAMP=`date +%F-%H%M` | |
MONGODUMP_PATH="/usr/bin/mongodump" | |
BACKUPS_DIR="/home/me/backups/$APP" | |
BACKUP_NAME="$APP-$TIMESTAMP" | |
S3_KEY="" | |
S3_SECRET="" | |
S3_BUCKET="" | |
AWS_PATH="/backups" | |
export LC_ALL=C | |
mkdir -p $BACKUPS_DIR | |
$MONGODUMP_PATH --db $MONGO_DATABASE -out $BACKUPS_DIR/$BACKUP_NAME | |
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUPS_DIR/$BACKUP_NAME | |
rm -rf $BACKUPS_DIR/$BACKUP_NAME | |
function uploadS3 { | |
date=$(date +"%a, %d %b %Y %T %z") | |
acl="x-amz-acl:public-read" | |
content_type='application/x-compressed' | |
string="PUT\n\n$content_type\n$date\n$acl\n/$S3_BUCKET$AWS_PATH/$BACKUP_NAME.tgz" | |
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3_SECRET}" -binary | base64) | |
curl -X PUT -T "$BACKUPS_DIR/$BACKUP_NAME.tgz" \ | |
-H "Host: $S3_BUCKET.s3.amazonaws.com" \ | |
-H "Date: $date" \ | |
-H "Content-Type: $content_type" \ | |
-H "$acl" \ | |
-H "Authorization: AWS ${S3_KEY}:$signature" \ | |
"https://$S3_BUCKET.s3.amazonaws.com$AWS_PATH/$BACKUP_NAME.tgz" | |
} | |
uploadS3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment