Fix errors in case of multiple mysql installations.

This commit is contained in:
Christoph 2024-01-17 01:40:49 +01:00
parent 62c2f65dbd
commit 845e9cf8ad
14 changed files with 976 additions and 345 deletions

View File

@ -276,6 +276,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi 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 [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file="" read_file=""
@ -377,7 +393,14 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
@ -578,13 +601,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -631,7 +672,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -658,6 +699,9 @@ if ! $QUIET_MODE ; then
echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION" echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo "" echo ""
echo " Database name................: $DATABASE_NAME" echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER" echo " Database user................: $DATABASE_USER"
@ -703,19 +747,19 @@ fi
# - Test if Database already exists # - 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 if [[ "$_result" = "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' already exists" fatal "Database '$DATABASE_NAME' already exists"
fi fi
echononl " Create database \033[1m$DATABASE_NAME\033[m (full UTF-8 support - ${DATABASE_CHARACTER_SET})" 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 "CREATE DATABASE $DATABASE_NAME CHARACTER SET ${DATABASE_CHARACTER_SET} COLLATE ${DATABASE_COLLATION}" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"
echononl " Create database \033[1m$DATABASE_NAME\033[m (UTF-8 support - utf8)" 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 "CREATE DATABASE $DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -744,7 +788,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
fi fi
echononl " Create database user '$DATABASE_USER' access from locahost" 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -759,7 +803,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Create database user '$DATABASE_USER' access from '$_ip' " 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -774,7 +818,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'" 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 "GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -788,7 +832,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'" 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 "GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -805,7 +849,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
echononl " Check if user '$DATABASE_USER' already exists for localhost .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = 'localhost'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -814,7 +858,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER'" 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -829,7 +873,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'" 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'" \ "GRANT USAGE ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -840,7 +884,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant all privileges to a user '$DATABASE_USER' on database '$DATABASE_NAME'" 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'" \ "GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -855,7 +899,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -864,7 +908,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'" 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -879,7 +923,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'" 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 "GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -889,7 +933,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'" 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'" \ "GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -906,7 +950,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -916,7 +960,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME'" 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 "GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -930,7 +974,7 @@ else
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -940,7 +984,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME' from $_ip" 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 "GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -957,7 +1001,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -273,6 +273,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi 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 [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file="" read_file=""
@ -374,7 +390,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -468,8 +492,26 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echononl " Get MySQL Version" echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)" _version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then 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 echo_failed
fatal "$(cat $tmp_log_file)" 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 else
echo_ok echo_ok
fi fi
@ -519,7 +561,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -546,6 +588,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Drop Database................: $DATABASE_NAME" echo " Drop Database................: $DATABASE_NAME"
if $DO_NOT_DELTE_USER ; then if $DO_NOT_DELTE_USER ; then
echo -e " Drop Database user...........: \033[33mNone\033[m" echo -e " Drop Database user...........: \033[33mNone\033[m"
@ -577,7 +621,7 @@ if ! $QUIET_MODE ; then
fi fi
echononl " Drop database '$DATABASE_NAME'.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
@ -586,7 +630,7 @@ else
fi fi
echononl " Delete all entries for database '$DATABASE_NAME' from table mysql.db .." 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 "DELETE FROM db WHERE Db = '$DATABASE_NAME'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -599,7 +643,7 @@ fi
# - Test if given user is associated with another database # - Test if given user is associated with another database
# - # -
echononl " Check if db user '$DATABASE_USER' is also 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)" "SELECT count(User) FROM db WHERE User = '$DATABASE_USER'" 2> $tmp_log_file)"
if [[ -z "$_count_user" ]]; then if [[ -z "$_count_user" ]]; then
echo_failed echo_failed
@ -610,7 +654,7 @@ else
warn "Db user '$DATABASE_USER' is already in use.\n So user will not be deleted from table 'mysql.user'." warn "Db user '$DATABASE_USER' is already in use.\n So user will not be deleted from table 'mysql.user'."
else else
echononl " Delete username '$DATABASE_USER' from table mysql.user" 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 "DELETE FROM user WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -623,7 +667,7 @@ fi
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -257,6 +257,23 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi 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 [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file="" read_file=""
@ -358,7 +375,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -428,13 +453,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -481,7 +524,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -508,6 +551,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo -e " Drop Database user...........: \033[33m$DATABASE_USER\033[m" echo -e " Drop Database user...........: \033[33m$DATABASE_USER\033[m"
echo "" echo ""
echo "" echo ""
@ -536,7 +581,7 @@ fi
# - Check if User already exists # - Check if User already exists
# - # -
echononl " Check if user '$DATABASE_USER' 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 user_exists=true
else else
user_exists=false user_exists=false
@ -548,7 +593,7 @@ echo_ok
# - Test given user is associated with a database # - Test given user is associated with a database
# - # -
echononl " Check if db user '$DATABASE_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)" "SELECT count(User) FROM db WHERE User = '$DATABASE_USER'" 2> $tmp_log_file)"
if [[ -z "$_count_user" ]]; then if [[ -z "$_count_user" ]]; then
echo_failed echo_failed
@ -565,7 +610,7 @@ fi
echononl " Delete username '$DATABASE_USER' from table mysql.db" echononl " Delete username '$DATABASE_USER' from table mysql.db"
if [[ $_count_user -gt 0 ]]; then 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 "DELETE FROM db WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -579,7 +624,7 @@ fi
echononl " Delete username '$DATABASE_USER' from table mysql.user" echononl " Delete username '$DATABASE_USER' from table mysql.user"
if $user_exists ; then 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 "DELETE FROM user WHERE User = '$DATABASE_USER'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -594,7 +639,7 @@ else
fi fi
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -245,6 +245,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi 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 # - Load Settings from configuration file mysql_credetials.conf
@ -297,7 +313,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -357,13 +381,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -410,7 +452,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -436,6 +478,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION" echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo "" echo ""
echo " Database name................: $DATABASE_NAME" echo " Database name................: $DATABASE_NAME"
@ -455,7 +499,7 @@ if ! $NON_INTERACTIVE_MODE ; then
fi 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 if [[ -z "$_result" ]]; then
fatal "No database '$DATABASE_NAME' found!" fatal "No database '$DATABASE_NAME' found!"
fi fi
@ -469,7 +513,7 @@ if ! $QUIET_MODE ; then
fi fi
echononl " Get table list from database '$DATABASE_NAME' .." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
@ -486,7 +530,7 @@ echononl " Drop tables form database '$DATABASE_NAME' .."
_failed=false _failed=false
> $tmp_log_file > $tmp_log_file
for _table in $_TABLES ; do 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 if [[ $? -ne 0 ]] ; then
_failed=true _failed=true
_tables_not_yet_deleted+=("$_table") _tables_not_yet_deleted+=("$_table")
@ -509,7 +553,7 @@ if $_failed ; then
_failed_again=false _failed_again=false
: > $tmp_log_file : > $tmp_log_file
for _table in "${_tables_not_yet_deleted[@]}" ; do 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 if [[ $? -ne 0 ]] ; then
_failed_again=true _failed_again=true
_tables_not_deleted+=("$_table") _tables_not_deleted+=("$_table")

View File

@ -82,6 +82,7 @@ usage() {
-c "u root -p '<password>'" -c "u root -p '<password>'"
-c "--login-path=local" -c "--login-path=local"
-c "--login-path=mysql-5.7"
-c "--defaults-file=/usr/local/mysql/sys-maint.cnf" -c "--defaults-file=/usr/local/mysql/sys-maint.cnf"
-c "-u root -S /run/mysqld/mysqld.sock" -c "-u root -S /run/mysqld/mysqld.sock"
@ -142,16 +143,16 @@ clean_up() {
fi fi
if [[ -n "$CUR_AUTOCOMMIT" ]] ; then 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 fi
if [[ -n "$CUR_FOREIGN_KEY_CHECKS" ]] ; then 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 fi
if [[ -n "$CUR_UNIQUE_CHECKS" ]] ; then 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 fi
if [[ -n "$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" ]] ; then 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 fi
# Perform program exit housekeeping # Perform program exit housekeeping
@ -445,6 +446,37 @@ else
NON_INTERACTIVE_MODE=true NON_INTERACTIVE_MODE=true
fi 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
echononl " Get DUMP command.."
mysqldump_command="$(which mysqldump)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysqldump" ]]; then
mysqldump_command="/usr/local/mysql/bin/mysqldump"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
# ---------- # ----------
# Read Configurations from $conf_file # Read Configurations from $conf_file
@ -515,46 +547,47 @@ if $BATCH_MODE ; then
fatal "No Database given \033[m(Option -d)\033[1m!" fatal "No Database given \033[m(Option -d)\033[1m!"
fi 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 if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -n "$DATABASE_SERVER" ]]; 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
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
fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'" fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'"
fi fi
@ -564,7 +597,7 @@ else
echo "" echo ""
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echo "Insert Database Server which should be created.." echo "Insert Database Server from which should dump should be created.."
echo "" echo ""
echo "" echo ""
echo -e " \033[33mType <Return> to accept the default (localhost).\033[m" echo -e " \033[33mType <Return> to accept the default (localhost).\033[m"
@ -597,33 +630,6 @@ else
if $DATABASE_CREDENTIALS_NEEDED ; then 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 ""
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
@ -638,6 +644,7 @@ else
\033[33m-u root -p'<password>'\033[m \033[33m-u root -p'<password>'\033[m
\033[33m--login-path=local\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--defaults-file=/usr/local/mysql/sys-maint.cnf\033[m
\033[33m-u root -S /run/mysqld/mysqld.sock\033[m" \033[33m-u root -S /run/mysqld/mysqld.sock\033[m"
echo "" echo ""
@ -694,6 +701,124 @@ else
fi # if $BATCH_MODE ; then 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"
mysqldump_command="/usr/local/mysql/bin/mysqldump"
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"
mysqldump_command="/usr/local/mysql/bin/mysqldump"
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 if ! $QUIET_MODE ; then
echo "" echo ""
echo "" echo ""
@ -708,6 +833,9 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
fi fi
echo "" echo ""
echo " MySQL Command................: ${mysql_command}"
echo " DUMP Commande................: ${mysqldump_command}"
echo ""
if [[ -n "$DATABASE_SERVER" ]] ; then if [[ -n "$DATABASE_SERVER" ]] ; then
echo " Database server..............: $DATABASE_SERVER" echo " Database server..............: $DATABASE_SERVER"
else else
@ -756,11 +884,11 @@ fi
echononl " Check connection to Database Server.." echononl " Check connection to Database Server.."
if [[ -n "$DATABASE_SERVER" ]] ; then 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'!" fatal "Cannot connect to Database Server '$DATABASE_SERVER'!"
fi fi
else 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!" fatal "Cannot connect to Database Server!"
fi fi
fi fi
@ -768,11 +896,11 @@ echo_done
echononl " Check connection to Database '$DATABASE_NAME'.." echononl " Check connection to Database '$DATABASE_NAME'.."
if [[ -n "$DATABASE_SERVER" ]] ; then 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'!" fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi fi
else 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'!" fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi fi
fi fi
@ -832,7 +960,7 @@ blank_line
# - GET current (global) Autocommit value # - GET current (global) Autocommit value
# - # -
echononl " GET current (global) value \033[1mautocommit\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -843,7 +971,7 @@ fi
# - GET current (global) value for 'foreign_key_checks' # - GET current (global) value for 'foreign_key_checks'
# - # -
echononl " GET current (global) value \033[1mforeign_key_checks\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -854,7 +982,7 @@ fi
# - GET current (global) value for 'unique_checks' # - GET current (global) value for 'unique_checks'
# - # -
echononl " GET current (global) value \033[1munique_checks\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -865,7 +993,7 @@ fi
# - GET current (global) value for 'innodb_flush_log_at_trx_commit' # - 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" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -876,7 +1004,7 @@ fi
# - GET current (global) value for 'max_allowed_packet' # - GET current (global) value for 'max_allowed_packet'
# - # -
echononl " GET current (global) value \033[1mmax_allowed_packet\033[m" echononl " GET current (global) value \033[1mmax_allowed_packet\033[m"
CUR_MAX_ALLOWED_PACKET="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'" | awk '{print$2}')" >> $log_file 2>&1 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -887,7 +1015,7 @@ fi
blank_line blank_line
echononl " Set Autocommit to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -896,7 +1024,7 @@ else
fi fi
echononl " Set foreign_key_checks to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -905,7 +1033,7 @@ else
fi fi
echononl " Set unique_checks to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -914,7 +1042,7 @@ else
fi fi
echononl " Set innodb_flush_log_at_trx_commit to 2" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -923,7 +1051,7 @@ else
fi fi
echononl " Set max_allowed_packet to 1G" echononl " Set max_allowed_packet to 1G"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1 ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1
if [[ $? -eq 0 ]];then if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -944,9 +1072,9 @@ fi
echononl " Dump Database '${DATABASE_NAME}'.." echononl " Dump Database '${DATABASE_NAME}'.."
b_timestamp=$(date +"%s") b_timestamp=$(date +"%s")
if [[ -n "$DATABASE_SERVER" ]] ; then if [[ -n "$DATABASE_SERVER" ]] ; then
mysqldump -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME > ${DATABASE_DUMP_FOLDER}/${DATABASE_DUMP_FILEi} 2> $log_file ${mysqldump_command} -h $DATABASE_SERVER $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME > ${DATABASE_DUMP_FOLDER}/${DATABASE_DUMP_FILEi} 2> $log_file
else else
mysqldump $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME > ${DATABASE_DUMP_FOLDER}/${DATABASE_DUMP_FILE} 2> $log_file ${mysqldump_command} $DATABASE_CREDENTIALS_ARGS $DATABASE_NAME > ${DATABASE_DUMP_FOLDER}/${DATABASE_DUMP_FILE} 2> $log_file
fi fi
retval=$? retval=$?
e_timestamp=$(date +"%s") e_timestamp=$(date +"%s")
@ -974,7 +1102,7 @@ if ! $QUIET_MODE ; then
fi fi
echononl " Set Autocommit to $CUR_AUTOCOMMIT" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_AUTOCOMMIT="" CUR_AUTOCOMMIT=""
@ -984,7 +1112,7 @@ else
fi fi
echononl " Set foreign_key_checks to $CUR_FOREIGN_KEY_CHECKS" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_FOREIGN_KEY_CHECKS="" CUR_FOREIGN_KEY_CHECKS=""
@ -994,7 +1122,7 @@ else
fi fi
echononl " Set unique_checks to $CUR_UNIQUE_CHECKS" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_UNIQUE_CHECKS="" CUR_UNIQUE_CHECKS=""
@ -1004,7 +1132,7 @@ else
fi fi
echononl " Set innodb_flush_log_at_trx_commit to $CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="" CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT=""
@ -1014,7 +1142,7 @@ else
fi fi
echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET" echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=$CUR_MAX_ALLOWED_PACKET" >> $log_file 2>&1 ${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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_MAX_ALLOWED_PACKET="" CUR_MAX_ALLOWED_PACKET=""

View File

@ -143,7 +143,7 @@ detect_mysql_version () {
mysqladmin=`realpath $(which mysqladmin) 2>/dev/null` mysqladmin=`realpath $(which mysqladmin) 2>/dev/null`
if [ -z "$mysqladmin" ]; then if [ -z "$mysqladmin" ]; then
if [ -x "/usr/local/mysql/bin/mysqladmin" ]; then if [ -x "/usr/local/mysql/bin/mysqladmin" ]; then
mysql=/usr/local/mysql/bin/mysqladmin mysqladmin=/usr/local/mysql/bin/mysqladmin
else else
echo echo
echo -e "\t[ Error ]: \"mysqladmin\" not found !!!" echo -e "\t[ Error ]: \"mysqladmin\" not found !!!"
@ -237,12 +237,21 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_credential_args="${_val_arr[1]}" mysql_credential_args="${_val_arr[1]}"
echononl " [ ${mysql_version} ]: Flush host cache.." echononl " [ ${mysql_version} ]: Flush host cache.."
$mysqladmin $mysql_credential_args flush-hosts $mysqladmin $mysql_credential_args flush-hosts 2> $tmp_log_file
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo_ok echo_ok
else 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 echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"
else
echo_ok
fi
fi
fi
fi fi
(( index_arr++ )) (( index_arr++ ))

View File

@ -242,8 +242,17 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo_ok echo_ok
else 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 echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"
else
echo_ok
fi
fi
fi
fi fi
(( index_arr++ )) (( index_arr++ ))

View File

@ -267,6 +267,21 @@ fi
# - # -
clear 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 # - Load Settings from configuration file mysql_credetials.conf
@ -319,7 +334,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -441,13 +464,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -494,7 +535,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -523,6 +564,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database user................: $DATABASE_USER" echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD" echo " Database password............: $DATABASE_PASSWD"
echo "" echo ""
@ -564,7 +607,7 @@ fi
# - Check if User already exists # - Check if User already exists
# - # -
echononl " Check if user '$DATABASE_USER' already exists for localhost .." 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 user_exists=true
else else
user_exists=false user_exists=false
@ -575,7 +618,7 @@ echo_ok
echononl " Create database user '$DATABASE_USER' access from locahost" echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -588,8 +631,8 @@ else
echo_skipped echo_skipped
fi fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'" echononl " Grant permissions to access and use the ${mysql_command} server to user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \ ${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \ "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -600,7 +643,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on all database " 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'" \ "GRANT SELECT, SHOW VIEW, EVENT, LOCK TABLES, RELOAD, REPLICATION CLIENT ON *.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -615,7 +658,7 @@ if $ACCESS_FROM_OUTSIDE ; then
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -624,7 +667,7 @@ if $ACCESS_FROM_OUTSIDE ; then
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'" 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -639,7 +682,7 @@ if $ACCESS_FROM_OUTSIDE ; then
fi fi
echononl " Allow access to user '$DATABASE_USER' on all databases from '$_ip'" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -649,7 +692,7 @@ if $ACCESS_FROM_OUTSIDE ; then
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from '$_ip'" 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'" \ "GRANT SELECT, SHOW VIEW, EVENT, LOCK TABLES, RELOAD, REPLICATION CLIENT ON *.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -666,7 +709,7 @@ fi
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -274,6 +274,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi 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 [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file="" read_file=""
@ -375,7 +391,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -514,13 +538,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -567,7 +609,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -596,6 +638,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database name................: $DATABASE_NAME" echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER" echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD" echo " Database password............: $DATABASE_PASSWD"
@ -637,7 +681,7 @@ fi
# - Check if Database already exists # - 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 if [[ "$_result" != "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' does not exists" fatal "Database '$DATABASE_NAME' does not exists"
fi fi
@ -645,7 +689,7 @@ fi
# - Check if User already exists # - Check if User already exists
# - # -
echononl " Check if user '$DATABASE_USER' already exists for localhost .." 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 user_exists=true
else else
user_exists=false user_exists=false
@ -670,7 +714,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Create database user '$DATABASE_USER' access from locahost" echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -688,7 +732,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -697,7 +741,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' access from '$_ip' " 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -715,7 +759,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'" 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 "GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -729,7 +773,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'" 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 "GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -748,7 +792,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echononl " Create database user '$DATABASE_USER' access from locahost" echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -762,7 +806,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'" 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'" \ "GRANT USAGE ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -773,7 +817,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant all privileges to a user '$DATABASE_USER' on database '$DATABASE_NAME'" 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'" \ "GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -788,7 +832,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -797,7 +841,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'" 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -812,7 +856,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'" 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 "GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -822,7 +866,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'" 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'" \ "GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -839,7 +883,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -849,7 +893,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME'" 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 "GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -863,7 +907,7 @@ else
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -873,7 +917,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME' from $_ip" 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 "GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -890,7 +934,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -289,6 +289,22 @@ if [[ -n "$mysql_credential_args" ]]; then
fi 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 if ! $NON_INTERACTIVE_MODE ; then
declare -i index_arr=0 declare -i index_arr=0
@ -313,7 +329,15 @@ if ! $NON_INTERACTIVE_MODE ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -435,13 +459,31 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else else
echo_ok echo_ok
fi fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
IFS='.' read -r -a version_arr <<< "$_version" IFS='.' read -r -a version_arr <<< "$_version"
declare -i MAJOR_VERSION="${version_arr[0]}" declare -i MAJOR_VERSION="${version_arr[0]}"
@ -488,7 +530,7 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--login-path=local"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
@ -517,6 +559,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database user................: $DATABASE_USER" echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD" echo " Database password............: $DATABASE_PASSWD"
echo "" echo ""
@ -558,7 +602,7 @@ fi
# - Check if User already exists # - Check if User already exists
# - # -
echononl " Check if user '$DATABASE_USER' already exists for localhost .." 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 user_exists=true
else else
user_exists=false user_exists=false
@ -583,7 +627,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Create database user '$DATABASE_USER' access from locahost" echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then 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'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -601,7 +645,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -610,7 +654,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' access from '$_ip' " 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -628,7 +672,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on all Databases" 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 "GRANT ALL ON *.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -642,7 +686,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on all Database from '$_ip'" 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 "GRANT ALL ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -661,12 +705,12 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echononl " Create database user '$DATABASE_USER' access from locahost" echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then if ! $user_exists ; then
echo "" #echo ""
echo "mysql $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'\""
echo "" #echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \ ${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \ "CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
# > $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"
@ -678,12 +722,12 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'" echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
echo "" #echo ""
echo "mysql $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'\""
echo "" #echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \ ${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \ "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost'" \
# > $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"
@ -695,7 +739,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo "" echo ""
echo "$MYSQL_CREDENTIAL_ARGS -N -s -e \"GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'\"" echo "$MYSQL_CREDENTIAL_ARGS -N -s -e \"GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'\""
echo "" echo ""
#mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \ #${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
# "GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'" \ # "GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'localhost'" \
# > $tmp_log_file 2>&1 # > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -710,7 +754,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .." 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)" "SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then if [[ -z "$_count" ]]; then
echo_failed echo_failed
@ -719,7 +763,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'" 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'" \ "CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -734,7 +778,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Allow access to user '$DATABASE_USER' on all databases from '$_ip'" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -744,7 +788,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from '$_ip'" 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'" \ "GRANT ALL privileges ON *.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1 > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@ -761,7 +805,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -771,7 +815,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases" 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 "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -785,7 +829,7 @@ else
for _ip in $IP_ADDRESSES ; do for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}" 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 "GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -795,7 +839,7 @@ else
fi fi
echononl " Grant all privileges to user '$DATABASE_USER' on all databases from $_ip" 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 "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
@ -812,7 +856,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.." 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
error "$(cat $tmp_log_file)" error "$(cat $tmp_log_file)"

View File

@ -378,6 +378,8 @@ declare -i index_i
declare -i index_arr=0 declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_command="$mysql"
_all_success=true _all_success=true
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}" IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
@ -385,7 +387,21 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_credential_args="${_val_arr[1]}" 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 found=false
if [[ -n "$GIVEN_DATABASE" ]] ; then if [[ -n "$GIVEN_DATABASE" ]] ; then
@ -498,13 +514,13 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
fi 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 for table in $TABLES ; do
# - Ommit InnoDB tables # - 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 [[ "${_engine,,}" = 'innodb' ]] ; then
if $VERBOSE ; then if $VERBOSE ; then
echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB" echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB"
@ -529,7 +545,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo -e " [$(date)] Optimize table '$table'" >> $log_file echo -e " [$(date)] Optimize table '$table'" >> $log_file
length_table_name=${#table} length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -540,7 +556,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file echo "" >> $log_file
echo " [$(date)] Repair table '$table'" >> $log_file echo " [$(date)] Repair table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -554,7 +570,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
else else
echo -e " [$(date)] Optimize table '$table'" >> $log_file echo -e " [$(date)] Optimize table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")" error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")"

View File

@ -378,6 +378,8 @@ declare -i index_i
declare -i index_arr=0 declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_command="$mysql"
_all_success=true _all_success=true
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}" IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
@ -385,7 +387,21 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
mysql_credential_args="${_val_arr[1]}" 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 found=false
if [[ -n "$GIVEN_DATABASE" ]] ; then if [[ -n "$GIVEN_DATABASE" ]] ; then
@ -427,13 +443,13 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file echo "" >> $log_file
echo -e " [$(date)] Optimize tables in database '${db}'.." >> $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 for table in $TABLES ; do
# - Ommit InnoDB tables # - 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 [[ "${_engine,,}" = 'innodb' ]] ; then
if $VERBOSE ; then if $VERBOSE ; then
echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB" echo -e " [$(date)] Ommit table '$table' - storage engine is InnoDB"
@ -459,7 +475,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
length_table_name=${#table} length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -470,7 +486,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file echo "" >> $log_file
echo " [$(date)] Repair table '$table'" >> $log_file echo " [$(date)] Repair table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -484,7 +500,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
else else
echo -e " [$(date)] Optimize table '$table'" >> $log_file echo -e " [$(date)] Optimize table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1 ${mysql_command} $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")" error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")"

View File

@ -197,17 +197,23 @@ else
terminal=false terminal=false
fi 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
if [ -x "/usr/local/mysql/bin/mysql" ]; then mysql_command="/usr/local/mysql/bin/mysql"
mysql=/usr/local/mysql/bin/mysql echo_ok
else else
echo_failed
fatal "No binary 'mysql' found!" fatal "No binary 'mysql' found!"
fi fi
fi fi
# - Print help? # - Print help?
# - # -
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
@ -277,7 +283,15 @@ if [[ ${#mysql_credential_args_arr[@]} -gt 0 ]] ; then
mysql_version="${_val_arr[0]}" mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}" 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 if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version" mysql_dist="MariaDB $mysql_version"
else else
@ -335,10 +349,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echononl " Get MySQL Version" 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 if [[ $? -ne 0 ]] ; then
echo_failed echo_failed
fatal "$(cat $tmp_log_file)" fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else else
echo_ok echo_ok
fi fi
@ -388,7 +420,9 @@ else
parameter manually." parameter manually."
fi fi
else 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" MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf"
@ -413,6 +447,8 @@ echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS" echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo "" echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
if [[ -n "$DATABASE_NAME" ]]; then if [[ -n "$DATABASE_NAME" ]]; then
echo " Database name................: $DATABASE_NAME" echo " Database name................: $DATABASE_NAME"
else else
@ -440,7 +476,7 @@ declare -i index_i
_all_success=true _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 if [[ -z "$DATABASE_NAME" ]] ; then
@ -482,7 +518,7 @@ for db in $DATABASES ; do
fi fi
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 if [[ $? -ne 0 ]]; then
_all_success=false _all_success=false
error "Getting tables of database '${db}' failed.\n $(cat "$tmp_log_file")" error "Getting tables of database '${db}' failed.\n $(cat "$tmp_log_file")"
@ -494,7 +530,7 @@ for db in $DATABASES ; do
# - Ommit InnoDB tables # - 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 [[ "${_engine,,}" = 'innodb' ]] ; then
echo -e " [$(date)] Ommit table '$table' - The storage engine (InnoDB) doesn't support repair" echo -e " [$(date)] Ommit table '$table' - The storage engine (InnoDB) doesn't support repair"
continue continue
@ -515,7 +551,7 @@ for db in $DATABASES ; do
fi fi
length_table_name=${#table} 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 if [[ $? -ne 0 ]]; then
@ -525,7 +561,7 @@ for db in $DATABASES ; do
else 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 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 "$tmp_log_file")"

View File

@ -81,6 +81,7 @@ usage() {
-c "u root -p '<password>'" -c "u root -p '<password>'"
-c "--login-path=local" -c "--login-path=local"
-c "--login-path=mysql-5.7"
-c "--defaults-file=/usr/local/mysql/sys-maint.cnf" -c "--defaults-file=/usr/local/mysql/sys-maint.cnf"
-c "-u root -S /run/mysqld/mysqld.sock" -c "-u root -S /run/mysqld/mysqld.sock"
@ -141,16 +142,16 @@ clean_up() {
fi fi
if [[ -n "$CUR_AUTOCOMMIT" ]] ; then 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 fi
if [[ -n "$CUR_FOREIGN_KEY_CHECKS" ]] ; then 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 fi
if [[ -n "$CUR_UNIQUE_CHECKS" ]] ; then 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 fi
if [[ -n "$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" ]] ; then 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 fi
# Perform program exit housekeeping # Perform program exit housekeeping
@ -444,6 +445,21 @@ else
NON_INTERACTIVE_MODE=true NON_INTERACTIVE_MODE=true
fi 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 # Read Configurations from $conf_file
@ -510,46 +526,47 @@ if $BATCH_MODE ; then
fatal "No Database given \033[m(Option -d)\033[1m!" fatal "No Database given \033[m(Option -d)\033[1m!"
fi 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 if [[ -z "$DATABASE_CREDENTIALS_ARGS" ]] && [[ -n "$DATABASE_SERVER" ]]; 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
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
fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'" fatal "Cannot detect database credentials on remote machines. You have to set Parameter '-c'"
fi fi
@ -559,7 +576,7 @@ else
echo "" echo ""
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
echo "Insert Database Server which should be created.." echo "Insert Database Server for which we should restore a database.."
echo "" echo ""
echo "" echo ""
echo -e " \033[33mType <Return> to accept the default (localhost).\033[m" echo -e " \033[33mType <Return> to accept the default (localhost).\033[m"
@ -591,33 +608,6 @@ else
if $DATABASE_CREDENTIALS_NEEDED ; then 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 ""
echo -e "\033[32m--\033[m" echo -e "\033[32m--\033[m"
echo "" echo ""
@ -632,6 +622,7 @@ else
\033[33m-u root -p'<password>'\033[m \033[33m-u root -p'<password>'\033[m
\033[33m--login-path=local\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--defaults-file=/usr/local/mysql/sys-maint.cnf\033[m
\033[33m-u root -S /run/mysqld/mysqld.sock\033[m" \033[33m-u root -S /run/mysqld/mysqld.sock\033[m"
echo "" echo ""
@ -689,6 +680,122 @@ else
fi # if $BATCH_MODE ; then 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 if ! $QUIET_MODE ; then
echo "" echo ""
echo "" echo ""
@ -703,6 +810,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}" echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
fi fi
echo "" echo ""
echo " MySQL Command................: ${mysql_command}"
echo ""
if [[ -n "$DATABASE_SERVER" ]] ; then if [[ -n "$DATABASE_SERVER" ]] ; then
echo " Database server..............: $DATABASE_SERVER" echo " Database server..............: $DATABASE_SERVER"
else else
@ -754,11 +863,11 @@ fi
echononl " Check connection to Database Server.." echononl " Check connection to Database Server.."
if [[ -n "$DATABASE_SERVER" ]] ; then 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'!" fatal "Cannot connect to Database Server '$DATABASE_SERVER'!"
fi fi
else 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!" fatal "Cannot connect to Database Server!"
fi fi
fi fi
@ -766,11 +875,11 @@ echo_done
echononl " Check connection to Database '$DATABASE_NAME'.." echononl " Check connection to Database '$DATABASE_NAME'.."
if [[ -n "$DATABASE_SERVER" ]] ; then 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'!" fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi fi
else 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'!" fatal "Cannot connect to Database '$DATABASE_NAME'!"
fi fi
fi fi
@ -830,7 +939,7 @@ blank_line
# - GET current (global) Autocommit value # - GET current (global) Autocommit value
# - # -
echononl " GET current (global) value \033[1mautocommit\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -841,7 +950,7 @@ fi
# - GET current (global) value for 'foreign_key_checks' # - GET current (global) value for 'foreign_key_checks'
# - # -
echononl " GET current (global) value \033[1mforeign_key_checks\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -852,7 +961,7 @@ fi
# - GET current (global) value for 'unique_checks' # - GET current (global) value for 'unique_checks'
# - # -
echononl " GET current (global) value \033[1munique_checks\033[m" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -863,7 +972,7 @@ fi
# - GET current (global) value for 'innodb_flush_log_at_trx_commit' # - 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" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -874,7 +983,7 @@ fi
# - GET current (global) value for 'max_allowed_packet' # - GET current (global) value for 'max_allowed_packet'
# - # -
echononl " GET current (global) value \033[1mmax_allowed_packet\033[m" echononl " GET current (global) value \033[1mmax_allowed_packet\033[m"
CUR_MAX_ALLOWED_PACKET="$(mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'" | awk '{print$2}')" >> $log_file 2>&1 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -885,7 +994,7 @@ fi
blank_line blank_line
echononl " Set Autocommit to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -894,7 +1003,7 @@ else
fi fi
echononl " Set foreign_key_checks to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -903,7 +1012,7 @@ else
fi fi
echononl " Set unique_checks to OFF" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -912,7 +1021,7 @@ else
fi fi
echononl " Set innodb_flush_log_at_trx_commit to 2" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -921,7 +1030,7 @@ else
fi fi
echononl " Set max_allowed_packet to 1G" echononl " Set max_allowed_packet to 1G"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1 ${mysql_command} $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=1073741824" >> $log_file 2>&1
if [[ $? -eq 0 ]];then if [[ $? -eq 0 ]];then
echo_ok echo_ok
else else
@ -942,9 +1051,9 @@ fi
echononl " Restore Database '${DATABASE_NAME}'.." echononl " Restore Database '${DATABASE_NAME}'.."
b_timestamp=$(date +"%s") b_timestamp=$(date +"%s")
if [[ -n "$DATABASE_SERVER" ]] ; then 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 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 fi
retval=$? retval=$?
e_timestamp=$(date +"%s") e_timestamp=$(date +"%s")
@ -972,7 +1081,7 @@ if ! $QUIET_MODE ; then
fi fi
echononl " Set Autocommit to $CUR_AUTOCOMMIT" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_AUTOCOMMIT="" CUR_AUTOCOMMIT=""
@ -982,7 +1091,7 @@ else
fi fi
echononl " Set foreign_key_checks to $CUR_FOREIGN_KEY_CHECKS" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_FOREIGN_KEY_CHECKS="" CUR_FOREIGN_KEY_CHECKS=""
@ -992,7 +1101,7 @@ else
fi fi
echononl " Set unique_checks to $CUR_UNIQUE_CHECKS" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_UNIQUE_CHECKS="" CUR_UNIQUE_CHECKS=""
@ -1002,7 +1111,7 @@ else
fi fi
echononl " Set innodb_flush_log_at_trx_commit to $CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" 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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="" CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT=""
@ -1012,7 +1121,7 @@ else
fi fi
echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET" echononl " Set max_allowed_packet to $CUR_MAX_ALLOWED_PACKET"
mysql $DATABASE_CREDENTIALS_ARGS -N -s -e "SET GLOBAL max_allowed_packet=$CUR_MAX_ALLOWED_PACKET" >> $log_file 2>&1 ${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 if [[ $? -eq 0 ]];then
echo_ok echo_ok
CUR_MAX_ALLOWED_PACKET="" CUR_MAX_ALLOWED_PACKET=""