Advertisement
Guest User

Untitled

a guest
May 15th, 2019
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.24 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -eu
  4.  
  5. MYSQL_USERNAME="root"
  6. MYSQL_PASSWORD="123admin_123"
  7.  
  8. FILTRO_BBDD=""
  9. FILTRO_TABLA=""
  10.  
  11. while [[ ! -z "${1-}" ]]; do
  12.     case "${1-}" in
  13.         --bbdd=*)
  14.         FILTRO_BBDD="AND table_schema = '${1#*=}'"
  15.         shift
  16.         ;;
  17.         --tabla=*)
  18.         FILTRO_TABLA="AND table_name = '${1#*=}'"
  19.         shift
  20.         ;;
  21.         *)
  22.         echo >&2 "WARN: Parametro desconocido: $1"
  23.         shift
  24.         ;;
  25.     esac
  26. done
  27.  
  28. mysql "--user=${MYSQL_USERNAME}" "--password=${MYSQL_PASSWORD}" -bs -e "SELECT CONCAT('\`', REPLACE(table_schema, '\`', '\`\`'), '\`.\`', REPLACE(table_name, '\`', '\`\`'), '\`') FROM information_schema.tables WHERE 1=1 ${FILTRO_BBDD} ${FILTRO_TABLA}" | while read tabla; do
  29.     mysql "--user=${MYSQL_USERNAME}" "--password=${MYSQL_PASSWORD}" -s -e "ANALYZE TABLE ${tabla}"
  30. done
  31.  
  32. mysql "--user=${MYSQL_USERNAME}" "--password=${MYSQL_PASSWORD}" -bs -e "SELECT CONCAT('\`', REPLACE(table_schema, '\`', '\`\`'), '\`.\`', REPLACE(table_name, '\`', '\`\`'), '\`') FROM information_schema.tables WHERE 1=1 ${FILTRO_BBDD} ${FILTRO_TABLA} AND engine <> 'InnoDB'" | while read tabla; do
  33.     mysql "--user=${MYSQL_USERNAME}" "--password=${MYSQL_PASSWORD}" -s -e "OPTIMIZE TABLE ${tabla}"
  34. done
  35.  
  36. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement