Advertisement
Guest User

Untitled

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