Skip to content

Instantly share code, notes, and snippets.

@jclausen
Created March 5, 2017 22:49
Show Gist options
  • Save jclausen/b7315b678e52da67746dfc42c2d56d5b to your computer and use it in GitHub Desktop.
Save jclausen/b7315b678e52da67746dfc42c2d56d5b to your computer and use it in GitHub Desktop.
MySQL Backup Databases to S3
#!/bin/bash
source ~/.bashrc
cd /tmp
# get a list of all the databases
DBS=`mysql -u${MYSQL_USER} --password=${MYSQL_PASS} -e"SHOW DATABASES;" --batch -s`
# Loop through all the databases and create a dump of each one, followed by
# piping to gzip to create a compressed verison of the dump.
for db in $DBS; do
if [ "$db" != "information_schema" ] && [ "$db" != "performance_schema" ]; then
file=${db}-$(date +%Y-%m-%d)-$(date +%s).sql
mysqldump \
-u ${MYSQL_USER} \
--password="${MYSQL_PASS}" \
${db} > ${file}
if [ "${?}" -eq 0 ]; then
gzip ${file}
echo "Copying ${file} to AWS Bucket ${S3_BUCKET}"
aws s3 cp --quiet ${file}.gz s3://${S3_BUCKET}/
echo "File transfer complete"
#rm ${file}.gz
else
echo "Error backing up $db to $file"
continue
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment