m7v
10/5/2015 - 1:14 PM

GET DB statistics

GET DB statistics

#!/bin/bash

user_dev="s12876"
pass_dev="RxbJb8EhRd6m2Zk"
host_dev="staging-12090"

declare -A bases_dev
bases_dev[eurailcommerce]="eurailcommercedev"
bases_dev[germanrailpassescommerce]="eurailcomdb46203"
bases_dev[interrailcommerce]="eurailcomdb46138"
bases_dev[jobs]="eurailcomdb52339"


user_stage="s12875"
pass_stage="xDzzcdghG5ZcFHd"
host_stage="staging-12090"

declare -A bases_stage
bases_stage[eurailcommerce]="eurailcommercestage1"
bases_stage[germanrailpassescommerce]="eurailcomdb46205"
bases_stage[interrailcommerce]="eurailcomdb46140"
bases_stage[jobs]="eurailcomdb52341"


echo "DEV"
echo ""

echo "Legend:"
for key in "${!bases_dev[@]}"; do echo "$key => ${bases_dev[$key]}"; done
echo ""

echo "Total DB sizes"
 mysql -h${host_dev} -u${user_dev} -p${pass_dev} -e "SELECT SQL_NO_CACHE IFNULL(table_schema,'Total') 'Database' , Round(Sum(data_length) / 1024 / 1024, 1) 'Data Size in MB',Round(Sum(index_length) / 1024 / 1024, 1) 'Index Size in MB', Round(SUM(data_length + index_length) / 1024 / 1024,1) 'Total in MB'  FROM   information_schema.tables GROUP  BY table_schema with ROLLUP;"
echo ""

for key in "${!bases_dev[@]}"; do
  echo "TOP 30 tables for $key :: ${bases_dev[$key]}"
  mysql -h${host_dev} -u${user_dev} -p${pass_dev} -e "SELECT SQL_NO_CACHE CONCAT(table_schema, '.', table_name) as Table_name, CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows, CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA, CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx, CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size, ROUND(index_length / data_length, 2) idxfrac FROM information_schema.TABLES WHERE table_schema = '${bases_dev[$key]}'  ORDER BY data_length + index_length DESC LIMIT 30;"
echo ""
done

echo ""
echo "--------------------------------------------------------------"
echo ""

echo "STAGE"
echo ""

echo "Legend:"
for key in "${!bases_stage[@]}"; do echo "$key => ${bases_stage[$key]}"; done
echo ""

echo "Total DB sizes"
 mysql -h${host_stage} -u${user_stage} -p${pass_stage} -e "SELECT SQL_NO_CACHE IFNULL(table_schema,'Total') 'Database' , Round(Sum(data_length) / 1024 / 1024, 1) 'Data Size in MB',Round(Sum(index_length) / 1024 / 1024, 1) 'Index Size in MB', Round(SUM(data_length + index_length) / 1024 / 1024,1) 'Total in MB'  FROM   information_schema.tables GROUP  BY table_schema with ROLLUP;"
echo ""

for key in "${!bases_stage[@]}"; do
  echo "TOP 30 tables for $key :: ${bases_stage[$key]}"
  mysql -h${host_stage} -u${user_stage} -p${pass_stage} -e "SELECT SQL_NO_CACHE CONCAT(table_schema, '.', table_name) as Table_name, CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows, CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA, CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx, CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size, ROUND(index_length / data_length, 2) idxfrac FROM information_schema.TABLES WHERE table_schema = '${bases_stage[$key]}'  ORDER BY data_length + index_length DESC LIMIT 30;"
echo""
done


echo ""
echo "--------------------------------------------------------------"
echo ""

echo "Disk Usage on Server"
echo ""
df -Th