Refactoring stage III (db)

This commit is contained in:
Serghey Rodin 2012-03-27 23:08:44 +03:00
parent 4fa3013716
commit 33717629f7
21 changed files with 518 additions and 913 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# info: add database # info: add database
# options: user db db_user db_password type [host] [encoding] # options: user database dbuser dbpass type [host] [charset]
# #
# The function creates the database concatenating username and user_db. # The function creates the database concatenating username and user_db.
# Supported yypes of databases you can get using v_list_sys_config script. # Supported yypes of databases you can get using v_list_sys_config script.
@ -22,8 +22,8 @@ dbuser="$user"_"$3"
dbpass=$4 dbpass=$4
type=$5 type=$5
host=$6 host=$6
encoding=${7-UTF8} charset=${7-UTF8}
encoding=$(echo "$encoding" |tr '[:lower:]' '[:upper:]') charset=$(echo "$charset" |tr '[:lower:]' '[:upper:]')
# Includes # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
@ -35,8 +35,8 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
check_args '5' "$#" 'user db dbuser dbpass type [host] [encoding]' check_args '5' "$#" 'user database dbuser dbpass type [host] [charset]'
validate_format 'user' 'database' 'dbuser' 'dbpass' 'encoding' validate_format 'user' 'database' 'dbuser' 'dbpass' 'charset'
is_system_enabled "$DB_SYSTEM" is_system_enabled "$DB_SYSTEM"
is_type_valid "$DB_SYSTEM" "$type" is_type_valid "$DB_SYSTEM" "$type"
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
@ -44,7 +44,8 @@ is_object_unsuspended 'user' 'USER' "$user"
is_object_free 'db' 'DB' "$database" is_object_free 'db' 'DB' "$database"
get_next_dbhost get_next_dbhost
is_object_valid "../../../conf/$type" 'HOST' "$host" is_object_valid "../../../conf/$type" 'HOST' "$host"
is_db_encoding_valid is_object_unsuspended "../../../conf/$type" 'HOST' "$host"
is_charset_valid
is_package_full 'DATABASES' is_package_full 'DATABASES'
#----------------------------------------------------------# #----------------------------------------------------------#
@ -53,8 +54,8 @@ is_package_full 'DATABASES'
# Switching on db type # Switching on db type
case $type in case $type in
mysql) create_db_mysql ;; mysql) add_mysql_database ;;
pgsql) create_db_pgsql ;; pgsql) add_pgsql_database ;;
esac esac
@ -67,15 +68,15 @@ increase_dbhost_values
increase_user_value "$user" '$U_DATABASES' increase_user_value "$user" '$U_DATABASES'
# Adding db to db conf # Adding db to db conf
v_str="DB='$database' USER='$dbuser' HOST='$host' TYPE='$type'" str="DB='$database' DBUSER='$dbuser' HOST='$host' TYPE='$type'"
v_str="$v_str CHARSET='$encoding' U_DISK='0' SUSPENDED='no' TIME='$TIME'" str="$str CHARSET='$charset' U_DISK='0' SUSPENDED='no' TIME='$TIME'"
v_str="$v_str DATE='$DATE'" str="$str DATE='$DATE'"
echo "$v_str" >> $USER_DATA/db.conf echo "$str" >> $USER_DATA/db.conf
chmod 660 $USER_DATA/db.conf chmod 660 $USER_DATA/db.conf
# Hiding password # Hiding password
EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT'" EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT'"
EVENT="$EVENT ARGUMENTS='$user $database $dbuser ***** $type $host'" EVENT="$EVENT ARGUMENTS='$user $database $dbuser ***** $type $host $charset'"
# Logging # Logging
log_history "$EVENT" log_history "$EVENT"

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# info: add new database server # info: add new database server
# options: type host port db_user db_password [max_db] [tpl] # options: type host port dbuser dbpass [max_db] [charsets] [template]
# #
# The function add new database server to the server pool. It supports local # The function add new database server to the server pool. It supports local
# and remote database servers, which is useful for clusters. By adding a host # and remote database servers, which is useful for clusters. By adding a host
@ -17,13 +17,13 @@
type=$1 type=$1
host=$2 host=$2
port=$3 port=$3
db_user=$4 dbuser=$4
db_password=$5 dbpass=$5
max_db=${6-300} max_db=${6-300}
template=${7-template1} charsets=${7-UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8}
template=${8-template1}
# Includes
# Importing variables
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -33,24 +33,12 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number args_usage='type host port dbuser dbpass [max_db] [charsets] [tpl]'
args_usage='type host port db_user db_password [max_db] [tpl]'
check_args '5' "$#" "$args_usage" check_args '5' "$#" "$args_usage"
validate_format 'host' 'port' 'dbuser' 'dbpass' 'max_db' 'charsets' 'template'
# Checking argument format is_system_enabled "$DB_SYSTEM"
validate_format 'host' 'port' 'db_user' 'db_password' 'max_db'
validate_format 'template'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking db type
is_type_valid "$DB_SYSTEM" "$type" is_type_valid "$DB_SYSTEM" "$type"
is_dbhost_new
# Checking host existance
is_db_host_new
# Checking host connection
case $type in case $type in
mysql) is_mysql_host_alive ;; mysql) is_mysql_host_alive ;;
pgsql) is_pgsql_host_alive ;; pgsql) is_pgsql_host_alive ;;
@ -63,17 +51,17 @@ esac
# Concatentating db host string # Concatentating db host string
case $type in case $type in
mysql) new_str="HOST='$host' USER='$db_user' PASSWORD='$db_password'"; mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass' PORT='$port'";
new_str="$new_str PORT='$port' MAX_DB='$max_db' U_SYS_USERS=''"; str="$str CHARSETS='$charsets' MAX_DB='$max_db' U_SYS_USERS='' ";
new_str="$new_str U_DB_BASES='0' ACTIVE='yes' DATE='$DATE'";; str="$str U_DB_BASES='0' SUSPENDED='no' TIME='$TIME' DATE='$DATE'";;
pgsql) new_str="HOST='$host' USER='$db_user' PASSWORD='$db_password'"; pgsql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass' PORT='$port'";
new_str="$new_str PORT='$port' TPL='$template'"; str="$str CHARSETS='$charsets' TPL='$template' MAX_DB='$max_db'";
new_str="$new_str MAX_DB='$max_db' U_SYS_USERS=''"; str="$str U_SYS_USERS='' U_DB_BASES='0' SUSPENDED='no'";
new_str="$new_str U_DB_BASES='0' ACTIVE='yes' DATE='$DATE'";; str="$str TIME='$TIME' DATE='$DATE'";;
esac esac
# Adding host to conf # Adding host to conf
echo "$new_str" >> $VESTA/conf/$type.conf echo "$str" >> $VESTA/conf/$type.conf
chmod 660 $VESTA/conf/$type.conf chmod 660 $VESTA/conf/$type.conf
@ -81,8 +69,10 @@ chmod 660 $VESTA/conf/$type.conf
# Vesta # # Vesta #
#----------------------------------------------------------# #----------------------------------------------------------#
# Hidding db pass # Hiding password
EVENT=$(echo $EVENT | sed -e "s/$db_password/xxxxxx/g") EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT'"
EVENT="$EVENT ARGUMENTS='$type $host $port $dbuser ****** $max_db $charsets"
EVENT="$EVENT $tpl'"
# Logging # Logging
log_event "$OK" "$EVENT" log_event "$OK" "$EVENT"

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# info: change database user password # info: change database password
# options: user db_name db_password # options: user database dbpass
# #
# The function for changing database user password to a database. It uses the # The function for changing database user password to a database. It uses the
# full name of database as argument. # full name of database as argument.
@ -13,9 +13,9 @@
# Argument defenition # Argument defenition
user=$1 user=$1
database=$2 database=$2
db_password=$3 dbpass=$3
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -25,41 +25,24 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '3' "$#" 'user database dbpass'
check_args '3' "$#" 'user db_name db_password' validate_format 'user' 'database' 'dbpass'
is_system_enabled "$DB_SYSTEM"
# Checking argument format
validate_format 'user' 'database' 'db_password'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
# Checking user is active
is_object_unsuspended 'user' 'USER' "$user" is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
# Checking db existance is_object_unsuspended 'db' 'DB' "$database"
is_db_valid
# Checking db is active
is_db_suspended
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Get database values
get_database_values
# Define database variables case $TYPE in
db_user=$(get_db_value '$USER') mysql) change_mysql_password ;;
host=$(get_db_value '$HOST') pgsql) change_pgsql_password ;;
type=$(get_db_value '$TYPE')
# Switching on db type
case $type in
mysql) change_db_mysql_password ;;
pgsql) change_db_pgsql_password ;;
esac esac
@ -68,7 +51,8 @@ esac
#----------------------------------------------------------# #----------------------------------------------------------#
# Hiding password # Hiding password
EVENT="$DATE $SCRIPT $user $database *****" EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT'"
EVENT="$EVENT ARGUMENTS='$user $database *****'"
# Logging # Logging
log_event "$OK" "$EVENT" log_event "$OK" "$EVENT"

View file

@ -14,7 +14,7 @@
user=$1 user=$1
database=$2 database=$2
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -24,41 +24,26 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '2' "$#" 'user database'
check_args '2' "$#" 'user db_name'
# Checking argument format
validate_format 'user' 'database' validate_format 'user' 'database'
is_system_enabled "$DB_SYSTEM"
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
# Checking user is active
is_object_unsuspended 'user' 'USER' "$user" is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
# Checking db existance is_object_unsuspended 'db' 'DB' "$database"
is_db_valid
# Checking db is active
is_db_suspended
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Get some variables we do not have now # Get database values
db_user=$(get_db_value '$USER') get_database_values
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Switching on db type # Switching on db type
case $type in case $TYPE in
mysql) del_db_mysql ;; mysql) delete_mysql_database ;;
pgsql) del_db_pgsql ;; pgsql) delete_pgsql_database ;;
esac esac
@ -66,15 +51,13 @@ esac
# Vesta # # Vesta #
#----------------------------------------------------------# #----------------------------------------------------------#
# Decreasing db value # Deleting database
decrease_db_value sed -i "/DB='$database' /d" $USER_DATA/db.conf
# Decreasing domain value # Decreasing counters
decrease_dbhost_values
decrease_user_value "$user" '$U_DATABASES' decrease_user_value "$user" '$U_DATABASES'
# Deleting vesta db record
del_db_vesta
# Logging # Logging
log_event "$OK" "$EVENT" log_event "$OK" "$EVENT"

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# info: delete database serve # info: delete database server
# options: type host # options: type host
# #
# The function for deleting the database host from vesta configuration. It will # The function for deleting the database host from vesta configuration. It will
@ -24,31 +24,19 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '2' "$#" "type host"
check_args '2' "$#" 'type host' validate_format 'type' 'host'
is_system_enabled "$DB_SYSTEM"
# Checking argument format
validate_format 'host'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking db type
is_type_valid "$DB_SYSTEM" "$type" is_type_valid "$DB_SYSTEM" "$type"
is_object_valid "../../conf/$type" 'HOST' "$host"
# Checking host existance is_dbhost_free
is_db_host_valid
# Checking db host users
is_db_host_free
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Deleting server
# Deleting host from conf sed -i "/HOST='$host' /d" $VESTA/conf/$type.conf
del_dbhost_vesta
#----------------------------------------------------------# #----------------------------------------------------------#

View file

@ -12,53 +12,29 @@
# Argument defenition # Argument defenition
user=$1 user=$1
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh
#----------------------------------------------------------# #----------------------------------------------------------#
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number
check_args '1' "$#" 'user' check_args '1' "$#" 'user'
# Checking argument format
validate_format 'user' validate_format 'user'
is_system_enabled "$DB_SYSTEM"
# Checking web system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config
conf="$USER_DATA/db.conf"
# Defining fileds to select
field='$DB'
# Defining search string
search_string="SUSPENDED='no'"
# Parsing unsuspeneded domains
databases=$(db_clear_search)
# Starting delete loop # Starting delete loop
for database in $databases; do for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
$BIN/v_delete_db_base "$user" "$database" $BIN/v_delete_database "$user" "$database"
rv="$?"
if [ "$rv" -ne '0' ]; then
log_event 'debug' "$rv $EVENT"
exit $rv
fi
done done

View file

@ -14,33 +14,18 @@ user=$1
database=$2 database=$2
format=${3-shell} format=${3-shell}
# Importing variables # Includes
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh
# Json function # Json function
json_list_db() { json_list_db() {
i=1 i=1
# Define words number
last_word=$(echo "$fields" | wc -w) last_word=$(echo "$fields" | wc -w)
# Reading file line by line
line=$(grep "DB='$database'" $conf) line=$(grep "DB='$database'" $conf)
# Print top bracket
echo '{' echo '{'
# Parsing key=value
eval $line eval $line
# Starting output loop
for field in $fields; do for field in $fields; do
# Parsing key=value
eval value=$field eval value=$field
# Checking first field
if [ "$i" -eq 1 ]; then if [ "$i" -eq 1 ]; then
echo -e "\t\"$value\": {" echo -e "\t\"$value\": {"
else else
@ -50,30 +35,18 @@ json_list_db() {
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\"," echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
fi fi
fi fi
# Updating iterator
(( ++i)) (( ++i))
done done
# If there was any output
if [ -n "$value" ]; then if [ -n "$value" ]; then
echo -e "\t}" echo -e "\t}"
fi fi
# Printing bottom json bracket
echo -e '}' echo -e '}'
} }
# Shell list for single database # Shell list for single database
shell_list_db() { shell_list_db() {
# Reading file line by line
line=$(grep "DB='$database'" $conf) line=$(grep "DB='$database'" $conf)
# Parsing key=value
eval $line eval $line
# Print result line
for field in $fields; do for field in $fields; do
eval key="$field" eval key="$field"
echo "${field//$/}: $key " echo "${field//$/}: $key "
@ -85,28 +58,20 @@ shell_list_db() {
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking args check_args '2' "$#" 'user database [format]'
check_args '2' "$#" 'user db_name [format]'
# Checking argument format
validate_format 'user' 'database' validate_format 'user' 'database'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
# Checking database exist
is_db_valid
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config
conf=$USER_DATA/db.conf
# Defining fileds to select # Defining fileds to select
fields='$DB $USER $HOST $TYPE $CHARSET $U_DISK $SUSPENDED $DATE' conf=$USER_DATA/db.conf
fields='$DB $DBUSER $HOST $TYPE $CHARSET $U_DISK $SUSPENDED $TIME $DATE'
# Listing database # Listing database
case $format in case $format in

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
# info: list database host # info: list database server
# options: type host [format] # options: type host [format]
# #
# The function for obtaining host's database parameters. # The function for obtaining database server parameters.
#----------------------------------------------------------# #----------------------------------------------------------#
@ -14,34 +14,18 @@ type=$1
host=$2 host=$2
format=${3-shell} format=${3-shell}
# Importing variables # Includes
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh
# Json function # Json function
json_list_dbhost() { json_list_dbhost() {
# Definigng variables
i=1 i=1
# Define words number
fields_count=$(echo "$fields" | wc -w) fields_count=$(echo "$fields" | wc -w)
# Reading file line by line
line=$(grep "HOST='$host'" $conf) line=$(grep "HOST='$host'" $conf)
# Print top bracket
echo '{' echo '{'
# Assign key=value
eval $line eval $line
# Starting output loop
for field in $fields; do for field in $fields; do
# Parsing key=value
eval value=$field eval value=$field
# Checking first field
if [ "$i" -eq 1 ]; then if [ "$i" -eq 1 ]; then
echo -e "\t\"$value\": {" echo -e "\t\"$value\": {"
else else
@ -53,25 +37,16 @@ json_list_dbhost() {
fi fi
(( ++i)) (( ++i))
done done
# If there was any output
if [ -n "$value" ]; then if [ -n "$value" ]; then
echo -e "\t}" echo -e "\t}"
fi fi
# Printing bottom json bracket
echo -e "}" echo -e "}"
} }
# Shell function # Shell function
shell_list_dbhost() { shell_list_dbhost() {
# Reading file line by line
line=$(grep "HOST='$host'" $conf) line=$(grep "HOST='$host'" $conf)
# Parsing key=value
eval $line eval $line
# Print result line
for field in $fields; do for field in $fields; do
eval key="$field" eval key="$field"
echo "${field//$/}: $key" echo "${field//$/}: $key"
@ -83,28 +58,19 @@ shell_list_dbhost() {
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking args
check_args '2' "$#" 'type host [format]' check_args '2' "$#" 'type host [format]'
# Checking argument format
validate_format 'host' validate_format 'host'
is_object_valid "../../conf/$type" 'HOST' "$host"
# Checking db type
is_type_valid "$DB_SYSTEM" "$type"
# Checking db host
is_db_host_valid
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config type
conf=$VESTA/conf/$type.conf
# Defining fileds to select # Defining fileds to select
fields='$HOST $PORT $MAX_DB $U_SYS_USERS $U_DB_BASES $ACTIVE $DATE' conf=$VESTA/conf/$type.conf
fields='$HOST $PORT $CHARSETS $MAX_DB $U_SYS_USERS $U_DB_BASES $TPL $SUSPENDED'
fields="$fields \$TIME \$DATE"
# Listing database # Listing database
case $format in case $format in

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# info: list data base servers # info: list database servers
# options: type [format] # options: type [format]
# #
# The function for obtaining the list of all hosts of the same databases' type. # The function for obtaining the list of all hosts of the same databases' type.
@ -13,7 +13,7 @@
type=$1 type=$1
format=${2-shell} format=${2-shell}
# Importing variables # Includes
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
@ -24,25 +24,21 @@ source $VESTA/func/shared.sh
# Checking args # Checking args
check_args '1' "$#" 'type [format]' check_args '1' "$#" 'type [format]'
# Checking db type
is_type_valid "$DB_SYSTEM" "$type"
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config type
conf=$VESTA/conf/$type.conf
# Defining fileds to select # Defining fileds to select
fields='$HOST $PORT $MAX_DB $U_SYS_USERS $U_DB_BASES $ACTIVE $DATE' conf=$VESTA/conf/$type.conf
fields='$HOST $PORT $CHARSETS $MAX_DB $U_SYS_USERS $U_DB_BASES $TPL $SUSPENDED'
fields="$fields \$TIME \$DATE"
# Listing database # Listing database
case $format in case $format in
json) json_list ;; json) json_list ;;
plain) nohead=1; shell_list;; plain) nohead=1; shell_list;;
shell) fields='$HOST $PORT $MAX_DB $U_DB_BASES $ACTIVE $DATE'; shell) fields='$HOST $PORT $MAX_DB $U_DB_BASES $SUSPENDED $DATE';
shell_list | column -t ;; shell_list | column -t ;;
*) check_args '2' '0' 'type [format]' *) check_args '2' '0' 'type [format]'
esac esac

View file

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# info: listing data bases # info: listing databases
# options: user [format] # options: user [format]
# #
# The function for obtaining the list of all user's databases. # The function for obtaining the list of all user's databases.
@ -13,7 +13,7 @@
user=$1 user=$1
format=${2-shell} format=${2-shell}
# Importing variables # Includes
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
@ -21,13 +21,8 @@ source $VESTA/func/shared.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking args
check_args '1' "$#" 'user [format]' check_args '1' "$#" 'user [format]'
# Checking argument format
validate_format 'user' validate_format 'user'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
@ -35,17 +30,15 @@ is_object_valid 'user' 'USER' "$user"
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config
conf=$USER_DATA/db.conf
# Defining fileds to select # Defining fileds to select
fields='$DB $USER $HOST $TYPE $CHARSET $U_DISK $SUSPENDED $DATE' conf=$USER_DATA/db.conf
fields='$DB $DBUSER $HOST $TYPE $CHARSET $U_DISK $SUSPENDED $TIME $DATE'
# Listing databases # Listing databases
case $format in case $format in
json) json_list ;; json) json_list ;;
plain) nohead=1; shell_list ;; plain) nohead=1; shell_list ;;
shell) fields='$DB $USER $HOST $TYPE $U_DISK $DATE'; shell) fields='$DB $DBUSER $HOST $TYPE $U_DISK $DATE';
shell_list | column -t ;; shell_list | column -t ;;
*) check_args '1' '0' 'user [format]' *) check_args '1' '0' 'user [format]'
esac esac

View file

@ -13,7 +13,7 @@
user=$1 user=$1
database=$2 database=$2
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -23,38 +23,26 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '2' "$#" 'user database'
check_args '2' "$#" 'user db_name'
# Checking argument format
validate_format 'user' 'database' validate_format 'user' 'database'
is_system_enabled "$DB_SYSTEM"
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
# Checking db existance is_object_valid 'db' 'DB' "$database"
is_db_valid is_object_unsuspended 'db' 'DB' "$database"
# Checking db is active
is_db_suspended
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Define database variables # Get database values
db_user=$(get_db_value '$USER') get_database_values
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Switching on db type # Switching on db type
case $type in case $type in
mysql) suspend_db_mysql ;; mysql) suspend_mysql_database ;;
pgsql) suspend_db_pgsql ;; pgsql) suspend_pgsql_database ;;
esac esac
@ -63,7 +51,7 @@ esac
#----------------------------------------------------------# #----------------------------------------------------------#
# Updating db value # Updating db value
update_db_base_value '$SUSPENDED' 'yes' update_object_value 'db' 'DB' "$database" '$SUSPENDED' 'yes'
increase_user_value "$user" '$SUSPENDED_DB' increase_user_value "$user" '$SUSPENDED_DB'
# Logging # Logging

47
bin/v_suspend_database_server Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
# info: suspend database server
# options: type host
#
# The function for suspending a database server.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
type=$1
host=$2
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '2' "$#" 'type host'
validate_format 'type' 'host'
is_system_enabled "$DB_SYSTEM"
is_object_valid "../../conf/$type" 'HOST' "$host"
is_object_unsuspended "../../conf/$type" 'HOST' "$host"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Suspend database creation on a server
update_object_value "../../conf/$type" 'HOST' "$host" '$SUSPENDED' 'yes'
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -12,26 +12,18 @@
# Argument defenition # Argument defenition
user=$1 user=$1
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh
#----------------------------------------------------------# #----------------------------------------------------------#
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number
check_args '1' "$#" 'user' check_args '1' "$#" 'user'
# Checking argument format
validate_format 'user' validate_format 'user'
is_system_enabled "$DB_SYSTEM"
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
@ -39,21 +31,9 @@ is_object_valid 'user' 'USER' "$user"
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config
conf="$USER_DATA/db.conf"
# Defining fileds to select
field='$DB'
# Defining search string
search_string="SUSPENDED='no'"
# Parsing unsuspeneded domains
databases=$(db_clear_search)
# Starting suspend loop # Starting suspend loop
for database in $databases; do for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
$BIN/v_suspend_db_base "$user" "$database" $BIN/v_suspend_database "$user" "$database"
done done

View file

@ -13,7 +13,7 @@
user=$1 user=$1
database=$2 database=$2
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -23,38 +23,25 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '2' "$#" 'user database'
check_args '2' "$#" 'user db_name'
# Checking argument format
validate_format 'user' 'database' validate_format 'user' 'database'
is_system_enabled "$DB_SYSTEM"
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
# Checking db existance is_object_suspended 'db' 'DB' "$database"
is_db_valid
# Checking db is active
is_db_unsuspended
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Define database variables # Get database values
db_user=$(get_db_value '$USER') get_database_values
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Switching on db type # Switching on db type
case $type in case $type in
mysql) unsuspend_db_mysql ;; mysql) unsuspend_mysql_database ;;
pgsql) unsuspend_db_pgsql ;; pgsql) unsuspend_pgsql_database ;;
esac esac
@ -63,7 +50,7 @@ esac
#----------------------------------------------------------# #----------------------------------------------------------#
# Updating db value # Updating db value
update_db_base_value '$SUSPENDED' 'no' update_object_value 'db' 'DB' "$database" '$SUSPENDED' 'no'
decrease_user_value "$user" '$SUSPENDED_DB' decrease_user_value "$user" '$SUSPENDED_DB'
# Logging # Logging

47
bin/v_unsuspend_database_server Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
# info: unsuspend database server
# options: type host
#
# The function for unsuspending a database server.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
type=$1
host=$2
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '2' "$#" 'type host'
validate_format 'type' 'host'
is_system_enabled "$DB_SYSTEM"
is_object_valid "../../conf/$type" 'HOST' "$host"
is_object_suspended "../../conf/$type" 'HOST' "$host"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Unsuspend database creation on a server
update_object_value "../../conf/$type" 'HOST' "$host" '$SUSPENDED' 'no'
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -22,16 +22,9 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number
check_args '1' "$#" 'user' check_args '1' "$#" 'user'
# Checking argument format
validate_format 'user' validate_format 'user'
is_system_enabled "$DB_SYSTEM"
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
@ -39,21 +32,9 @@ is_object_valid 'user' 'USER' "$user"
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Defining config # Starting unsuspend loop
conf="$USER_DATA/db.conf" for database in $(search_objects 'db' 'SUSPENDED' "yes" 'DB'); do
$BIN/v_unsuspend_database "$user" "$database"
# Defining fileds to select
field='$DB'
# Defining search string
search_string="SUSPENDED='yes'"
# Parsing unsuspeneded domains
databases=$(db_clear_search)
# Starting suspend loop
for database in $databases; do
$BIN/v_unsuspend_db_base "$user" "$database"
done done

View file

@ -13,7 +13,7 @@
user=$1 user=$1
database=$2 database=$2
# Importing variables # Includes
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh source $VESTA/func/shared.sh
source $VESTA/func/db.sh source $VESTA/func/db.sh
@ -23,54 +23,35 @@ source $VESTA/func/db.sh
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking arg number check_args '2' "$#" 'user database'
check_args '2' "$#" 'user db_name'
# Checking argument format
validate_format 'user' 'database' validate_format 'user' 'database'
is_system_enabled "$DB_SYSTEM"
# Checking web system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user" is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
# Checking db existance is_object_valid 'db' 'DB' "$database"
is_db_valid is_object_unsuspended 'db' 'DB' "$database"
# Checking db is active
is_db_suspended
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Get some variables we do not have now # Get database values
db_user=$(get_db_value '$USER') get_database_values
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Switching on db type # Switching on db type
case $type in case $TYPE in
mysql) disk_usage=$(get_disk_db_mysql); ret_val="$?" ;; mysql) get_mysql_disk_usage ;;
pgsql) disk_usage=$(get_disk_db_pgsql); ret_val="$?" ;; pgsql) get_pgsql_disk_usage ;;
esac esac
# Checking ret_val
if [ "$ret_val" -ne '0' ]; then
exit $ret_val
fi
#----------------------------------------------------------# #----------------------------------------------------------#
# Vesta # # Vesta #
#----------------------------------------------------------# #----------------------------------------------------------#
# Updating disk value in config # Updating disk usage
update_db_base_value '$U_DISK' "$disk_usage" update_object_value 'db' 'DB' "$database" '$U_DISK' "$usage"
# Recalculating user disk space
recalc_user_disk_usage recalc_user_disk_usage
# Logging # Logging

47
bin/v_update_databases_disk Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
# info: update databases disk usage
# options: user
#
# The function recalculates disk usage for all user databases.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
# Importing variables
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'user'
validate_format 'user'
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Starting update loop
for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
$BIN/v_update_database_disk "$user" "$database"
done
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -1,83 +0,0 @@
#!/bin/bash
# info: update databases disk usage
# options: user
#
# The function recalculates disk usage for all user databases.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
# Importing variables
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '1' "$#" 'user'
# Checking argument format
validate_format 'user'
# Checking web system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_object_valid 'user' 'USER' "$user"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config
conf="$USER_DATA/db.conf"
# Defining fileds to select
field='$DB'
# Defining search string
search_string="SUSPENDED='no'"
# Parsing unsuspeneded domains
databases=$(db_clear_search)
for database in $databases; do
# Define database variables
db_user=$(get_db_value '$USER')
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Switching on db type
case $type in
mysql) disk_usage=$(get_disk_db_mysql); ret_val="$?" ;;
pgsql) disk_usage=$(get_disk_db_pgsql); ret_val="$?" ;;
esac
# Updating disk value in config
if [ "$ret_val" -eq '0' ]; then
update_db_base_value '$U_DISK' "$disk_usage"
fi
done
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Recalculating user disk space
recalc_user_disk_usage
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -30,13 +30,13 @@ get_next_dbhost() {
fi fi
} }
# Database encoding validation # Database charset validation
is_db_encoding_valid() { is_charset_valid() {
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
eval $host_str eval $host_str
if [ -z "$(echo $ENCODINGS | grep -wi $encoding )" ]; then if [ -z "$(echo $CHARSETS | grep -wi $charset )" ]; then
echo "Error: encoding $encoding not exist" echo "Error: charset $charset not exist"
log_event "$E_NOTEXIST $EVENT" log_event "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST exit $E_NOTEXIST
fi fi
@ -67,7 +67,7 @@ increase_dbhost_values() {
# Decrease database host value # Decrease database host value
decrease_dbhost_values() { decrease_dbhost_values() {
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/$TYPE.conf)
eval $host_str eval $host_str
old_dbbases="U_DB_BASES='$U_DB_BASES'" old_dbbases="U_DB_BASES='$U_DB_BASES'"
@ -80,12 +80,12 @@ decrease_dbhost_values() {
sed -e ':a;N;$!ba;s/\n/,/g') sed -e ':a;N;$!ba;s/\n/,/g')
new_users="U_SYS_USERS='$U_SYS_USERS'" new_users="U_SYS_USERS='$U_SYS_USERS'"
sed -i "s/$old_dbbases/$new_dbbases/g" $VESTA/conf/$type.conf sed -i "s/$old_dbbases/$new_dbbases/g" $VESTA/conf/$TYPE.conf
sed -i "s/$old_users/$new_users/g" $VESTA/conf/$type.conf sed -i "s/$old_users/$new_users/g" $VESTA/conf/$TYPE.conf
} }
# Create MySQL database # Create MySQL database
create_db_mysql() { add_mysql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
eval $host_str eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
@ -102,26 +102,24 @@ create_db_mysql() {
exit $E_DB exit $E_DB
fi fi
query="CREATE DATABASE $database CHARACTER SET $encoding" query="CREATE DATABASE $database CHARACTER SET $charset"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
query="GRANT ALL ON $database.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass'" query="GRANT ALL ON $database.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ "$HOST" = 'localhost' ]; then query="GRANT ALL ON $database.* TO '$dbuser'@'localhost'
query="GRANT ALL ON $database.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass'"
IDENTIFIED BY '$dbpass'" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
fi
} }
# Create PostgreSQL database # Create PostgreSQL database
create_db_pgsql() { add_pgsql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
eval $host_str eval $host_str
export PGPASSWORD="$PASSWORD" export PGPASSWORD="$PASSWORD"
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: postgresql config parsion failed" echo "Error: postgresql config parsing failed"
log_event "$E_PARSING" "$EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
@ -134,12 +132,12 @@ create_db_pgsql() {
exit $E_DB exit $E_DB
fi fi
query="CREATE ROLE $db_user WITH LOGIN PASSWORD '$db_password'" query="CREATE ROLE $dbuser WITH LOGIN PASSWORD '$dbpass'"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
query="CREATE DATABASE $database OWNER $db_user" query="CREATE DATABASE $database OWNER $dbuser"
if [ "$TPL" = 'template0' ]; then if [ "$TPL" = 'template0' ]; then
query="$query ENCODING '$encoding' TEMPLATE $TPL" query="$query ENCODING '$charset' TEMPLATE $TPL"
else else
query="$query TEMPLATE $TPL" query="$query TEMPLATE $TPL"
fi fi
@ -150,9 +148,9 @@ create_db_pgsql() {
query="GRANT CONNECT ON DATABASE template1 to $db_user" query="GRANT CONNECT ON DATABASE template1 to $db_user"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
} }
# Check if database host do not exist in config
is_dbhost_new() { is_dbhost_new() {
if [ -e "$VESTA/conf/$type.conf" ]; then if [ -e "$VESTA/conf/$type.conf" ]; then
check_host=$(grep "HOST='$host'" $VESTA/conf/$type.conf) check_host=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
@ -164,248 +162,153 @@ is_dbhost_new() {
fi fi
} }
# Check MySQL database host
is_mysql_host_alive() { is_mysql_host_alive() {
# Checking connection query='SELECT VERSION()'
sql="mysql -h $host -u $db_user -p$db_password -P$port -e" mysql -h $host -u $dbuser -p$dbpass -P $port -e "$query" &> /dev/null
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?" if [ '0' -ne "$?" ]; then
if [ '0' -ne "$code" ]; then echo "Error: Connection to $host failed"
echo "Error: Connect failed" log_event "$E_DB" "$EVENT"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
} }
# Check PostgreSQL database host
is_pgsql_host_alive() { is_pgsql_host_alive() {
# Checking connection export PGPASSWORD="$dbpass"
export PGPASSWORD="$db_password" psql -h $host -U $dbuser -p $port -c "SELECT VERSION()" &> /dev/null
sql="psql -h $host -U $db_user -p $port -c " if [ '0' -ne "$?" ]; then
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?" echo "Error: Connection to $host failed"
if [ '0' -ne "$code" ]; then log_event "$E_DB" "$EVENT"
echo "Error: Connect failed"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
} }
is_db_suspended() { # Get database values
config="$USER_DATA/db.conf" get_database_values() {
check_db=$(grep "DB='$database'" $config|grep "SUSPENDED='yes'")
# Checking result
if [ ! -z "$check_db" ]; then
echo "Error: db suspended"
log_event 'debug' "$E_SUSPENDED $EVENT"
exit $E_SUSPENDED
fi
}
is_db_unsuspended() {
config="$USER_DATA/db.conf"
check_db=$(grep "DB='$database'" $config|grep "SUSPENDED='yes'")
# Checking result
if [ -z "$check_db" ]; then
echo "Error: db unsuspended"
log_event 'debug' "$E_UNSUSPENDED $EVENT"
exit $E_UNSUSPENDED
fi
}
is_db_user_valid() {
config="$USER_DATA/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_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
}
change_db_mysql_password() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/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_PARSING $EVENT"
exit $E_PARSING
fi
# Checking connection
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
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'" $VESTA/conf/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_PARSING $EVENT"
exit $E_PARSING
fi
# Checking connection
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
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'" $USER_DATA/db.conf) db_str=$(grep "DB='$database'" $USER_DATA/db.conf)
eval $db_str
# 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() { # Change MySQL database password
# Defining vars change_mysql_password() {
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
for key in $host_str; do eval $host_str
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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken" echo "Error: mysql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
# Deleting database & checking result query="GRANT ALL ON $database.* TO '$DBUSER'@'%' IDENTIFIED BY '$dbpass'"
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
# Deleting user query="GRANT ALL ON $database.* TO '$DBUSER'@'localhost'
check_users=$(grep "USER='$db_user'" $USER_DATA/db.conf |wc -l) IDENTIFIED BY '$dbpass'"
if [ 1 -ge "$check_users" ]; then mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
$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() { # Change PostgreSQL database password
# Defining vars change_pgsql_password() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do eval $host_str
eval ${key%%=*}=${key#*=}
done
export PGPASSWORD="$PASSWORD" 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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken" echo "Error: postgresql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?" psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB" "$EVENT"
exit $E_DB exit $E_DB
fi fi
# Deleting database & checking result query="ALTER ROLE $DBUSER WITH LOGIN PASSWORD '$dbpass'"
$sql "REVOKE ALL PRIVILEGES ON DATABASE $database FROM $db_user">/dev/null psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
$sql "DROP DATABASE $database" >/dev/null 2>&1;code="$?"
if [ '0' -ne "$code" ]; then
echo "Error: Connect failed"
log_event 'debug' "$E_DB $EVENT"
exit $E_DB
fi
# Deleting user
check_users=$(grep "USER='$db_user'" $USER_DATA/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'
} }
# Delete MySQL database
del_db_vesta() { delete_mysql_database() {
conf="$USER_DATA/db.conf" host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
# Parsing domains if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
string=$( grep -n "DB='$database'" $conf | cut -f 1 -d : ) echo "Error: mysql config parsing failed"
if [ -z "$string" ]; then log_event "$E_PARSING" "$EVENT"
echo "Error: parse error"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
sed -i "$string d" $conf
query='SELECT VERSION()'
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$?" ]; then
echo "Error: Connection failed"
log_event "$E_DB $EVENT"
exit $E_DB
fi
query="DROP DATABASE $database"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
query="REVOKE ALL ON $database.* FROM '$DBUSER'@'%'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
query="REVOKE ALL ON $database.* FROM '$DBUSER'@'localhost'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ "$(grep "DBUSER='$DBUSER'" $USER_DATA/db.conf |wc -l)" -lt 2 ]; then
query="DROP USER '$DBUSER'@'%'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
query="DROP USER '$DBUSER'@'localhost'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
fi
} }
# Delete PostgreSQL database
delete_pgsql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
eval $host_str
export PGPASSWORD="$PASSWORD"
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: postgresql config parsing failed"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
fi
query='SELECT VERSION()'
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ '0' -ne "$?" ]; then
echo "Error: Connection failed"
log_event "$E_DB" "$EVENT"
exit $E_DB
fi
query="REVOKE ALL PRIVILEGES ON DATABASE $database FROM $DBUSER"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
query="DROP DATABASE $database"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ "$(grep "DBUSER='$DBUSER'" $USER_DATA/db.conf |wc -l)" -lt 2 ]; then
query="REVOKE CONNECT ON DATABASE template1 FROM $db_user"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
query="DROP ROLE $db_user"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
fi
}
dump_db_mysql() { dump_db_mysql() {
# Defining vars # Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
@ -475,285 +378,169 @@ dump_db_pgsql() {
export PGPASSWORD='pgsql' export PGPASSWORD='pgsql'
} }
# Check if database server is in use
is_dbhost_free() {
is_db_host_free() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf) host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
for key in $host_str; do eval $host_str
eval ${key%%=*}=${key#*=}
done
# Checking U_DB_BASES
if [ 0 -ne "$U_DB_BASES" ]; then if [ 0 -ne "$U_DB_BASES" ]; then
echo "Error: host is used" echo "Error: host $HOST is used"
log_event 'debug' "$E_INUSE $EVENT" log_event "$E_INUSE" "$EVENT"
exit $E_INUSE exit $E_INUSE
fi fi
} }
del_dbhost_vesta() { # Suspend MySQL database
conf="$VESTA/conf/$type.conf" suspend_mysql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
# Parsing domains eval $host_str
string=$( grep -n "HOST='$host'" $conf | cut -f 1 -d : )
if [ -z "$string" ]; then
echo "Error: parse error"
log_event 'debug' "$E_PARSING $EVENT"
exit $E_PARSING
fi
sed -i "$string d" $conf
}
update_db_base_value() {
key="$1"
value="$2"
# Defining conf
conf="$USER_DATA/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'" $VESTA/conf/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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken" echo "Error: mysql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
# Suspending user query="REVOKE ALL ON $database.* FROM '$DBUSER'@'%'"
$sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
$sql "FLUSH PRIVILEGES"
query="REVOKE ALL ON $database.* FROM '$DBUSER'@'localhost'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
} }
suspend_db_pgsql() { # Suspend PostgreSQL database
# Defining vars suspend_pgsql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
for key in $host_str; do eval $host_str
eval ${key%%=*}=${key#*=}
done
export PGPASSWORD="$PASSWORD" 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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken" echo "Error: postgresql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?" psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB" "$EVENT"
exit $E_DB exit $E_DB
fi fi
# Suspending user query="REVOKE ALL PRIVILEGES ON $database FROM $DBUSER"
$sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
export PGPASSWORD='pgsql'
} }
unsuspend_db_mysql() { # Unsuspend MySQL database
# Defining vars unsuspend_mysql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
for key in $host_str; do eval $host_str
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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken" echo "Error: mysql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
# Unsuspending user query="GRANT ALL ON $database.* FROM '$DBUSER'@'%'"
$sql "GRANT ALL ON $database.* to '$db_user'@'%'" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
$sql "FLUSH PRIVILEGES"
query="GRANT ALL ON $database.* FROM '$DBUSER'@'localhost'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
} }
unsuspend_db_pgsql() { # Unsuspend PostgreSQL database
# Defining vars unsuspend_pgsql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
for key in $host_str; do eval $host_str
eval ${key%%=*}=${key#*=}
done
export PGPASSWORD="$PASSWORD" 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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken" echo "Error: postgresql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?" psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB" "$EVENT"
exit $E_DB exit $E_DB
fi fi
# Unsuspending user query="GRANT ALL PRIVILEGES ON DATABASE $database TO $DBUSER"
$sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
export PGPASSWORD='pgsql'
} }
db_clear_search() { # Get MySQL disk usage
# Defining delimeter get_mysql_disk_usage() {
IFS=$'\n' host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
# 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'" $VESTA/conf/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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken" echo "Error: mysql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1; code="$?" mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB $EVENT"
exit $E_DB exit $E_DB
fi fi
# Deleting database & checking result query="SELECT SUM( data_length + index_length ) / 1024 / 1024 \"Size\"
query="SELECT sum( data_length + index_length ) / 1024 / 1024 \"Size\" FROM information_schema.TABLES WHERE table_schema='$database'"
FROM information_schema.TABLES WHERE table_schema='$database'" usage=$(mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" |tail -n1)
raw_size=$($sql "$query" |tail -n 1) if [ "$usage" == 'NULL' ] || [ "${usage:0:1}" -eq '0' ]; then
usage=1
# Checking null output (this means error btw)
if [ "$raw_size" == 'NULL' ]; then
raw_size='0'
fi fi
usage=$(printf "%0.f\n" $usage)
# 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() { # Get MySQL disk usage
# Defining vars get_pgsql_disk_usage() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf) host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
for key in $host_str; do eval $host_str
eval ${key%%=*}=${key#*=}
done
export PGPASSWORD="$PASSWORD" 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 if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $TPL ]; then
echo "Error: config is broken" echo "Error: postgresql config parsing failed"
log_event 'debug' "$E_PARSING $EVENT" log_event "$E_PARSING" "$EVENT"
exit $E_PARSING exit $E_PARSING
fi fi
# Checking connection query='SELECT VERSION()'
$sql "SELECT VERSION()" >/dev/null 2>&1;code="$?" psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
if [ '0' -ne "$code" ]; then if [ '0' -ne "$?" ]; then
echo "Error: Connect failed" echo "Error: Connection failed"
log_event 'debug' "$E_DB $EVENT" log_event "$E_DB" "$EVENT"
exit $E_DB exit $E_DB
fi fi
# Raw query query="SELECT pg_database_size('$database');"
usage=$(psql -h $HOST -U $USER -p $PORT -c "$query")
raq_query=$($sql "SELECT pg_database_size('$database');") usage=$(echo "$usage" | grep -v "-" | grep -v 'row' | sed -e "/^$/d")
raw_size=$(echo "$raq_query" | grep -v "-" | grep -v 'row' |\ usage=$(echo "$usage" | grep -v "pg_database_size" | awk '{print $1}')
sed -e "/^$/d" |grep -v "pg_database_size" | awk '{print $1}') if [ -z "$usage" ]; then
usage=0
# Checking null output (this means error btw)
if [ -z "$raw_size" ]; then
raw_size='0'
fi fi
usage=$(($usage / 1048576))
# Converting to MB if [ "$usage" -eq '0' ]; then
size=$(expr $raw_size / 1048576) usage=1
# Rounding zero size
if [ "$size" -eq '0' ]; then
echo '1'
else
echo "$size"
fi fi
} }

View file

@ -173,7 +173,7 @@ is_object_suspended() {
spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'") spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
fi fi
if [ -z "$spnd" ]; then if [ -z "$spnd" ]; then
echo "Error: $3 is suspended" echo "Error: $3 is not suspended"
log_event "$E_SUSPENDED" "$EVENT" log_event "$E_SUSPENDED" "$EVENT"
exit $E_SUSPENDED exit $E_SUSPENDED
fi fi
@ -187,7 +187,7 @@ is_object_unsuspended() {
spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'") spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
fi fi
if [ ! -z "$spnd" ]; then if [ ! -z "$spnd" ]; then
echo "Error: $3 is not suspended" echo "Error: $3 is suspended"
log_event "$E_UNSUSPENDED" "$EVENT" log_event "$E_UNSUSPENDED" "$EVENT"
exit $E_UNSUSPENDED exit $E_UNSUSPENDED
fi fi
@ -620,6 +620,8 @@ validate_format(){
auth_pass) validate_format_password "$arg" ;; auth_pass) validate_format_password "$arg" ;;
auth_user) validate_format_username "$arg" "$arg_name" ;; auth_user) validate_format_username "$arg" "$arg_name" ;;
backup) validate_format_date "$arg" ;; backup) validate_format_date "$arg" ;;
charset) validate_format_username "$arg" "$arg_name" ;;
charsets) validate_format_common "$arg" 'charsets' ;;
database) validate_format_database "$arg" ;; database) validate_format_database "$arg" ;;
day) validate_format_mhdmw "$arg" $arg_name ;; day) validate_format_mhdmw "$arg" $arg_name ;;
dbpass) validate_format_password "$arg" ;; dbpass) validate_format_password "$arg" ;;
@ -630,11 +632,10 @@ validate_format(){
dom_alias) validate_format_domain "$arg" ;; dom_alias) validate_format_domain "$arg" ;;
dvalue) validate_format_dvalue "$arg";; dvalue) validate_format_dvalue "$arg";;
email) validate_format_email "$arg" ;; email) validate_format_email "$arg" ;;
encoding) validate_format_username "$arg" "$arg_name" ;;
exp) validate_format_date "$arg" ;; exp) validate_format_date "$arg" ;;
extentions) validate_format_common "$arg" 'extentions' ;; extentions) validate_format_common "$arg" 'extentions' ;;
fname) validate_format_username "$arg" "$arg_name" ;; fname) validate_format_username "$arg" "$arg_name" ;;
host) validate_format_username "$arg" "$arg_name" ;; host) validate_format_domain "$arg" "$arg_name" ;;
hour) validate_format_mhdmw "$arg" $arg_name ;; hour) validate_format_mhdmw "$arg" $arg_name ;;
id) validate_format_int "$arg" ;; id) validate_format_int "$arg" ;;
interface) validate_format_interface "$arg" ;; interface) validate_format_interface "$arg" ;;