Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #**********************************************************************************
  2. #* Name: export_mysql.sh *
  3. #* Author: Tom Carrio *
  4. #* Function: export mysql database data to a flatfile *
  5. #* Last Modif.: 02/22/2016 *
  6. #**********************************************************************************
  7.  
  8. set +e # turn off errexit
  9.  
  10. SCRIPT="export_mysql"
  11. UN="USERNAME"
  12. PW="PASSWORD"
  13. BACK_DIR="/dbservices/backup_data/2016_migration"
  14. MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
  15. VERBOSE=""
  16. LOGGER=""
  17.  
  18. while test $# -gt 0; do
  19. case "$1" in
  20. -h|--help)
  21. printf "%s - %s\n\n" $SCRIPT "export SQL dump of all databases and tables"
  22. printf "%s [options]\n\n" $SCRIPT
  23. printf "%s" "options:\n"
  24. printf "%25s %25s" "-h, --help" "show brief help"
  25. printf "%25s %25s" "-v, --verbose" "show verbose output"
  26. printf "%25s %25s" "-l, --log=FILE" "specify a file to store log in"
  27. printf "%25s %25s" "-u, --user=NAME" "specify the user to access mysql"
  28. printf "%25s %25s" "-p, --password=PASSWORD" "specify the password to access mysql"
  29. printf "%25s %25s" "-e, --execute=PROGRAM" "specify the path to mysqldump"
  30. exit 0
  31. ;;
  32. -v|--verbose)
  33. VERBOSE="2>&1"
  34. shift
  35. ;;
  36. --action*)
  37. PROCESS=`echo $1 | sed -e 's/^[^=]*=//g'`
  38. shift
  39. ;;
  40. -u)
  41. shift
  42. if test $# -gt 0; then
  43. UN=$1
  44. else
  45. echo "No username was specified"
  46. exit 1
  47. fi
  48. shift
  49. ;;
  50. --user*)
  51. UN=`echo $1 | sed -e 's/^[^=]*=//g'`
  52. shift
  53. ;;
  54. -p)
  55. shift
  56. if test $# -gt 0; then
  57. PW=$1
  58. else
  59. printf "%s" "Please enter the password: "
  60. stty -echo
  61. read PW
  62. stty echo
  63. printf "\n"
  64. if [[ -z $PW ]]; then
  65. echo "No password was entered"
  66. exit 1
  67. fi
  68. fi
  69. shift
  70. ;;
  71. --password*)
  72. PW=`echo $1 | sed -e 's/^[^=]*=//g'`
  73. if [[ -z $PW ]]; then
  74. echo "No password was entered"
  75. exit 1
  76. fi
  77. shift
  78. ;;
  79. -e)
  80. shift
  81. if test $# -gt 0; then
  82. MYSQLDUMP=$1
  83. else
  84. echo "No executable was specified"
  85. exit 1
  86. fi
  87. shift
  88. ;;
  89. --execute*)
  90. MYSQLDUMP=`echo $1 | sed -e 's/^[^=]*=//g'`
  91. if [[ -z $MYSQLDUMP ]]; then
  92. echo "No executable was specified"
  93. exit 1
  94. fi
  95. shift
  96. ;;
  97. --output-dir*)
  98. OUTPUT=`echo $1 | sed -e 's/^[^=]*=//g'`
  99. shift
  100. ;;
  101. *)
  102. break
  103. ;;
  104. esac
  105. done
  106.  
  107. if [ ! -d $BACK_DIR ]; then
  108. mkdir -p $BACK_DIR
  109. fi
  110.  
  111. if [ ! -f $MYSQLDUMP ]; then
  112. MYSQLDUMP=`which mysql`
  113. fi
  114.  
  115. mysql --user=$UN --password=$PW < /dbservices/migration/show_dbs.sql | sed '1d' | while read DB_NAME
  116. do
  117. if $MYSQLDUMP --user=$UN --password=$PW $DB_NAME > $BACK_DIR/$DB_NAME.sql; then
  118. mysql --user=$UN --password=$PW $DB_NAME < /dbservices/migration/show_tables.sql | sed '1d' | while read TABLE
  119. do
  120. $MYSQLDUMP --user=$UN --password=$PW $DB_NAME $TABLE > $BACK_DIR/$DB_NAME.$TABLE.sql
  121. done
  122. else
  123. printf "Error occurred during database %s. " $DB_NAME
  124. if [[ -z "$TABLE" ]]; then
  125. printf "Last table in use was %s." $TABLE
  126. fi
  127. printf "\n"
  128. continue
  129. fi
  130. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement