Skip to content

Instantly share code, notes, and snippets.

@phillcoxon
Last active April 1, 2018 13:11
Show Gist options
  • Select an option

  • Save phillcoxon/8622263 to your computer and use it in GitHub Desktop.

Select an option

Save phillcoxon/8622263 to your computer and use it in GitHub Desktop.
Backup mysql database and push to S3 via cron
#!/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}
@nareshnaani

Copy link
Copy Markdown

where is the host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment