Created
July 16, 2013 21:58
-
-
Save cedricdekimpe/6015543 to your computer and use it in GitHub Desktop.
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 | |
bucket="BUCKETNAME" | |
expireweeks=3 | |
databases=`mysql -e "SHOW DATABASES;" | tr -d "| " | grep -v "\(Database\|information_schema\|mysql\)"` | |
datestamp=`date +"%m-%d-%y"` | |
timestamp=`date +"%H%M%S"` | |
for db in $databases; do | |
filename="$db-$datestamp-$timestamp.sql.gz" | |
tmpfile="/tmp/$filename" | |
object="s3://$bucket/$datestamp/$filename" | |
echo -e "Dumping \e[0;34m$db\e[0m to \e[0;35m$tmpfile\e[0m..." | |
mysqldump --force --opt --databases $db | gzip -c > $tmpfile | |
echo -e "Moving \e[0;34m$tmpfile\e[0m to \e[0;35m$object\e[0m..." | |
s3cmd put $tmpfile $object | |
echo -e "Removing \e[1;31m$tmpfile\e[0m" | |
rm -f $tmpfile | |
echo -e "\e[1;32mDatabase: $db backed up successfully to S3\e[0m" | |
done; | |
echo -e "Cleaning up backups more than \e[1;31m$expireweeks weeks old\e[0m..." | |
# Removes buckets back $expireweeks -> $expireweeks + 5 days | |
# Considered acceptable since this cron is intended to be run nightly | |
for (( i=($expireweeks*7); i<=($expireweeks*7+5); i++ )) | |
do | |
datestamp=`date +"%m-%d-%y" -d "$i day ago"` | |
object="s3://$bucket/$datestamp" | |
s3cmd del -r $object | |
done; | |
echo -e "\e[1;42;33mBackup process COMPLETE.\e[0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment