Created
April 23, 2015 14:13
-
-
Save croaker/2f42dfe72b76b86dbdd8 to your computer and use it in GitHub Desktop.
Simple MySQL Backup
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 | |
# What is required? | |
# | |
# - An installed MySQL Client | |
# - An installed and configured AWS-CLI | |
MYSQLDUMP=/usr/bin/mysqldump | |
DUMP_FILE_SUFFIX=`date +%F`.sql | |
BACKUP_FOLDER=/tmp/backups | |
BACKUP_BUCKET=a_bucket_name | |
LOG_FILE=/tmp/mysql-backup.log | |
DATABASES="a_database_name" | |
DB_HOST="127.0.0.1" | |
DB_USER="root" | |
DB_PASSWORD="password" | |
dump_db () { | |
log_message "Dumping ${1}..." | |
mysqldump --host $DB_HOST --user $DB_USER --password=$DB_PASSWORD \ | |
--add-drop-table --no-create-db --single-transaction \ | |
${1} > ${2} | |
log_result | |
} | |
compress_dump () { | |
log_message "Compressing ${1}..." | |
tar -C $BACKUP_FOLDER --remove-files -zcf $1.tar.gz `basename "$1"` | |
log_result | |
} | |
cleanup_dumps () { | |
log_message "Clearing old dumps.." | |
find $BACKUP_FOLDER -type f -mtime +14 \ | |
-exec sh -c 'test $(date +%u -r "$1") = 1 || rm "$1"' -- {} \; | |
log_result | |
} | |
sync_s3 () { | |
log_message "Syncing S3..." | |
aws s3 sync $BACKUP_FOLDER s3://$BACKUP_BUCKET/ --delete --exclude "*" --include "*.sql.tar.gz" | |
log_result | |
} | |
log_message () { | |
echo "`date +"%F %T"`: $1" >> $LOG_FILE | |
} | |
log_result () { | |
if [ "$?" = "0" ]; then | |
log_message "done." | |
else | |
log_message "FAILED!" | |
fi | |
} | |
for DB in $DATABASES | |
do | |
DUMP_FILENAME="$BACKUP_FOLDER/${DB}_$DUMP_FILE_SUFFIX" | |
dump_db $DB $DUMP_FILENAME | |
compress_dump $DUMP_FILENAME | |
cleanup_dumps | |
sync_s3 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment