Compare commits

..

17 Commits

Author SHA1 Message Date
7b97a5d6ff create_database.sh: a really small changes on scipt output. 2025-03-11 10:51:09 +01:00
014cf46b74 Merge branch 'master' of https://git.oopen.de/script/mysql 2025-01-17 02:23:43 +01:00
e5ba1decfb dump_database.sh: fix error dumpimg remote database statement. 2025-01-17 02:23:23 +01:00
b890525173 dump_database.sh: fix error getting commandline valiues for mysql credentials. 2025-01-16 17:58:43 +01:00
2ae83440cb README.create-user: some changes.. 2024-03-30 11:43:41 +01:00
35f224d9f2 Add file 'README.create-user'. 2024-03-30 11:34:09 +01:00
845e9cf8ad Fix errors in case of multiple mysql installations. 2024-01-17 01:40:49 +01:00
62c2f65dbd Delete file 'databases-web0-alt.lst'. 2023-12-10 12:14:40 +01:00
8efe89e79a Add not very well tested script 'dump_database.sh'. 2023-11-07 18:48:53 +01:00
be0404d9d6 restore_database.sh: while restoring the database set value 'max_allowed_packet' to '1G'. 2023-11-07 17:53:02 +01:00
d0d7c01ba3 Merge branch 'master' of https://git.oopen.de/script/mysql 2023-08-13 16:28:52 +02:00
a585a90b56 create_database.sh: add default database user. 2023-08-13 16:28:32 +02:00
5bdb646de7 optimize_mysql_tables.sh, optimize_mysql_tables-ND.sh: some design changes.. 2023-08-09 11:09:11 +02:00
30ca2c1a72 flush_host_cache.sh, flush_query_cache.sh: support parallel installations. 2023-08-09 09:15:49 +02:00
8876617d4c create_database.sh: some minbor changes in script output. 2023-08-09 09:10:35 +02:00
cd815ee66f Add support vor unix socket located at /run/mysqld folder. 2023-08-02 16:06:15 +02:00
270def192f Add script'mysqltuner.pl'. 2023-05-04 14:38:09 +02:00
17 changed files with 9940 additions and 550 deletions

105
README.create-user Normal file
View File

@ -0,0 +1,105 @@
# ----------
# MariaDB > 10.3
# ----------
# ---
# - Backup user
# ---
USER=backup
PASS=backup
mysql -u root -S /run/mysqld/mysqld.sock -N -s -e "CREATE USER '${USER}'@'localhost' IDENTIFIED BY '${PASS}'"
mysql -u root -S /run/mysqld/mysqld.sock -N -s -e "GRANT USAGE ON *.* TO '${USER}'@'localhost'"
mysql -u root -S /run/mysqld/mysqld.sock -N -s -e "GRANT SELECT, SHOW VIEW, EVENT, LOCK TABLES, EXECUTE, RELOAD, BINLOG MONITOR, REPLICATION CLIENT ON *.* TO '${USER}'@'localhost'"
mysql -u root -S /run/mysqld/mysqld.sock -N -s -e "FLUSH PRIVILEGES"
# ---
# - Admin user (Warenform)
# ---
CREATE USER IF NOT EXISTS 'admin'@'localhost' IDENTIFIED BY PASSWORD '*B45517A8959A464158F62B12FE7CDBAD79DCA343'; FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
# ----------
# MySQL: Option/Variable --login-path
# ----------
# as user backup
#
mysql_config_editor set --login-path=local --socket=/run/mysqld/mysqld.sock --user=backup --password
# as user root
#
mysql_config_editor set --login-path=local --socket=/run/mysqld/mysqld.sock --user=root --password
# ----------
# MySQL 5.7
# ----------
# ---
# - Backup user
# ---
MYSQL_CREDENTIAL_ARGS="--login-path=local"
USER=backup
PASS=backup
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"INSERT INTO user (Host,User,authentication_string,Select_priv,Reload_priv,Super_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv,Execute_priv,ssl_cipher,x509_issuer,x509_subject) VALUES('localhost','${USER}',password('${PASS}'),'Y','Y','Y','Y','Y','Y','Y','Y','','','');"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"CREATE USER IF NOT EXISTS 'admin'@'localhost' IDENTIFIED WITH mysql_native_password ; UPDATE user SET authentication_string = '*B45517A8959A464158F62B12FE7CDBAD79DCA343' WHERE user = 'admin'; FLUSH PRIVILEGES;"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e "FLUSH PRIVILEGES";
# ---
# - Admin user (Warenform)
# ---
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"CREATE USER 'admin'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*B45517A8959A464158F62B12FE7CDBAD79DCA343' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e "FLUSH PRIVILEGES";
# ----------
# MySQL 8.x
# ---------
MYSQL_CREDENTIAL_ARGS="--login-path=local"
USER=backup
PASS=backup
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"INSERT INTO user (Host,User,Select_priv,Reload_priv,Super_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv,Execute_priv,ssl_cipher,x509_issuer,x509_subject) VALUES('localhost','${USER}','Y','Y','Y','Y','Y','Y','Y','Y','','','');-"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"CREATE USER '${USER}'@'localhost' IDENTIFIED WITH mysql_native_password BY '${PASS}'"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e \
"SET PASSWORD FOR 'backup'@'localhost' = 'backup';"
mysql ${MYSQL_CREDENTIAL_ARGS} -N -s -e "FLUSH PRIVILEGES";

View File

@ -84,11 +84,11 @@ fatal(){
if $terminal ; then
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m"
else
echo " \033[31m\033[1mFatal error\033[m:"
echo -e " \033[31m\033[1mFatal error\033[m: \033[1mScript will be interrupted.\033[m"
fi
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
else
if [[ -n "$*" ]] ; then
echo " [ Fatal ]: $*"
@ -156,6 +156,13 @@ echo_skipped() {
fi
}
blank_line() {
if $terminal ; then
echo ""
fi
}
is_number() {
return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1);
@ -276,6 +283,23 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
blank_line
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file=""
@ -377,7 +401,14 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> ${tmp_log_file})"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
@ -435,22 +466,19 @@ if ! $NON_INTERACTIVE_MODE ; then
echo "Insert Database user who will grant full access to database '${DATABASE_NAME}'.."
echo ""
echo ""
if [ -z "$DATABASE_USER" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
while [ "X$DATABASE_USER" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
read DATABASE_USER
done
_DATABASE_USER="$DATABASE_NAME"
else
_DATABASE_USER="$DATABASE_USER"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_USER}]: "
read DATABASE_USER
if [[ "X$DATABASE_USER" = "X" ]]; then
DATABASE_USER=$_DATABASE_USER
fi
fi
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_USER}]: "
read DATABASE_USER
if [[ "X$DATABASE_USER" = "X" ]]; then
DATABASE_USER=$_DATABASE_USER
fi
fi
if $DATABASE_PASSWD_NEEDED ; then
@ -491,16 +519,16 @@ if ! $NON_INTERACTIVE_MODE ; then
echo " utf8"
echo ""
if [ -z "$DATABASE_CHARACTER_SET" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert character_set for database '${DATABASE_NAME}': "
read DATABASE_CHARACTER_SET
while [ "X$DATABASE_CHARACTER_SET" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
echononl "insert character_set for database '${DATABASE_NAME}': "
read DATABASE_CHARACTER_SET
done
else
_DATABASE_CHARACTER_SET="$DATABASE_CHARACTER_SET"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_CHARACTER_SET}]: "
echononl "Insert character_set for database '${DATABASE_NAME}' [${_DATABASE_CHARACTER_SET}]: "
read DATABASE_CHARACTER_SET
if [[ "X$DATABASE_CHARACTER_SET" = "X" ]]; then
DATABASE_CHARACTER_SET=$_DATABASE_CHARACTER_SET
@ -526,16 +554,16 @@ if ! $NON_INTERACTIVE_MODE ; then
echo " utf8_bin"
echo ""
if [ -z "$DATABASE_COLLATION" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert collation for database '${DATABASE_NAME}': "
read DATABASE_COLLATION
while [ "X$DATABASE_COLLATION" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert collation for database '${DATABASE_NAME}': "
read DATABASE_COLLATION
done
else
_DATABASE_COLLATION="$DATABASE_COLLATION"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_COLLATION}]: "
echononl "Insert collation for database '${DATABASE_NAME}' [${_DATABASE_COLLATION}]: "
read DATABASE_COLLATION
if [[ "X$DATABASE_COLLATION" = "X" ]]; then
DATABASE_COLLATION=$_DATABASE_COLLATION
@ -581,10 +609,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -634,7 +680,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -661,6 +707,9 @@ if ! $QUIET_MODE ; then
echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER"
@ -706,19 +755,19 @@ fi
# - Test if Database already exists
# -
_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
_result="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
if [[ "$_result" = "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' already exists"
fi
echononl " Create database \033[1m$DATABASE_NAME\033[m (full UTF-8 support - ${DATABASE_CHARACTER_SET})"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE DATABASE $DATABASE_NAME CHARACTER SET ${DATABASE_CHARACTER_SET} COLLATE ${DATABASE_COLLATION}" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
echononl " Create database \033[1m$DATABASE_NAME\033[m (UTF-8 support - utf8)"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE DATABASE $DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -747,7 +796,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
fi
echononl " Create database user '$DATABASE_USER' access from locahost"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -762,7 +811,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Create database user '$DATABASE_USER' access from '$_ip' "
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -777,7 +826,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -791,7 +840,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -808,7 +857,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = 'localhost'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -817,7 +866,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok
echononl " Create database user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -832,7 +881,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -843,7 +892,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to a user '$DATABASE_USER' on database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -858,7 +907,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -867,7 +916,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -882,7 +931,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -892,7 +941,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -909,7 +958,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -919,7 +968,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -933,7 +982,7 @@ else
for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -943,7 +992,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME' from $_ip"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -960,7 +1009,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -1,113 +0,0 @@
123comics
abw
abw_kita
abw_sprachschule
alphabuendnis_cw
alternative_wipo
andreafrank
annen
arbeitszeittuev
artikelbox
bause
birkwald
birkwald2
bit6neu
bksf
bluefrog
cafe_hudl
callinus
dacapo
demasi
demasi2
diakonieverbund
dialogstiftung
dijv
dspp
egypt_at_work
ejh_bochum
ejh_geltow
ejh_schweicheln
emt_zentralrat
ensemble1800
faireintegra2021
faireintegration
faire_mob_db
faire_mob_dbdemo
faire_mobilitaet
fgmm
frauencoaching
freieschule
gambio2
gender
gerechtigkeittv
gleichstellung3
gleichstellungsbericht
green_economy
haendel2
hedtfeld
hellepanke
hellepankeold
hillel
hoff
information_schema
iponphoto
jao
jongleur_till
kescher
keshet
kilele
kilele_test
kipping2
kleinpetersberg
klute2
koall
kontext3
kontext3_test
kontext_cms
kontextdemo
korte
langensiepen
lay3
lebenlernen
liebich3
linkeeu
links_bewegt
lsvrlp_kontext3
mysql
netzwerkstatt
performance_schema
photoforwork
platinit
pragerfruehling
rabbinerordination
radialstiftung
roespel
rosalux_eu
schalauske
schindler
scholz
schreiber
schwabe
shadesillegality
solimod
sopoinfo
spa_test
spd_laage
sprachenatelier
sprachenatelier3
sprachenatelier4
spriesterbach
stressbarometer
stressbarometer_igm
sys
test
trabant
troost
tsc_project
ttip
tvet_indonesia
unconference_rsa
wagenknecht2
wfinterim
willems
wwl_intellektuelle

View File

@ -273,6 +273,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file=""
@ -374,7 +390,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> ${tmp_log_file})"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -468,8 +492,26 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -508,6 +550,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -517,7 +561,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -544,6 +588,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Drop Database................: $DATABASE_NAME"
if $DO_NOT_DELTE_USER ; then
echo -e " Drop Database user...........: \033[33mNone\033[m"
@ -575,7 +621,7 @@ if ! $QUIET_MODE ; then
fi
echononl " Drop database '$DATABASE_NAME'.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "DROP DATABASE $DATABASE_NAME" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "DROP DATABASE $DATABASE_NAME" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
@ -584,7 +630,7 @@ else
fi
echononl " Delete all entries for database '$DATABASE_NAME' from table mysql.db .."
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM db WHERE Db = '$DATABASE_NAME'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -597,7 +643,7 @@ fi
# - Test if given user is associated with another database
# -
echononl " Check if db user '$DATABASE_USER' is also associated with another database"
_count_user="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count_user="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM db WHERE User = '$DATABASE_USER'" 2> $tmp_log_file)"
if [[ -z "$_count_user" ]]; then
echo_failed
@ -608,7 +654,7 @@ else
warn "Db user '$DATABASE_USER' is already in use.\n So user will not be deleted from table 'mysql.user'."
else
echononl " Delete username '$DATABASE_USER' from table mysql.user"
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM user WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -621,7 +667,7 @@ fi
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -257,6 +257,23 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file=""
@ -358,7 +375,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> ${tmp_log_file})"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -428,10 +453,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -470,6 +513,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -479,7 +524,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -506,6 +551,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo -e " Drop Database user...........: \033[33m$DATABASE_USER\033[m"
echo ""
echo ""
@ -534,7 +581,7 @@ fi
# - Check if User already exists
# -
echononl " Check if user '$DATABASE_USER' exists .."
if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
if [[ "$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
user_exists=true
else
user_exists=false
@ -546,7 +593,7 @@ echo_ok
# - Test given user is associated with a database
# -
echononl " Check if db user '$DATABASE_USER' is associated with a database"
_count_user="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count_user="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM db WHERE User = '$DATABASE_USER'" 2> $tmp_log_file)"
if [[ -z "$_count_user" ]]; then
echo_failed
@ -563,7 +610,7 @@ fi
echononl " Delete username '$DATABASE_USER' from table mysql.db"
if [[ $_count_user -gt 0 ]]; then
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM db WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -577,7 +624,7 @@ fi
echononl " Delete username '$DATABASE_USER' from table mysql.user"
if $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"DELETE FROM user WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -592,7 +639,7 @@ else
fi
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -245,6 +245,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
# -------------
# - Load Settings from configuration file mysql_credetials.conf
@ -297,7 +313,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -357,10 +381,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -399,6 +441,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -408,7 +452,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -434,6 +478,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database name................: $DATABASE_NAME"
@ -453,7 +499,7 @@ if ! $NON_INTERACTIVE_MODE ; then
fi
_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
_result="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
if [[ -z "$_result" ]]; then
fatal "No database '$DATABASE_NAME' found!"
fi
@ -467,7 +513,7 @@ if ! $QUIET_MODE ; then
fi
echononl " Get table list from database '$DATABASE_NAME' .."
_TABLES="$(mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "show tables" 2> $tmp_log_file)"
_TABLES="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "show tables" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
@ -484,7 +530,7 @@ echononl " Drop tables form database '$DATABASE_NAME' .."
_failed=false
> $tmp_log_file
for _table in $_TABLES ; do
mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed=true
_tables_not_yet_deleted+=("$_table")
@ -507,7 +553,7 @@ if $_failed ; then
_failed_again=false
: > $tmp_log_file
for _table in "${_tables_not_yet_deleted[@]}" ; do
mysql $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS $DATABASE_NAME -N -s -e "DROP TABLE \`$_table\`" >> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
_failed_again=true
_tables_not_deleted+=("$_table")

1173
dump_database.sh Executable file

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,8 @@
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
#LOGGING=true
LOGGING=false
@ -11,6 +13,27 @@ LOGGING=false
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
@ -34,6 +57,65 @@ fatal(){
clean_up 1
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ]: $*"
fi
echo ""
}
warn (){
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
else
echo " [ Warning ]: $*"
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
else
echo " [ Info ]: $*"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[80G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[80G[ \033[37mskipped\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
@ -61,7 +143,7 @@ detect_mysql_version () {
mysqladmin=`realpath $(which mysqladmin) 2>/dev/null`
if [ -z "$mysqladmin" ]; then
if [ -x "/usr/local/mysql/bin/mysqladmin" ]; then
mysql=/usr/local/mysql/bin/mysqladmin
mysqladmin=/usr/local/mysql/bin/mysqladmin
else
echo
echo -e "\t[ Error ]: \"mysqladmin\" not found !!!"
@ -87,16 +169,23 @@ fi
#-----------------------------
#---------------------------------------
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_skipped
if $terminal ;then
warn "No Configuration File found. Loading defaults.."
fi
fi
#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush host cache.."
fi
if [[ -z "$mysql_credential_args" ]]; then
@ -135,14 +224,39 @@ if [[ -z "$mysql_credential_args" ]]; then
fi
# Flush host cache
#
$mysqladmin $mysql_credential_args flush-hosts
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing host cache failed !!"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing host cache"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
exit 0
blank_line
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
echononl " [ ${mysql_version} ]: Flush host cache.."
$mysqladmin $mysql_credential_args flush-hosts 2> $tmp_log_file
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysqladmin" ]] ; then
/usr/local/mysql/bin/mysqladmin $mysql_credential_args flush-hosts
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
fi
fi
fi
(( index_arr++ ))
done
clean_up 0

View File

@ -3,8 +3,151 @@
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
#LOGGING=true
LOGGING=false
tmp_log_file="$(mktemp)"
# -------------
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
else
echo -e " \033[31m\033[1mFatal error\033[m: \033[1mScript will be interrupted.\033[m"
fi
else
if [[ -n "$*" ]] ; then
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated.."
else
echo " Fatal error: Script was terminated.."
fi
fi
echo ""
clean_up 1
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ]: $*"
fi
echo ""
}
warn (){
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
else
echo " [ Warning ]: $*"
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
else
echo " [ Info ]: $*"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[80G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[80G[ \033[37mskipped\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
if [[ -z "$_MYSQLD_VERSION" ]]; then
fatal "No installed MySQL server or distribution found!"
elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then
MYSQL_CUR_DISTRIBUTION="MariaDB"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then
MYSQL_CUR_DISTRIBUTION="Percona"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then
MYSQL_CUR_DISTRIBUTION="MySQL"
fi
MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)"
MYSQL_MAJOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1)"
MYSQL_MINOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f2)"
MYSQL_PATCH_LEVEL="$(echo $MYSQL_VERSION | cut -d '.' -f3)"
MYSQL_MAIN_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1,2)"
}
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
mysql=`realpath $(which mysql) 2>/dev/null`
if [ -z "$mysql" ]; then
@ -18,41 +161,6 @@ if [ -z "$mysql" ]; then
fi
fi
# Get current version
#
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
CURRENT_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)"
MYSQL_MAIN_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1,2`
MYSQL_MAJOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1`
MYSQL_MINOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f2`
MYSQL_PATCH_LEVEL=`echo $CURRENT_VERSION | cut -d '.' -f3`
if [[ -z "$_MYSQLD_VERSION" ]]; then
echo ""
echo -e "\t[ Error ]: No installed MySQL server or distribution found!"
echo ""
elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then
MYSQL_CUR_DISTRIBUTION="MariaDB"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then
MYSQL_CUR_DISTRIBUTION="Percona"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then
MYSQL_CUR_DISTRIBUTION="MySQL"
fi
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]]; then
if [[ "$MYSQL_MAJOR_VERSION" -gt 8 ]] \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -gt 0 ]] ) \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -eq 0 ]] \
&& [[ $MYSQL_PATCH_LEVEL -ge 3 ]] ); then
echo ""
echo -e "\t[ Error ]: Query cache is no longer supported since (MySQL 8.0.3)"
echo ""
exit
fi
fi
#---------------------------------------
#-----------------------------
@ -60,16 +168,35 @@ fi
#-----------------------------
#---------------------------------------
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_skipped
if $terminal ;then
warn "No Configuration File found. Loading defaults.."
fi
fi
#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
if [[ -z "$mysql_credential_args" ]]; then
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MYSQL_MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MYSQL_MAJOR_VERSION -eq 10 ]] && [[ $MYSQL_MINOR_VERSION -gt 3 ]] )) ; then
detect_mysql_version
MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
MINOR_VERSION="$MYSQL_MINOR_VERSION"
PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
mysql_credential_args="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
@ -94,24 +221,43 @@ if [[ -z "$mysql_credential_args" ]]; then
parameter manually."
fi
fi
fi
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush query cache.."
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
# Flush Query Cache
#
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE"
blank_line
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing query cache failed !!"
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing query cache"
fi
echononl " [ ${mysql_version} ]: Flush query cache.."
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
fi
fi
fi
exit 0
(( index_arr++ ))
done
clean_up 0

View File

@ -267,6 +267,21 @@ fi
# -
clear
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
# -------------
# - Load Settings from configuration file mysql_credetials.conf
@ -319,7 +334,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -441,10 +464,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -483,6 +524,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
mysql_credential_args="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -492,7 +535,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -521,6 +564,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD"
echo ""
@ -562,7 +607,7 @@ fi
# - Check if User already exists
# -
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
if [[ "$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
user_exists=true
else
user_exists=false
@ -573,7 +618,7 @@ echo_ok
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -586,8 +631,8 @@ else
echo_skipped
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
echononl " Grant permissions to access and use the ${mysql_command} server to user '$DATABASE_USER'"
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -598,7 +643,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on all database "
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT SELECT, SHOW VIEW, EVENT, LOCK TABLES, RELOAD, REPLICATION CLIENT ON *.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -613,7 +658,7 @@ if $ACCESS_FROM_OUTSIDE ; then
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -622,7 +667,7 @@ if $ACCESS_FROM_OUTSIDE ; then
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -637,7 +682,7 @@ if $ACCESS_FROM_OUTSIDE ; then
fi
echononl " Allow access to user '$DATABASE_USER' on all databases from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -647,7 +692,7 @@ if $ACCESS_FROM_OUTSIDE ; then
fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT SELECT, SHOW VIEW, EVENT, LOCK TABLES, RELOAD, REPLICATION CLIENT ON *.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -664,7 +709,7 @@ fi
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -274,6 +274,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file=""
@ -375,7 +391,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -514,10 +538,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -556,6 +598,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
mysql_credential_args="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -565,7 +609,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -594,6 +638,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD"
@ -635,7 +681,7 @@ fi
# - Check if Database already exists
# -
_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
_result="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
if [[ "$_result" != "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' does not exists"
fi
@ -643,7 +689,7 @@ fi
# - Check if User already exists
# -
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
if [[ "$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
user_exists=true
else
user_exists=false
@ -668,7 +714,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -686,7 +732,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -695,7 +741,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echo_ok
echononl " Create database user '$DATABASE_USER' access from '$_ip' "
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -713,7 +759,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -727,7 +773,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -746,7 +792,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -760,7 +806,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -771,7 +817,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to a user '$DATABASE_USER' on database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -786,7 +832,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -795,7 +841,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -810,7 +856,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -820,7 +866,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -837,7 +883,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -847,7 +893,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -861,7 +907,7 @@ else
for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -871,7 +917,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME' from $_ip"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -888,7 +934,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

View File

@ -289,6 +289,22 @@ if [[ -n "$mysql_credential_args" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if ! $NON_INTERACTIVE_MODE ; then
declare -i index_arr=0
@ -313,7 +329,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -435,10 +459,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@ -477,6 +519,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
mysql_credential_args="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -486,7 +530,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -515,6 +559,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD"
echo ""
@ -556,7 +602,7 @@ fi
# - Check if User already exists
# -
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
if [[ "$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
user_exists=true
else
user_exists=false
@ -581,7 +627,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -599,7 +645,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -608,7 +654,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echo_ok
echononl " Create database user '$DATABASE_USER' access from '$_ip' "
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -626,7 +672,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on all Databases"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON *.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -640,7 +686,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on all Database from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -659,12 +705,12 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
echo ""
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'\""
echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
# > $tmp_log_file 2>&1
#echo ""
#echo "${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'\""
#echo ""
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
@ -676,12 +722,12 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
echo ""
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'\""
echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \
# > $tmp_log_file 2>&1
#echo ""
#echo "${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'\""
#echo ""
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
@ -693,7 +739,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo ""
echo "$MYSQL_CREDENTIAL_ARGS -N -s -e \"GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'\""
echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
#${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'" \
# > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -708,7 +754,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@ -717,7 +763,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -732,7 +778,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Allow access to user '$DATABASE_USER' on all databases from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -742,7 +788,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@ -759,7 +805,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -769,7 +815,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -783,7 +829,7 @@ else
for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -793,7 +839,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from $_ip"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@ -810,7 +856,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"

7264
mysqltuner.pl Executable file

File diff suppressed because it is too large Load Diff

View File

@ -5,16 +5,21 @@ working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
# - Lock directory exists, until the script ends. So
# - we can check, if a previos instanze is already running.
# -
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# -------------
# - Variable settings
# - Variable (default) settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
VERBOSE=false
DEFAULT_to_addresses="argus@oopen.de"
# -------------
# --- Some functions
@ -64,7 +69,14 @@ usage() {
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
if [[ ${1} -gt 0 ]] ; then
[[ -f "${log_file}" ]] && cp ${log_file} "/var/log/"
else
rm -f "/var/log/$(basename "${log_file}")"
fi
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
@ -159,6 +171,12 @@ trim() {
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
@ -182,28 +200,16 @@ detect_mysql_version () {
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Is this script running on terminal ?
# -
# -------------
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
# -------------
# - Read Commandline Parameters
@ -230,27 +236,58 @@ if [[ "$(trim $*)" =~ " -h" ]] || [[ "$(trim $*)" =~ " --help" ]] ; then
fi
# -------------
# - Job is already running?
# -------------
## - If job already runs, stop execution..
## -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum=`date +"%d.%m.%Y"`
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e "\tExiting now.."
echo ""
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $from_address $_to_address
done
exit 1
fi
# -------------
# - Load Settings from configuration file
# -------------
if [[ -n "$1 " ]] ; then
DATABASES="$1"
GIVEN_DATABASE="$1"
else
DATABASES=""
GIVEN_DATABASE=""
fi
if $terminal ; then
echo ""
fi
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
source "$conf_file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fatal "$(cat $log_file)"
fi
else
echo_skipped
@ -260,7 +297,23 @@ else
fi
#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
[[ -z "${to_addresses}" ]] && to_addresses="${DEFAULT_to_addresses}"
# -------------
# - Some default values
# -------------
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
if [[ -z "$mysql_credential_args" ]]; then
@ -298,31 +351,80 @@ if [[ -z "$mysql_credential_args" ]]; then
fi
fi
[[ -z "$log_file" ]] && log_file="$DEFAULT_LOG_FILE"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
declare -i length_table_name
declare -i number_blank_signd
declare -i index_i
for _val in ${mysql_credential_args_arr[@]} ; do
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_command="$mysql"
_all_success=true
IFS=':' read -a _val_arr <<< "${_val}"
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of databases at host '$(hostname -f)'."
fi
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of databases at host '$(hostname -f)'." > $log_file
if [[ -z "$DATABASES" ]] ; then
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
DATABASES="$(${mysql_command} $mysql_credential_args -N -s -e "show databases" 2> $log_file)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
DATABASES="$(${mysql_command} $mysql_credential_args -N -s -e "show databases" 2> $log_file)"
if [[ $? -ne 0 ]] ; then
error "$(cat $log_file)"
continue
fi
fi
fi
fi
found=false
if [[ -n "$GIVEN_DATABASE" ]] ; then
for db in $DATABASES ; do
if [[ "$db" = "$GIVEN_DATABASE" ]]; then
DATABASES="$GIVEN_DATABASE"
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of database '$GIVEN_DATABASE'."
fi
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of database '$GIVEN_DATABASE'." > $log_file
found=true
fi
done
if ! $found ; then
continue
fi
else
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of databases at host '$(hostname -f)'."
fi
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of databases at host '$(hostname -f)'." > $log_file
fi
length_table_name=0
@ -350,25 +452,25 @@ for _val in ${mysql_credential_args_arr[@]} ; do
if [[ -d "/var/www/html/projekte/nd/htdocs503" ]] ; then
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension > $tmp_log_file 2>&1
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
error "Error while moving '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
else
_htdocs_nd_moved=true
fi
if $_htdocs_nd_moved ; then
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while linking '/var/www/html/projekte/nd/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
error "Error while linking '/var/www/html/projekte/nd/htdocs' --> 'htdocs503'.\n$(cat $log_file)"
else
_htdocs_nd_symlinked=true
fi
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
else
@ -384,23 +486,23 @@ for _val in ${mysql_credential_args_arr[@]} ; do
if [[ -d "/var/www/html/projekte/nd-archiv/htdocs503" ]] ; then
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension > $tmp_log_file 2>&1
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
error "Error while moving '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
else
_htdocs_nd_archiv_moved=true
fi
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while linking '/var/www/html/projekte/nd-archiv/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
error "Error while linking '/var/www/html/projekte/nd-archiv/htdocs' --> 'htdocs503'.\n$(cat $log_file)"
else
_htdocs_nd_archiv_symlinked=true
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
else
@ -412,13 +514,13 @@ for _val in ${mysql_credential_args_arr[@]} ; do
fi
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
TABLES=`${mysql_command} $mysql_credential_args $db -N -s -e "show tables"`
for table in $TABLES ; do
# - Ommit InnoDB tables
# -
_engine="$($mysql $mysql_credential_args -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
_engine="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
if [[ "${_engine,,}" = 'innodb' ]] ; then
if $VERBOSE ; then
echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB"
@ -443,7 +545,7 @@ for _val in ${mysql_credential_args_arr[@]} ; do
echo -e " [$(date)] Optimize table '$table'" >> $log_file
length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
@ -454,27 +556,27 @@ for _val in ${mysql_credential_args_arr[@]} ; do
echo "" >> $log_file
echo " [$(date)] Repair table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
_all_success=false
error "Repairing table '$table' failed.\n$(cat "$tmp_log_file")"
error "Repairing table '$table' failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
echo -e " [$(date)] Optimize table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")"
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
@ -492,43 +594,43 @@ for _val in ${mysql_credential_args_arr[@]} ; do
if [[ "$db" = "nd" ]]; then
if $_htdocs_nd_symlinked ; then
rm /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
rm /var/www/html/projekte/nd/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while removing symlink '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
error "Error while removing symlink '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
fi
fi
if $_htdocs_nd_moved ; then
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving back '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
error "Error while moving back '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
fi
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
elif [[ "$db" = "nd_archiv" ]]; then
if $_htdocs_nd_archiv_symlinked ; then
rm /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
rm /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
fi
fi
if $_htdocs_nd_archiv_moved ; then
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving back '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
error "Error while moving back '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
fi
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
fi
@ -545,14 +647,28 @@ for _val in ${mysql_credential_args_arr[@]} ; do
info_messages_arr+=("MySQL $mysql_version: The optimization of the MySQL tables of all databases were successful.")
fi
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL databases at host $(hostname -f)."
echo ""
if [[ -n "$GIVEN_DATABASE" ]] ; then
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL database '$GIVEN_DATABASE'."
echo ""
fi
echo "" >> $log_file
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL database '$GIVEN_DATABASE'." >> $log_file
echo "" >> $log_file
else
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL databases at host $(hostname -f)."
echo ""
fi
echo "" >> $log_file
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
echo "" >> $log_file
fi
echo "" >> $log_file
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
echo "" >> $log_file
(( index_arr++ ))
done
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then

View File

@ -5,16 +5,21 @@ working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
# - Lock directory exists, until the script ends. So
# - we can check, if a previos instanze is already running.
# -
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# -------------
# - Variable settings
# - Variable (default) settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
VERBOSE=false
DEFAULT_to_addresses="argus@oopen.de"
# -------------
# --- Some functions
@ -64,7 +69,14 @@ usage() {
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
if [[ ${1} -gt 0 ]] ; then
[[ -f "${log_file}" ]] && cp ${log_file} "/var/log/"
else
rm -f "/var/log/$(basename "${log_file}")"
fi
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
@ -159,6 +171,12 @@ trim() {
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
@ -182,29 +200,16 @@ detect_mysql_version () {
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Is this script running on terminal ?
# -
# -------------
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
# -------------
# - Read Commandline Parameters
@ -231,6 +236,39 @@ if [[ "$(trim $*)" =~ " -h" ]] || [[ "$(trim $*)" =~ " --help" ]] ; then
fi
# -------------
# - Job is already running?
# -------------
## - If job already runs, stop execution..
## -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum=`date +"%d.%m.%Y"`
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e "\tExiting now.."
echo ""
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $from_address $_to_address
done
exit 1
fi
# -------------
# - Load Settings from configuration file
# -------------
@ -241,17 +279,15 @@ else
GIVEN_DATABASE=""
fi
if $terminal ; then
echo ""
fi
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
source "$conf_file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fatal "$(cat $log_file)"
fi
else
echo_skipped
@ -261,7 +297,23 @@ else
fi
#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
[[ -z "${to_addresses}" ]] && to_addresses="${DEFAULT_to_addresses}"
# -------------
# - Some default values
# -------------
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
if [[ -z "$mysql_credential_args" ]]; then
@ -299,18 +351,35 @@ if [[ -z "$mysql_credential_args" ]]; then
fi
fi
[[ -z "$log_file" ]] && log_file="$DEFAULT_LOG_FILE"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
declare -i length_table_name
declare -i number_blank_signd
declare -i index_i
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_command="$mysql"
_all_success=true
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
@ -318,7 +387,21 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_credential_args="${_val_arr[1]}"
DATABASES="$($mysql $mysql_credential_args -N -s -e "show databases")"
DATABASES="$(${mysql_command} $mysql_credential_args -N -s -e "show databases" 2> $log_file)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
DATABASES="$(${mysql_command} $mysql_credential_args -N -s -e "show databases" 2> $log_file)"
if [[ $? -ne 0 ]] ; then
error "$(cat $log_file)"
continue
fi
fi
fi
fi
found=false
if [[ -n "$GIVEN_DATABASE" ]] ; then
@ -360,13 +443,13 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file
echo -e " [$(date)] Optimize tables in database '${db}'.." >> $log_file
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
TABLES=`${mysql_command} $mysql_credential_args $db -N -s -e "show tables"`
for table in $TABLES ; do
# - Ommit InnoDB tables
# -
_engine="$($mysql $mysql_credential_args -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
_engine="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
if [[ "${_engine,,}" = 'innodb' ]] ; then
if $VERBOSE ; then
echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB"
@ -392,7 +475,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
@ -403,27 +486,27 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file
echo " [$(date)] Repair table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
_all_success=false
error "Repairing table '$table' failed.\n$(cat "$tmp_log_file")"
error "Repairing table '$table' failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
echo -e " [$(date)] Optimize table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")"
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."

View File

@ -197,17 +197,23 @@ else
terminal=false
fi
mysql=`which mysql`
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "No binary 'mysql' found!"
fi
fi
# - Print help?
# -
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
@ -277,7 +283,15 @@ if [[ ${#mysql_credential_args_arr[@]} -gt 0 ]] ; then
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@ -335,13 +349,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}"
@ -377,6 +409,8 @@ else
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
mysql_credential_args="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
@ -386,7 +420,9 @@ else
parameter manually."
fi
else
if [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf"
@ -411,6 +447,8 @@ echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
if [[ -n "$DATABASE_NAME" ]]; then
echo " Database name................: $DATABASE_NAME"
else
@ -438,7 +476,7 @@ declare -i index_i
_all_success=true
DATABASES="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "show databases")"
DATABASES="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "show databases")"
if [[ -z "$DATABASE_NAME" ]] ; then
@ -480,7 +518,7 @@ for db in $DATABASES ; do
fi
fi
TABLES="$($mysql $MYSQL_CREDENTIAL_ARGS $db -N -s -e "show tables" 2> $tmp_log_file )"
TABLES="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS $db -N -s -e "show tables" 2> $tmp_log_file )"
if [[ $? -ne 0 ]]; then
_all_success=false
error "Getting tables of database '${db}' failed.\n $(cat "$tmp_log_file")"
@ -492,7 +530,7 @@ for db in $DATABASES ; do
# - Ommit InnoDB tables
# -
_engine="$($mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
_engine="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'")"
if [[ "${_engine,,}" = 'innodb' ]] ; then
echo -e " [$(date)] Ommit table '$table' - The storage engine (InnoDB) doesn't support repair"
continue
@ -513,7 +551,7 @@ for db in $DATABASES ; do
fi
length_table_name=${#table}
$mysql $MYSQL_CREDENTIAL_ARGS $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
@ -523,7 +561,7 @@ for db in $DATABASES ; do
else
$mysql $MYSQL_CREDENTIAL_ARGS $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")"

View File

@ -81,6 +81,7 @@ usage() {
-c "u root -p '<password>'"
-c "--login-path=local"
-c "--login-path=mysql-5.7"
-c "--defaults-file=/usr/local/mysql/sys-maint.cnf"
-c "-u root -S /run/mysqld/mysqld.sock"
@ -141,16 +142,16 @@ clean_up() {
fi
if [[ -n "$CUR_AUTOCOMMIT" ]] ; then
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" > /dev/null 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" > /dev/null 2>&1
fi
if [[ -n "$CUR_FOREIGN_KEY_CHECKS" ]] ; then
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_FOREIGN_KEY_CHECKS'" > /dev/null 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_FOREIGN_KEY_CHECKS'" > /dev/null 2>&1
fi
if [[ -n "$CUR_UNIQUE_CHECKS" ]] ; then
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_UNIQUE_CHECKS'" > /dev/null 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_UNIQUE_CHECKS'" > /dev/null 2>&1
fi
if [[ -n "$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" ]] ; then
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT'" > /dev/null 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT'" > /dev/null 2>&1
fi
# Perform program exit housekeeping
@ -444,6 +445,21 @@ else
NON_INTERACTIVE_MODE=true
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
# ----------
# Read Configurations from $conf_file
@ -510,46 +526,47 @@ if $BATCH_MODE ; then
fatal "No Database given \033[m(Option -d)\033[1m!"
fi
if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -z "$DATABASE_SERVER" ]]; then
# if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -z "$DATABASE_SERVER" ]]; then
#
# # Try to detect local MySQL Installation
# #
# detect_mysql_version
#
# MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
# MINOR_VERSION="$MYSQL_MINOR_VERSION"
# PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
#
# if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
# || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
# if [[ -S "/tmp/mysql.sock" ]]; then
# DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock"
# elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
# DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock"
# elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
# DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
# else
# fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
#
# Use configuration file "$conf_file" or commandline Parameter or set
# thr mysql credentials."
# fi
# else
# if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
# DATABASE_CREDENTIALS_ARGS="--login-path=local"
# elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
# DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
# DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf"
# else
# fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
#
# Use configuration file "$conf_file" to set
# parameter manually."
# fi
# fi
# fi
# Try to detect local MySQL Installation
#
detect_mysql_version
MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
MINOR_VERSION="$MYSQL_MINOR_VERSION"
PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
Use configuration file "$conf_file" or commandline Parameter or set
thr mysql credentials."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
DATABASE_CREDENTIALS_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf"
else
fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
Use configuration file "$conf_file" to set
parameter manually."
fi
fi
elif [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -n "$DATABASE_SERVER" ]]; then
if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -n "$DATABASE_SERVER" ]]; then
fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'"
fi
@ -559,7 +576,7 @@ else
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Database Server which should be created.."
echo "Insert Database Server for which we should restore a database.."
echo ""
echo ""
echo -e " \033[33mType <Return> to accept the default (localhost).\033[m"
@ -591,33 +608,6 @@ else
if $DATABASE_CREDENTIALS_NEEDED ; then
# Try to detect local MySQL Installation
#
detect_mysql_version
MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
MINOR_VERSION="$MYSQL_MINOR_VERSION"
PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
_DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
_DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
_DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
_DATABASE_CREDENTIALS_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
_DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
_DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf"
fi
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
@ -632,6 +622,7 @@ else
\033[33m-u root -p'<password>'\033[m
\033[33m--login-path=local\033[m
\033[33m--login-path=mysql-5.7\033[m
\033[33m--defaults-file=/usr/local/mysql/sys-maint.cnf\033[m
\033[33m-u root -S /run/mysqld/mysqld.sock\033[m"
echo ""
@ -689,6 +680,122 @@ else
fi # if $BATCH_MODE ; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
echononl "Check connection to Database Server.."
if [[ -n "$DATABASE_SERVER" ]] ; then
if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2> $log_file) ; then
if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
fatal "$(cat $log_file)"
fi
else
fatal "$(cat $log_file)"
fi
fi
else
if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2> $log_file) ; then
if [[ "$(cat $log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
fatal "$(cat $log_file)"
fi
else
fatal "$(cat $log_file)"
fi
fi
fi
if [[ -n "$DATABASE_CREDENTIALS_ARGS" ]] ; then
# - Get MySQL Version
# -
echo ""
echo -e "\033[32m--\033[m"
echo ""
echononl "Get MySQL Version"
_version="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SELECT VERSION()" 2> $log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $log_file)"
else
echo_ok
fi
echo ""
echo "_version: $_version"
echo ""
echo ""
echo ""
IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}"
declare -i MINOR_VERSION="${version_arr[1]}"
_path_level="${version_arr[2]}"
declare -i PATCH_LEVEL="${_path_level%%-*}"
## - Get current MySQL Distribution
## -
echononl " Get current MySQL distribution .."
if [[ -z "$_version" ]]; then
echo_failed
fatal "No installed MySQL server or distribution found!"
elif [[ "$_version" =~ MariaDB ]]; then
MYSQL_CUR_DISTRIBUTION="MariaDB"
else
MYSQL_CUR_DISTRIBUTION="MySQL"
fi
echo_ok
echo ""
echo -e "\033[32m--\033[m"
echo ""
else
# Try to detect local MySQL Installation
#
detect_mysql_version
MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
MINOR_VERSION="$MYSQL_MINOR_VERSION"
PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /run/mysqld/mysqld.sock"
elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then
DATABASE_CREDENTIALS_ARGS="-u root -S /var/run/mysqld/mysqld.sock"
else
fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
Use configuration file "$conf_file" or commandline Parameter or set
thr mysql credentials."
fi
else
if $(${mysql_command} --login-path=local -e ";" > /dev/null 2>&1) ; then
DATABASE_CREDENTIALS_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
DATABASE_CREDENTIALS_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
DATABASE_CREDENTIALS_ARGS="--defaults-file=/etc/mysql/debian.cnf"
else
fatal "Parameter 'DATABASE_CREDENTIALS_ARGS' cannot be determined automated.
Use configuration file "$conf_file" to set
parameter manually."
fi
fi
fi
if ! $QUIET_MODE ; then
echo ""
echo ""
@ -703,6 +810,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
fi
echo ""
echo " MySQL Command................: ${mysql_command}"
echo ""
if [[ -n "$DATABASE_SERVER" ]] ; then
echo " Database server..............: $DATABASE_SERVER"
else
@ -754,11 +863,11 @@ fi
echononl " Check connection to Database Server.."
if [[ -n "$DATABASE_SERVER" ]] ; then
if ! $(mysql -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then
if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then
fatal "Cannot connect to Database Server '$DATABASE_SERVER'!"
fi
else
if ! $(mysql $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then
if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS -e ";" > /dev/null 2>&1) ; then
fatal "Cannot connect to Database Server!"
fi
fi
@ -766,11 +875,11 @@ echo_done
echononl " Check connection to Database '$DATABASE_NAME'.."
if [[ -n "$DATABASE_SERVER" ]] ; then
if ! $(mysql -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then
if ! $(${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then
fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi
else
if ! $(mysql $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then
if ! $(${mysql_command} $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME -e ";" > /dev/null 2>&1) ; then
fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi
fi
@ -830,7 +939,7 @@ blank_line
# - GET current (global) Autocommit value
# -
echononl " GET current (global) value \033[1mautocommit\033[m"
CUR_AUTOCOMMIT="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2>&1
CUR_AUTOCOMMIT="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -841,7 +950,7 @@ fi
# - GET current (global) value for 'foreign_key_checks'
# -
echononl " GET current (global) value \033[1mforeign_key_checks\033[m"
CUR_FOREIGN_KEY_CHECKS="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'foreign_key_checks'" | awk '{print$2}')" >> $log_file 2>&1
CUR_FOREIGN_KEY_CHECKS="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'foreign_key_checks'" | awk '{print$2}')" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -852,7 +961,7 @@ fi
# - GET current (global) value for 'unique_checks'
# -
echononl " GET current (global) value \033[1munique_checks\033[m"
CUR_UNIQUE_CHECKS="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'unique_checks'" | awk '{print$2}')" >> $log_file 2>&1
CUR_UNIQUE_CHECKS="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'unique_checks'" | awk '{print$2}')" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -863,7 +972,18 @@ fi
# - GET current (global) value for 'innodb_flush_log_at_trx_commit'
# -
echononl " GET current (global) value \033[1minnodb_flush_log_at_trx_commit\033[m"
CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit'" | awk '{print$2}')" >> $log_file 2>&1
CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit'" | awk '{print$2}')" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
# - GET current (global) value for 'max_allowed_packet'
# -
echononl " GET current (global) value \033[1mmax_allowed_packet\033[m"
CUR_MAX_ALLOWED_PACKET="$(${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'" | awk '{print$2}')" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -874,7 +994,7 @@ fi
blank_line
echononl " Set Autocommit to OFF"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='OFF'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='OFF'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -883,7 +1003,7 @@ else
fi
echononl " Set foreign_key_checks to OFF"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='OFF'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='OFF'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -892,7 +1012,7 @@ else
fi
echononl " Set unique_checks to OFF"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='OFF'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='OFF'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -901,7 +1021,16 @@ else
fi
echononl " Set innodb_flush_log_at_trx_commit to 2"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=2" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=2" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
echononl " Set max_allowed_packet to 1G"
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
else
@ -922,9 +1051,9 @@ fi
echononl " Restore Database '${DATABASE_NAME}'.."
b_timestamp=$(date +"%s")
if [[ -n "$DATABASE_SERVER" ]] ; then
mysql -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1
${mysql_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1
else
mysql $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME < $DATABASE_DUMP_FILE > $log_file 2>&1
fi
retval=$?
e_timestamp=$(date +"%s")
@ -952,7 +1081,7 @@ if ! $QUIET_MODE ; then
fi
echononl " Set Autocommit to $CUR_AUTOCOMMIT"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL autocommit='$CUR_AUTOCOMMIT'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
CUR_AUTOCOMMIT=""
@ -962,7 +1091,7 @@ else
fi
echononl " Set foreign_key_checks to $CUR_FOREIGN_KEY_CHECKS"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='$CUR_FOREIGN_KEY_CHECKS'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL foreign_key_checks='$CUR_FOREIGN_KEY_CHECKS'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
CUR_FOREIGN_KEY_CHECKS=""
@ -972,7 +1101,7 @@ else
fi
echononl " Set unique_checks to $CUR_UNIQUE_CHECKS"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='$CUR_UNIQUE_CHECKS'" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL unique_checks='$CUR_UNIQUE_CHECKS'" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
CUR_UNIQUE_CHECKS=""
@ -982,7 +1111,7 @@ else
fi
echononl " Set innodb_flush_log_at_trx_commit to $CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" >> $log_file 2>&1
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT=""
@ -991,6 +1120,16 @@ else
error "$(cat $log_file)"
fi
echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET"
${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=$CUR_MAX_ALLOWED_PACKET" >> $log_file 2>&1
if [[ $? -eq 0 ]];then
echo_ok
CUR_MAX_ALLOWED_PACKET=""
else
echo_failed
error "$(cat $log_file)"
fi
blank_line
# - Start Apache Webserver