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: May 13, 2019 10:42 AM
Views: 9
  1. #!/usr/bin/env bash
  2.  
  3. DB_DRIVER="postgres" # mysql|postgres
  4. DUMP_NAME="C:/Users/s.shevchenko/dump_`date +%Y_%m_%d`.sql" # ${TEMP}/dump.sql
  5. DUMPER="C:/OSPanel/modules/database/PostgreSQL-9.6-x64/bin/pg_dump.exe"
  6.  
  7. REMOTE_DB_HOST=""
  8. REMOTE_DB_PORT=""
  9. REMOTE_DB_NAME=""
  10. REMOTE_DB_USER=""
  11. REMOTE_DB_PASS=""
  12.  
  13. LOCAL_DB_HOST="127.0.0.1"
  14. LOCAL_DB_PORT="5432"
  15. LOCAL_DB_NAME="postgres"
  16. LOCAL_DB_USER="postgres"
  17. LOCAL_DB_PASS="postgres"
  18.  
  19. COLOR_RED="\033[0;31m"
  20. COLOR_GREEN="\033[0;32m"
  21. COLOR_YELLOW="\033[1;33m"
  22. COLOR_NONE="\033[0m"
  23.  
  24. # Создание дампа и выкачавние его к себе на машину
  25. function createDampFromDB {
  26.     echo -e "${COLOR_YELLOW}Dump name: ${DUMP_NAME}${COLOR_NONE}"
  27.     echo -e "${COLOR_YELLOW}Create dump file...${COLOR_NONE}"
  28.     case ${DB_DRIVER} in
  29.         postgres)
  30.             echo "    Select driver PostgresSQL"
  31.             set PGPASSWORD=${REMOTE_DB_PASS}
  32.             postgres pg_dump -Fc -c ${REMOTE_DB_NAME} --username=${REMOTE_DB_USER} --host=${REMOTE_DB_HOST} --port=${REMOTE_DB_PORT}
  33.             set PGPASSWORD=
  34.             ;;
  35.         mysql)
  36.             echo "    Select driver MySQL"
  37.             mysqldump -h${REMOTE_DB_HOST} -P${REMOTE_DB_PORT} -u${REMOTE_DB_USER} -p${REMOTE_DB_PASS} ${REMOTE_DB_NAME} > ${DUMP_NAME}
  38.             ;;
  39.     esac
  40.     echo -e "    ${COLOR_GREEN}Create dump file is complete!${COLOR_NONE}"
  41. }
  42.  
  43. # Импортирование дампа
  44. function importDump {
  45.     echo -e "${COLOR_YELLOW}Importing dump file(${DUMP_NAME})...${COLOR_NONE}"
  46.     case ${DB_DRIVER} in
  47.         postgres)
  48.             echo "    Select driver PostgresSQL"
  49.             set PGPASSWORD=${LOCAL_DB_PASS}
  50.             psql -h${LOCAL_DB_HOST} -p${LOCAL_DB_PORT} -U${LOCAL_DB_USER} -W${LOCAL_DB_PASS} ${LOCAL_DB_NAME} < ${DUMP_NAME}
  51.             set PGPASSWORD=
  52.             ;;
  53.         mysql)
  54.             echo "    Select driver MySQL"
  55.             mysql -u username -p database_name < file.sql
  56.             ;;
  57.     esac
  58.     echo -e "    ${COLOR_GREEN}Import dump file is complete!${COLOR_NONE}"
  59. }
  60.  
  61. # Удаление дампа
  62. function cleanCache {
  63.     echo -e "${COLOR_YELLOW}Remove dump file...${COLOR_NONE}"
  64.     rm ${DUMP_NAME}
  65.     echo -e "    ${COLOR_GREEN}Clean cache is complete!${COLOR_NONE}"
  66. }
  67.  
  68. createDampFromDB
  69. importDump
  70. #cleanCache
  71. echo -e "${COLOR_GREEN}Importing dump file from ${REMOTE_DB_USER}@${REMOTE_DB_HOST}:${REMOTE_DB_PORT}.${REMOTE_DB_NAME} to ${LOCAL_DB_USER}@${LOCAL_DB_HOST}:${LOCAL_DB_PORT}.${LOCAL_DB_NAME} is SUCCESSFUL!${COLOR_NONE}"
  72.