DB Connection improvements

DB timeout fixed, check for duplicate dbname improved, fixed remote db delete.
This commit is contained in:
Cristhian Martínez Ochoa 2017-11-08 14:43:39 -07:00
parent 24543a5d8a
commit e63b236f89
4 changed files with 70 additions and 55 deletions

View file

@ -9,52 +9,63 @@ end=`tput sgr0`
db_delete() {
local domain="$1"
if [[ $(conf_read mysql) == "true" ]]; then
# Get dbname and dbuser of a WP site
local name=$( grep -F "DB_NAME" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local user=$( grep -F "DB_USER" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local host=$( grep -F "DB_HOST" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local url=$(echo "$host" | cut -f 1 -d ':')
local port=$(echo "$host" | cut -f 2 -d ':')
dbsetup="DELETE FROM mysql.user WHERE User='$user';DROP DATABASE IF EXISTS $name;DELETE FROM mysql.db WHERE Db='$name' OR Db='$name\\_%';FLUSH PRIVILEGES;"
local done="0"
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
while [[ $done != "1" ]]
do
done="1"
if [[ $host == "localhost" ]]; then
sudo mysql --connect_timeout 10 --user=root -p$ROOT_PASS -e "$dbsetup"
else
echo ""
echo "${gre} External DB found in $domain "
read -p "${blu}External DB root username [root]: " uroot
read -p "External DB root password: " proot
echo "${end}"
sudo mysql --connect_timeout 10 -h "$url" -P "$port" -u"$uroot" -p"$proot" -e "$dbsetup"
fi
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database delete failed."
echo "============================================"
echo ""
echo "${blu} Retry [Y/n]? "
while read -r -n 1 -s answer; do
answer=${answer:-y}
if [[ $answer = [YyNn] ]]; then
break
fi
done
if [[ $answer == [Nn] ]]; then
done="1"
fi
fi
done
else
echo "${red} [ERROR] Failed to delete $domain Database. MySQL was not found in your server! ${end}"
if [[ $(conf_read mysql-client) != "true" ]]; then
echo "${gre}MySQL Client is not installed and we need it to stablish a connection with your Database."
echo "Wait while we install MySQL Client... installing!!!${end}"
mysql_client_install > /dev/null 2>&1 &
echo "${gre}MySQL Client has been successfully installed!${end}"
fi
# Get dbname and dbuser of a WP site
local name=$( grep -F "DB_NAME" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local user=$( grep -F "DB_USER" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local host=$( grep -F "DB_HOST" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
local url=$(echo "$host" | cut -f 1 -d ':')
local port=$(echo "$host" | cut -f 2 -d ':')
local done="0"
dbsetup="DELETE FROM mysql.user WHERE User='$user';DROP DATABASE IF EXISTS $name;DELETE FROM mysql.db WHERE Db='$name' OR Db='$name\\_%';FLUSH PRIVILEGES;"
while [[ $done != "1" ]]
do
done="1"
if [[ $host == "localhost" ]]; then
# Check if MySQL installed
if [[ ! -d /etc/mysql || $(conf_read mysql) != "true" ]]; then
echo "${red}[ERROR] Seems like MySQL is not installed or Webinoly can not detect it! ( $domain ) ${end}"
fi
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "$dbsetup"
else
echo ""
echo "${gre} External DB found in $domain "
read -p "${blu}External DB root username [root]: " uroot
read -p "External DB root password: " proot
echo "${end}"
sudo mysql --connect-timeout=10 -h "$url" -P "$port" -u"$uroot" -p"$proot" -e "$dbsetup"
fi
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database delete failed."
echo "============================================"
echo ""
echo "${blu} Retry [Y/n]? "
while read -r -n 1 -s answer; do
answer=${answer:-y}
if [[ $answer = [YyNn] ]]; then
break
fi
done
if [[ $answer == [Nn] ]]; then
done="1"
fi
else
echo "${gre}Database of your site ${blu}${domain}${gre} has been successfully deleted! ${end}"
fi
done
}

View file

@ -65,7 +65,7 @@ wpinstall() {
# Check if localhost or external DB
if [[ $dbhost != "localhost" && ( "$setupmysql" == y || "$setupmysql" == Y ) ]]; then
if [[ $(conf_read mysql-client) != "true" ]]; then
echo "${gre}MySQL Client is not installed and we need it to conect with your external server."
echo "${gre}MySQL Client is not installed and we need it to stablish a connection with your external server."
echo "Wait while we install MySQL Client... installing!!!${end}"
mysql_client_install > /dev/null 2>&1 &
echo "${gre}MySQL Client has been successfully installed!${end}"
@ -91,9 +91,9 @@ wpinstall() {
# Chech connection to DB first
if [[ $dbhost == "localhost" ]]; then
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
sudo mysql --connect_timeout 10 --user=root -p$ROOT_PASS -e "quit"
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit"
else
sudo mysql --connect_timeout 10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
fi
if [[ $? != "0" ]]; then
done="0"
@ -104,9 +104,10 @@ wpinstall() {
continue 2;
fi
if [[ $dbhost == "localhost" ]]; then
local newdbname=$(sudo mysqlshow --user=root -p$ROOT_PASS $dbname | grep -v Wildcard | grep -ow $dbname)
# https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result
local newdbname=$(sudo mysqlshow --user=root -p$ROOT_PASS | grep -ow $dbname)
else
local newdbname=$(sudo mysqlshow -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" $dbname | grep -v Wildcard | grep -ow $dbname)
local newdbname=$(sudo mysqlshow -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" | grep -ow $dbname)
fi
if [ "$newdbname" == "$dbname" ]; then
@ -123,6 +124,9 @@ wpinstall() {
if [[ $dbreuse != y && $dbreuse != Y ]]; then
echo ""
read -p "Please enter a new DB_NAME for your Database: " newdbname
if [[ -z "$newdbname" ]]; then
newdbname="$dbname"
fi
dbname="$newdbname"
elif [[ $dbreuse == y || $dbreuse == Y ]]; then
# If you want to use the DB that already exist, abort DB creation.
@ -146,9 +150,9 @@ wpinstall() {
if [[ "$setupmysql" == y || "$setupmysql" == Y ]] ; then
if [[ $dbhost == "localhost" ]]; then
local dbsetup="CREATE DATABASE $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@$dbhost IDENTIFIED BY '$dbpass';FLUSH PRIVILEGES;"
sudo mysql --connect_timeout 10 --user=root -p$ROOT_PASS -e "$dbsetup"
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "$dbsetup"
else
sudo mysql --connect_timeout 10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_
CREATE DATABASE ${dbname};
grant usage on ${dbname}.* to ${dbuser}@\`%\` identified by '${dbpass}';
grant all privileges on ${dbname}.* to ${dbuser}@\`%\`;
@ -167,7 +171,7 @@ _EOF_
if [[ $done != "1" ]]; then
echo "${red} Some error ocurred during Database Configuration."
echo "${blu} Retry [Y/n]? ${end}"
echo "${blu} Retry [Y/n]?"
while read -r -n 1 -s done; do
done=${done:-y}
if [[ $done = [YyNn] ]]; then
@ -180,6 +184,7 @@ _EOF_
fi
done
fi
echo "${end}"
#WP-Config.php auto-setup

View file

@ -66,7 +66,6 @@ elif [[ "$domain" == "-delete-all" && -z "$type" && -z "$cache" ]]; then
if [[ -a $site/wp-config.php || -a $site/htdocs/wp-config.php ]]; then
domi=$(echo $site | cut -f 4 -d "/")
db_delete $domi
echo "${gre}Database of your site ${red}${domi}${gre} has been successfully deleted! ${end}"
fi
done

2
weby
View file

@ -2,7 +2,7 @@
# Webinoly script.
# This script is designed to install latest Webinoly.
webyversion="1.0.2beta"
webyversion="1.0.2"
if [[ -n "$1" ]]; then
setup="$1"