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

Email Option Additions #8

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
87 changes: 58 additions & 29 deletions mysqltos3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ MYSQLPASS=password
S3BUCKET=bucketname
FILENAME=filename
DATABASE='--all-databases'
REMOVE_BACKUPS=true
# the following line prefixes the backups with the defined directory. it must be blank or end with a /
S3PATH=mysql_backup/
S3CMDPATH=
# 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=./

# File to capture the output.
OUTPUT_FILE=${TMP_PATH}output.log
ECHO_OUTPUT=true

# Email options.
MAILPATH=
ENABLE_EMAIL=false
TO_ADDRESS=''
FROM_ADDRESS=''
SUBJECT=''

DATESTAMP=$(date +".%m.%d.%Y")
DAY=$(date +"%d")
Expand All @@ -23,45 +36,61 @@ DAYOFWEEK=$(date +"%A")
PERIOD=${1-day}
if [ ${PERIOD} = "auto" ]; then
if [ ${DAY} = "01" ]; then
PERIOD=month
PERIOD=month
elif [ ${DAYOFWEEK} = "Sunday" ]; then
PERIOD=week
PERIOD=week
else
PERIOD=day
PERIOD=day
fi
fi

echo "Selected period: $PERIOD."
# Take the output and redirect it to a file.
{
echo "Backup output from ${DATESTAMP}."

echo "Selected period: ${PERIOD}."

echo "Starting backing up the database to a file..."
echo "Starting backing up the database to a file..."

# dump all databases
${MYSQLDUMPPATH}mysqldump --quick --user=${MYSQLROOT} --password=${MYSQLPASS} ${DATABASE} > ${TMP_PATH}${FILENAME}.sql
# 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..."
echo "Done backing up the database to a file."
echo "Starting compression..."

tar czf ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz ${TMP_PATH}${FILENAME}.sql
tar czf ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz ${TMP_PATH}${FILENAME}.sql
echo "Done compressing the backup file."

echo "Done compressing the backup file."
if [ ${REMOVE_BACKUPS} = true ]; then
# we want at least two backups, two months, two weeks, and two days
echo "Removing old backup (2 ${PERIOD}s ago)..."
${S3CMDPATH}s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
echo "Old backup removed."
fi

# 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..."
${S3CMDPATH}s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/ s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
echo "Past backup moved."

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

# 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 "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."
if [ ${ENABLE_EMAIL} = true ]; then
${MAILPATH}mail -s "${SUBJECT}" -r "${FROM_ADDRESS}" "${TO_ADDRESS}" < ${OUTPUT_FILE}
echo "Sent mail."
fi
} > ${OUTPUT_FILE} 2>&1

if [ ${ECHO_OUTPUT} = true ]; then
cat ${OUTPUT_FILE}
rm ${OUTPUT_FILE}
fi