Skip to content

Instantly share code, notes, and snippets.

@aaronlevin
Last active December 19, 2015 00:16
Show Gist options
  • Save aaronlevin/9946791bee5ac1470ea6 to your computer and use it in GitHub Desktop.
Save aaronlevin/9946791bee5ac1470ea6 to your computer and use it in GitHub Desktop.
Pure bash MySQL Dump --> S3
#!/usr/bin/env bash
# modified from: http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
readonly db_host="xxxx"
readonly db_user="xxxx"
readonly db_pass='xxxx'
readonly db_db="xxx"
readonly aws_key="xxxx"
readonly aws_secret="xxxxx"
readonly s3_bucket="xxx"
io_mysql_dump() {
local host="${1}"
local user="${2}"
local pass="${3}"
local db="${4}"
local file="${5}"
mysqldump \
--host="${host}" \
--user="${user}" \
--password="${pass}" \
"${db}" \
| gzip \
> "${file}"
}
io_s3_upload() {
local file="${1}" # /path/to/file/to/upload.tar.gz
local bucket="${2}" # your-bucket
local s3Key="${3}"
local s3Secret="${4}"
local filename="$(basename ${file})"
local resource="/${bucket}/${filename}"
local contentType="application/x-compressed-tar"
local dateValue=`date -R`
local stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
local signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -X PUT -T "${file}" \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${filename}
}
io_day_of_month() {
date "+%d"
}
main() {
local dump_path="/home/weirdcanada"
local dump_name="weirdcanada-db-$(io_day_of_month).sql.gz"
local dump_full="${dump_path}/${dump_name}"
echo "Creating MySQL Dump at ${dump_full} ..."
io_mysql_dump "${db_host}" "${db_user}" "${db_pass}" "${db_db}" "${dump_name}"
echo "Uploading to Amazon s3://${s3_bucket}/${dump_name}"
io_s3_upload "${dump_full}" "${s3_bucket}" "${aws_key}" "${aws_secret}"
echo "Cleaning up files"
rm -rf "${dump_full}"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment