From d4d07db02f59475bdae8c5f2dcc0c04e72552e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristhian=20Mart=C3=ADnez=20Ochoa?= Date: Mon, 23 Jan 2023 20:43:03 -0700 Subject: [PATCH] mysql external - No passwords are passed in command line now for better security. - Better check for master admin users. - No need to ask for external credentials when not really needed. --- lib/bkp | 22 ++++--- lib/general | 168 +++++++++++++++++++++++++++++++++++----------------- lib/sites | 92 +++++++++++++--------------- lib/verify | 12 ++-- lib/webin | 6 +- usr/site | 11 ++-- 6 files changed, 185 insertions(+), 126 deletions(-) diff --git a/lib/bkp b/lib/bkp index d31298f..011496b 100644 --- a/lib/bkp +++ b/lib/bkp @@ -91,12 +91,12 @@ bkp_local_db() { [[ -n $subfolder ]] && local subname=$(echo $subfolder | sed "s/\//_/g") fi - if [[ $wp_dbhost_host != "localhost" && ( -z $extdb_user || -z $extdb_pass || -z $extdb_url || -z $extdb_port ) ]]; then + if [[ $wp_dbhost_host != "localhost" && ( -z $wp_dbhost_host || -z $wp_dbhost_port ) ]]; then echo "${red}[ERROR] Invalid data for External Database!${end}" exit 1 # Duplicate check: we need this only to prevent error in the next is_wp_installed - elif [[ $wp_dbhost_host != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ]]; then + elif [[ $wp_dbhost_host != "localhost" && $(check_mysql_connection $extdb_url $extdb_port any -login-file -master-admin) != "true" ]]; then echo "${red}[ERROR] External DB Connection failed!${end}" exit 1 elif [[ $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) != "true" ]]; then @@ -125,11 +125,11 @@ bkp_local_db() { local checkdbname=$(sudo mysqlshow --user=admin | grep -ow $dbname) else external_db_parse - if [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ]]; then + if [[ $(check_mysql_connection $extdb_url $extdb_port any -login-file -master-admin) != "true" ]]; then echo "${red}[ERROR] Cannot connect with your External Database!${end}" exit 1 fi - local checkdbname=$(sudo mysqlshow -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" | grep -ow $dbname) + local checkdbname=$(sudo mysqlshow --defaults-group-suffix=_${extdb_url}:${extdb_port}_default -h "$extdb_url" -P "$extdb_port" | grep -ow $dbname) fi if [[ $checkdbname != $dbname ]]; then @@ -176,10 +176,10 @@ bkp_local_db() { [[ -z $filename ]] && local filename="webinoly-backup-db_${fn}_$(date +%F)-$(date +%T).sql" - if [[ ( $wp_dbhost_host == "localhost" || -z $external_db ) && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then + if [[ ( $wp_dbhost_host == "localhost" || ( -z $extdb_url && -z $extdb_port )) && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then sudo mysqldump --user=admin --single-transaction --lock-tables --quick $db_name_list > $destination/$filename - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - sudo mysqldump -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" --single-transaction --lock-tables --quick $db_name_list > $destination/$filename + elif [[ $(check_mysql_connection $extdb_url $extdb_port any -login-file -master-admin) == "true" ]]; then + sudo mysqldump --defaults-group-suffix=_${extdb_url}:${extdb_port}_default -h "$extdb_url" -P "$extdb_port" --single-transaction --lock-tables --quick $db_name_list > $destination/$filename else echo "${red}[ERROR] DB Connection failed!${end}" exit 1 @@ -584,6 +584,9 @@ check_exported_conf() { tar -C / -xf $file $HOME/.aws/credentials --absolute-names fi if [[ $(conf_read mysql) == "true" ]]; then + if tar -tvf $file --absolute-names | grep -oq "$MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf"; then + tar -C / -xf $file $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf --absolute-names + fi if tar -tvf $file --absolute-names | grep -oq "$MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly.cnf"; then tar -C / -xf $file $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly.cnf --absolute-names sudo systemctl restart mysql @@ -643,6 +646,7 @@ export_server() { [[ -f /opt/webinoly/templates/source/custom_header_http_webinoly.data ]] && local include="$include /opt/webinoly/templates/source/custom_header_http_webinoly.data" [[ -f /opt/webinoly/templates/source/custom_header_https_webinoly.data ]] && local include="$include /opt/webinoly/templates/source/custom_header_https_webinoly.data" [[ -f /opt/webinoly/templates/source/custom_header_html_webinoly.data ]] && local include="$include /opt/webinoly/templates/source/custom_header_html_webinoly.data" + [[ -f $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf ]] && local include="$include $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly.cnf" [[ -f $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly.cnf ]] && local include="$include $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly.cnf" [[ -d /etc/nginx/certs ]] && local include="$include /etc/nginx/certs" @@ -887,7 +891,7 @@ import_site() { # Database recovery - create users because they are not imported if [[ -n $db_create_users && $overwrite != "on" && $(is_wp $domain) == "true" ]]; then - wp_conf_retrieve $domain true false + wp_conf_retrieve $domain false false if [[ $wp_dbhost_host == "localhost" && -n $wp_dbname && -n $wp_dbuser && -n $wp_dbpass && -n $(sudo mysqlshow --user=admin "${mysql_params[@]}" | grep -ow $wp_dbname) ]]; then echo "${blu}${dim}Restoring database for${end}${dim} $domain ${blu}${dim}site!${end}" @@ -912,7 +916,7 @@ _EOF_ do local suby="/$(echo $sit | cut -f 2- -d "_" -s | cut -f -1 -d "-" -s | sed "s/_/\//g")" if [[ -n $suby && -f /var/www/$domain/htdocs$suby/wp-config.php ]]; then - wp_conf_retrieve $domain true false $suby + wp_conf_retrieve $domain false false $suby if [[ $wp_dbhost_host == "localhost" && -n $wp_dbname && -n $wp_dbuser && -n $wp_dbpass && -n $(sudo mysqlshow --user=admin "${mysql_params[@]}" | grep -ow $wp_dbname) ]]; then echo "${blu}${dim}Restoring database for${end}${dim} ${domain}${suby} ${blu}${dim}site!${end}" diff --git a/lib/general b/lib/general index a3936ab..004f841 100644 --- a/lib/general +++ b/lib/general @@ -329,12 +329,18 @@ check_mysql_connection() { # Unix socket: $(check_mysql_connection localhost /var/run/mysqld/mysqld.sock) # Custom port: $(check_mysql_connection localhost 3307) # External DB: $(check_mysql_connection $dburl $dbport $dburoot $dbproot) + # External DB if login group exist: $(check_mysql_connection $dburl $dbport $dburoot -login-file) + # External DB if login group exist and is master-admin: $(check_mysql_connection $dburl $dbport any -login-file -master-admin) + # External DB check and save it as master-admin: $(check_mysql_connection $dburl $dbport $dburoot $dbproot -master-admin) # Examples for especific User connection: # Localhost: $(check_mysql_connection localhost $wp_dbuser $wp_dbpass) # Unix socket: $(check_mysql_connection localhost $wp_dbuser $wp_dbpass /var/run/mysqld/mysqld.sock) # Custom port: $(check_mysql_connection localhost $wp_dbuser $wp_dbpass 3307) # External DB: $(check_mysql_connection $extdb_url $extdb_port $wp_dbuser $wp_dbpass) + # External DB if login group exist: $(check_mysql_connection $extdb_url $extdb_port $wp_dbuser -login-file) + # External DB if login group exist and is master-admin: $(check_mysql_connection $extdb_url $extdb_port any -login-file -master-admin) + # External DB check and save it as master-admin: $(check_mysql_connection $extdb_url $extdb_port $wp_dbuser $wp_dbpass -master-admin) # Examples for especific DBname/User connection: # Note: Error message is not displayed! @@ -342,18 +348,55 @@ check_mysql_connection() { # Unix socket: $(check_mysql_connection localhost $wp_dbuser $wp_dbpass $wp_dbname /var/run/mysqld/mysqld.sock) # Custom port: $(check_mysql_connection localhost $wp_dbuser $wp_dbpass $wp_dbname 3307) # External DB: $(check_mysql_connection $extdb_url $extdb_port $wp_dbuser $wp_dbpass $wp_dbname) + # External DB if login group exist: $(check_mysql_connection $extdb_url $extdb_port $wp_dbuser -login-file $wp_dbname) + # Master-admin not needed here for specific dbs. local query="quit" local error_display="true" if [[ -n $1 && ${1,,} != "localhost" && $(is_url $1) =~ ^(http|https|true|http\+ip|https\+ip|ip)$ && -n $2 && -n $3 && -n $4 ]]; then check_for_mysql_client - if [[ -n $5 ]]; then + local suffix_group_name="${1}:${2}_${3}" + local user_param="-u${3}" + + if [[ -n $5 && $5 == "-master-admin" ]]; then + # 'default' is a reserved word, real usernames should never use it, it's possible, but not practical! + local suffix_group_name="${1}:${2}_default" + elif [[ -n $5 ]]; then local query="use $5" local error_display="false" fi - sudo mysql --connect-timeout=10 -h "$1" -P "$2" -u"$3" -p"$4" -e "$query" 2>/dev/null - [[ $? != "0" ]] && local out="false" + + # Create or update the login file + if [[ $4 != "-login-file" ]]; then + mysql_login_cnf + sudo sed -i "/\[client_${suffix_group_name}\]/,/# ClientEnd/{/.*/d}" $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf + echo "[client_${suffix_group_name}] +host = $1 +port = $2 +user = $3 +password = $4 +# ClientEnd" >> $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf + else + [[ $5 == "-master-admin" ]] && unset user_param # take the user from the login group! + local error_display="false" + fi + + # Just for the record: We don't use the 'mysql_conf_editor' just because it can not be unattended :( + # --defaults-group-suffix should always be at the beginning, otherwise it fails. + sudo mysql --defaults-group-suffix=_${suffix_group_name} --connect-timeout=10 -h "$1" -P "$2" $user_param -e "$query" 2>/dev/null + if [[ $? != "0" ]]; then + local out="false" + sudo sed -i "/\[client_${suffix_group_name}\]/,/# ClientEnd/{/.*/d}" $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf + else + # Display a warning message when not enough privileges! + if [[ $5 == "-master-admin" ]]; then + local priv=$(sudo mysql --defaults-group-suffix=_${suffix_group_name} --connect-timeout=10 -h "$1" -P "$2" -e "SHOW GRANTS FOR CURRENT_USER();") + if ! [[ -n $(echo $priv | grep -Fo "WITH GRANT OPTION") && ( -n $(echo $priv | grep -Fo "GRANT ALL PRIVILEGES") || ( -n $(echo $priv | grep -Fo "CREATE USER") && -n $(echo $priv | grep -Fo "ALTER"))) ]]; then + echo "${red}${dim}[WARNING] Seems like '$(echo $priv | grep -oP -m 1 'Grants for \K\w+')' is not a Master user!${end}" >&2 + fi + fi + fi elif [[ ${1,,} == "localhost" && $(conf_read mysql) == "true" ]]; then if [[ -n $2 && -n $3 ]]; then if [[ -S $4 ]]; then @@ -381,9 +424,9 @@ check_mysql_connection() { fi if [[ $out == "false" && $error_display != "false" ]]; then - echo "${red}============================================" >&2 - echo " [Error] Database conection failed." >&2 - echo "============================================${end}" >&2 + echo "${red}===================================================" >&2 + echo " [Error] Database conection failed! (${1})" >&2 + echo "===================================================${end}" >&2 echo "" >&2 echo "false" elif [[ $out == "false" ]]; then @@ -397,7 +440,7 @@ check_mysql_connection() { check_external_db_saved() { if [[ -n $(conf_read external-dbh) && -n $(conf_read external-dbu) && -n $(conf_read external-dbp) && -n $(conf_read external-dbx) ]]; then external_db="[$(conf_read external-dbu),$(conf_read external-dbp),$(conf_read external-dbh):$(conf_read external-dbx)]" - [[ $(conf_read quiet) != "true" ]] && echo "${blu}${dim}External DB credentials found!${end}" >&2 + [[ $(conf_read quiet) != "true" ]] && echo "${blu}${dim}External DB credentials found in your saved configuration!${end}" >&2 fi } @@ -426,7 +469,7 @@ external_db_parse() { return fi - if [[ $(check_mysql_connection $url $port $user $pass) != "true" ]]; then + if [[ $(check_mysql_connection $url $port $user $pass -master-admin) != "true" ]]; then echo "${red}[ERROR] Cannot connect with your External Database!${end}" return else @@ -510,7 +553,7 @@ wp_conf_retrieve() { # wp_conf_retrieve example.com # wp_conf_retrieve example.com false false /subfolder # $1 is domain - # $2 set to false if you want to skip external_db questions. + # $2 set to false if you want to skip external_db questions. ONLY NEEDED (true) when you need master-admin privileges for your DB queries! # $3 set to false if you want to disable WP Domain Mapping check (wp_pref will be for the main site when disabled, wp_ instead of wp_3_). # $4 is subfolder @@ -573,33 +616,56 @@ wp_conf_retrieve() { #[[ -z $wp_dbhost_port ]] && wp_dbhost_port="3306" + # Only used when External DB and Master-Admin privileges are required! + # Example: Cloning site because its needed to create new dbs and users. + # Example: Delete site because its required to drop users. if [[ $2 != "false" && $wp_dbhost_host != "localhost" && -z $wp_dbhost_socket && $(is_url $wp_dbhost) =~ ^(http|https|true|http\+ip|https\+ip|ip)$ ]]; then - if [[ -z $external_db && -n $(conf_read external-dbh) && -n $(conf_read external-dbx) && $wp_dbhost == "$(conf_read external-dbh):$(conf_read external-dbx)" ]]; then - check_external_db_saved - elif [[ -z $external_db && -n $(conf_read external-dbh) ]]; then - echo "${dim}[INFO] External Database saved credentials found, but we cannot use it because not the same host!${end}" >&2 - fi - - if [[ -n $external_db ]]; then - external_db_parse - if [[ $wp_dbhost != $extdb_host ]]; then - unset external_db - unset extdb_user - unset extdb_pass - unset extdb_host - unset extdb_url - unset extdb_port + # Don't needed if already exist a login-file with master-admin privileges! + if [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port any -login-file -master-admin) != "true" ]]; then + if [[ -z $external_db && -n $(conf_read external-dbh) && -n $(conf_read external-dbx) && $wp_dbhost == "$(conf_read external-dbh):$(conf_read external-dbx)" ]]; then + check_external_db_saved + elif [[ -z $external_db && -n $(conf_read external-dbh) ]]; then + echo "${dim}[INFO] External Database saved credentials found, but we cannot use it because not the same host!${end}" >&2 fi - fi - if [[ -z $external_db ]]; then - echo "" >&2 - echo "${gre}External DB${blu} '${wp_dbhost}' ${gre}found in:${blu}${dim} ${1}${4} ${end}" >&2 - read -p "${blu}External DB username: ${end}" extdb_user - read -p "${blu}External DB password: ${end}" extdb_pass + + if [[ -n $external_db ]]; then + external_db_parse + if [[ $wp_dbhost != $extdb_host ]]; then + unset external_db + unset extdb_user + unset extdb_pass + unset extdb_host + unset extdb_url + unset extdb_port + fi + fi + if [[ -z $external_db ]]; then + local done="0" + while [[ $done -lt "3" ]] + do + echo "" >&2 + echo "${gre}External DB${blu} '${wp_dbhost}' ${gre}found in:${blu}${dim} ${1}${4} ${end}" >&2 + read -p "${blu}External DB Master Username: ${end}" extdb_user + read -p "${blu}External DB password: ${end}" extdb_pass + if [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $extdb_user $extdb_pass -master-admin) == "true" ]]; then + extdb_host=$wp_dbhost + extdb_url=$wp_dbhost_host + extdb_port=$wp_dbhost_port + external_db="[${extdb_user},${extdb_pass},${extdb_host}]" + break + fi + local done=$(($done+1)) + done + fi + else + # We always need these variables for non-WP sites! + unset external_db + unset extdb_user + unset extdb_pass extdb_host=$wp_dbhost extdb_url=$wp_dbhost_host extdb_port=$wp_dbhost_port - external_db="[${extdb_user},${extdb_pass},${extdb_host}]" + echo "${blu}${dim}External DB Credentials found!${end}" >&2 fi fi @@ -616,10 +682,10 @@ wp_conf_retrieve() { if [[ $(is_wp_multisite $maindom $4) =~ ^(subdomain|subdirectory)$ ]]; then local dbq="USE $wp_dbname; SELECT blog_id FROM ${wp_dbpref}blogs WHERE domain='$1' OR domain='www.$1';" - if [[ $wp_dbhost == "localhost" && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then + if [[ $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then local bid=$(sudo mysql --connect-timeout=10 --user=admin -e "$dbq" "${mysql_params[@]}") - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - local bid=$(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbq") + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + local bid=$(sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$dbq") fi wp_blogid=$(echo $bid | cut -f 2 -d " " -s) @@ -997,7 +1063,7 @@ is_cache() { is_wp_multisite() { if [[ $(is_wp $1 $2) == "true" ]]; then - wp_conf_retrieve $1 true false $2 # 3th parameter should always be 'false' to prevent an infinite loop! + wp_conf_retrieve $1 false false $2 # 3th parameter should always be 'false' to prevent an infinite loop! if [[ -n $wp_dbhost_host && -n $wp_dbname && -n $wp_dbpref ]]; then local dbsetup="SELECT * FROM information_schema.tables WHERE table_schema = '$wp_dbname' AND table_name = '${wp_dbpref}sitemeta' LIMIT 1;" local dbsetuc="USE $wp_dbname; SELECT meta_value FROM ${wp_dbpref}sitemeta where meta_key='subdomain_install';" @@ -1005,9 +1071,9 @@ is_wp_multisite() { if [[ $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then wpmu=$(sudo mysql --connect-timeout=10 --user=admin -e "$dbsetup" "${mysql_params[@]}") [[ -n $wpmu ]] && mutype=$(sudo mysql --connect-timeout=10 --user=admin -e "$dbsetuc" "${mysql_params[@]}") - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - wpmu=$(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") - [[ -n $wpmu ]] && mutype=$(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetuc") + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + wpmu=$(sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$dbsetup") + [[ -n $wpmu ]] && mutype=$(sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$dbsetuc") fi if [[ $(echo $mutype | cut -f 2 -d " " -s) == "1" ]]; then @@ -1031,7 +1097,7 @@ is_wp_installed() { # When you create a WP site, DB is created only after the initial WP installation wizard is completed. if [[ $(is_wp $1 $2) == "true" ]]; then - wp_conf_retrieve $1 true false $2 + wp_conf_retrieve $1 false false $2 # It makes no sense checking for mapped domains, that's why is set to false. # is_wp_installed will return true even if domain is not mapped, only a domain parked pointing to a WP site. # if we want to check for mapped domains: wp_conf_retrieve $1 true false $2 > /dev/null (silenced echoed messages because affects this function) @@ -1043,8 +1109,8 @@ is_wp_installed() { if [[ $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then [[ -n $(sudo mysql --connect-timeout=10 --user=admin -e "$dbsetup" "${mysql_params[@]}") ]] && echo "true" || echo "false" - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - [[ -n $(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") ]] && echo "true" || echo "false" + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + [[ -n $(sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$dbsetup") ]] && echo "true" || echo "false" else echo "false" fi @@ -1224,7 +1290,7 @@ edit_wp_db_url_multisite() { # $3 - WP BlogID to force (optional) if [[ -n $1 && -n $2 && $(is_wp_multisite $1) =~ ^(subdomain|subdirectory)$ ]]; then - wp_conf_retrieve $1 true true $subfolder + wp_conf_retrieve $1 false true $subfolder # Force WP blogID if [[ -n $3 && $3 =~ ^[0-9]+$ ]]; then @@ -1234,7 +1300,7 @@ edit_wp_db_url_multisite() { if [[ $wp_dbhost_host == "localhost" && -n $(sudo mysql --connect-timeout=10 --user=admin -e "$dbsetup" "${mysql_params[@]}") ]]; then echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2 - elif [[ $wp_dbhost_host != "localhost" && -n $(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") ]]; then + elif [[ $wp_dbhost_host != "localhost" && -n $(sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$dbsetup") ]]; then echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2 else echo "${red}${dim}[ERROR] WordPress blog ID (${wp_dbpref}) not found!${end}" >&2 @@ -1249,14 +1315,12 @@ edit_wp_db_url_multisite() { USE $wp_dbname; UPDATE ${wp_dbpref_main}blogs SET domain='$2' WHERE blog_id='${wp_blogid}'; UPDATE ${wp_dbpref_main}blogs SET path='/' WHERE blog_id='${wp_blogid}'; -FLUSH PRIVILEGES; _EOF_ - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" <<_EOF_ + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" <<_EOF_ USE $wp_dbname; UPDATE ${wp_dbpref_main}blogs SET domain='$2' WHERE blog_id='${wp_blogid}'; UPDATE ${wp_dbpref_main}blogs SET path='/' WHERE blog_id='${wp_blogid}'; -FLUSH PRIVILEGES; _EOF_ else echo "${red}${dim}[ERROR] WordPress Multisite database cannot be updated!${end}" >&2 @@ -1276,7 +1340,7 @@ edit_wp_db_url() { #IMPORTANT NOTE: Always be sure to take "www" Force-Redirect into consideration before updating WP Url in database. if [[ -n $1 && -n $2 && $(is_wp_installed $1 $3) == "true" ]]; then - wp_conf_retrieve $1 true true $3 + wp_conf_retrieve $1 false true $3 if [[ -n $wp_dbhost_host && -n $wp_dbname && -n $wp_dbpref ]]; then if [[ $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) == "true" ]]; then @@ -1284,16 +1348,14 @@ edit_wp_db_url() { USE $wp_dbname; UPDATE ${wp_dbpref}options SET option_value='$2' WHERE option_name='home'; UPDATE ${wp_dbpref}options SET option_value='$2' WHERE option_name='siteurl'; -FLUSH PRIVILEGES; _EOF_ echo "${gre}${dim}WordPress site${blu} ${1}${3} ${gre}database URL updated! ${blu}(${2})${end}" - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" <<_EOF_ + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" <<_EOF_ USE $wp_dbname; UPDATE ${wp_dbpref}options SET option_value='$2' WHERE option_name='home'; UPDATE ${wp_dbpref}options SET option_value='$2' WHERE option_name='siteurl'; -FLUSH PRIVILEGES; _EOF_ echo "${gre}${dim}WordPress site${blu} ${1}${3} ${gre}database URL updated! ${blu}(${2})${end}" @@ -1428,7 +1490,7 @@ mysql_login_cnf() { sudo chown -R root:root $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf echo "# Webinoly MySQL/MariaDB Login Configuration File -# You can use the 'mysql-config-editor' and then remove this file. + " >> $MYSQL_CONF_PATH/${MYSQL_CONF_PREF}-webinoly-login.cnf fi } diff --git a/lib/sites b/lib/sites index 5b17ec3..9b80be2 100644 --- a/lib/sites +++ b/lib/sites @@ -188,16 +188,23 @@ wpinstall() { echo "${blu}" if [[ $dbhost != "localhost" && ( $type == [135] && ( -z $dburoot || -z $dbproot )) ]]; then - read -p "External DB username: " dburoot - read -p "External DB password: " dbproot - echo "" + # Don't ask for password if already in file! + if [[ $(check_mysql_connection $dburl $dbport any -login-file -master-admin) != "true" ]]; then + read -p "${blu}External DB Master Username: " dburoot + read -p "External DB password: " dbproot + echo "" + else + local ext_login="true" + fi fi - if [[ -n $retry && $retry -ge 3 ]]; then + if [[ $ext_login == "true" ]]; then + echo "${blu}${dim}External DB Credentials found!${end}" + elif [[ -n $retry && $retry -ge 3 ]]; then setupmysql="N" local db_error_flag="true" echo "${red}[ERROR] Database creation failed after 3 retries! ${blu}" - elif [[ ( $dbhost == "localhost" && $(check_mysql_connection localhost) != "true" ) || ( $dbhost != "localhost" && $(check_mysql_connection $dburl $dbport $dburoot $dbproot) != "true" ) ]]; then + elif [[ ( $dbhost == "localhost" && $(check_mysql_connection localhost) != "true" ) || ( $dbhost != "localhost" && $(check_mysql_connection $dburl $dbport $dburoot $dbproot -master-admin) != "true" ) ]]; then # Reset variables local dburl="" local dbport="" @@ -229,7 +236,7 @@ wpinstall() { # https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result local newdbname=$(sudo mysqlshow --user=admin | grep -ow $dbname) else - local newdbname=$(sudo mysqlshow -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" | grep -ow $dbname) + local newdbname=$(sudo mysqlshow --defaults-group-suffix=_${dburl}:${dbport}_default -h "$dburl" -P "$dbport" | grep -ow $dbname) fi if [[ $newdbname == $dbname || $dbname != $(dbword_check $dbname) ]]; then @@ -278,7 +285,7 @@ wpinstall() { # https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result [[ -z $(sudo mysql --connect-timeout=10 --user=admin -e "SELECT User FROM mysql.user;" | grep -ow $dbuser) ]] && newdbuser="" else - [[ -z $(sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "SELECT User FROM mysql.user;" | grep -ow $dbuser) ]] && newdbuser="" + [[ -z $(sudo mysql --defaults-group-suffix=_${dburl}:${dbport}_default --connect-timeout=10 -h "$dburl" -P "$dbport" -e "SELECT User FROM mysql.user;" | grep -ow $dbuser) ]] && newdbuser="" fi if [[ $newdbuser == $dbuser || $dbuser != $(dbword_check $dbuser user) ]]; then @@ -368,7 +375,7 @@ _EOF_ elif [[ -z $dbreassign ]]; then # We can not use GRAN ALL PRIVILIGES with AWS RDS, that's why are all separate. - sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_ + sudo mysql --defaults-group-suffix=_${dburl}:${dbport}_default --connect-timeout=10 -h "$dburl" -P "$dbport" <<_EOF_ CREATE DATABASE ${dbname}; CREATE USER '${dbuser}'@'%' IDENTIFIED BY '${dbpass}'; GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%'; @@ -387,7 +394,7 @@ _EOF_ elif [[ $dbreassign == "db" ]]; then # We can not use GRAN ALL PRIVILIGES with AWS RDS, that's why are all separate. - sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_ + sudo mysql --defaults-group-suffix=_${dburl}:${dbport}_default --connect-timeout=10 -h "$dburl" -P "$dbport" <<_EOF_ CREATE USER '${dbuser}'@'%' IDENTIFIED BY '${dbpass}'; GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%'; FLUSH PRIVILEGES; @@ -405,7 +412,7 @@ _EOF_ elif [[ $dbreassign == "dbuser" ]]; then # We can not use GRAN ALL PRIVILIGES with AWS RDS, that's why are all separate. - sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_ + sudo mysql --defaults-group-suffix=_${dburl}:${dbport}_default --connect-timeout=10 -h "$dburl" -P "$dbport" <<_EOF_ CREATE DATABASE ${dbname}; GRANT $(db_user_role) on ${dbname}.* to '${dbuser}'@'%'; FLUSH PRIVILEGES; @@ -493,20 +500,22 @@ db_delete() { if [[ -z $wp_dbhost_host || -z $wp_dbname || -z $wp_dbuser ]]; then echo "${red}[ERROR] Database${blu} ${1}${2} ${red}cannot be deleted! ${dim}(WP configuration corrupted)${end}" return - elif [[ ( $wp_dbhost_host == "localhost" && ( $(conf_read mysql) != "true" || $(check_mysql_connection localhost $mysql_param) != "true" )) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ) ]]; then + elif [[ ( $wp_dbhost_host == "localhost" && ( $(conf_read mysql) != "true" || $(check_mysql_connection localhost $mysql_param) != "true" )) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port any -login-file -master-admin) != "true" ) ]]; then echo "${red}[ERROR] Database${blu} ${1}${2} ${red}cannot be deleted! ${dim}(DB Connection failed)${end}" return fi + # We use this instead of 'DROP USER' because we have both 'user'@'%' and 'user'@'localhost' (we changed to localhost in recent versions) + # DROP USER use '%' when empty, and wildcard does not match 'localhost' (I don't know why). dbsetup="DELETE FROM mysql.user WHERE User='$wp_dbuser';DROP DATABASE IF EXISTS $wp_dbname;DELETE FROM mysql.db WHERE Db='$wp_dbname' OR Db='$wp_dbname\\_%';FLUSH PRIVILEGES;" if [[ $wp_dbhost_host == "localhost" ]]; then sudo mysql --connect-timeout=10 --user=admin -e "$dbsetup" "${mysql_params[@]}" echo "${gre}${dim}Database of your site ${blu}${1}${2}${gre} has been successfully deleted! ${end}" else if [[ $extdb_user == "root" && -z $extdb_pass ]]; then - echo "${red}Action aborted, database not deleted!${end}" + echo "${red}Action aborted, database not deleted! ${dim}(root user without password not allowed!)${end}" else - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup" + sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_default --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -e "$dbsetup" echo "${gre}${dim}Database of your site ${blu}${1}${2}${gre} has been successfully deleted! ${end}" fi fi @@ -1210,7 +1219,7 @@ parked_domain() { sed -i "/include \/var\/www\/$parked\/\*-nginx.conf;/a \ include \/var\/www\/${parked}/*-$(echo $domain | sed "s/[^0-9A-Za-z]/_/g")_parked.conf;" /etc/nginx/sites-available/$domain # Prevent asking multiple times in case of external DB. - [[ -n $domain_mapping_wp_id && $domain_mapping_wp_id =~ ^[0-9]+$ && $(is_wp $parked) == "true" ]] && wp_conf_retrieve $parked true false $subfolder + [[ -n $domain_mapping_wp_id && $domain_mapping_wp_id =~ ^[0-9]+$ && $(is_wp $parked) == "true" ]] && wp_conf_retrieve $parked false false $subfolder # WordPress Domain Mapping if [[ $domain_mapping_wp_id == 1 ]]; then @@ -1657,15 +1666,10 @@ cloning_site() { if [[ $(echo "$(wp_config_read $domain DB_HOST $subfolder)" | cut -f 1 -d ':' ) == "localhost" ]]; then [[ -z $subfolder ]] && sudo site $domain -delete=force > /dev/null 2>&1 || sudo site $domain -subfolder=$subfolder -delete=force > /dev/null 2>&1 else - if [[ -n $extdb_user && -n $extdb_pass && -n $extdb_host ]]; then - if [[ -z $subfolder ]]; then - sudo site $domain -delete=force -external_db=[${extdb_user},${extdb_pass},${extdb_host}] > /dev/null 2>&1 - else - sudo site $domain -subfolder=$subfolder -delete=force -external_db=[${extdb_user},${extdb_pass},${extdb_host}] > /dev/null 2>&1 - fi + if [[ -z $subfolder ]]; then + sudo site $domain -delete=force > /dev/null 2>&1 else - echo "${red}[ERROR] External DB data corrupted!${end}" - exit 1 + sudo site $domain -subfolder=$subfolder -delete=force > /dev/null 2>&1 fi fi else @@ -1790,7 +1794,7 @@ cloning_site() { edit_wp_db_url $domain "http://${domain}${subfolder}" $subfolder else site -mysql=[$wp_dbhost,$dom,$usr,$AUTOGENPASS_WPDB,$extdb_user,$extdb_pass] > /dev/null 2>&1 - mysqldump -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" $wp_dbname | mysql -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" $dom + mysqldump --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_default -h "$wp_dbhost_host" -P "$wp_dbhost_port" $wp_dbname | mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_default -h "$wp_dbhost_host" -P "$wp_dbhost_port" $dom # No need of force-redirect check because this is a new and different site. edit_wp_db_url $domain "http://${domain}${subfolder}" $subfolder @@ -1843,16 +1847,16 @@ wp_replace_content() { fi if [[ -n $wp_dbhost_host && -n $wp_dbname && -n $wp_dbpref ]]; then - if [[ ( $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) != "true" ) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ) ]]; then + if [[ ( $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) != "true" ) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) != "true" ) ]]; then exit 1 fi - query="USE $wp_dbname; UPDATE ${wp_dbpref}posts SET post_content = REPLACE (post_content,'$search','$replace'); FLUSH PRIVILEGES;" + query="USE $wp_dbname; UPDATE ${wp_dbpref}posts SET post_content = REPLACE (post_content,'$search','$replace');" if [[ $wp_dbhost_host == "localhost" ]]; then sudo mysql --connect-timeout=10 --user=admin -e "$query" "${mysql_params[@]}" else - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$query" + sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" -e "$query" fi echo "${gre}Content successfully replaced in your WP site!${end}" else @@ -1865,7 +1869,7 @@ wp_replace_content() { list_sites() { echo "" - if ! [[ ${list,,} =~ ^(true|disabled|main|parked|wordpress|php|html|proxy|forward|tools|default|ssl|nossl|sslstaging|accesslog|noaccesslog|cache|nocache|adminauth|noadminauth|debug|nodebug|wpenv)$ ]]; then + if ! [[ ${list,,} =~ ^(true|disabled|main|parked|wordpress|php|html|proxy|forward|tools|default|ssl|nossl|sslstaging|accesslog|noaccesslog|cache|nocache|adminauth|noadminauth|debug|nodebug|wpenv)$ ]]; then echo "${red}[ERROR] Invalid value for list filter! ${end}" exit 1 fi @@ -1974,14 +1978,8 @@ site_info_subfolder() { fi if [[ $typesite == "wp" ]]; then - local wph=$(wp_config_read $domain DB_HOST $subfolder) - if [[ ${wph,,} == "localhost" || $(echo "${wph,,}" | cut -f 1 -d ':') == "localhost" || -n $external_db || $wph == "$(conf_read external-dbh):$(conf_read external-dbx)" ]]; then - local wpmu=$(is_wp_multisite $domain $subfolder) - wp_conf_retrieve $domain true true $subfolder - else - local wpmu="${dim}Not Available${end}" - wp_conf_retrieve $domain false false $subfolder - fi + wp_conf_retrieve $domain false false $subfolder + local wpmu=$(is_wp_multisite $domain $subfolder) echo "" echo "${blu}Cache:${end} $cache" @@ -1992,7 +1990,7 @@ site_info_subfolder() { [[ -n $(wp_config_read $domain WP_ENVIRONMENT_TYPE $subfolder) ]] && echo "${blu}Environment:${end} $(wp_config_read $domain WP_ENVIRONMENT_TYPE $subfolder)" echo "" - echo "${blu}Database:${end} $wph" + echo "${blu}Database:${end} $wp_dbhost" echo "${blu}DB Name:${end} $wp_dbname" echo "${blu}DB User:${end} $wp_dbuser" echo "${blu}DB Prefix:${end} $wp_dbpref" @@ -2051,14 +2049,8 @@ site_info() { fi if [[ $typesite == "WordPress" || ( $typesite == "Parked" && $(is_wp $domain) == "true" ) ]]; then - local wph=$(wp_config_read $domain DB_HOST) - if [[ ${wph,,} == "localhost" || $(echo "${wph,,}" | cut -f 1 -d ':') == "localhost" || -n $external_db || $wph == "$(conf_read external-dbh):$(conf_read external-dbx)" ]]; then - local wpmu=$(is_wp_multisite $domain) - wp_conf_retrieve $domain true true - else - local wpmu="${dim}Not Available${end}" - wp_conf_retrieve $domain false false - fi + wp_conf_retrieve $domain false false + local wpmu=$(is_wp_multisite $domain) echo "" echo "${blu}Cache:${end} $cache" @@ -2069,7 +2061,7 @@ site_info() { [[ -n $(wp_config_read $domain WP_ENVIRONMENT_TYPE $subfolder) ]] && echo "${blu}Environment:${end} $(wp_config_read $domain WP_ENVIRONMENT_TYPE $subfolder)" echo "" - echo "${blu}Database:${end} $wph" + echo "${blu}Database:${end} $wp_dbhost" echo "${blu}DB Name:${end} $wp_dbname" echo "${blu}DB User:${end} $wp_dbuser" echo "${blu}DB Prefix:${end} $wp_dbpref" @@ -2120,8 +2112,8 @@ wp_env_type() { fi # WordPress Reading Settings: Discourage Search Engines - wp_conf_retrieve $domain true true $subfolder - if [[ -n $wp_dbhost_host && $wp_dbhost_host != "localhost" && ( -z $extdb_url || -z $extdb_port || -z $extdb_user || -z $extdb_pass ) ]]; then + wp_conf_retrieve $domain false true $subfolder + if [[ -n $wp_dbhost_host && $wp_dbhost_host != "localhost" && ( -z $wp_dbhost_host || -z $wp_dbhost_port || -z $wp_dbuser || -z $wp_dbpass ) ]]; then echo "${red}${dim}[ERROR] Discourage Search Engines WP option not updated! (External DB data not available)${end}" elif [[ -n $wp_dbhost_host && -n $wp_dbname && -n $wp_dbpref ]]; then if [[ $(is_wp_installed $domain $subfolder) == "true" ]]; then @@ -2129,14 +2121,12 @@ wp_env_type() { sudo mysql --connect-timeout=10 --user=admin "${mysql_params[@]}" <<_EOF_ USE $wp_dbname; UPDATE ${wp_dbpref}options SET option_value='$index' WHERE option_name='blog_public'; -FLUSH PRIVILEGES; _EOF_ - elif [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) == "true" ]]; then - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" <<_EOF_ + elif [[ $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port $wp_dbuser $wp_dbpass) == "true" ]]; then + sudo mysql --defaults-group-suffix=_${wp_dbhost_host}:${wp_dbhost_port}_${wp_dbuser} --connect-timeout=10 -h "$wp_dbhost_host" -P "$wp_dbhost_port" -u"$wp_dbuser" <<_EOF_ USE $wp_dbname; UPDATE ${wp_dbpref}options SET option_value='$index' WHERE option_name='blog_public'; -FLUSH PRIVILEGES; _EOF_ else diff --git a/lib/verify b/lib/verify index 709dbd8..ef8b59e 100644 --- a/lib/verify +++ b/lib/verify @@ -651,11 +651,13 @@ fi # MySQL/MariaDB Connection if [[ $(conf_read mysql) == "true" ]]; then - local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt ) - [[ -z $ROOT_PASS ]] && ROOT_PASS="dUmb" - if ! sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit" 2>/dev/null; then - echo "- [ERROR] MySQL/MariaDB Connection to localhost failed! (root)" - local ver_four_err="1" + if [[ -n $(conf_read mysql-root) ]]; then # Dynvar can be removed safely! + local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt ) + [[ -z $ROOT_PASS ]] && ROOT_PASS="dUmb" + if ! sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit" 2>/dev/null; then + echo "- [ERROR] MySQL/MariaDB Connection to localhost failed! (root)" + local ver_four_err="1" + fi fi if ! sudo mysql --connect-timeout=10 --user=admin -e "quit" 2>/dev/null; then diff --git a/lib/webin b/lib/webin index cdbc965..758ded8 100644 --- a/lib/webin +++ b/lib/webin @@ -1063,14 +1063,14 @@ database_import() { if [[ -n $external_db ]]; then external_db_parse - if [[ $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ]]; then + if [[ $(check_mysql_connection $extdb_url $extdb_port any -login-file -master-admin) != "true" ]]; then echo "${red}[ERROR] Cannot connect with your External Database!${end}" exit 1 fi echo "${dim}Wait while we import your db... ${end}" - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" < $file - sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "FLUSH PRIVILEGES;" + sudo mysql --defaults-group-suffix=_${extdb_url}:${extdb_port}_default --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" < $file + sudo mysql --defaults-group-suffix=_${extdb_url}:${extdb_port}_default --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -e "FLUSH PRIVILEGES;" echo "${gre}Database successfully imported!${end}" elif [[ -z $external_db && $(check_mysql_connection localhost) == "true" ]]; then check_for_mysql -ask diff --git a/usr/site b/usr/site index a02492b..1c9924d 100644 --- a/usr/site +++ b/usr/site @@ -217,8 +217,8 @@ elif [[ -n $multisite_convert && -f /etc/nginx/sites-available/$domain ]]; then exit 1 fi - # We need to call this function in the parent shell to preserve variables and avoid asking two or more times for the same values in case of external db. - wp_conf_retrieve $domain true false $subfolder + # We need to call this function in the parent shell to preserve variables. + wp_conf_retrieve $domain false false $subfolder if [[ $(is_wp_installed $domain $subfolder) != "true" ]]; then echo "${red}[ERROR] WordPress site${blu} $domain$subfolder ${red}is empty and still not configured!${end}" @@ -378,7 +378,8 @@ elif [[ -n $clone_from ]]; then if [[ -z $wp_dbhost ||-z $wp_dbhost_host || -z $wp_dbname || -z $wp_dbpref ]]; then echo "${red}[ERROR] Can not retrieve WP data ${dim}(WP Configuration corrupted)!${end}" exit 1 - elif [[ ( $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) != "true" ) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ) ]]; then + elif [[ ( $wp_dbhost_host == "localhost" && $(check_mysql_connection localhost $mysql_param) != "true" ) || ( $wp_dbhost_host != "localhost" && $(check_mysql_connection $wp_dbhost_host $wp_dbhost_port any -login-file -master-admin) != "true" ) ]]; then + echo "${red}[ERROR] Database connection failed! ${dim}($wp_dbhost)${end}" exit 1 elif [[ $(is_wp_multisite $clone_from $subfolder) != "false" ]]; then echo "${red}[ERROR] WordPress Multisite can not be cloned!${end}" @@ -401,8 +402,8 @@ elif [[ -n $replace_content && -f /etc/nginx/sites-available/$domain ]]; then exit 1 fi - #We need to call this function in the parent shell to preserve variables and avoid asking two times for the same values in case of external db. - wp_conf_retrieve $domain true true $subfolder + #We need to call this function in the parent shell to preserve variables. + wp_conf_retrieve $domain false true $subfolder if [[ $(is_wp_installed $domain $subfolder) != "true" ]]; then echo "${red}[ERROR] WordPress is empty and still not configured in ${domain}${subfolder} site!${end}"