Guest User

Untitled

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