webinoly/lib/general
Cristhian Martínez Ochoa c3365e5bd3 db-delete-all fixed
Infinite loop caused by background process
2018-11-02 11:40:12 -07:00

197 lines
6.2 KiB
Bash

#!/bin/bash
# echo colors
red=`tput setaf 1`
gre=`tput setaf 2`
blu=`tput setaf 6`
end=`tput sgr0`
db_delete() {
local domain="$1"
# Get dbname and dbuser of a WP site
if [[ -a /var/www/$domain/wp-config.php ]]; then
wpconfpath="/var/www/$domain/wp-config.php"
elif [[ -a /var/www/$domain/htdocs/wp-config.php ]]; then
wpconfpath="/var/www/$domain/htdocs/wp-config.php"
else
return
fi
local name=$( grep -F "DB_NAME" $wpconfpath | cut -f 4 -d "'" )
local user=$( grep -F "DB_USER" $wpconfpath | cut -f 4 -d "'" )
local host=$( grep -F "DB_HOST" $wpconfpath | 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" || $(conf_read mysql-client) != "true" ]]; then
echo "${red}[ERROR] Seems like MySQL is not installed or Webinoly can not detect it!"
echo " - Database can not be deleted -${blu} $domain ${end}"
return
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"
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}
[[ $answer = [YyNn] ]] && break
done
[[ $answer == [Nn] ]] && done="1"
else
echo "${gre}Database of your site ${blu}${domain}${gre} has been successfully deleted! ${end}"
fi
else
if [[ $dbdatadel == "true" ]]; then
local uroot=$(echo "${dbdata}" | cut -d',' -f 3 -s)
local proot=$(echo "${dbdata}" | cut -d',' -f 4 -s)
fi
if [[ -z $uroot || -z $proot ]]; then
echo ""
echo "${gre}External DB found in $domain (Press 'Enter' key twice to skip and not delete)"
read -p "${blu}External DB root username [root]: " uroot
uroot=${uroot:-root}
read -p "External DB root password: " proot
fi
if [[ $uroot == "root" && -z $proot ]]; then
echo "${red} Action aborted, database not deleted!"
continue 1;
else
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
sudo mysql --connect-timeout=10 -h "$url" -P "$port" -u"$uroot" -p"$proot" -e "$dbsetup"
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}
[[ $answer = [YyNn] ]] && break
done
[[ $answer == [Nn] ]] && done="1"
else
echo "${gre}Database of your site ${blu}${domain}${gre} has been successfully deleted! ${end}"
fi
fi
echo "${end}"
fi
done
}
check_osname() {
local distr=`echo $(lsb_release -i | cut -d':' -f 2)`
local osver=`echo $(lsb_release -c | cut -d':' -f 2)`
if ! [[ $distr == "Ubuntu" && $osver =~ ^(trusty|xenial|zesty|artful|bionic)$ ]]; then
echo "${red}" >&2
echo "****************************************************************************" >&2
echo "**** This OS is not supported by Webinoly and could not work properly ****" >&2
echo "****************************************************************************" >&2
echo "${end}" >&2
fi
echo $osver
}
pre_install() {
sudo apt -qq update
if [[ $(conf_read pre-packs) != true ]]; then
# Check for missing essential packages
api-events_update i1
sudo apt -y -qq install dialog
sudo apt -y -qq install software-properties-common
[[ $(check_osname) != "bionic" ]] && sudo apt -y -qq install python-software-properties
sudo apt -y -qq install pwgen
sudo apt -y -qq install unzip
sudo apt -y -qq install zip
conf_write pre-packs true
api-events_update i2
fi
}
mysql_client_install() {
api-events_update im2
local osname=$(check_osname)
# Cases with MariaDB v10.2 support
case "$osname" in
"trusty")
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository "deb [arch=amd64,i386,ppc64el] http://mirrors.syringanetworks.net/mariadb/repo/10.2/ubuntu $osname main"
;;
"xenial"|"zesty"|"artful"|"bionic")
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
sudo add-apt-repository "deb [arch=amd64,i386,ppc64el] http://mirrors.syringanetworks.net/mariadb/repo/10.2/ubuntu $osname main"
;;
esac
pre_install
sudo apt -y install mariadb-client
conf_write mysql-client true
echo "${gre}MySQL Client has been successfully installed!${end}"
api-events_update im3
}
tools_port() {
local in="$1"
while ! [[ $port -ge 0 && $port -le 65535 && $port =~ ^[0-9]+$ ]] 2>/dev/null
do
if [[ -z "$in" || $in == "empty" ]]; then
read -p "${blu}Tools Port [Default: 22222]: " port
port=${port:-22222}
else
port="$in"
in="empty"
fi
done
conf_write tools-port $port
echo "${gre} Port $port has been enabled to access all your Tools! ${end}"
}
api-events_update() {
conf_write status-api $1
if [[ -a /opt/webinoly/lib/api-events ]]; then
source /opt/webinoly/lib/api-events
api-events_catch_status $1
fi
}
conf_read() {
local val=$(grep -w "^${1}:.*" /opt/webinoly/webinoly.conf | cut -f 2 -d ':')
echo $val
}
conf_write() {
[[ ! -a /opt/webinoly/webinoly.conf ]] && sudo touch /opt/webinoly/webinoly.conf
#if requested VAR exists overwrite it, if not, create it.
sed -i "/^${1}:/d" /opt/webinoly/webinoly.conf
sh -c "echo -n '$1:$2\n' >> /opt/webinoly/webinoly.conf"
}
conf_delete() {
sed -i "/^${1}:/d" /opt/webinoly/webinoly.conf
}