Last active
April 1, 2018 13:11
-
-
Save phillcoxon/8622263 to your computer and use it in GitHub Desktop.
Backup mysql database and push to S3 via cron
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 | |
| BACKUP_DIR="/home/user/backup/" | |
| DB_NAME="database_name" | |
| DB_USER="database_user" | |
| DB_PASS="database_password" | |
| BUCKET_NAME="sqls3.bucketname" | |
| # Back up the mysql database | |
| # Set a symlink to the latest version of the db | |
| # Push the latest version to Amazon S3 | |
| # Note: set automatic expiry on S3 bucket so that files are expired after 30 (or whatever suits) days | |
| # Execute this script periodically (daily? twice daily?) via cron | |
| # | |
| # Note: depends on s3cmd at http://s3tools.org/s3cmd | |
| mysqldump --add-drop-table -u${DB_USER} -p${DB_PASS} $DB | gzip > ${BACKUP_DIR}${DB_NAME}_`date +%F`.sql.gz | |
| # create symlink to latest dump | |
| rm ${BACKUP_DIR}${DB_NAME}-latest.sql.gz | |
| ln -s ${BACKUP_DIR}${DB_NAME}_`date +%F`.sql.gz ${BACKUP_DIR}${DB_NAME}-latest.sql.gz | |
| # delete files older than 5 days | |
| find $BACKUP_DIR -mtime 5 -delete | |
| # push the latest database to Amazon s3 | |
| s3cmd put ${BACKUP_DIR}${DB_NAME}_`date +%F`.sql.gz s3://${BUCKET_NAME} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is the host