ControlC
· Pastebin
Login
Register
ControlC
/
Create paste
Paste content
Up to 100 KB of text. BBCode formatting is supported.
Title
- optional
Content
B
I
U
S
</>
Colors ↓
Sizes ↓
Size 1
Size 2
Size 3
Size 4
Size 5
Size 6
Size 7
#!/bin/bash\r\n #************************************************************************/\r\n #* Author: Unknown Create Date: 08/03/23 */\r\n #* Purpose: This Code copied from an online source nnd is designed to */\r\n #* SYNC datasbases. The original design was Sync MySQL */\r\n #* Databases for Websites across different stages (live, dev, */\r\n #* test). */\r\n #* Source: https://bash.cyberciti.biz/guide/Menu_driven_scripts */\r\n #* Type: A menu driven shell script sample template. */\r\n #************************************************************************/\r\n #* Modified: Nyle E. Davis Modify Date: 08/03/23 */\r\n #* Changes: Changed allowing local 2 local DB & table 2 table syncs */\r\n #* Trucated the var names to fit the editor scren. */\r\n #************************************************************************/\r\n \r\n # ----------------------------------\r\n # Step #1: Define global variables\r\n # ----------------------------------\r\n RED='\033[0;41;30m'\r\n STD='\033[0;0;39m'\r\n TODAY=$(date +\"%Y-%m-%d\")\r\n\r\n # --------------------------------------\r\n # Define the arrays\r\n # indexes as follows:\r\n # 0 = type : wp | magento | local | table\r\n # 1 = host : my.db-ho.st\r\n # 2 = user : username\r\n # 3 = dbn : name of the database\r\n # 4 = pwrd : password\r\n # 5 = subdomain / prefix : for Magento's core_config_data change \r\n # and to compare $SRC with $DEST\r\n # --------------------------------------\r\n declare -a live_mag=(mag my-live.db-ho.st user db pwrd www)\r\n declare -a live_wp=(wp my-live.db-ho.st user db pwrd www)\r\n declare -a dev_mag=(mag my-dev.db-ho.st user db pwrd dev)\r\n declare -a dev_wp=(wp my-dev.db-ho.st user db pwrd dev)\r\n declare -a test_mag=(mag my-test.db-ho.st user db pwrd test)\r\n declare -a test_wp=(wp my-test.db-ho.st user db pwrd test)\r\n declare -a dev2_mag=(mag my-dev.db-ho.st user db pwrd dev2)\r\n declare -a dev2_wp=(wp my-dev.db-ho.st user db pwrd dev2)\r\n declare -a loc1_ldb1=(loc 1.db-ho.st user db pwrd loc1)\r\n declare -a loc2_ldb2=(loc 2.db-ho.st user db pwrd loc2)\r\n declare -a tab1_tab1=(ltb-1.db-ho.st user db pwrd ltb1)\r\n declare -a tab2_tab2=(ltb-2.db-ho.st user db pwrd ltb2)\r\n\r\n # ----------------------------------\r\n # Step #2: User defined function\r\n # ---------------------------------- \r\n init() {\r\n clear\r\n echo \"##################################\"\r\n echo \"## A simple script to keep your ##\"\r\n echo \"## MySQL Databases in sync. ##\"\r\n echo \"##################################\"\r\n }\r\n \r\n select_source_menu() {\r\n echo \"\"\r\n echo \"Select a SOURCE database.\"\r\n echo \"\"\r\n echo \"1. Live\"\r\n echo \"2. Dev\"\r\n echo \"3. Testing\"\r\n echo \"4. Nav\"\r\n echo \"5. Local\"\r\n echo \"6. Table\"\r\n echo \"~~~~~~~~~~~~~~~~~~~~~\"\r\n echo \"0. Exit\"\r\n read_options_1\r\n }\r\n\r\n select_destination_menu() {\r\n init\r\n echo \"\"\r\n echo \"Select a DESTINATION database.\"\r\n echo \"\"\r\n echo \"A. Live\"\r\n echo \"B. Dev\"\r\n echo \"C. Testing\"\r\n echo \"D. Nav\"\r\n echo \"E. Local\"\r\n echo \"F. Table\"\r\n echo \"~~~~~~~~~~~~~~~~~~~~~\"\r\n echo \"0. Exit\"\r\n read_options_2\r\n }\r\n \r\n select_confirmation_menu() {\r\n get_db_variables\r\n init\r\n echo \"\"\r\n echo \"### ATTENTION!!! ###\"\r\n echo \"This will delete all information in the $DEST-database.\"\r\n echo \"\"\r\n echo \"Are you sure?\"\r\n echo \"\"\r\n echo \"(y)es\"\r\n echo \"(n)ot really\"\r\n echo \"~~~~~~~~~~~~~~~~~~~~~\"\r\n echo \"0. Exit\"\r\n read_options_2\r\n }\r\n \r\n select_remains_menu() {\r\n init\r\n echo \"\"\r\n echo \"What about the created SQL dumpfiles?\"\r\n echo \"What are we gonna do with them?\"\r\n echo \"\"\r\n echo \"(k)eep them as zipped backup-file.\"\r\n echo \"(d)elete them.\"\r\n read_options_2\r\n }\r\n\r\n select_restart_menu() {\r\n init\r\n echo \"\"\r\n echo \"All done. What now?\"\r\n echo \"\"\r\n echo \"r) Restart sync.\"\r\n echo \"0) Exit.\"\r\n read_options_2\r\n }\r\n\r\n # read input from the keyboard and take a action\r\n # Exit when user the user select 0 form the menu option.\r\n read_options_1(){\r\n local choice\r\n read -p \"Enter choice: \" choice\r\n case $choice in\r\n 1) SRC=live;select_destination_menu ;;\r\n 2) SRC=dev;select_destination_menu ;;\r\n 3) SRC=testing;select_destination_menu ;;\r\n 4) SRC=nav;select_destination_menu ;;\r\n 5) SRC=loc;select_destination_menu ;;\r\n 6) SRC=tab;select_destination_menu ;;\r\n 0) exit 1 ;;\r\n *) echo -e \"${RED}Error...${STD}\" && sleep 2 && start\r\n esac\r\n }\r\n \r\n read_options_2(){\r\n local choice\r\n read -p \"Enter choice: \" choice\r\n case $choice in\r\n A) DEST=live;select_confirmation_menu ;;\r\n B) DEST=dev;select_confirmation_menu ;;\r\n C) DEST=testing;select_confirmation_menu ;;\r\n D) DEST=nav;select_confirmation_menu ;;\r\n E) DEST=loc;select_confirmation_menu ;;\r\n F) DEST=tab;select_confirmation_menu ;;\r\n y) sync_db ;;\r\n n) start ;;\r\n k) backup ;;\r\n d) delete_files ;;\r\n r) start ;;\r\n 82517) sync_db ;;\r\n 0) exit 1 ;;\r\n *) echo -e \"${RED}Error...${STD}\" && sleep 2 && select_destination_menu\r\n esac\r\n }\r\n\r\n\r\n get_db_variables(){\r\n case $SRC in\r\n live) SRC_MAGE=(\"${live_mag[@]}\");SRC_WP=(\"${live_wp[@]}\") ;;\r\n dev) SRC_MAGE=(\"${dev_mag[@]}\");SRC_WP=(\"${dev_wp[@]}\") ;;\r\n test) SRC_MAGE=(\"${test_mag[@]}\");SRC_WP=(\"${test_wp[@]}\") ;;\r\n nav) SRC_MAGE=(\"${nav_mag[@]}\");SRC_WP=(\"${nav_wp[@]}\") ;;\r\n esac\r\n \r\n case $DEST in\r\n live) DEST_MAGE=(\"${live_mag[@]}\");DEST_WP=(\"${live_wp[@]}\") ;;\r\n dev) DEST_MAGE=(\"${dev_mag[@]}\");DEST_WP=(\"${dev_wp[@]}\") ;;\r\n test) DEST_MAGE=(\"${test_mag[@]}\");DEST_WP=(\"${test_wp[@]}\") ;;\r\n nav) DEST_MAGE=(\"${nav_mag[@]}\");DEST_WP=(\"${nav_wp[@]}\") ;;\r\n esac\r\n \r\n if [ ${SRC_MAGE[5]} == ${DEST_MAGE[5]} ]; then\r\n echo -e \"${RED}Error: source and destination must not be the same.${STD}\"\r\n echo -e \"${RED}Source is: ${SRC_MAGE[5]}.${STD}\"\r\n echo -e \"${RED}Destination is: ${DEST_MAGE[5]}.${STD}\" && sleep 5\r\n start\r\n fi\r\n \r\n \r\n if [ ${DEST_MAGE[5]} == www ]; then\r\n init\r\n echo \"\"\r\n echo \"##################################\"\r\n echo \"########## ATTENTION!!!! #########\"\r\n echo \"##################################\"\r\n echo \"\"\r\n echo \"This will delete all information in the $DEST-database and\"\r\n echo \"will overwrite it with information from $SRC\"\r\n echo \"\"\r\n echo \"##################################\"\r\n echo \"There is NO WAY of restoring the data from $DEST\"\r\n echo \"unless you made a backup once.\"\r\n echo \"\"\r\n echo \"##################################\"\r\n echo \"\"\r\n echo \"So: Are you REALLY DEFINITELY sure?\"\r\n echo \"\"\r\n echo \"##################################\"\r\n echo \"\"\r\n echo \"Enter 82517 for yes.\"\r\n echo \"(n) to abort.\"\r\n echo \"~~~~~~~~~~~~~~~~~~~~~\"\r\n echo \"0. Exit\"\r\n read_options_2\r\n fi\r\n }\r\n\r\n ync_db(){\r\n ## The real purpose of this bash script\r\n echo \"Now copying MySQL databases from $SRC to $DEST...\"\r\n # indexes as follows:\r\n # 0 = type : wordpress | mag\r\n # 1 = host : db13.variomedia.de\r\n # 2 = user : u36052 | u36065 ..\r\n # 3 = database name : db36052 | db36065 ..\r\n # 4 = password\r\n # 5 = subdomain / prefix : for Magento's core_config_data change and to compare $SRC with $DEST\r\n \r\n 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\r\n \r\n echo \"mag DB dumped to ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql\"\r\n \r\n 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\r\n \r\n echo \"WordPress DB dumped to ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql\"\r\n echo \"Now importing into $DEST database...\"\r\n \r\n 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\r\n \r\n echo \"mag DB imported from ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql\"\r\n \r\n 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\r\n \r\n echo \"WordPress DB imported from ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql\"\r\n echo \"Now changing the 'base_url's for the destination Magento-DB.\"\r\n \r\n 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;'\r\n \r\n echo \"Copying databases done. Now clearing ${DEST_MAGE[5]} Magento cache\" && sleep 2\r\n \r\n rm -rfv ~/${DEST_MAGE[5]}/var/cache/\r\n select_remains_menu\r\n }\r\n\r\n\r\n\r\n backup(){\r\n echo \"Zipping SQL dumpfiles and saving them as backup...\"\r\n 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\r\n delete_files\r\n }\r\n \r\n delete_files(){\r\n echo \"Deleting sql-files...\"\r\n rm -fv ~/sql-backup/$TODAY-${SRC_MAGE[0]}-${SRC_MAGE[5]}.sql ~/sql-backup/$TODAY-${SRC_WP[0]}-${SRC_WP[5]}.sql\r\n select_restart_menu\r\n }\r\n\r\n\r\n\r\n # ----------------------------------------------\r\n # Step #3: Trap CTRL+C, CTRL+Z and quit singles\r\n # ----------------------------------------------\r\n trap '' SIGINT SIGQUIT SIGTSTP\r\n \r\n # -----------------------------------\r\n # Step #4: Main logic - infinite loop\r\n # ------------------------------------\r\n \r\n \r\n start() {\r\n unset -v SRC\r\n unset -v DEST\r\n unset -v SRC_MAGE\r\n unset -v SRC_WP\r\n unset -v DEST_MAGE\r\n unset -v DEST_WP\r\n init\r\n select_source_menu\r\n }\r\n \r\n start
Password
Anyone with the link will still need this password to view.
Expires
1 hour
3 hours
6 hours
12 hours
24 hours
48 hours
72 hours
Sign in to enable "Never expires".
Create paste
Please verify you are human
Cancel