1. #!/bin/bash
  2. #************************************************************************/
  3. #* Author: Unknown Create Date: 08/03/23 */
  4. #* Purpose: This Code copied from an online source nnd is designed to */
  5. #* SYNC datasbases. The original design was Sync MySQL */
  6. #* Databases for Websites across different stages (live, dev, */
  7. #* test). */
  8. #* Source: https://bash.cyberciti.biz/guide/Menu_driven_scripts */
  9. #* Type: A menu driven shell script sample template. */
  10. #************************************************************************/
  11. #* Modified: Nyle E. Davis Modify Date: 08/03/23 */
  12. #* Changes: Changed allowing local 2 local DB & table 2 table syncs */
  13. #* Trucated the var names to fit the editor scren. */
  14. #************************************************************************/
  15. # ----------------------------------
  16. # Step #1: Define global variables
  17. # ----------------------------------
  18.   RED='\033[0;41;30m'
  19. STD='\033[0;0;39m'
  20. TODAY=$(date +"%Y-%m-%d")
  21. # --------------------------------------
  22. # Define the arrays
  23. # indexes as follows:
  24. # 0 = type : wp | magento | local | table
  25. # 1 = host : my.db-ho.st
  26. # 2 = user : username
  27. # 3 = dbn : name of the database
  28. # 4 = pwrd : password
  29. # 5 = subdomain / prefix : for Magento's core_config_data change
  30. # and to compare $SRC with $DEST
  31. # --------------------------------------
  32. declare -a live_mag=(mag my-live.db-ho.st user db pwrd www)
  33. declare -a live_wp=(wp my-live.db-ho.st user db pwrd www)
  34. declare -a dev_mag=(mag my-dev.db-ho.st user db pwrd dev)
  35. declare -a dev_wp=(wp my-dev.db-ho.st user db pwrd dev)
  36. declare -a test_mag=(mag my-test.db-ho.st user db pwrd test)
  37. declare -a test_wp=(wp my-test.db-ho.st user db pwrd test)
  38. declare -a dev2_mag=(mag my-dev.db-ho.st user db pwrd dev2)
  39. declare -a dev2_wp=(wp my-dev.db-ho.st user db pwrd dev2)
  40. declare -a loc1_ldb1=(loc 1.db-ho.st user db pwrd loc1)
  41. declare -a loc2_ldb2=(loc 2.db-ho.st user db pwrd loc2)
  42. declare -a tab1_tab1=(ltb-1.db-ho.st user db pwrd ltb1)
  43. declare -a tab2_tab2=(ltb-2.db-ho.st user db pwrd ltb2)
  44. # ----------------------------------
  45. # Step #2: User defined function
  46. # ---------------------------------- 
  47. init() {
  48. clear
  49. echo "##################################"
  50. echo "## A simple script to keep your ##"
  51. echo "## MySQL Databases in sync. ##"
  52. echo "##################################"
  53. }
  54.  
  55. select_source_menu() {
  56. echo ""
  57. echo "Select a SOURCE database."
  58. echo ""
  59. echo "1. Live"
  60. echo "2. Dev"
  61. echo "3. Testing"
  62. echo "4. Nav"
  63. echo "5. Local"
  64. echo "6. Table"
  65. echo "~~~~~~~~~~~~~~~~~~~~~"
  66. echo "0. Exit"
  67. read_options_1
  68. }
  69. select_destination_menu() {
  70. init
  71. echo ""
  72. echo "Select a DESTINATION database."
  73. echo ""
  74. echo "A. Live"
  75. echo "B. Dev"
  76. echo "C. Testing"
  77. echo "D. Nav"
  78. echo "E. Local"
  79. echo "F. Table"
  80. echo "~~~~~~~~~~~~~~~~~~~~~"
  81. echo "0. Exit"
  82. read_options_2
  83. }
  84.  
  85. select_confirmation_menu() {
  86. get_db_variables
  87. init
  88. echo ""
  89. echo "### ATTENTION!!! ###"
  90. echo "This will delete all information in the $DEST-database."
  91. echo ""
  92. echo "Are you sure?"
  93. echo ""
  94. echo "(y)es"
  95. echo "(n)ot really"
  96. echo "~~~~~~~~~~~~~~~~~~~~~"
  97. echo "0. Exit"
  98. read_options_2
  99. }
  100.  
  101. select_remains_menu() {
  102. init
  103. echo ""
  104. echo "What about the created SQL dumpfiles?"
  105. echo "What are we gonna do with them?"
  106. echo ""
  107. echo "(k)eep them as zipped backup-file."
  108. echo "(d)elete them."
  109. read_options_2
  110. }
  111. select_restart_menu() {
  112. init
  113. echo ""
  114. echo "All done. What now?"
  115. echo ""
  116. echo "r) Restart sync."
  117. echo "0) Exit."
  118. read_options_2
  119. }
  120. # read input from the keyboard and take a action
  121. # Exit when user the user select 0 form the menu option.
  122. read_options_1(){
  123. local choice
  124. read -p "Enter choice: " choice
  125. case $choice in
  126. 1) SRC=live;select_destination_menu ;;
  127. 2) SRC=dev;select_destination_menu ;;
  128. 3) SRC=testing;select_destination_menu ;;
  129. 4) SRC=nav;select_destination_menu ;;
  130. 5) SRC=loc;select_destination_menu ;;
  131. 6) SRC=tab;select_destination_menu ;;
  132. 0) exit 1 ;;
  133. *) echo -e "${RED}Error...${STD}" && sleep 2 && start
  134. esac
  135. }
  136.  
  137. read_options_2(){
  138. local choice
  139. read -p "Enter choice: " choice
  140. case $choice in
  141. A) DEST=live;select_confirmation_menu ;;
  142. B) DEST=dev;select_confirmation_menu ;;
  143. C) DEST=testing;select_confirmation_menu ;;
  144. D) DEST=nav;select_confirmation_menu ;;
  145. E) DEST=loc;select_confirmation_menu ;;
  146. F) DEST=tab;select_confirmation_menu ;;
  147. y) sync_db ;;
  148. n) start ;;
  149. k) backup ;;
  150. d) delete_files ;;
  151. r) start ;;
  152. 82517) sync_db ;;
  153. 0) exit 1 ;;
  154. *) echo -e "${RED}Error...${STD}" && sleep 2 && select_destination_menu
  155. esac
  156. }
  157. get_db_variables(){
  158. case $SRC in
  159. live) SRC_MAGE=("${live_mag[@]}");SRC_WP=("${live_wp[@]}") ;;
  160. dev) SRC_MAGE=("${dev_mag[@]}");SRC_WP=("${dev_wp[@]}") ;;
  161. test) SRC_MAGE=("${test_mag[@]}");SRC_WP=("${test_wp[@]}") ;;
  162. nav) SRC_MAGE=("${nav_mag[@]}");SRC_WP=("${nav_wp[@]}") ;;
  163. esac
  164.  
  165. case $DEST in
  166. live) DEST_MAGE=("${live_mag[@]}");DEST_WP=("${live_wp[@]}") ;;
  167. dev) DEST_MAGE=("${dev_mag[@]}");DEST_WP=("${dev_wp[@]}") ;;
  168. test) DEST_MAGE=("${test_mag[@]}");DEST_WP=("${test_wp[@]}") ;;
  169. nav) DEST_MAGE=("${nav_mag[@]}");DEST_WP=("${nav_wp[@]}") ;;
  170. esac
  171.  
  172. if [ ${SRC_MAGE[5]} == ${DEST_MAGE[5]} ]; then
  173. echo -e "${RED}Error: source and destination must not be the same.${STD}"
  174. echo -e "${RED}Source is: ${SRC_MAGE[5]}.${STD}"
  175. echo -e "${RED}Destination is: ${DEST_MAGE[5]}.${STD}" && sleep 5
  176. start
  177. fi
  178. if [ ${DEST_MAGE[5]} == www ]; then
  179. init
  180. echo ""
  181. echo "##################################"
  182. echo "########## ATTENTION!!!! #########"
  183. echo "##################################"
  184. echo ""
  185. echo "This will delete all information in the $DEST-database and"
  186. echo "will overwrite it with information from $SRC"
  187. echo ""
  188. echo "##################################"
  189. echo "There is NO WAY of restoring the data from $DEST"
  190. echo "unless you made a backup once."
  191. echo ""
  192. echo "##################################"
  193. echo ""
  194. echo "So: Are you REALLY DEFINITELY sure?"
  195. echo ""
  196. echo "##################################"
  197. echo ""
  198. echo "Enter 82517 for yes."
  199. echo "(n) to abort."
  200. echo "~~~~~~~~~~~~~~~~~~~~~"
  201. echo "0. Exit"
  202. read_options_2
  203. fi
  204. }
  205. ync_db(){
  206. ## The real purpose of this bash script
  207. echo "Now copying MySQL databases from $SRC to $DEST..."
  208. # indexes as follows:
  209. # 0 = type : wordpress | mag
  210. # 1 = host : db13.variomedia.de
  211. # 2 = user : u36052 | u36065 ..
  212. # 3 = database name : db36052 | db36065 ..
  213. # 4 = password
  214. # 5 = subdomain / prefix : for Magento's core_config_data change and to compare $SRC with $DEST
  215.  
  216. mysqldump -h ${SRC_MAGE[1]} -u ${SRC_MAGE[2]} -p${SRC_MAGE[4]} ${SRC_MAGE[3]} > ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql
  217.  
  218. echo "mag DB dumped to ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql"
  219.  
  220. mysqldump -h ${SRC_WP[1]} -u ${SRC_WP[2]} -p${SRC_WP[4]} ${SRC_WP[3]} > ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql
  221.  
  222. echo "WordPress DB dumped to ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql"
  223. echo "Now importing into $DEST database..."
  224.  
  225. mysql -h ${DEST_MAGE[1]} -u ${DEST_MAGE[2]} -p${DEST_MAGE[4]} ${DEST_MAGE[3]} < ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql
  226.  
  227. echo "mag DB imported from ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql"
  228.  
  229. mysql -h ${DEST_WP[1]} -u ${DEST_WP[2]} -p${DEST_WP[4]} ${DEST_WP[3]} < ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql
  230.  
  231. echo "WordPress DB imported from ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql"
  232. echo "Now changing the 'base_url's for the destination Magento-DB."
  233.  
  234. mysql -h ${DEST_MAGE[1]} -u ${DEST_MAGE[2]} -p${DEST_MAGE[4]} ${DEST_MAGE[3]} -e 'UPDATE `core_config_data` SET `value` = "http://'${DEST_MAGE[5]}'.my-website.com/" WHERE `core_config_data`.`config_id` = 7; UPDATE `core_config_data` SET `value` = "http://'${DEST_MAGE[5]}'.my-website.com/" WHERE `core_config_data`.`config_id` = 8;'
  235.  
  236. echo "Copying databases done. Now clearing ${DEST_MAGE[5]} Magento cache" && sleep 2
  237.  
  238. rm -rfv ~/${DEST_MAGE[5]}/var/cache/
  239. select_remains_menu
  240. }
  241. backup(){
  242. echo "Zipping SQL dumpfiles and saving them as backup..."
  243. cat ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql | gzip -v > ~/sql-backup/$TODAY-sql-backup-${SRC_MAGE[5]}.gz
  244. delete_files
  245. }
  246.  
  247. delete_files(){
  248. echo "Deleting sql-files..."
  249. rm -fv ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql
  250. select_restart_menu
  251. }
  252. # ----------------------------------------------
  253. # Step #3: Trap CTRL+C, CTRL+Z and quit singles
  254. # ----------------------------------------------
  255. trap '' SIGINT SIGQUIT SIGTSTP
  256.  
  257. # -----------------------------------
  258. # Step #4: Main logic - infinite loop
  259. # ------------------------------------
  260.  
  261.  
  262. start() {
  263. unset -v SRC
  264. unset -v DEST
  265. unset -v SRC_MAGE
  266. unset -v SRC_WP
  267. unset -v DEST_MAGE
  268. unset -v DEST_WP
  269. init
  270. select_source_menu
  271. }
  272.  
  273. start