Skip to content

Instantly share code, notes, and snippets.

@prochor666
Created September 20, 2019 09:17
Show Gist options
  • Save prochor666/5de8e7061cadc8a2a2325fa423025ded to your computer and use it in GitHub Desktop.
Save prochor666/5de8e7061cadc8a2a2325fa423025ded to your computer and use it in GitHub Desktop.
MySQL data size script
#!/bin/bash
# ---------------------------------------------------------------------
# Mysql size
# prochor666@gmail.com
# This script is licensed under GNU GPL version 2.0 or above
#
# Usage
# mysize.sh [-v]
#
# Params
# -v = verbose
# ---------------------------------------------------------------------
clear
# Setup
CSVFILE="/var/log/mysql/mysql-size.csv"
MUSER="root"
MPASS="YourPassword"
MHOST="localhost"
MYSQLUTIL="$(which mysqldiskusage)"
VERBOSE=$1
COMMAND="$MYSQLUTIL --server=$MUSER:$MPASS@$MHOST --format=csv -q -h -a -m"
RESULT=$($COMMAND)
if [[ "$VERBOSE" == "-v" ]]; then
echo "*********************************************************************"
echo "Running command"
echo "$COMMAND"
echo " "
fi
echo "type;name;size" > $CSVFILE
while read -r line
do
if [[ $line == *","* ]]; then
IFS=','
read -a strarr <<< "$line"
if [[ ${#strarr[*]} == 2 ]]; then
if [[ "$VERBOSE" == "-v" ]]; then
echo "Data detected, ${#strarr[*]} cols on a row ${strarr[0]}..."
fi
if [[ ${strarr[0]} == *"/mysql-bin."* ]]; then
ROW="binlog;${strarr[0]};${strarr[1]}"
elif [[ ${strarr[0]} == "ib"* ]]; then
ROW="innodb;${strarr[0]};${strarr[1]}"
elif [[ ${strarr[0]} == *"mysql-slow."* ]]; then
ROW="slowquery;${strarr[0]};${strarr[1]}"
else
ROW="table;${strarr[0]};${strarr[1]}"
fi
echo $ROW >> $CSVFILE
fi
fi
done < <(printf '%s\n' "$RESULT")
if [[ "$VERBOSE" == "-v" ]]; then
echo " "
echo "*********************************************************************"
echo "Your CSV file dump ($CSVFILE):"
echo " "
echo "$(cat $CSVFILE)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment