Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Created on: July 26, 2018
- # Created by: Andrew Abraham
- # Purpose:
- # This script will restore a local mysql database to a Azure Mysql Server.
- # It is set to drop the target DB prior to restoring this dump.
- # I created to restore a copy of the a local db to azure every night using a cron.
- ## Vars ##
- DB=''
- MY_LOC_USER=''
- MY_LOC_PASS=''
- MY_AZ_USER=''
- MY_AZ_PASS=''
- AZ_HOST=''
- DUMP_FILE='/mysql/dump.sql'
- LOG_FILE='/var/log/mysql_restore_azure.log'
- ## Functions ##
- dump_db (){
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- Starting ${DB} dump to ${DUMP_FILE} ..." | tee -a ${LOG_FILE}
- mysqldump -u $1 -p"$2" --databases --add-drop-database --events --triggers --routines $3 > $4 && \
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- Completed dumped to ${DUMP_FILE} successfully!" | tee -a ${LOG_FILE}
- }
- prep_dump (){
- DUMP="$1"
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- Refactoring MyISAM to InnoDB & removing Definer references in dump file..." | tee -a ${LOG_FILE}
- sed -i 's/MyISAM/InnoDB/g' ${DUMP} && \
- sed -i 's/DEFINER=[^*]*\*/\*/g' ${DUMP} && \
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- Completed dump file preperation for restore to Azure!" | tee -a ${LOG_FILE}
- }
- restore_db (){
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- Starting DB restore to Azure..." | tee -a ${LOG_FILE}
- mysql -u $1 -p"$2" -h "$3" < "$4" && \
- echo "$(date '+%Y-%m-%d %H:%M:%S') -- ${DB} successfully restored to ${AZ_HOST}" | tee -a ${LOG_FILE}
- }
- ## Main ##
- dump_db "${MY_LOC_USER}" "${MY_LOC_PASS}" "${DB}" "${DUMP_FILE}" && \
- prep_dump "${DUMP_FILE}" && \
- restore_db "${MY_AZ_USER}" "${MY_AZ_PASS}" "${AZ_HOST}" "${DUMP_FILE}"
Add Comment
Please, Sign In to add comment