Last active
February 6, 2017 21:07
-
-
Save archatas/40e79e39d8b652357115 to your computer and use it in GitHub Desktop.
Bash script to save database backups for every day of the week in files like 1-Monday.sql, 2-Tuesday.sql, etc. This should be executed daily as a cron job.
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
#! /usr/bin/env bash | |
SECONDS=0 | |
PROJECT_PATH=/home/example | |
CRON_LOG_FILE=${PROJECT_PATH}/logs/backup_db.log | |
BACKUP_PATH=${PROJECT_PATH}/db_backups/$(LC_ALL=en_US.UTF-8 date +"%w-%A").sql | |
USER=dbusername | |
DATABASE=dbname | |
PASS=dbpassword | |
EXCLUDED_TABLES=( | |
django_session | |
) | |
IGNORED_TABLES_STRING='' | |
for TABLE in "${EXCLUDED_TABLES[@]}" | |
do : | |
IGNORED_TABLES_STRING+=" --ignore-table=${DATABASE}.${TABLE}" | |
done | |
echo "Creating DB Backup" > ${CRON_LOG_FILE} | |
date >> ${CRON_LOG_FILE} | |
cd ${PROJECT_PATH} | |
mkdir -p db_backups | |
echo "Dump structure" >> ${CRON_LOG_FILE} | |
mysqldump -u ${USER} -p${PASS} --single-transaction --no-data ${DATABASE} > ${BACKUP_PATH} 2>> ${CRON_LOG_FILE} | |
echo "Dump content" >> ${CRON_LOG_FILE} | |
mysqldump -u ${USER} -p${PASS} ${DATABASE} ${IGNORED_TABLES_STRING} >> ${BACKUP_PATH} 2>> ${CRON_LOG_FILE} | |
echo "Finished." >> ${CRON_LOG_FILE} | |
duration=$SECONDS | |
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed." >> ${CRON_LOG_FILE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment