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

View file

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

View file

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

View file

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

View file

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

View file

@ -12,53 +12,29 @@
# Argument defenition
user=$1
# Importing variables
# Includes
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_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended '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)
# Starting delete loop
for database in $databases; do
$BIN/v_delete_db_base "$user" "$database"
rv="$?"
if [ "$rv" -ne '0' ]; then
log_event 'debug' "$rv $EVENT"
exit $rv
fi
for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
$BIN/v_delete_database "$user" "$database"
done

View file

@ -14,33 +14,18 @@ user=$1
database=$2
format=${3-shell}
# Importing variables
# Includes
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
# Json function
json_list_db() {
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
eval $line
# 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
@ -50,30 +35,18 @@ json_list_db() {
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
fi
fi
# Updating iterator
(( ++i))
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
shell_list_db() {
# Reading file line by line
line=$(grep "DB='$database'" $conf)
# Parsing key=value
eval $line
# Print result line
for field in $fields; do
eval key="$field"
echo "${field//$/}: $key "
@ -85,28 +58,20 @@ shell_list_db() {
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '2' "$#" 'user db_name [format]'
# Checking argument format
check_args '2' "$#" 'user database [format]'
validate_format 'user' 'database'
# Checking user
is_object_valid 'user' 'USER' "$user"
# Checking database exist
is_db_valid
is_object_valid 'db' 'DB' "$database"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config
conf=$USER_DATA/db.conf
# 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
case $format in

View file

@ -1,8 +1,8 @@
#!/bin/bash
# info: list database host
# info: list database server
# 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
format=${3-shell}
# Importing variables
# Includes
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
# Json function
json_list_dbhost() {
# Definigng variables
i=1
# Define words number
fields_count=$(echo "$fields" | wc -w)
# Reading file line by line
line=$(grep "HOST='$host'" $conf)
# Print top bracket
echo '{'
# Assign key=value
eval $line
# 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
@ -53,25 +37,16 @@ json_list_dbhost() {
fi
(( ++i))
done
# If there was any output
if [ -n "$value" ]; then
echo -e "\t}"
fi
# Printing bottom json bracket
echo -e "}"
}
# Shell function
shell_list_dbhost() {
# Reading file line by line
line=$(grep "HOST='$host'" $conf)
# Parsing key=value
eval $line
# Print result line
for field in $fields; do
eval key="$field"
echo "${field//$/}: $key"
@ -83,28 +58,19 @@ shell_list_dbhost() {
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '2' "$#" 'type host [format]'
# Checking argument format
validate_format 'host'
# Checking db type
is_type_valid "$DB_SYSTEM" "$type"
# Checking db host
is_db_host_valid
is_object_valid "../../conf/$type" 'HOST' "$host"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config type
conf=$VESTA/conf/$type.conf
# 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
case $format in

View file

@ -13,7 +13,7 @@
type=$1
format=${2-shell}
# Importing variables
# Includes
source $VESTA/func/shared.sh
@ -24,25 +24,21 @@ source $VESTA/func/shared.sh
# Checking args
check_args '1' "$#" 'type [format]'
# Checking db type
is_type_valid "$DB_SYSTEM" "$type"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config type
conf=$VESTA/conf/$type.conf
# 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
case $format in
json) json_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 ;;
*) check_args '2' '0' 'type [format]'
esac

View file

@ -13,7 +13,7 @@
user=$1
format=${2-shell}
# Importing variables
# Includes
source $VESTA/func/shared.sh
@ -21,13 +21,8 @@ source $VESTA/func/shared.sh
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '1' "$#" 'user [format]'
# Checking argument format
validate_format 'user'
# Checking user
is_object_valid 'user' 'USER' "$user"
@ -35,17 +30,15 @@ is_object_valid 'user' 'USER' "$user"
# Action #
#----------------------------------------------------------#
# Defining config
conf=$USER_DATA/db.conf
# 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
case $format in
json) json_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 ;;
*) check_args '1' '0' 'user [format]'
esac

View file

@ -13,7 +13,7 @@
user=$1
database=$2
# Importing variables
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
@ -23,38 +23,26 @@ source $VESTA/func/db.sh
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user db_name'
# Checking argument format
check_args '2' "$#" 'user database'
validate_format 'user' 'database'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
# Checking db existance
is_db_valid
# Checking db is active
is_db_suspended
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
is_object_unsuspended 'db' 'DB' "$database"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Define database variables
db_user=$(get_db_value '$USER')
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Get database values
get_database_values
# Switching on db type
case $type in
mysql) suspend_db_mysql ;;
pgsql) suspend_db_pgsql ;;
mysql) suspend_mysql_database ;;
pgsql) suspend_pgsql_database ;;
esac
@ -63,7 +51,7 @@ esac
#----------------------------------------------------------#
# Updating db value
update_db_base_value '$SUSPENDED' 'yes'
update_object_value 'db' 'DB' "$database" '$SUSPENDED' 'yes'
increase_user_value "$user" '$SUSPENDED_DB'
# 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
user=$1
# Importing variables
# Includes
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 db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
@ -39,21 +31,9 @@ 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)
# Starting suspend loop
for database in $databases; do
$BIN/v_suspend_db_base "$user" "$database"
for database in $(search_objects 'db' 'SUSPENDED' "no" 'DB'); do
$BIN/v_suspend_database "$user" "$database"
done

View file

@ -13,7 +13,7 @@
user=$1
database=$2
# Importing variables
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
@ -23,38 +23,25 @@ source $VESTA/func/db.sh
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user db_name'
# Checking argument format
check_args '2' "$#" 'user database'
validate_format 'user' 'database'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
# Checking db existance
is_db_valid
# Checking db is active
is_db_unsuspended
is_object_valid 'db' 'DB' "$database"
is_object_suspended 'db' 'DB' "$database"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Define database variables
db_user=$(get_db_value '$USER')
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Get database values
get_database_values
# Switching on db type
case $type in
mysql) unsuspend_db_mysql ;;
pgsql) unsuspend_db_pgsql ;;
mysql) unsuspend_mysql_database ;;
pgsql) unsuspend_pgsql_database ;;
esac
@ -63,7 +50,7 @@ esac
#----------------------------------------------------------#
# Updating db value
update_db_base_value '$SUSPENDED' 'no'
update_object_value 'db' 'DB' "$database" '$SUSPENDED' 'no'
decrease_user_value "$user" '$SUSPENDED_DB'
# 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 #
#----------------------------------------------------------#
# Checking arg number
check_args '1' "$#" 'user'
# Checking argument format
validate_format 'user'
# Checking db system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
@ -39,21 +32,9 @@ 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='yes'"
# Parsing unsuspeneded domains
databases=$(db_clear_search)
# Starting suspend loop
for database in $databases; do
$BIN/v_unsuspend_db_base "$user" "$database"
# Starting unsuspend loop
for database in $(search_objects 'db' 'SUSPENDED' "yes" 'DB'); do
$BIN/v_unsuspend_database "$user" "$database"
done

View file

@ -13,7 +13,7 @@
user=$1
database=$2
# Importing variables
# Includes
source $VESTA/conf/vesta.conf
source $VESTA/func/shared.sh
source $VESTA/func/db.sh
@ -23,54 +23,35 @@ source $VESTA/func/db.sh
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user db_name'
# Checking argument format
check_args '2' "$#" 'user database'
validate_format 'user' 'database'
# Checking web system is enabled
is_system_enabled 'DB_SYSTEM'
# Checking user
is_system_enabled "$DB_SYSTEM"
is_object_valid 'user' 'USER' "$user"
# Checking db existance
is_db_valid
# Checking db is active
is_db_suspended
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'db' 'DB' "$database"
is_object_unsuspended 'db' 'DB' "$database"
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Get some variables we do not have now
db_user=$(get_db_value '$USER')
host=$(get_db_value '$HOST')
type=$(get_db_value '$TYPE')
# Get database values
get_database_values
# 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="$?" ;;
case $TYPE in
mysql) get_mysql_disk_usage ;;
pgsql) get_pgsql_disk_usage ;;
esac
# Checking ret_val
if [ "$ret_val" -ne '0' ]; then
exit $ret_val
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Updating disk value in config
update_db_base_value '$U_DISK' "$disk_usage"
# Recalculating user disk space
# Updating disk usage
update_object_value 'db' 'DB' "$database" '$U_DISK' "$usage"
recalc_user_disk_usage
# 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
}
# Database encoding validation
is_db_encoding_valid() {
# Database charset validation
is_charset_valid() {
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
eval $host_str
if [ -z "$(echo $ENCODINGS | grep -wi $encoding )" ]; then
echo "Error: encoding $encoding not exist"
if [ -z "$(echo $CHARSETS | grep -wi $charset )" ]; then
echo "Error: charset $charset not exist"
log_event "$E_NOTEXIST $EVENT"
exit $E_NOTEXIST
fi
@ -67,7 +67,7 @@ increase_dbhost_values() {
# Decrease database host value
decrease_dbhost_values() {
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
host_str=$(grep "HOST='$HOST'" $VESTA/conf/$TYPE.conf)
eval $host_str
old_dbbases="U_DB_BASES='$U_DB_BASES'"
@ -80,12 +80,12 @@ decrease_dbhost_values() {
sed -e ':a;N;$!ba;s/\n/,/g')
new_users="U_SYS_USERS='$U_SYS_USERS'"
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_dbbases/$new_dbbases/g" $VESTA/conf/$TYPE.conf
sed -i "s/$old_users/$new_users/g" $VESTA/conf/$TYPE.conf
}
# Create MySQL database
create_db_mysql() {
add_mysql_database() {
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
@ -102,26 +102,24 @@ create_db_mysql() {
exit $E_DB
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
query="GRANT ALL ON $database.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass'"
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'
IDENTIFIED BY '$dbpass'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
fi
}
# Create PostgreSQL database
create_db_pgsql() {
add_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 parsion failed"
echo "Error: postgresql config parsing failed"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
fi
@ -134,12 +132,12 @@ create_db_pgsql() {
exit $E_DB
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
query="CREATE DATABASE $database OWNER $db_user"
query="CREATE DATABASE $database OWNER $dbuser"
if [ "$TPL" = 'template0' ]; then
query="$query ENCODING '$encoding' TEMPLATE $TPL"
query="$query ENCODING '$charset' TEMPLATE $TPL"
else
query="$query TEMPLATE $TPL"
fi
@ -150,9 +148,9 @@ create_db_pgsql() {
query="GRANT CONNECT ON DATABASE template1 to $db_user"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
}
# Check if database host do not exist in config
is_dbhost_new() {
if [ -e "$VESTA/conf/$type.conf" ]; then
check_host=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
@ -164,248 +162,153 @@ is_dbhost_new() {
fi
}
# Check MySQL database host
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_DB $EVENT"
query='SELECT VERSION()'
mysql -h $host -u $dbuser -p$dbpass -P $port -e "$query" &> /dev/null
if [ '0' -ne "$?" ]; then
echo "Error: Connection to $host failed"
log_event "$E_DB" "$EVENT"
exit $E_DB
fi
}
# Check PostgreSQL database host
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_DB $EVENT"
export PGPASSWORD="$dbpass"
psql -h $host -U $dbuser -p $port -c "SELECT VERSION()" &> /dev/null
if [ '0' -ne "$?" ]; then
echo "Error: Connection to $host failed"
log_event "$E_DB" "$EVENT"
exit $E_DB
fi
}
is_db_suspended() {
config="$USER_DATA/db.conf"
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"
# Get database values
get_database_values() {
db_str=$(grep "DB='$database'" $USER_DATA/db.conf)
# Parsing key=value
for keys in $db_str; do
eval ${keys%%=*}=${keys#*=}
done
# Self reference
eval value="$key"
# Print value
echo "$value"
eval $db_str
}
del_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
# Change MySQL database password
change_mysql_password() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $EVENT"
echo "Error: mysql config parsing failed"
log_event "$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"
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
# 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_DB $EVENT"
exit $E_DB
fi
query="GRANT ALL ON $database.* TO '$DBUSER'@'%' IDENTIFIED BY '$dbpass'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
# Deleting user
check_users=$(grep "USER='$db_user'" $USER_DATA/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"
query="GRANT ALL ON $database.* TO '$DBUSER'@'localhost'
IDENTIFIED BY '$dbpass'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
}
del_db_pgsql() {
# Defining vars
# Change PostgreSQL database password
change_pgsql_password() {
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
eval $host_str
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"
echo "Error: postgresql config parsing failed"
log_event "$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"
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
# 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_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'
query="ALTER ROLE $DBUSER WITH LOGIN PASSWORD '$dbpass'"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
}
del_db_vesta() {
conf="$USER_DATA/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_PARSING $EVENT"
# Delete MySQL database
delete_mysql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: mysql config parsing failed"
log_event "$E_PARSING" "$EVENT"
exit $E_PARSING
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() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/mysql.conf)
@ -475,285 +378,169 @@ dump_db_pgsql() {
export PGPASSWORD='pgsql'
}
is_db_host_free() {
# Defining vars
# Check if database server is in use
is_dbhost_free() {
host_str=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
# Checking U_DB_BASES
eval $host_str
if [ 0 -ne "$U_DB_BASES" ]; then
echo "Error: host is used"
log_event 'debug' "$E_INUSE $EVENT"
echo "Error: host $HOST is used"
log_event "$E_INUSE" "$EVENT"
exit $E_INUSE
fi
}
del_dbhost_vesta() {
conf="$VESTA/conf/$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_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
# Suspend MySQL database
suspend_mysql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $EVENT"
echo "Error: mysql config parsing failed"
log_event "$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"
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
# Suspending user
$sql "REVOKE ALL ON $database.* FROM '$db_user'@'%'"
$sql "FLUSH PRIVILEGES"
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
}
suspend_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
# Suspend PostgreSQL database
suspend_pgsql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
eval $host_str
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"
echo "Error: postgresql config parsing failed"
log_event "$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"
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
# Suspending user
$sql "REVOKE ALL PRIVILEGES ON $database FROM $db_user">/dev/null
export PGPASSWORD='pgsql'
query="REVOKE ALL PRIVILEGES ON $database FROM $DBUSER"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
}
unsuspend_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
# Unsuspend MySQL database
unsuspend_mysql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $EVENT"
echo "Error: mysql config parsing failed"
log_event "$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"
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
# Unsuspending user
$sql "GRANT ALL ON $database.* to '$db_user'@'%'"
$sql "FLUSH PRIVILEGES"
query="GRANT ALL ON $database.* FROM '$DBUSER'@'%'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
query="GRANT ALL ON $database.* FROM '$DBUSER'@'localhost'"
mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" &> /dev/null
}
unsuspend_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
# Unsuspend PostgreSQL database
unsuspend_pgsql_database() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
eval $host_str
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"
echo "Error: postgresql config parsing failed"
log_event "$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"
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
# Unsuspending user
$sql "GRANT ALL PRIVILEGES ON DATABASE $database TO $db_user" >/dev/null
export PGPASSWORD='pgsql'
query="GRANT ALL PRIVILEGES ON DATABASE $database TO $DBUSER"
psql -h $HOST -U $USER -p $PORT -c "$query" &> /dev/null
}
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'" $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
# Get MySQL disk usage
get_mysql_disk_usage() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/mysql.conf)
eval $host_str
if [ -z $HOST ] || [ -z $USER ] || [ -z $PASSWORD ] || [ -z $PORT ]; then
echo "Error: config is broken"
log_event 'debug' "$E_PARSING $EVENT"
echo "Error: mysql config parsing failed"
log_event "$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"
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
# 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'"
raw_size=$($sql "$query" |tail -n 1)
# Checking null output (this means error btw)
if [ "$raw_size" == 'NULL' ]; then
raw_size='0'
usage=$(mysql -h $HOST -u $USER -p$PASSWORD -P $PORT -e "$query" |tail -n1)
if [ "$usage" == 'NULL' ] || [ "${usage:0:1}" -eq '0' ]; then
usage=1
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
usage=$(printf "%0.f\n" $usage)
}
get_disk_db_pgsql() {
# Defining vars
host_str=$(grep "HOST='$host'" $VESTA/conf/pgsql.conf)
for key in $host_str; do
eval ${key%%=*}=${key#*=}
done
# Get MySQL disk usage
get_pgsql_disk_usage() {
host_str=$(grep "HOST='$HOST'" $VESTA/conf/pgsql.conf)
eval $host_str
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"
echo "Error: postgresql config parsing failed"
log_event "$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"
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
# 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'
query="SELECT pg_database_size('$database');"
usage=$(psql -h $HOST -U $USER -p $PORT -c "$query")
usage=$(echo "$usage" | grep -v "-" | grep -v 'row' | sed -e "/^$/d")
usage=$(echo "$usage" | grep -v "pg_database_size" | awk '{print $1}')
if [ -z "$usage" ]; then
usage=0
fi
# Converting to MB
size=$(expr $raw_size / 1048576)
# Rounding zero size
if [ "$size" -eq '0' ]; then
echo '1'
else
echo "$size"
usage=$(($usage / 1048576))
if [ "$usage" -eq '0' ]; then
usage=1
fi
}

View file

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