Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Updates etc at: https://github.com/woxxy/MySQL-backup-to-Amazon-S3
- # Under a MIT license
- # change these variables to what you need
- MYSQLROOT=root
- MYSQLPASS=password
- S3BUCKET=bucketname
- FILENAME=filename
- DATABASE='--all-databases'
- # the following line prefixes the backups with the defined directory. it must be blank or end with a /
- S3PATH=mysql_backup/
- # when running via cron, the PATHs MIGHT be different. If you have a custom/manual MYSQL install, you should set this manually like MYSQLDUMPPATH=/usr/local/mysql/bin/
- MYSQLDUMPPATH=
- #tmp path.
- TMP_PATH=~/
- PERIOD=${1-day}
- echo "Selected period: $PERIOD."
- echo "Starting backing up the database to a file..."
- # dump all databases
- ${MYSQLDUMPPATH}mysqldump --quick --user=${MYSQLROOT} --password=${MYSQLPASS} ${DATABASE} > ${TMP_PATH}${FILENAME}.sql
- echo "Done backing up the database to a file."
- echo "Starting compression..."
- tar czf ${TMP_PATH}${FILENAME}.tar.gz ${TMP_PATH}${FILENAME}.sql
- echo "Done compressing the backup file."
- # we want at least two backups, two months, two weeks, and two days
- echo "Removing old backup (2 ${PERIOD}s ago)..."
- s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
- echo "Old backup removed."
- echo "Moving the backup from past $PERIOD to another folder..."
- s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/ s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
- echo "Past backup moved."
- # upload all databases
- echo "Uploading the new backup..."
- s3cmd put -f ${TMP_PATH}${FILENAME}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/
- echo "New backup uploaded."
- echo "Removing the cache files..."
- # remove databases dump
- rm ${TMP_PATH}${FILENAME}.sql
- rm ${TMP_PATH}${FILENAME}.tar.gz
- echo "Files removed."
- echo "All done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement