Guest User

Untitled

a guest
Jun 3rd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Help!?!?!!!!1111omgwtfbbq
  4. showHelp() {
  5. cat <<'EOF'
  6.  
  7. Options:
  8.  
  9. -?, --help print this help message
  10. -v, --verbose verbose mode
  11. -V, --verbose-more be even more verbose
  12. -a, --analyze run analyze over all found tables
  13. -o, --optimize run optimize over all found tables
  14. -d, --database limit actions to specified database
  15. -t, --table limit actions to table in found databases
  16. --statement-table statement to run over every table found
  17. --statement-database statement to run over every table found
  18.  
  19. Statements may contain %s as a placeholder for table|database name.
  20.  
  21.  
  22. Usage examples:
  23.  
  24. This will analyze and optimize all tables in database named `somedb`:
  25. $ myloop -a -o -d somedb
  26.  
  27. This will drop all tables in every database and then drop the database:
  28. $ myloop --statement-table "DROP TABLE %s" --statement-database "DROP DATABASE %s"
  29.  
  30. EOF
  31. }
  32.  
  33. # Check any other arguments provided
  34. if [[ $1 ]]; then
  35.  
  36. # Loop through them
  37. while [[ $1 == -* ]]; do
  38.  
  39. # Check if any valid arguments are passed
  40. case "$1" in
  41.  
  42. -\?|--help) # User wants help
  43. showHelp && exit;;
  44.  
  45. -v|--verbose) # User wants verbose mode
  46. verbose=1
  47. shift;;
  48.  
  49. -V|--verbose-more) # User wants verbose mode
  50. verbose=2
  51. shift;;
  52.  
  53. -a|--analyze) # User wants to analyze target
  54. analyze=1
  55. shift;;
  56.  
  57. -o|--optimize) # User wants to optimize target
  58. optimize=1
  59. shift;;
  60.  
  61. -d|--database) # User wants to limit the command to specified database
  62. only_database="$2"
  63. shift 2;;
  64.  
  65. -t|--table) # User wants to limit the command to specified table
  66. only_table="$2"
  67. shift 2;;
  68.  
  69. --statement-table) # User passed a statement to be run over table(s)
  70. statement_table="$2"
  71. shift 2;;
  72.  
  73. --statement-database) # User passed a statement to be run over database(s)
  74. statement_database="$2"
  75. shift 2;;
  76.  
  77. -*) # Invalid argument passed
  78. echo "Invalid argument: $1"
  79. echo $usage && exit 1;;
  80.  
  81. esac
  82.  
  83. done
  84.  
  85. fi
  86.  
  87. # List of databases we want to exclude
  88. # Add databases you never want to touch here
  89. exclude_databases=(
  90. information_schema
  91. mysql
  92. )
  93.  
  94. # Get username and password from user
  95. read -rp "Enter username: " username
  96. read -rsp "Enter password: " password
  97. echo ""
  98.  
  99. # Function to check if element contains pattern
  100. isIn() {
  101.  
  102. # Make sure arguments are kept local and do not overwrite variables outside of function scope
  103. local pattern="$1" element
  104. shift
  105.  
  106. # Loop through element items
  107. for element; do
  108.  
  109. # If we have a match
  110. [[ $pattern = $element ]] && return 0
  111.  
  112. done
  113.  
  114. # If nothing matched
  115. return 1
  116. }
  117.  
  118. # Function that loops over all tables in database
  119. loopTables() {
  120.  
  121. # Get a list of all tables inside current database
  122. IFS=$'\n' read -d '' -a tables < <(mysql -u"$username" -p"$password" -D"$database" -s --skip-column-names -e "SHOW TABLES")
  123.  
  124. # Loop through tables found
  125. for table in "${tables[@]}"; do
  126.  
  127. # If there was no specific table specified as target, or current table matches specified
  128. if [[ ! $only_table || $only_table = $table ]]; then
  129.  
  130. # If user wants to analyze
  131. if [[ $analyze ]]; then
  132.  
  133. # Check if verbose mode is on
  134. if [[ $verbose ]]; then
  135.  
  136. # Check if extra verbose mode is requested
  137. if [[ $verbose = 2 ]]; then
  138. echo "$database $table ANALYZE TABLE $table"
  139. fi
  140.  
  141. mysql -u"$username" -p"$password" -D "$database" -s -e "ANALYZE TABLE $table"
  142.  
  143. # Don't be verbose
  144. else
  145. mysql -u"$username" -p"$password" -D "$database" -e "ANALYZE TABLE $table" > /dev/null
  146. fi
  147. fi
  148.  
  149. # If user wants to optimize
  150. if [[ $optimize ]]; then
  151.  
  152. # Check if verbose mode is on
  153. if [[ $verbose ]]; then
  154.  
  155. # Check if extra verbose mode is requested
  156. if [[ $verbose = 2 ]]; then
  157. echo "$database $table OPTIMIZE TABLE $table"
  158. fi
  159.  
  160. mysql -u"$username" -p"$password" -D "$database" -s -e "OPTIMIZE TABLE $table"
  161.  
  162. # Don't be verbose
  163. else
  164. mysql -u"$username" -p"$password" -D "$database" -e "OPTIMIZE TABLE $table" > /dev/null
  165. fi
  166. fi
  167.  
  168. # If user wants to run statement on table
  169. if [[ $statement_table ]]; then
  170.  
  171. # Prepare statement
  172. printf -v statement "$statement_table" "$table"
  173.  
  174. # Check if verbose mode is on
  175. if [[ $verbose ]]; then
  176.  
  177. # Check if extra verbose mode is requested
  178. if [[ $verbose = 2 ]]; then
  179. echo echo "$database $table $statement"
  180. fi
  181.  
  182. mysql -u"$username" -p"$password" -D "$database" -s -e "$statement"
  183.  
  184. # Don't be verbose
  185. else
  186. mysql -u"$username" -p"$password" -D "$database" -e "$statement" > /dev/null
  187. fi
  188. fi
  189.  
  190. fi
  191.  
  192. done
  193. }
  194.  
  195. # Send 'show databases' to mysql server and parse the returned result
  196. IFS=$'\n' read -d '' -a databases < <(mysql -u"$username" -p"$password" -s --skip-column-names -e "SHOW DATABASES")
  197.  
  198. # Loop through found databases
  199. for database in "${databases[@]}"; do
  200.  
  201. # Check if database found is not in our exclude list
  202. if ! isIn "$database" "${exclude_databases[@]}"; then
  203.  
  204. # If there was no specific database specified as target, or current database matches specified
  205. if [[ ! $only_database || $only_database = $database ]]; then
  206.  
  207. # If user wants to analyze and/or optimize or run a statement on tables
  208. if [[ $analyze || $optimize || $statement_table ]]; then
  209. loopTables
  210. fi
  211.  
  212. # Run database statement if provided
  213. if [[ $statement_database ]]; then
  214.  
  215. # Prepare statement
  216. printf -v statement "$statement_database" "$database"
  217.  
  218. # Check if verbose mode is on
  219. if [[ $verbose ]]; then
  220.  
  221. # Check if extra verbose mode is requested
  222. if [[ $verbose = 2 ]]; then
  223. echo echo "$database $table $statement"
  224. fi
  225.  
  226. mysql -u"$username" -p"$password" -D "$database" -s -e "$statement"
  227.  
  228. # Don't be verbose
  229. else
  230. mysql -u"$username" -p"$password" -D "$database" -e "$statement" > /dev/null
  231. fi
  232. fi
  233.  
  234. fi
  235.  
  236. fi
  237.  
  238. done
Add Comment
Please, Sign In to add comment