Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Import a dump folder to a database
  4. # Will remove the table, view and trigger just before importing each table
  5.  
  6.  
  7. # Where is located your dumps.
  8. # The folder must contain one file per table and must use the .sql.gz extension
  9. DUMP_FOLDER="$1"
  10.  
  11. # MySQL information
  12. MY_HOST="$2"
  13. MY_USER="$3"
  14. MY_DB="$4"
  15. # For security reasons, you should not specify this in the command line (if not specified, the script will prompt for it)
  16. MY_PASS="$5"
  17.  
  18. # binaries
  19. AWK=$(which awk)
  20. GREP=$(which grep)
  21. MYSQL=$(which mysql)
  22. WC=$(which wc)
  23. ZCAT=$(which zcat)
  24.  
  25. if [ -z "${MY_PASS}" ]
  26. then
  27. read -p "MySQL password: " -s MY_PASS
  28. fi
  29.  
  30. MYSQL_OPT="-h ${MY_HOST} -u ${MY_USER} -p$MY_PASS ${MY_DB}"
  31.  
  32. # get max table length (string)
  33. MAX_TABLE_LEN=$(${MYSQL} ${MYSQL_OPT} -e "SHOW TABLES" | ${AWK} "{ print $1 }" | ${GREP} -v '^Tables' | ${WC} -L)
  34.  
  35. for file in `ls -1 ${DUMP_FOLDER}/*.sql.gz`
  36. do
  37. table=$(basename $file)
  38. table=${table%.sql.gz}
  39.  
  40. printf "Table %-*s " ${MAX_TABLE_LEN} $table
  41.  
  42. echo -n "DROP ... "
  43. ${MYSQL} ${MYSQL_OPT} -e "SET FOREIGN_KEY_CHECKS = 0; \
  44. DROP TABLE IF EXISTS $table; \
  45. DROP VIEW IF EXISTS $table; \
  46. SET FOREIGN_KEY_CHECKS = 1"
  47.  
  48. echo -n "TRIGGERS ... "
  49. TRIGGERS=$(${MYSQL} ${MYSQL_OPT} -e "SHOW TRIGGERS IN ${MY_DB} WHERE \`Table\` = '${table}'\G" | ${GREP} "Trigger: " | ${AWK} '{ print $2 }')
  50.  
  51. if [ -n "${TRIGGERS}" ]
  52. then
  53. for trigger in ${TRIGGERS}
  54. do
  55. ${MYSQL} ${MYSQL_OPT} -e "DROP TRIGGER IF EXISTS $trigger"
  56. done
  57. fi
  58.  
  59. echo -n "IMPORT ... "
  60. ${ZCAT} $file | ${MYSQL} ${MYSQL_OPT}
  61.  
  62. echo "DONE"
  63. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement