Compare commits
19 Commits
11b1077ae0
...
master
Author | SHA1 | Date | |
---|---|---|---|
7b97a5d6ff | |||
014cf46b74 | |||
e5ba1decfb | |||
b890525173 | |||
2ae83440cb | |||
35f224d9f2 | |||
845e9cf8ad | |||
62c2f65dbd | |||
8efe89e79a | |||
be0404d9d6 | |||
d0d7c01ba3 | |||
a585a90b56 | |||
5bdb646de7 | |||
30ca2c1a72 | |||
8876617d4c | |||
cd815ee66f | |||
270def192f | |||
a7f3c27011 | |||
e0eb52d8d3 |
105
README.create-user
Normal file
105
README.create-user
Normal 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";
|
||||
|
||||
|
@ -84,11 +84,11 @@ fatal(){
|
||||
if $terminal ; then
|
||||
if [[ -n "$*" ]] ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
else
|
||||
echo " \033[31m\033[1mFatal error\033[m:"
|
||||
fi
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
|
||||
echo -e " \033[31m\033[1mScript will be interrupted.\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 ]: $*"
|
||||
@ -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"
|
||||
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
|
||||
|
||||
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,13 +609,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)"
|
||||
_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]}"
|
||||
@ -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)"
|
||||
|
@ -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
|
||||
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)"
|
||||
|
@ -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,13 +453,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)"
|
||||
_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]}"
|
||||
@ -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)"
|
||||
|
@ -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,13 +381,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)"
|
||||
_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]}"
|
||||
@ -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
1173
dump_database.sh
Executable file
File diff suppressed because it is too large
Load Diff
@ -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
|
||||
|
@ -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
|
||||
|
@ -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,13 +464,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)"
|
||||
_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]}"
|
||||
@ -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)"
|
||||
|
@ -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,13 +538,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)"
|
||||
_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]}"
|
||||
@ -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)"
|
||||
|
@ -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,13 +459,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)"
|
||||
_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]}"
|
||||
@ -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)"
|
||||
|
@ -32,13 +32,17 @@ fi
|
||||
declare -i key_buffer_size
|
||||
declare -i query_cache_size
|
||||
declare -i tmp_table_size
|
||||
declare -i max_heap_table_size
|
||||
declare -i max_tmp_table_size
|
||||
declare -i innodb_buffer_pool_size
|
||||
declare -i innodb_additional_mem_pool_size
|
||||
declare -i innodb_log_buffer_size
|
||||
declare -i aria_pagecache_buffer_size
|
||||
declare -i max_connections
|
||||
declare -i sort_buffer_size
|
||||
declare -i read_buffer_size
|
||||
declare -i read_rnd_buffer_size
|
||||
declare -i max_allowed_packet
|
||||
declare -i join_buffer_size
|
||||
declare -i thread_stack
|
||||
declare -i binlog_cache_size
|
||||
@ -49,12 +53,16 @@ query_cache_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'query_cache_size'" | awk '{print$2}'`
|
||||
tmp_table_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'tmp_table_size'" | awk '{print$2}'`
|
||||
max_heap_table_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'max_heap_table_size'" | awk '{print$2}'`
|
||||
innodb_buffer_pool_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'innodb_buffer_pool_size'" | awk '{print$2}'`
|
||||
innodb_additional_mem_pool_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'innodb_additional_mem_pool_size'" | awk '{print$2}'`
|
||||
innodb_log_buffer_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'innodb_log_buffer_size'" | awk '{print$2}'`
|
||||
aria_pagecache_buffer_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'aria_pagecache_buffer_size'" | awk '{print$2}'`
|
||||
max_connections=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'max_connections'" | awk '{print$2}'`
|
||||
sort_buffer_size=`mysql $mysql_credential_args -N -s -e \
|
||||
@ -65,6 +73,8 @@ read_rnd_buffer_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'read_rnd_buffer_size'" | awk '{print$2}'`
|
||||
join_buffer_size=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'join_buffer_size'" | awk '{print$2}'`
|
||||
max_allowed_packet=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'max_allowed_packet'" | awk '{print$2}'`
|
||||
thread_stack=`mysql $mysql_credential_args -N -s -e \
|
||||
"SHOW VARIABLES LIKE 'thread_stack'" | awk '{print$2}'`
|
||||
binlog_cache_size=`mysql $mysql_credential_args -N -s -e \
|
||||
@ -126,6 +136,13 @@ elif [ $tmp_table_size -gt 1024 ]; then
|
||||
_tmp_table_size=" (`expr $tmp_table_size / 1024`K)"
|
||||
fi
|
||||
|
||||
_max_heap_table_size=""
|
||||
if [ $max_heap_table_size -gt 1048576 ]; then
|
||||
_max_heap_table_size=" (`expr $max_heap_table_size / 1024 / 1024`M)"
|
||||
elif [ $max_heap_table_size -gt 1024 ]; then
|
||||
_max_heap_table_size=" (`expr $max_heap_table_size / 1024`K)"
|
||||
fi
|
||||
|
||||
_innodb_buffer_pool_size=""
|
||||
if [ $innodb_buffer_pool_size -gt 1048576 ]; then
|
||||
_innodb_buffer_pool_size=" (`expr $innodb_buffer_pool_size / 1024 / 1024`M)"
|
||||
@ -147,6 +164,13 @@ elif [ $innodb_log_buffer_size -gt 1024 ]; then
|
||||
_innodb_log_buffer_size=" (`expr $innodb_log_buffer_size / 1024`K)"
|
||||
fi
|
||||
|
||||
_aria_pagecache_buffer_size=""
|
||||
if [ $aria_pagecache_buffer_size -gt 1048576 ]; then
|
||||
_aria_pagecache_buffer_size=" (`expr $aria_pagecache_buffer_size / 1024 / 1024`M)"
|
||||
elif [ $aria_pagecache_buffer_size -gt 1024 ]; then
|
||||
_aria_pagecache_buffer_size=" (`expr $aria_pagecache_buffer_size / 1024`K)"
|
||||
fi
|
||||
|
||||
_sort_buffer_size=""
|
||||
if [ $sort_buffer_size -gt 1048576 ]; then
|
||||
_sort_buffer_size=" (`expr $sort_buffer_size / 1024 / 1024`M)"
|
||||
@ -175,6 +199,13 @@ elif [ $join_buffer_size -gt 1024 ]; then
|
||||
_join_buffer_size=" (`expr $join_buffer_size / 1024`K)"
|
||||
fi
|
||||
|
||||
_max_allowed_packet=""
|
||||
if [ $max_allowed_packet -gt 1048576 ]; then
|
||||
_max_allowed_packet=" (`expr $max_allowed_packet / 1024 / 1024`M)"
|
||||
elif [ $max_allowed_packet -gt 1024 ]; then
|
||||
_max_allowed_packet=" (`expr $max_allowed_packet / 1024`K)"
|
||||
fi
|
||||
|
||||
_thread_stack=""
|
||||
if [ $thread_stack -gt 1048576 ]; then
|
||||
_thread_stack=" (`expr $thread_stack / 1024 / 1024`M)"
|
||||
@ -189,14 +220,30 @@ elif [ $binlog_cache_size -gt 1024 ]; then
|
||||
_binlog_cache_size=" (`expr $binlog_cache_size / 1024`K)"
|
||||
fi
|
||||
|
||||
if [ $max_heap_table_size -gt $tmp_table_size ] ; then
|
||||
max_tmp_table_size=$max_heap_table_size
|
||||
else
|
||||
max_tmp_table_size=$tmp_table_size
|
||||
fi
|
||||
|
||||
_max_tmp_table_size=""
|
||||
if [ $max_tmp_table_size -gt 1048576 ]; then
|
||||
_max_tmp_table_size=" (`expr $max_tmp_table_size / 1024 / 1024`M)"
|
||||
elif [ $max_tmp_table_size -gt 1024 ]; then
|
||||
_max_tmp_table_size=" (`expr $maxtmpp_table_size / 1024`K)"
|
||||
fi
|
||||
|
||||
echo -e "\tGlobal Buffers"
|
||||
echo -e "\t--------------"
|
||||
echo -e "\tkey_buffer_size...................: $key_buffer_size $_key_buffer_size"
|
||||
echo -e "\tquery_cache_size..................: $query_cache_size $_query_cache_size"
|
||||
echo -e "\ttmp_table_size....................: $tmp_table_size $_tmp_table_size"
|
||||
echo -e "\tmax_tmp_table_size................: $max_tmp_table_size $_max_tmp_table_size"
|
||||
echo -e "\t tmp_table_size.................: $tmp_table_size $_tmp_table_size"
|
||||
echo -e "\t max_heap_table_size............: $max_heap_table_size $_max_heap_table_size"
|
||||
echo -e "\tinnodb_buffer_pool_size...........: $innodb_buffer_pool_size $_innodb_buffer_pool_size"
|
||||
echo -e "\tinnodb_additional_mem_pool_size...: $innodb_additional_mem_pool_size $_innodb_additional_mem_pool_size"
|
||||
echo -e "\tinnodb_log_buffer_size............: $innodb_log_buffer_size $_innodb_log_buffer_size"
|
||||
echo -e "\taria_pagecache_buffer_size........: $aria_pagecache_buffer_size $_aria_pagecache_buffer_size"
|
||||
echo ""
|
||||
echo -e "\tmax_connections...................: $max_connections"
|
||||
echo ""
|
||||
@ -206,20 +253,23 @@ echo -e "\tsort_buffer_size..................: $sort_buffer_size $_sort_buffer_s
|
||||
echo -e "\tread_buffer_size..................: $read_buffer_size $_read_buffer_size"
|
||||
echo -e "\tread_rnd_buffer_size..............: $read_rnd_buffer_size $_read_rnd_buffer_size"
|
||||
echo -e "\tjoin_buffer_size..................: $join_buffer_size $_join_buffer_size"
|
||||
echo -e "\tmax_allowed_packet................: $max_allowed_packet $_max_allowed_packet"
|
||||
echo -e "\tthread_stack......................: $thread_stack $_thread_stack"
|
||||
echo -e "\tbinlog_cache_size.................: $binlog_cache_size $_binlog_cache_size"
|
||||
|
||||
declare -i max_memory_usage
|
||||
max_memory_usage=`expr $key_buffer_size \
|
||||
+ $query_cache_size \
|
||||
+ $tmp_table_size \
|
||||
+ $max_tmp_table_size \
|
||||
+ $innodb_buffer_pool_size \
|
||||
+ $innodb_additional_mem_pool_size \
|
||||
+ $innodb_log_buffer_size \
|
||||
+ $aria_pagecache_buffer_size \
|
||||
+ $max_connections \* \( $sort_buffer_size \
|
||||
+ $read_buffer_size \
|
||||
+ $read_rnd_buffer_size \
|
||||
+ $join_buffer_size \
|
||||
+ $max_allowed_packet \
|
||||
+ $thread_stack \
|
||||
+ $binlog_cache_size \)`
|
||||
|
||||
@ -230,14 +280,16 @@ max_memory_usage_mb=`expr $max_memory_usage / 1024 / 1024`
|
||||
declare -i max_memory_allocated
|
||||
max_memory_allocated=`expr $key_buffer_size \
|
||||
+ $query_cache_size \
|
||||
+ $tmp_table_size \
|
||||
+ $max_tmp_table_size \
|
||||
+ $innodb_buffer_pool_size \
|
||||
+ $innodb_additional_mem_pool_size \
|
||||
+ $innodb_log_buffer_size \
|
||||
+ $aria_pagecache_buffer_size \
|
||||
+ $max_used_connections \* \( $sort_buffer_size \
|
||||
+ $read_buffer_size \
|
||||
+ $read_rnd_buffer_size \
|
||||
+ $join_buffer_size \
|
||||
+ $max_allowed_packet \
|
||||
+ $thread_stack \
|
||||
+ $binlog_cache_size \)`
|
||||
|
||||
|
7264
mysqltuner.pl
Executable file
7264
mysqltuner.pl
Executable file
File diff suppressed because it is too large
Load Diff
@ -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]}"
|
||||
|
||||
|
||||
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
|
||||
|
||||
if [[ -z "$DATABASES" ]] ; then
|
||||
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
|
||||
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,6 +647,16 @@ 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 [[ -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)."
|
||||
@ -553,6 +665,10 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
||||
echo "" >> $log_file
|
||||
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
|
||||
echo "" >> $log_file
|
||||
fi
|
||||
|
||||
(( index_arr++ ))
|
||||
|
||||
done
|
||||
|
||||
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then
|
||||
|
@ -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."
|
||||
|
@ -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,10 +349,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
|
||||
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
|
||||
@ -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")"
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user