Last active
October 12, 2015 04:17
-
-
Save richardkall/3969264 to your computer and use it in GitHub Desktop.
Backup MySQL database to 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/sh | |
| # Dumps MySQL database to a file, uploads to S3 and rotates out old backups. | |
| # Set correct dates | |
| NOWDATE=`date +%Y-%m-%d` | |
| LASTDATE=$(date +%Y-%m-%d --date='1 week ago') | |
| DESTINATION=/var/backup | |
| DB= | |
| HOST= | |
| USER= | |
| PORT=3306 | |
| PASS= | |
| BUCKET= | |
| # Dump each database to its own file | |
| mysqldump --databases $DB -v -h $HOST -u $USER -P $PORT -p$PASS > $DESTINATION/$DB-$NOWDATE.sql | |
| # Tar all the databases | |
| tar -czf $DESTINATION/$DB-$NOWDATE.tar.gz $DESTINATION | |
| # Upload database to S3 | |
| s3cmd put $DESTINATION/$DB-$NOWDATE.tar.gz s3://$BUCKET/$NOWDATE/ | |
| # Rotate out old backups | |
| s3cmd del --recursive s3://$BUCKET/db/$LASTDATE/ | |
| # Remove all local dumps | |
| rm $DESTINATION/$DB-$NOWDATE.tar.gz | |
| for FILE in $(echo $(ls $DESTINATION)) | |
| do | |
| rm $DESTINATION/$FILE | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment