Skip to content

Commit b1fd783

Browse files
author
Rob Frawley 2nd
committed
Merge pull request #2 in SH/ubuntu-server-helper-collection from feature/mysql-defrag-rewrite to master
* commit '89eb5ccb1049946ce74f2a2088bb9148e496cf58': suppress error from mysql_config_editor suppress error from mysql_config_editor switch to mysql_config_editor changes to defragment script to use common mysql control login
2 parents 2aa891d + 89eb5cc commit b1fd783

File tree

1 file changed

+81
-29
lines changed

1 file changed

+81
-29
lines changed

mysql/mysql-defragment.bash

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,83 @@
11
#!/bin/bash
2-
echo "MYSQL TABLE OPTOMIZER SCRIPT v0.5"
3-
echo ""
4-
echo "Enter the password for the mysql root@localhost user when prompted."
5-
mysql_config_editor set --skip-warn --login-path=local --host=localhost --user=root --password
6-
7-
mysql --login-path=local -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do
8-
mysql --login-path=local -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do
9-
if [ "$datafree" != "NULL" ] && [ "$datafree" -gt 0 ] ; then
10-
fragmentation=$(($datafree * 100 / $datalength))
11-
echo " - $database.$name is $fragmentation% fragmented."
12-
echo " |- Size : $(( $datalength / 1024 / 1024 )) MB"
13-
echo -n " |- Action : "
14-
if [ "$database.$name" == "world-dev.ProjectFileContent" ] ; then
15-
echo "Skipping per explicit request."
16-
else
17-
echo "Performing optomization routing."
18-
echo " |- Command : mysql --login-path=local -NBe \"OPTIMIZE TABLE '$name';\" \"$database\""
19-
echo -n " |- Optimizing:"
20-
mysql --login-path=local -NBe "OPTIMIZE TABLE $name;" "$database" > /dev/null
21-
if [ "$?" -eq 0 ]; then
22-
echo "complete."
23-
else
24-
echo "FAILURE!"
25-
sleep 20
26-
fi
27-
fi
28-
echo ""
29-
fi
30-
done
2+
3+
##
4+
## Scribe Inc
5+
## Perform a hot-backup of ${bin_mysql} data
6+
##
7+
8+
## Gain self-awareness and common library
9+
readonly SELF_DIRPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
10+
readonly BOOTSTRAP_FILENAME="common-bootstrap.bash"
11+
readonly BOOTSTRAP_FILEPATH="${SELF_DIRPATH}/../${BOOTSTRAP_FILENAME}"
12+
13+
## Include common bootstrap
14+
source "${BOOTSTRAP_FILEPATH}"
15+
16+
##
17+
## User Configuration
18+
##
19+
db_user="${MYSQL_CONTROL_USER}"
20+
db_pass="${MYSQL_CONTROL_PASS}"
21+
db_host="localhost"
22+
23+
## Display welcome message
24+
function out_welcome_custom
25+
{
26+
#
27+
# Display welcome message text
28+
#
29+
out_lines \
30+
"${SELF_SCRIPT_NAME}" \
31+
"" \
32+
"This script handles optimizing (defragmenting) all mysql tables for a given host."
33+
}
34+
35+
#
36+
# Check for (required) first and second parameters
37+
#
38+
if [[ $(echo "$@" | grep -E -e "\-?\-h(elp)?\b") ]]; then
39+
40+
#
41+
# Display usage and exit with non-zero value
42+
#
43+
out_usage
44+
45+
fi
46+
47+
## Welcome message
48+
out_welcome
49+
50+
## Check for require bins
51+
check_bins_and_setup_abs_path_vars mysql grep mysql_config_editor
52+
53+
${bin_mysql_config_editor} set --skip-warn --login-path=local --host=localhost --user=${db_user} --password=${db_pass} > /dev/null 2>&1
54+
55+
${bin_mysql} --login-path=local -NBe "SHOW DATABASES;" | ${bin_grep} -v 'lost+found' | while read database ; do
56+
${bin_mysql} --login-path=local -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do
57+
if [ "$datafree" != "NULL" ] && [ "$datafree" -gt 0 ] ; then
58+
fragmentation=$(($datafree * 100 / $datalength))
59+
echo " - $database.$name is $fragmentation% fragmented."
60+
echo " |- Size : $(( $datalength / 1024 / 1024 )) MB"
61+
echo -n " |- Action : "
62+
if [ "$database.$name" == "world-dev.ProjectFileContent" ] ; then
63+
echo "Skipping per explicit request."
64+
else
65+
echo "Performing optomization routing."
66+
echo " |- Command : ${bin_mysql} --login-path=local -NBe \"OPTIMIZE TABLE '$name';\" \"$database\""
67+
echo -n " |- Optimizing:"
68+
${bin_mysql} --login-path=local -NBe "OPTIMIZE TABLE $name;" "$database" > /dev/null
69+
if [ "$?" -eq 0 ]; then
70+
echo "Complete!"
71+
else
72+
echo "FAILURE!"
73+
sleep 20
74+
fi
75+
fi
76+
echo ""
77+
fi
78+
done
3179
done
80+
81+
out_done "Completed!"
82+
83+
## EOF

0 commit comments

Comments
 (0)