Skip to content

Instantly share code, notes, and snippets.

@urielaero
Last active March 4, 2019 22:51
Show Gist options
  • Save urielaero/ca7d612b2923ce0105d0cdd7e4226bd4 to your computer and use it in GitHub Desktop.
Save urielaero/ca7d612b2923ce0105d0cdd7e4226bd4 to your computer and use it in GitHub Desktop.
mysqldump and upload to s3
#!/bin/bash
# mysql -u root -p
PASSWORD="PASSWORD"
USER="USER"
bucket="BUCKETNAME"
s3Key="S3KEY"
s3Secret="S3SECRETKEY"
echo "----- $(date) -----"
echo "run backup"
SHOWDBS=$(mysql --user=$USER --password=$PASSWORD --execute="show databases;" --silent)
date=`date +%Y%m%d`
file="${date}_compress.tar.gz"
mkdir -p _backups
cd _backups
for line in $SHOWDBS; do
if [[ $line == *"FILTERBY"* ]]; then
# FILTERBY if check for a string in db name for backup only that.
echo "run backup! $line"
mysqldump -u root -p$PASSWORD $line > "${line}.sql"
fi
done
tar czf $file *
echo "upload to s3..."
# source http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
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.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${file}
echo "done"
echo "______________"
cd ..
rm -rf _backups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment