Skip to content

Instantly share code, notes, and snippets.

@javierfdezg
Created June 1, 2016 07:25
Show Gist options
  • Save javierfdezg/885555298f218326063fa0133345ad31 to your computer and use it in GitHub Desktop.
Save javierfdezg/885555298f218326063fa0133345ad31 to your computer and use it in GitHub Desktop.
MongoDB Backup and upload to S3
#!/bin/bash
cd /tmp
rm -rf backup
mkdir backup
cd backup
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_USER=""
MONGO_PASS=""
MONGO_DBAUTH=""
MONGO_DB=""
$MONGODUMP_PATH --authenticationDatabase $MONGO_DBAUTH --db $MONGO_DB --username $MONGO_USER --password $MONGO_PASS
TIMESTAMP=`date +%Y%m%d%H%M%S`
mv dump $TIMESTAMP
tar -zcvf $TIMESTAMP.tar.gz $TIMESTAMP
s3Key=""
s3Secret=""
bucket=""
file="$TIMESTAMP.tar.gz"
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -X PUT -T "${file}" \
-H "Host: ${bucket}.s3-eu-west-1.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3-eu-west-1.amazonaws.com/${file}
cd
rm -rf /tmp/backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment