From 0e55148e5df135367f47763c0d0c485199c7eab6 Mon Sep 17 00:00:00 2001 From: Alika Larsen Date: Mon, 24 Mar 2014 02:12:24 -0700 Subject: [PATCH 1/2] Adding in a daily, weekly, monthly backup rotation. --- mysqltos3.sh | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) mode change 100644 => 100755 mysqltos3.sh diff --git a/mysqltos3.sh b/mysqltos3.sh old mode 100644 new mode 100755 index a8a9e6d..fef5e12 --- a/mysqltos3.sh +++ b/mysqltos3.sh @@ -10,16 +10,21 @@ 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/ +S3PATH= # 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=~/ +TMP_PATH=/tmp/ DATESTAMP=$(date +".%m.%d.%Y") DAY=$(date +"%d") DAYOFWEEK=$(date +"%A") +# Grandfather-father-son rotation scheme: http://en.wikipedia.org/wiki/Backup_rotation_scheme#Grandfather-father-son +ROTATE_DAY=6 +ROTATE_WEEK=4 +ROTATE_MONTH=12 + PERIOD=${1-day} if [ ${PERIOD} = "auto" ]; then if [ ${DAY} = "01" ]; then @@ -27,36 +32,48 @@ if [ ${PERIOD} = "auto" ]; then elif [ ${DAYOFWEEK} = "Sunday" ]; then PERIOD=week else - PERIOD=day + PERIOD=day fi fi -echo "Selected period: $PERIOD." +if [ ${PERIOD} = "month" ]; then + ROTATE=${ROTATE_MONTH} +elif [ ${PERIOD} = "week" ]; then + ROTATE=${ROTATE_WEEK} +else + ROTATE=${ROTATE_DAY} +fi + +echo "Rotating ${PERIOD} backups (${ROTATE})." -echo "Starting backing up the database to a file..." +for num in $(seq $ROTATE -1 1) +do + if [ ${ROTATE} = ${num} ]; then + echo "\tRemoving oldest backup (${ROTATE} ${PERIOD}s ago)..." + s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ + echo "\tOldest backup removed." + else + echo "\tMoving the backup from past ${num} ${PERIOD}s to $((num+1))" + s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ s3://${S3BUCKET}/${S3PATH}${PERIOD}/$((num+1))/ + echo "\tPast backup moved." + fi +done +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 +cd ${TMP_PATH} +tar cvzf ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz ${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 +# upload backup (s3cmd supports pre-upload encryption: -e) echo "Uploading the new backup..." -s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/ +s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/1/ echo "New backup uploaded." echo "Removing the cache files..." From 8eb917a1ceff6df79dc268fcdabf807d72e7e9ab Mon Sep 17 00:00:00 2001 From: Alika Larsen Date: Tue, 25 Mar 2014 17:28:20 -0700 Subject: [PATCH 2/2] s3cmd not doing delete on move. Adding timing info. --- mysqltos3.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mysqltos3.sh b/mysqltos3.sh index fef5e12..f641d30 100755 --- a/mysqltos3.sh +++ b/mysqltos3.sh @@ -13,6 +13,7 @@ DATABASE='--all-databases' S3PATH= # 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= +S3CMDPATH=~/s3cmd/location/ #tmp path. TMP_PATH=/tmp/ @@ -44,17 +45,20 @@ else ROTATE=${ROTATE_DAY} fi +STARTTIME="$(date +%s%N)" + echo "Rotating ${PERIOD} backups (${ROTATE})." for num in $(seq $ROTATE -1 1) do if [ ${ROTATE} = ${num} ]; then echo "\tRemoving oldest backup (${ROTATE} ${PERIOD}s ago)..." - s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ + ${S3CMDPATH}s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ echo "\tOldest backup removed." else echo "\tMoving the backup from past ${num} ${PERIOD}s to $((num+1))" - s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ s3://${S3BUCKET}/${S3PATH}${PERIOD}/$((num+1))/ + ${S3CMDPATH}s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ s3://${S3BUCKET}/${S3PATH}${PERIOD}/$((num+1))/ + ${S3CMDPATH}s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/${num}/ echo "\tPast backup moved." fi done @@ -73,7 +77,7 @@ echo "Done compressing the backup file." # upload backup (s3cmd supports pre-upload encryption: -e) echo "Uploading the new backup..." -s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/1/ +${S3CMDPATH}s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/1/ echo "New backup uploaded." echo "Removing the cache files..." @@ -82,3 +86,7 @@ rm ${TMP_PATH}${FILENAME}.sql rm ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz echo "Files removed." echo "All done." + +ENDTIME="$(($(date +%s%N)-STARTTIME))" +S="$((ENDTIME/1000000000))" +printf "Timing: Days:%02d Hours:%02d Minutes:%02d Seconds:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}"