DB Connection improvements
DB timeout fixed, check for duplicate dbname improved, fixed remote db delete.
This commit is contained in:
parent
24543a5d8a
commit
e63b236f89
27
lib/general
27
lib/general
|
@ -9,29 +9,40 @@ end=`tput sgr0`
|
||||||
|
|
||||||
db_delete() {
|
db_delete() {
|
||||||
local domain="$1"
|
local domain="$1"
|
||||||
if [[ $(conf_read mysql) == "true" ]]; then
|
|
||||||
|
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
|
# 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 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 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 host=$( grep -F "DB_HOST" /var/www/$domain/wp-config.php | cut -f 4 -d "'" )
|
||||||
local url=$(echo "$host" | cut -f 1 -d ':')
|
local url=$(echo "$host" | cut -f 1 -d ':')
|
||||||
local port=$(echo "$host" | cut -f 2 -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;"
|
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" ]]
|
while [[ $done != "1" ]]
|
||||||
do
|
do
|
||||||
done="1"
|
done="1"
|
||||||
if [[ $host == "localhost" ]]; then
|
if [[ $host == "localhost" ]]; then
|
||||||
sudo mysql --connect_timeout 10 --user=root -p$ROOT_PASS -e "$dbsetup"
|
# 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
|
else
|
||||||
echo ""
|
echo ""
|
||||||
echo "${gre} External DB found in $domain "
|
echo "${gre} External DB found in $domain "
|
||||||
read -p "${blu}External DB root username [root]: " uroot
|
read -p "${blu}External DB root username [root]: " uroot
|
||||||
read -p "External DB root password: " proot
|
read -p "External DB root password: " proot
|
||||||
echo "${end}"
|
echo "${end}"
|
||||||
sudo mysql --connect_timeout 10 -h "$url" -P "$port" -u"$uroot" -p"$proot" -e "$dbsetup"
|
sudo mysql --connect-timeout=10 -h "$url" -P "$port" -u"$uroot" -p"$proot" -e "$dbsetup"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $? != "0" ]; then
|
if [ $? != "0" ]; then
|
||||||
|
@ -50,11 +61,11 @@ db_delete() {
|
||||||
if [[ $answer == [Nn] ]]; then
|
if [[ $answer == [Nn] ]]; then
|
||||||
done="1"
|
done="1"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
echo "${gre}Database of your site ${blu}${domain}${gre} has been successfully deleted! ${end}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
else
|
|
||||||
echo "${red} [ERROR] Failed to delete $domain Database. MySQL was not found in your server! ${end}"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
21
lib/sites
21
lib/sites
|
@ -65,7 +65,7 @@ wpinstall() {
|
||||||
# Check if localhost or external DB
|
# Check if localhost or external DB
|
||||||
if [[ $dbhost != "localhost" && ( "$setupmysql" == y || "$setupmysql" == Y ) ]]; then
|
if [[ $dbhost != "localhost" && ( "$setupmysql" == y || "$setupmysql" == Y ) ]]; then
|
||||||
if [[ $(conf_read mysql-client) != "true" ]]; 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}"
|
echo "Wait while we install MySQL Client... installing!!!${end}"
|
||||||
mysql_client_install > /dev/null 2>&1 &
|
mysql_client_install > /dev/null 2>&1 &
|
||||||
echo "${gre}MySQL Client has been successfully installed!${end}"
|
echo "${gre}MySQL Client has been successfully installed!${end}"
|
||||||
|
@ -91,9 +91,9 @@ wpinstall() {
|
||||||
# Chech connection to DB first
|
# Chech connection to DB first
|
||||||
if [[ $dbhost == "localhost" ]]; then
|
if [[ $dbhost == "localhost" ]]; then
|
||||||
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
|
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
|
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
|
fi
|
||||||
if [[ $? != "0" ]]; then
|
if [[ $? != "0" ]]; then
|
||||||
done="0"
|
done="0"
|
||||||
|
@ -104,9 +104,10 @@ wpinstall() {
|
||||||
continue 2;
|
continue 2;
|
||||||
fi
|
fi
|
||||||
if [[ $dbhost == "localhost" ]]; then
|
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
|
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
|
fi
|
||||||
|
|
||||||
if [ "$newdbname" == "$dbname" ]; then
|
if [ "$newdbname" == "$dbname" ]; then
|
||||||
|
@ -123,6 +124,9 @@ wpinstall() {
|
||||||
if [[ $dbreuse != y && $dbreuse != Y ]]; then
|
if [[ $dbreuse != y && $dbreuse != Y ]]; then
|
||||||
echo ""
|
echo ""
|
||||||
read -p "Please enter a new DB_NAME for your Database: " newdbname
|
read -p "Please enter a new DB_NAME for your Database: " newdbname
|
||||||
|
if [[ -z "$newdbname" ]]; then
|
||||||
|
newdbname="$dbname"
|
||||||
|
fi
|
||||||
dbname="$newdbname"
|
dbname="$newdbname"
|
||||||
elif [[ $dbreuse == y || $dbreuse == Y ]]; then
|
elif [[ $dbreuse == y || $dbreuse == Y ]]; then
|
||||||
# If you want to use the DB that already exist, abort DB creation.
|
# 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 [[ "$setupmysql" == y || "$setupmysql" == Y ]] ; then
|
||||||
if [[ $dbhost == "localhost" ]]; then
|
if [[ $dbhost == "localhost" ]]; then
|
||||||
local dbsetup="CREATE DATABASE $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@$dbhost IDENTIFIED BY '$dbpass';FLUSH PRIVILEGES;"
|
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
|
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};
|
CREATE DATABASE ${dbname};
|
||||||
grant usage on ${dbname}.* to ${dbuser}@\`%\` identified by '${dbpass}';
|
grant usage on ${dbname}.* to ${dbuser}@\`%\` identified by '${dbpass}';
|
||||||
grant all privileges on ${dbname}.* to ${dbuser}@\`%\`;
|
grant all privileges on ${dbname}.* to ${dbuser}@\`%\`;
|
||||||
|
@ -167,7 +171,7 @@ _EOF_
|
||||||
|
|
||||||
if [[ $done != "1" ]]; then
|
if [[ $done != "1" ]]; then
|
||||||
echo "${red} Some error ocurred during Database Configuration."
|
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
|
while read -r -n 1 -s done; do
|
||||||
done=${done:-y}
|
done=${done:-y}
|
||||||
if [[ $done = [YyNn] ]]; then
|
if [[ $done = [YyNn] ]]; then
|
||||||
|
@ -180,6 +184,7 @@ _EOF_
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
echo "${end}"
|
||||||
|
|
||||||
|
|
||||||
#WP-Config.php auto-setup
|
#WP-Config.php auto-setup
|
||||||
|
|
|
@ -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
|
if [[ -a $site/wp-config.php || -a $site/htdocs/wp-config.php ]]; then
|
||||||
domi=$(echo $site | cut -f 4 -d "/")
|
domi=$(echo $site | cut -f 4 -d "/")
|
||||||
db_delete $domi
|
db_delete $domi
|
||||||
echo "${gre}Database of your site ${red}${domi}${gre} has been successfully deleted! ${end}"
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue