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
2012-05-15 13:14:52 +03:00
S3BUCKET = bucketname
FILENAME = filename
DATABASE = '--all-databases'
2012-09-06 17:37:13 -03:00
# the following line prefixes the backups with the defined directory. it must be blank or end with a /
S3PATH = mysql_backup/
2011-09-17 20:40:22 +02:00
# 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
2012-11-28 05:56:05 -05:00
DATESTAMP = $( date +".%m.%d.%Y" )
2012-12-03 06:34:01 -05:00
DAY = $( date +"%d" )
DAYOFWEEK = $( date +"%A" )
2012-12-03 06:45:31 -05:00
PERIOD = ${ 1 -day }
if [ ${ PERIOD } = "auto" ] ; then
if [ ${ DAY } = "01" ] ; then
PERIOD = month
elif [ ${ DAYOFWEEK } = "Sunday" ] ; then
PERIOD = week
else
PERIOD = day
fi
2012-12-03 06:34:01 -05:00
fi
2012-11-28 05:56:05 -05:00
2011-09-20 17:06:29 +02:00
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-08-29 10:16:09 +02:00
${ MYSQLDUMPPATH } mysqldump --quick --user= ${ MYSQLROOT } --password= ${ MYSQLPASS } ${ DATABASE } > ${ TMP_PATH } ${ FILENAME } .sql
2011-09-17 16:15:31 +02:00
2011-09-20 17:06:29 +02:00
echo "Done backing up the database to a file."
2011-09-20 17:43:46 +02:00
echo "Starting compression..."
2011-09-20 17:06:29 +02:00
2012-11-28 05:56:05 -05:00
tar czf ${ TMP_PATH } ${ FILENAME } ${ DATESTAMP } .tar.gz ${ TMP_PATH } ${ FILENAME } .sql
2011-09-20 17:06:29 +02:00
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
2011-09-20 17:06:29 +02:00
echo " Removing old backup (2 ${ PERIOD } s ago)... "
2012-09-06 17:37:13 -03:00
s3cmd del --recursive s3://${ S3BUCKET } /${ S3PATH } previous_${ PERIOD } /
2011-09-20 17:06:29 +02:00
echo "Old backup removed."
echo " Moving the backup from past $PERIOD to another folder... "
2012-09-06 17:37:13 -03:00
s3cmd mv --recursive s3://${ S3BUCKET } /${ S3PATH } ${ PERIOD } / s3://${ S3BUCKET } /${ S3PATH } previous_${ PERIOD } /
2011-09-20 17:06:29 +02:00
echo "Past backup moved."
2011-09-17 16:15:31 +02:00
# upload all databases
2011-09-20 17:06:29 +02:00
echo "Uploading the new backup..."
2012-11-28 05:56:05 -05:00
s3cmd put -f ${ TMP_PATH } ${ FILENAME } ${ DATESTAMP } .tar.gz s3://${ S3BUCKET } /${ S3PATH } ${ PERIOD } /
2011-09-20 17:06:29 +02:00
echo "New backup uploaded."
2011-09-17 16:15:31 +02:00
2011-09-20 17:06:29 +02:00
echo "Removing the cache files..."
2011-09-17 16:15:31 +02:00
# remove databases dump
2012-08-29 10:16:09 +02:00
rm ${ TMP_PATH } ${ FILENAME } .sql
2012-11-28 05:56:05 -05:00
rm ${ TMP_PATH } ${ FILENAME } ${ DATESTAMP } .tar.gz
2011-09-20 17:06:29 +02:00
echo "Files removed."
2012-05-11 00:44:43 +09:00
echo "All done."