Created
August 5, 2014 14:56
-
-
Save doctorallen/d11332779b5a01e9e990 to your computer and use it in GitHub Desktop.
Rolling Backups
This file contains 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/sh | |
MYSQLROOT=test | |
MYSQLPASS=test | |
S3BUCKET=TAG_Backups | |
FILENAME=test | |
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=~/ | |
DATESTAMP=$(date +".%m.%d.%Y") | |
DAY=$(date +"%d") | |
DAYOFWEEK=$(date +"%A") | |
WEEKAGO=$(date +".%m.%d.%Y" -d "1 week ago") | |
FOURWEEKSAGO=$(date +".%m.%d.%Y" -d "4 week ago") | |
SIXMONTHSAGO=$(date +".%m.%d.%Y" -d "6 months ago") | |
PERIOD=${1-day} | |
if [ ${PERIOD} = "auto" ]; then | |
if [ ${DAY} = "01" ]; then | |
PERIOD=month | |
AGO=${SIXMONTHSAGO} | |
elif [ ${DAYOFWEEK} = "Sunday" ]; then | |
PERIOD=week | |
AGO=${FOURWEEKSAGO} | |
else | |
PERIOD=day | |
AGO=${WEEKAGO} | |
fi | |
fi | |
echo "-------------------$DATESTAMP------------------------" | |
echo "Starting backing up the database to a file..." | |
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}${DATESTAMP}.tar.gz ${TMP_PATH}${FILENAME}.sql | |
echo "Done compressing the backup file." | |
# removing any backups that are in the old folder | |
echo "Removing old backups..." | |
s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}old/ | |
echo "Old backups removed." | |
echo "Moving ${PERIOD} backup from ${AGO} to another folder..." | |
s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${FILENAME}${AGO}.tar.gz s3://${S3BUCKET}/${S3PATH}old/ | |
echo "Past backup moved." | |
# upload all databases | |
echo "Uploading the new backup..." | |
s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.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}${DATESTAMP}.tar.gz | |
echo "Files removed." | |
echo "All done." | |
echo "-----------------------------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment