Last active
May 9, 2017 21:09
-
-
Save subimage/e4264322504c223f1636a04b3c60dc7a to your computer and use it in GitHub Desktop.
UNIX bash shell script to store 30 days worth of MySQL backups on Amazon 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 | |
# Backs up MySQL databases with zero downtime using Percona Xtrabackup. | |
# Stores 30 days worth on Amazon s3. | |
# These {{ double bracket }} things are Ansible/Jinja (ansible-vault) variables. | |
# Fill them in for your own usage. | |
MYSQLROOT=root | |
MYSQLPASS="{{ mysql_root_pass }}" | |
S3BUCKET={{ mysql_backup_bucket }} | |
BACKUP_DIR={{ mysql_archive_path }} | |
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") | |
# Performance tuning | |
NUM_THREADS=4 | |
USE_MEMORY={{ (ansible_memory_mb.real.total * 0.25) | int }}M | |
BACKUP_COMMAND="/usr/bin/innobackupex --user=${MYSQLROOT} --password=${MYSQLPASS} --no-timestamp ${BACKUP_DIR}/latest/ --parallel=${NUM_THREADS} --use-memory=${USE_MEMORY}" | |
CURRENT_ARCHIVE="${BACKUP_DIR}/mysql_${TIMESTAMP}.tar.gz" | |
echo "= Remove previous backup ------------------------------------------------" | |
rm -rf ${BACKUP_DIR}/latest/* | |
echo "= Ensure file permissions are intelligent -------------------------------" | |
chmod -R 700 ${BACKUP_DIR} | |
echo "= Back that SQL up... ---------------------------------------------------" | |
${BACKUP_COMMAND} | |
echo "= Prepare backup for immediate use --------------------------------------" | |
${BACKUP_COMMAND} --apply-log | |
echo "= Compress mysql archive ------------------------------------------------" | |
tar czf ${CURRENT_ARCHIVE} -C ${BACKUP_DIR}/latest/ ${BACKUP_DIR}/latest/* | |
echo "= Upload archive to s3 --------------------------------------------------" | |
s3cmd put -f ${CURRENT_ARCHIVE} s3://${S3BUCKET}/ | |
echo "= Remove 30 day old LOCAL backups ---------------------------------------" | |
ls -1tr ${BACKUP_DIR}|head -n -30|xargs -d '\n' rm -rf -- | |
echo "= Remove 30 day old s3 backups ------------------------------------------" | |
s3cmd ls s3://${S3BUCKET} | while read -r line; | |
do | |
createDate=`echo $line|awk {'print $1" "$2'}` | |
createDate=`date -d"$createDate" +%s` | |
olderThan=`date -d"-$2" +%s` | |
if [[ $createDate -lt $olderThan ]] | |
then | |
fileName=`echo $line|awk {'print $4'}` | |
echo $fileName | |
if [[ $fileName != "" ]] | |
then | |
s3cmd del "$fileName" | |
fi | |
fi | |
done; | |
echo "" | |
echo "MySQL backup done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment