Files
MySQL-backup-to-Amazon-S3/mysqltos3.sh

51 lines
1.5 KiB
Bash
Raw Normal View History

2011-09-17 16:15:31 +02:00
#!/bin/sh
# Updates etc at: https://github.com/woxxy/MySQL-backup-to-Amazon-S3
# Under a MIT license
2011-09-17 18:54:16 +02:00
# change these variables to what you need
2011-09-17 18:52:16 +02:00
MYSQLROOT=root
MYSQLPASS=password
S3BUCKET=my-db-backup-bucket
# 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=
2012-05-11 00:44:43 +09:00
#tmp path.
TMP_PATH=~/
2011-09-17 18:52:16 +02:00
PERIOD=${1-day}
echo "Selected period: $PERIOD."
echo "Starting backing up the database to a file..."
2011-09-17 16:15:31 +02:00
# dump all databases
2012-05-11 00:44:43 +09:00
${MYSQLDUMPPATH}mysqldump --quick --user=${MYSQLROOT} --password=${MYSQLPASS} --all-databases > ${TMP_PATH}all-databases.sql
2011-09-17 16:15:31 +02:00
echo "Done backing up the database to a file."
2011-09-20 17:43:46 +02:00
echo "Starting compression..."
2012-05-11 00:50:57 +09:00
tar czf ${TMP_PATH}all-databases.tar.gz ${TMP_PATH}all-databases.sql
echo "Done compressing the backup file."
2011-09-17 16:15:31 +02:00
# we want at least two backups, two months, two weeks, and two days
echo "Removing old backup (2 ${PERIOD}s ago)..."
2011-09-17 18:53:21 +02:00
s3cmd del --recursive s3://${S3BUCKET}/previous_${PERIOD}/
echo "Old backup removed."
echo "Moving the backup from past $PERIOD to another folder..."
2011-09-17 18:52:16 +02:00
s3cmd mv --recursive s3://${S3BUCKET}/${PERIOD}/ s3://${S3BUCKET}/previous_${PERIOD}/
echo "Past backup moved."
2011-09-17 16:15:31 +02:00
# upload all databases
echo "Uploading the new backup..."
2012-05-11 00:50:57 +09:00
s3cmd put -f ${TMP_PATH}all-databases.tar.gz s3://${S3BUCKET}/${PERIOD}/
echo "New backup uploaded."
2011-09-17 16:15:31 +02:00
echo "Removing the cache files..."
2011-09-17 16:15:31 +02:00
# remove databases dump
2012-05-11 00:50:57 +09:00
rm ${TMP_PATH}all-databases.sql
rm ${TMP_PATH}all-databases.tar.gz
echo "Files removed."
2012-05-11 00:44:43 +09:00
echo "All done."