Guest User

Untitled

a guest
Jul 27th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Created on: July 26, 2018
  4. # Created by: Andrew Abraham
  5. # Purpose:
  6. # This script will restore a local mysql database to a Azure Mysql Server.
  7. # It is set to drop the target DB prior to restoring this dump.
  8. # I created to restore a copy of the a local db to azure every night using a cron.
  9.  
  10.  
  11. ## Vars ##
  12. DB=''
  13. MY_LOC_USER=''
  14. MY_LOC_PASS=''
  15. MY_AZ_USER=''
  16. MY_AZ_PASS=''
  17. AZ_HOST=''
  18. DUMP_FILE='/mysql/dump.sql'
  19. LOG_FILE='/var/log/mysql_restore_azure.log'
  20.  
  21.  
  22. ## Functions ##
  23. dump_db (){
  24. echo "$(date '+%Y-%m-%d %H:%M:%S') -- Starting ${DB} dump to ${DUMP_FILE} ..." | tee -a ${LOG_FILE}
  25. mysqldump -u $1 -p"$2" --databases --add-drop-database --events --triggers --routines $3 > $4 && \
  26. echo "$(date '+%Y-%m-%d %H:%M:%S') -- Completed dumped to ${DUMP_FILE} successfully!" | tee -a ${LOG_FILE}
  27. }
  28.  
  29. prep_dump (){
  30. DUMP="$1"
  31. echo "$(date '+%Y-%m-%d %H:%M:%S') -- Refactoring MyISAM to InnoDB & removing Definer references in dump file..." | tee -a ${LOG_FILE}
  32. sed -i 's/MyISAM/InnoDB/g' ${DUMP} && \
  33. sed -i 's/DEFINER=[^*]*\*/\*/g' ${DUMP} && \
  34. echo "$(date '+%Y-%m-%d %H:%M:%S') -- Completed dump file preperation for restore to Azure!" | tee -a ${LOG_FILE}
  35. }
  36.  
  37. restore_db (){
  38. echo "$(date '+%Y-%m-%d %H:%M:%S') -- Starting DB restore to Azure..." | tee -a ${LOG_FILE}
  39. mysql -u $1 -p"$2" -h "$3" < "$4" && \
  40. echo "$(date '+%Y-%m-%d %H:%M:%S') -- ${DB} successfully restored to ${AZ_HOST}" | tee -a ${LOG_FILE}
  41. }
  42.  
  43.  
  44. ## Main ##
  45. dump_db "${MY_LOC_USER}" "${MY_LOC_PASS}" "${DB}" "${DUMP_FILE}" && \
  46. prep_dump "${DUMP_FILE}" && \
  47. restore_db "${MY_AZ_USER}" "${MY_AZ_PASS}" "${AZ_HOST}" "${DUMP_FILE}"
Add Comment
Please, Sign In to add comment