-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_db_size.sh
84 lines (73 loc) · 2.55 KB
/
check_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# ANSI color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to display help
display_help() {
echo -e "${YELLOW}Usage: $0 [-m/-g] schema_name${NC}"
echo -e "${YELLOW}Options:${NC}"
echo -e "${YELLOW} -m ${NC}Display size in ${GREEN}MB${NC} (default if no option provided)"
echo -e "${YELLOW} -g ${NC}Display size in ${GREEN}GB${NC}"
echo -e "${YELLOW} -h ${NC}Display this ${GREEN}help${NC} message"
echo -e "${YELLOW}Examples:${NC}"
echo -e "${GREEN} $0 -m my_schema ${NC}# Display size in ${GREEN}MB${NC} for 'my_schema'"
echo -e "${GREEN} $0 -g another_schema ${NC}# Display size in ${GREEN}GB${NC} for 'another_schema'"
exit 0
}
# Function to execute SQL queries and display results based on the option
execute_query() {
local schema="$1"
local option="$2"
local query=""
if [[ "$option" == "-g" ]]; then
# Query to get size in GB
query="SELECT table_schema AS \`Database\`,
ROUND(SUM(data_length + index_length) / (1024 * 1024 * 1024), 2) AS \`Size_in_GB\`
FROM information_schema.tables
WHERE table_schema LIKE '${schema}%'
GROUP BY table_schema;"
else
# Default: Query to get size in MB
query="SELECT table_schema AS \`Database\`,
SUM(data_length + index_length) / (1024 * 1024) AS \`Size_in_MB\`
FROM information_schema.tables
WHERE table_schema LIKE '${schema}%'
GROUP BY table_schema;"
fi
# Execute the query using mysql command-line tool and color the output
mysql -e "$query"
}
# Check if no arguments provided or too many arguments
if [ "$#" -lt 1 ] || [ "$#" -gt 3 ]; then
echo -e "${RED}Error: Incorrect number of arguments.${NC}"
display_help
fi
# Parse command-line options
while getopts ":hmg" option; do
case "$option" in
m)
selected_option="-m"
;;
g)
selected_option="-g"
;;
h)
display_help
;;
*)
echo -e "${RED}Error: Invalid option. Please use -m for MB, -g for GB, or -h for help.${NC}"
exit 1
;;
esac
done
# Check if an option is provided, if not, default to MB
if [ -z "$selected_option" ]; then
selected_option="-m"
fi
# Set the schema name based on the provided argument
schema_index=$((OPTIND))
schema_name="${!schema_index}"
# Call the function with the specified schema name and option
execute_query "$schema_name" "$selected_option"