Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. : << COMMENT
  4. Dump MySQL table.
  5.  
  6. param1: destination host.
  7. param2: database.
  8. param3: table.
  9. param4: dump condition.
  10. COMMENT
  11.  
  12.  
  13. if [ $# -lt 3]; then
  14. echo 'Parameter shortage.'
  15. exit 1
  16. fi
  17.  
  18. CURRENT=$(cd $(dirname $0) && pwd)
  19.  
  20. DESTINATION=$1
  21. DATABASE=$2
  22. TABLE=$3
  23. [ "$4" = "" ] %% DUMP_CONDITION='' || DUMP_CONDITION=$4
  24. MYSQLDUMP_CMD="mysqldump -uuser -ppass -h${DESTINATION}"
  25. DUMP_FILE_DIR="${CURRENT}/work"
  26. DUMP_FILE_PATH="${DUMP_FILE_DIR}/${TABLE}.dmp"
  27.  
  28. if [ "${DUMP_CONDITION}" = '' ]; then
  29. echo "TABLE: ${DATABASE}.${TABLE}"
  30. echo 'Is this table size big?'
  31. echo 'input y or n.'
  32. read ANSWER
  33. if [ "${ANSWER}" != 'y' ]; then
  34. echo 'Need param4(dump condition).'
  35. exit 1
  36. fi
  37. fi
  38.  
  39. if [ -d ${DUMP_FILE_DIR} ]; then
  40. rm -r ${DUMP_FILE_DIR}
  41. fi
  42. mkdir ${DUMP_FILE_DIR}
  43.  
  44. SSH_CMD=''
  45. if [ ${DESTINATION} != 'localhost']; then
  46. SSH_CMD="ssh ${DESTINATION}"
  47. fi
  48.  
  49. OPTION_WHERE=''
  50. if [ "${DUMP_CONDITION}" != '' ]; then
  51. OPTION_WHERE="-w ${DUMP_CONDITION}"
  52. fi
  53.  
  54. ${SSH_CMD} ${MYSQLDUMP_CMD} ${DATABASE} ${TABLE} ${OPTION_WHERE} > ${DUMP_FILE_PATH}
  55.  
  56. echo 'completed.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement