| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052 |
- is_db_valid() {
- config="$V_USERS/$user/db.conf"
- check_db=$(grep "DB='$database'" $config)
- # Checking result
- if [ -z "$check_db" ]; then
- echo "Error: db not added"
- log_event 'debug' "$E_DB_NOTEXIST $V_EVENT"
- exit $E_DB_NOTEXIST
- fi
- }
- is_db_new() {
- # Parsing domain values
- check_db=$(grep -F "DB='$database'" $V_USERS/$user/db.conf)
- # Checking result
- if [ ! -z "$check_db" ]; then
- echo "Error: db exist"
- log_event 'debug' "$E_DB_EXIST $V_EVENT"
- exit $E_DB_EXIST
- fi
- }
- # Shell list for single database
- db_shell_single_list() {
- # Reading file line by line
- line=$(grep "DB='$database'" $conf)
- # Parsing key=value
- for key in $line; do
- eval ${key%%=*}=${key#*=}
- done
- # Print result line
- for field in $fields; do
- eval key="$field"
- echo "${field//$/}: $key "
- done
- }
- # Json single list
- db_json_single_list() {
- i=1
- # Define words number
- last_word=$(echo "$fields" | wc -w)
- # Reading file line by line
- line=$(grep "DB='$database'" $conf)
- # Print top bracket
- echo '{'
- # Parsing key=value
- for key in $line; do
- eval ${key%%=*}=${key#*=}
- done
- # Starting output loop
- for field in $fields; do
- # Parsing key=value
- eval value=$field
- # Checking first field
- if [ "$i" -eq 1 ]; then
- echo -e "\t\"$value\": {"
- else
- if [ "$last_word" -eq "$i" ]; then
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
- else
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
- fi
- fi
- # Updating iterator
- i=$(( i + 1))
- done
- # If there was any output
- if [ -n "$value" ]; then
- echo -e "\t}"
- fi
- # Printing bottom json bracket
- echo -e "}"
- }
- # Shell list for single database host
- dbhost_shell_single_list() {
- # Reading file line by line
- line=$(grep "HOST='$host'" $conf)
- # Parsing key=value
- for key in $line; do
- eval ${key%%=*}=${key#*=}
- done
- # Print result line
- for field in $fields; do
- eval key="$field"
- echo "${field//$/}: $key"
- done
- }
- # Json list for single db host
- dbhost_json_single_list() {
- # Definigng variables
- i=1 # iterator
- # Define words number
- last_word=$(echo "$fields" | wc -w)
- # Reading file line by line
- line=$(grep "HOST='$host'" $conf)
- # Print top bracket
- echo '{'
- # Parsing key=value
- for key in $line; do
- eval ${key%%=*}=${key#*=}
- done
- # Starting output loop
- for field in $fields; do
- # Parsing key=value
- eval value=$field
- # Checking first field
- if [ "$i" -eq 1 ]; then
- echo -e "\t\"$value\": {"
- else
- if [ "$last_word" -eq "$i" ]; then
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
- else
- echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
- fi
- fi
- # Updating iterator
- i=$(( i + 1))
- done
- # If there was any output
- if [ -n "$value" ]; then
- echo -e "\t}"
- fi
- # Printing bottom json bracket
- echo -e "}"
- }
- # Checking database host existance
- is_db_host_valid() {
- config="$V_DB/$type.conf"
- check_db=$(grep "HOST='$host'" $config)
- # Checking result
- if [ -z "$check_db" ]; then
- echo "Error: host not added"
- log_event 'debug' "$E_DBHOST_NOTEXIST $V_EVENT"
- exit $E_DBHOST_NOTEXIST
- fi
- }
- get_next_db_host() {
- # Defining vars
- config="$V_DB/$type.conf"
- host="empty"
- host_str=$(grep "ACTIVE='yes'" $config)
- # Checking rows count
- check_row=$(echo "$host_str"|wc -l)
- # Checking empty result
- if [ 0 -eq "$check_row" ]; then
- echo "$host"
- exit
- fi
- # Checking one host
- if [ 1 -eq "$check_row" ]; then
- for key in $host_str; do
- eval ${key%%=*}="${key#*=}"
- done
- users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
- if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ];then
- host=$HOST
- fi
- echo "$host"
- exit
- fi
- # Defining balancing function
- weight_balance() {
- ow='100' # old_weght
- IFS=$'\n'
- for db in $host_str; do
- for key in $(echo $db|sed -e "s/' /'\n/g"); do
- eval ${key%%=*}="${key#*=}"
- done
- weight=$(echo "$U_DB_BASES * 100 / $MAX_DB"|bc)
- users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
- if [ "$ow" -gt "$weight" ] && [ $MAX_USERS -gt "$users" ]; then
- host="$HOST"
- ow="$weight"
- fi
- done
- }
- # Defining random balancing function
- random_balance() {
- # Parsing host pool
- HOST_LIST=''
- IFS=$'\n'
- for db in $host_str; do
- for key in $(echo $db|sed -e "s/' /'\n/g"); do
- eval ${key%%=*}="${key#*=}"
- done
- users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
- if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ]
- then
- HOST_LIST="$HOST_LIST$HOST "
- fi
- done
- # Checking one host
- if [ 2 -eq $(echo -e "${HOST_LIST// /\n}"|wc -l) ]; then
- host="${HOST_LIST// /\n}"# should test with disabled host
- else
- # Selecting all hosts
- HOSTS=($(echo -e "${HOST_LIST// /\n}"))
- num=${#HOSTS[*]}
- host="${HOSTS[$((RANDOM%num))]}"
- fi
- }
- # Defining first balancing function
- first_balance() {
- # Parsing host pool
- IFS=$'\n'
- for db in $host_str; do
- for key in $(echo $db|sed -e "s/' /'\n/g"); do
- eval ${key%%=*}="${key#*=}"
- done
- users=$(echo -e "${U_SYS_USERS//,/\n}"|wc -l)
- if [ "$MAX_DB" -gt "$U_DB_BASES" ] && [ $MAX_USERS -gt "$users" ]
- then
- host="$HOST"
- break
- fi
- done
- }
- # Parsing domain values
- db_balance=$(grep "DB_BALANCE='" $V_CONF/vesta.conf|cut -f 2 -d \')
- case $db_balance in
- weight) weight_balance "$config" ;;
- random) random_balance "$config" ;;
- first) first_balance "$config" ;;
- *) random_balance "$config" ;;
- esac
- echo "$host"
- }
- increase_db_value() {
- # Defining vars
- conf="$V_DB/$type.conf"
- host_str=$(grep "HOST='$host'" $conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- # Increasing db_bases usage value
- U_DB_BASES=$((U_DB_BASES + 1))
- # Adding user to SYS_USERS pool
- if [ -z "$U_SYS_USERS" ]; then
- U_SYS_USERS="$user"
- else
- check_users=$(echo $U_SYS_USERS|sed -e "s/,/\n/g"|grep -w "$user")
- if [ -z "$check_users" ]; then
- U_SYS_USERS="$U_SYS_USERS,$user"
- fi
- fi
- # Concatenating db string
- case $type in
- mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
- new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
- new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
- new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
- new_str="$new_str DATE='$DATE'";;
- pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
- new_str="$new_str PORT='$PORT' TPL='$TPL'";
- new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
- new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
- new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
- new_str="$new_str DATE='$DATE'";;
- esac
- # Changing config
- sed -i "s/$host_str/$new_str/g" $conf
- }
- decrease_db_value() {
- # Defining vars
- conf="$V_DB/$type.conf"
- host_str=$(grep "HOST='$host'" $conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- # Decreasing db_bases usage value
- U_DB_BASES=$((U_DB_BASES - 1))
- # Checking user databases on that host
- udb=$(grep "TYPE='$type'" $V_USERS/$user/db.conf|grep "HOST='$host'"|wc -l)
- if [ 2 -gt "$udb" ]; then
- U_SYS_USERS=$(echo "$U_SYS_USERS" | sed -e "s/,/\n/g" |\
- sed -e "/^$user$/d" | sed -e :a -e '$!N;s/\n/,/;ta')
- fi
- # Concatenating db string
- case $type in
- mysql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
- new_str="$new_str PORT='$PORT' MAX_USERS='$MAX_USERS'";
- new_str="$new_str MAX_DB='$MAX_DB' U_SYS_USERS='$U_SYS_USERS'";
- new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
- new_str="$new_str DATE='$DATE'";;
- pgsql) new_str="HOST='$HOST' USER='$USER' PASSWORD='$PASSWORD'";
- new_str="$new_str PORT='$PORT' TPL='$TPL'";
- new_str="$new_str MAX_USERS='$MAX_USERS' MAX_DB='$MAX_DB'";
- new_str="$new_str U_SYS_USERS='$U_SYS_USERS'";
- new_str="$new_str U_DB_BASES='$U_DB_BASES' ACTIVE='$ACTIVE'";
- new_str="$new_str DATE='$DATE'";;
- esac
- # Changing config
- sed -i "s/$host_str/$new_str/g" $conf
- }
- create_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Adding database & checking result
- $sql "CREATE DATABASE $database CHARACTER SET $encoding" > /dev/null 2>&1
- code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Adding user with password (% will give access to db from any ip)
- $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
- IDENTIFIED BY '$db_password'"
- # Adding grant for localhost (% doesn't do that )
- if [ "$host" = 'localhost' ]; then
- $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
- IDENTIFIED BY '$db_password'"
- fi
- # Flushing priveleges
- $sql "FLUSH PRIVILEGES"
- }
- create_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Adding new role
- $sql "CREATE ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
- code=$?
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Adding database & checking result
- sql_q="CREATE DATABASE $database OWNER $db_user" > /dev/null
- if [ "$TPL" = 'template0' ]; then
- sql_q="$sql_q ENCODING '$encoding' TEMPLATE $TPL" > /dev/null
- else
- sql_q="$sql_q TEMPLATE $TPL" > /dev/null
- fi
- $sql "$sql_q" >/dev/null
- $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" > /dev/null
- $sql "GRANT CONNECT ON DATABASE template1 to $db_user" > /dev/null
- export PGPASSWORD='pgsql'
- }
- is_db_host_new() {
- if [ -e "$V_DB/$type.conf" ]; then
- check_host=$(grep "HOST='$host'" $V_DB/$type.conf)
- if [ ! -z "$check_host" ]; then
- echo "Error: db host exist"
- log_event 'debug' "$E_DBHOST_EXIST $V_EVENT"
- exit $E_DBHOST_EXIST
- fi
- fi
- }
- is_mysql_host_alive() {
- # Checking connection
- sql="mysql -h $host -u $db_user -p$db_password -P$port -e"
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- }
- is_pgsql_host_alive() {
- # Checking connection
- export PGPASSWORD="$db_password"
- sql="psql -h $host -U $db_user -p $port -c "
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- }
- is_db_suspended() {
- config="$V_USERS/$user/db.conf"
- check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
- # Checking result
- if [ ! -z "$check_db" ]; then
- echo "Error: db suspended"
- log_event 'debug' "$E_DB_SUSPENDED $V_EVENT"
- exit $E_DB_SUSPENDED
- fi
- }
- is_db_unsuspended() {
- config="$V_USERS/$user/db.conf"
- check_db=$(grep "DB='$database'" $config|grep "SUSPEND='yes'")
- # Checking result
- if [ -z "$check_db" ]; then
- echo "Error: db unsuspended"
- log_event 'debug' "$E_DB_UNSUSPENDED $V_EVENT"
- exit $E_DB_UNSUSPENDED
- fi
- }
- is_db_user_valid() {
- config="$V_USERS/$user/db.conf"
- check_db=$(grep "DB='$database'" $config|grep "USER='$db_user'")
- # Checking result
- if [ -z "$check_db" ]; then
- echo "Error: dbuser not exist"
- log_event 'debug' "$E_DBUSER_NOTEXIST $V_EVENT"
- exit $E_DBUSER_NOTEXIST
- fi
- }
- change_db_mysql_password() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Changing user password
- $sql "GRANT ALL ON $database.* TO '$db_user'@'%' \
- IDENTIFIED BY '$db_password'"
- $sql "GRANT ALL ON $database.* TO '$db_user'@'localhost' \
- IDENTIFIED BY '$db_password'"
- #$sql "SET PASSWORD FOR '$db_user'@'%' = PASSWORD('$db_password');"
- $sql "FLUSH PRIVILEGES"
- }
- change_db_pgsql_password() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- $sql "ALTER ROLE $db_user WITH LOGIN PASSWORD '$db_password'" >/dev/null
- export PGPASSWORD='pgsql'
- }
- get_db_value() {
- # Defining vars
- key="$1"
- db_str=$(grep "DB='$database'" $V_USERS/$user/db.conf)
- # Parsing key=value
- for keys in $db_str; do
- eval ${keys%%=*}=${keys#*=}
- done
- # Self reference
- eval value="$key"
- # Print value
- echo "$value"
- }
- del_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Deleting database & checking result
- $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Deleting user
- check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
- if [ 1 -ge "$check_users" ]; then
- $sql "DROP USER '$db_user'@'%'"
- if [ "$host" = 'localhost' ]; then
- $sql "DROP USER '$db_user'@'localhost'"
- fi
- else
- $sql "REVOKE ALL ON $database.* from '$db_user'@'%'"
- if [ "$host" = 'localhost' ]; then
- $sql "REVOKE ALL ON $database.* from '$db_user'@'localhost'"
- fi
- fi
- $sql "FLUSH PRIVILEGES"
- }
- del_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Deleting database & checking result
- $sql "REVOKE ALL PRIVILEGES ON DATABASE $database FROM $db_user">/dev/null
- $sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Deleting user
- check_users=$(grep "USER='$db_user'" $V_USERS/$user/db.conf |wc -l)
- if [ 1 -ge "$check_users" ]; then
- $sql "REVOKE CONNECT ON DATABASE template1 FROM $db_user" >/dev/null
- $sql "DROP ROLE $db_user" >/dev/null
- fi
- export PGPASSWORD='pgsql'
- }
- del_db_vesta() {
- conf="$V_USERS/$user/db.conf"
- # Parsing domains
- string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : )
- if [ -z "$string" ]; then
- echo "Error: parse error"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- sed -i "$string d" $conf
- }
- dump_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- dumper="mysqldump -h $HOST -u $USER -p$PASSWORD -P$PORT -r"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Dumping database
- $dumper $dump $database
- # Dumping user grants
- $sql "SHOW GRANTS FOR $db_user@localhost" | grep -v "Grants for" > $grants
- $sql "SHOW GRANTS FOR $db_user@'%'" | grep -v "Grants for" >> $grants
- }
- dump_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- dumper="pg_dump -h $HOST -U $USER -p $PORT -c -d -O -x -i -f"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Dumping database
- $dumper $dump $database
- # Dumping user grants
- md5=$($sql "SELECT rolpassword FROM pg_authid WHERE rolname='$db_user';")
- md5=$(echo "$md5" | head -n 1 | cut -f 2 -d ' ')
- pw_str="UPDATE pg_authid SET rolpassword='$md5' WHERE rolname='$db_user';"
- gr_str="GRANT ALL PRIVILEGES ON DATABASE $database to '$db_user'"
- echo -e "$pw_str\n$gr_str" >> $grants
- export PGPASSWORD='pgsql'
- }
- is_db_host_free() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/$type.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- # Checking U_DB_BASES
- if [ 0 -ne "$U_DB_BASES" ]; then
- echo "Error: host is used"
- log_event 'debug' "$E_DBHOST_BUSY $V_EVENT"
- exit $E_DBHOST_BUSY
- fi
- }
- del_dbhost_vesta() {
- conf="$V_DB/$type.conf"
- # Parsing domains
- string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
- if [ -z "$string" ]; then
- echo "Error: parse error"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- sed -i "$string d" $conf
- }
- update_db_base_value() {
- key="$1"
- value="$2"
- # Defining conf
- conf="$V_USERS/$user/db.conf"
- # Parsing conf
- db_str=$(grep -n "DB='$database'" $conf)
- str_number=$(echo $db_str | cut -f 1 -d ':')
- str=$(echo $db_str | cut -f 2 -d ':')
- # Reading key=values
- for keys in $str; do
- eval ${keys%%=*}=${keys#*=}
- done
- # Defining clean key
- c_key=$(echo "${key//$/}")
- eval old="${key}"
- # Escaping slashes
- old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
- new=$(echo "$value" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
- # Updating conf
- sed -i "$str_number s/$c_key='${old//\*/\\*}'/$c_key='${new//\*/\\*}'/g"\
- $conf
- }
- suspend_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Suspending user
- $sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
- $sql "FLUSH PRIVILEGES"
- }
- suspend_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Suspending user
- $sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
- export PGPASSWORD='pgsql'
- }
- unsuspend_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Unsuspending user
- $sql "GRANT ALL ON $database.* to '$db_user'@'%'"
- $sql "FLUSH PRIVILEGES"
- }
- unsuspend_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Unsuspending user
- $sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
- export PGPASSWORD='pgsql'
- }
- db_clear_search() {
- # Defining delimeter
- IFS=$'\n'
- # Reading file line by line
- for line in $(grep $search_string $conf); do
- # Parsing key=val
- for key in $line; do
- eval ${key%%=*}=${key#*=}
- done
- # Print result line
- eval echo "$field"
- done
- }
- get_disk_db_mysql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/mysql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- sql="mysql -h $HOST -u $USER -p$PASSWORD -P$PORT -e"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Deleting database & checking result
- query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\"
- FROM information_schema.TABLES WHERE table_schema='$database'"
- raw_size=$($sql "$query" |tail -n 1)
- # Checking null output (this means error btw)
- if [ "$raw_size" == 'NULL' ]; then
- raw_size='0'
- fi
- # Rounding zero size
- if [ "${raw_size:0:1}" -eq '0' ]; then
- raw_size='1'
- fi
- # Printing round size in mb
- printf "%0.f\n" $raw_size
- }
- get_disk_db_pgsql() {
- # Defining vars
- host_str=$(grep "HOST='$host'" $V_DB/pgsql.conf)
- for key in $host_str; do
- eval ${key%%=*}=${key#*=}
- done
- export PGPASSWORD="$PASSWORD"
- sql="psql -h $HOST -U $USER -p $PORT -c"
- # Checking empty vars
- if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
- echo "Error: config is broken"
- log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
- exit $E_PARSE_ERROR
- fi
- # Checking connection
- $sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
- if [ '0' -ne "$code" ]; then
- echo "Error: Connect failed"
- log_event 'debug' "$E_DBHOST_UNAVAILABLE $V_EVENT"
- exit $E_DBHOST_UNAVAILABLE
- fi
- # Raw query
- raq_query=$($sql "SELECT pg_database_size('$database');")
- raw_size=$(echo "$raq_query" | grep -v "-" | grep -v 'row' |\
- sed -e "/^$/d" |grep -v "pg_database_size" | awk '{print $1}')
- # Checking null output (this means error btw)
- if [ -z "$raw_size" ]; then
- raw_size='0'
- fi
- # Converting to MB
- size=$(expr $raw_size / 1048576)
- # Rounding zero size
- if [ "$size" -eq '0' ]; then
- echo '1'
- else
- echo "$size"
- fi
- }
|