Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Bash
Posted by: Сергей Шевченко
Added: Mar 13, 2019 12:40 PM
Modified: Mar 21, 2019 12:01 PM
Views: 0
  1. #!/usr/bin/env bash
  2.  
  3. DB_DRIVER="postgres" # mysql|postgres
  4. DUMP_NAME=${TEMP}/dump.sql
  5.  
  6. REMOTE_DB_HOST="10.101.24.2"
  7. REMOTE_DB_PORT="5432"
  8. REMOTE_DB_NAME="postgres"
  9. REMOTE_DB_USER="sshevchenko"
  10. REMOTE_DB_PASS="droh7eRie7a"
  11.  
  12. LOCAL_DB_HOST="127.0.0.1"
  13. LOCAL_DB_PORT="5432"
  14. LOCAL_DB_NAME="44fz_dev"
  15. LOCAL_DB_USER="postgres"
  16. LOCAL_DB_PASS=""
  17.  
  18. COLOR_RED="\033[0;31m"
  19. COLOR_GREEN="\033[0;32m"
  20. COLOR_YELLOW="\033[1;33m"
  21. COLOR_NONE="\033[0m"
  22.  
  23. # Создание дампа и выкачавние его к себе на машину
  24. function createDampFromDB {
  25.     echo -e "${COLOR_YELLOW}Dump name: ${DUMP_NAME}${COLOR_NONE}"
  26.     echo -e "${COLOR_YELLOW}Create dump file...${COLOR_NONE}"
  27.     case ${DB_DRIVER} in
  28.         postgres)
  29.             echo "    Select driver PostgresSQL"
  30.             pg_dump -h${REMOTE_DB_HOST} -p${REMOTE_DB_PORT} -U${REMOTE_DB_USER} --password="${REMOTE_DB_PASS}" ${REMOTE_DB_NAME} > ${DUMP_NAME}
  31.             ;;
  32.         mysql)
  33.             echo "    Select driver MySQL"
  34.             mysqldump -h${REMOTE_DB_HOST} -P${REMOTE_DB_PORT} -u${REMOTE_DB_USER} -p${REMOTE_DB_PASS} ${REMOTE_DB_NAME} > ${DUMP_NAME}
  35.             ;;
  36.     esac
  37. }
  38.  
  39. # Импортирование дампа
  40. function importDump {
  41.     echo -e "${COLOR_YELLOW}Importing dump file(${DUMP_NAME})...${COLOR_NONE}"
  42.     case ${DB_DRIVER} in
  43.         postgres)
  44.             psql -h${LOCAL_DB_HOST} -p${LOCAL_DB_PORT} -U${LOCAL_DB_USER} -W${LOCAL_DB_PASS} ${LOCAL_DB_NAME} < ${DUMP_NAME}
  45.             ;;
  46.         mysql)
  47.             echo "    Select driver MySQL"
  48.             mysql -h${REMOTE_DB_HOST} -P${REMOTE_DB_PORT} -u${REMOTE_DB_USER} -p${REMOTE_DB_PASS} ${REMOTE_DB_NAME} < ${DUMP_NAME}
  49.             ;;
  50.     esac
  51. }
  52.  
  53. # Удаление дампа
  54. function cleanCache {
  55.     echo -e "${COLOR_YELLOW}Remove dump file...${COLOR_NONE}"
  56.     rm ${DUMP_NAME}
  57. }
  58.  
  59. createDampFromDB
  60. importDump
  61. cleanCache
  62. echo -e "${COLOR_GREEN}Importing dump is SUCCESSFUL!${COLOR_NONE}"