Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #!/bin/bash
  2. # usage: drupal-quick-dump user host database D8 ("D8" is optional)
  3. # remember to chmod u+x drupal-quick-dump.sh
  4.  
  5. BOLD="$(tput bold)"
  6. RED="$(tput setaf 1)"
  7. GREEN="$(tput setaf 2)"
  8. MAG="$(tput setaf 5)"
  9. RESET="$(tput sgr0)"
  10. USER="$1"
  11. HOST="$2"
  12. DB="$3"
  13. # the fourth parameter might include "D8" as a compatibility flag
  14. DATE=$(date +%Y%m%d-%H%M)
  15. # trap read debug
  16.  
  17. if [ -z "$1" ]
  18. then
  19. echo "Please provide the database user, host and database name, after the command. Add \"D8\" for a compatible dumpfile."
  20. for f in /var/www/drupal/sites/*/settings*; do
  21. if [ -e "$f" ]
  22. then
  23. # print out connection string info for compatible deployments
  24. grep -E "^ *('username'|'password'|'database'|'host')" /var/www/drupal/sites/*/settings*
  25. break
  26. fi
  27. done
  28. exit 1
  29. fi
  30.  
  31. # Get User Password
  32. echo "Please provide the password for ${USER} on db ${DB} hosted at ${HOST}:"
  33. read -rse PASS
  34.  
  35. # Dump Structure
  36. TABLES=$(mysql --skip-column-names -e 'show tables' -u ${USER} -p${PASS} -h ${HOST} ${DB})
  37. # test if connection was unsuccessful. If so, bail.
  38. if [[ ${TABLES} == 0 ]]
  39. then
  40. echo "bad mysql connection info"
  41. exit 1
  42. fi
  43. # Continue of connection retrieved data, here, the schema
  44. echo "${RED}Starting to dump the table structure.${RESET}"
  45. mysqldump --complete-insert --disable-keys --single-transaction --no-data -u ${USER} --password=${PASS} -h ${HOST} ${DB} ${TABLES} > "${DB}.${DATE}".sql
  46.  
  47. # Dump Data, Excluding Certain Tables
  48. echo "${MAG}Starting to dump the table data.${RESET}"
  49.  
  50. if [ "$4" == "D8" ]
  51. then
  52. echo "${RED}Dumping D8 tables.${RESET}"
  53. XTABLES=$(echo "$TABLES" | grep -Ev "^(cache.*|session|watchdog)$")
  54. else
  55. echo "${RED}Dumping D7 tables.${RESET}"
  56. XTABLES=$(echo "$TABLES" | grep -Ev "^(accesslog|cache.*|flood|search_.*|semaphore|sessions|feeds_log|watchdog)$")
  57. fi
  58.  
  59. mysqldump --complete-insert --disable-keys --single-transaction --no-create-info -u ${USER} --password=${PASS} -h ${HOST} ${DB} ${XTABLES} >> "${DB}.${DATE}".sql
  60.  
  61. echo "${MAG}Starting to gzip dump.${RESET}"
  62. echo "${GREEN}"
  63. gzip -v "${DB}.${DATE}".sql
  64. echo "${RESET}"
  65.  
  66. echo "${BOLD}Done!${RESET}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement