-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_table_db_size.sh
54 lines (49 loc) · 1.68 KB
/
check_table_db_size.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Function to display table sizes in MB
show_table_sizes_in_MB() {
echo " "
mysql -e "SELECT table_name AS 'Table', ROUND(((data_length + index_length) / 1024 / 1024), 2) AS 'Size (MB)' FROM information_schema.tables WHERE table_schema = '$1' ORDER BY (data_length + index_length) DESC;"
}
# Function to display table sizes in GB
show_table_sizes_in_GB() {
echo " "
mysql -e "SELECT table_name AS 'Table', ROUND(((data_length + index_length) / 1024 / 1024 / 1024), 2) AS 'Size (GB)' FROM information_schema.tables WHERE table_schema = '$1' ORDER BY (data_length + index_length) DESC;"
}
# Help message
help_message() {
echo " "
echo "Usage: $0 [-m|-g] database_name"
echo "Options:"
echo " -m Display table sizes in megabytes (MB)"
echo " -g Display table sizes in gigabytes (GB)"
echo " -h Display this help message"
exit 1
}
# Check for the number of arguments provided
if [ "$#" -lt 2 ]; then
echo " "
echo -e "\e[31mError: Insufficient arguments. Provide an option (-m or -g) along with a database name.\e[0m" >&2
help_message
fi
# Check for options
while getopts ":m:g:h" opt; do
case $opt in
m)
show_table_sizes_in_MB "${@: -1}"
;;
g)
show_table_sizes_in_GB "${@: -1}"
;;
h)
help_message
;;
\?)
echo -e "\e[31mError: Invalid option: -$OPTARG. Use -m for MB, -g for GB, or -h for help.\e[0m" >&2
exit 1
;;
:)
echo -e "\e[31mError: Option -$OPTARG requires an argument. Use -m for MB, -g for GB, or -h for help.\e[0m" >&2
exit 1
;;
esac
done