Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in daily, weekly, and monthly backup rotations. #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 42 additions & 17 deletions mysqltos3.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,74 @@ 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=
S3CMDPATH=~/s3cmd/location/
#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
PERIOD=month
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 "Starting backing up the database to a file..."
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)..."
${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))"
${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

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}/
${S3CMDPATH}s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/1/
echo "New backup uploaded."

echo "Removing the cache files..."
Expand All @@ -65,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}"