#!/bin/bash # info: add new database server # options: TYPE HOST 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 # you can set limit for number of databases on a host. Template parameter is # used only for PostgreSQL and has an default value "template1". You can read # more about templates in official PostgreSQL documentation. #----------------------------------------------------------# # Variable&Function # #----------------------------------------------------------# # Argument definition type=$1 host=$2 dbuser=$3 password=$4; HIDE=4 max_db=${6-500} charsets=${7-UTF8,LATIN1,WIN1250,WIN1251,WIN1252,WIN1256,WIN1258,KOI8} template=${8-template1} # Includes source $VESTA/func/main.sh source $VESTA/func/db.sh source $VESTA/conf/vesta.conf is_mysql_host_alive() { mycnf=$(mktemp) echo "[client]">$mycnf echo "host='$HOST'" >> $mycnf echo "user='$USER'" >> $mycnf echo "password='$PASSWORD'" >> $mycnf chmod 600 $mycnf mysql --defaults-file=$mycnf -e 'SELECT VERSION()' >/dev/null 2>&1 rm $mycnf if [ '0' -ne "$?" ]; then echo "Error: MySQL connection to $host failed" log_event "$E_CONNECT" "$ARGUMENTS" exit $E_CONNECT fi } is_pgsql_host_alive() { export PGPASSWORD="$dbpass" psql -h $host -U $dbuser -c "SELECT VERSION()" > /dev/null 2>&1 if [ '0' -ne "$?" ]; then echo "Error: PostgreSQL connection to $host failed" log_event "$E_CONNECT" "$ARGUMENTS" exit $E_CONNECT fi } #----------------------------------------------------------# # Verifications # #----------------------------------------------------------# args_usage='TYPE HOST DBUSER DBPASS [MAX_DB] [CHARSETS] [TPL]' check_args '4' "$#" "$args_usage" is_format_valid 'host' 'dbuser' 'max_db' 'charsets' 'template' #is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM' #is_type_valid "$DB_SYSTEM" "$type" is_dbhost_new is_password_valid dbpass="$password" case $type in mysql) is_mysql_host_alive ;; pgsql) is_pgsql_host_alive ;; esac #----------------------------------------------------------# # Action # #----------------------------------------------------------# # Generating timestamp time_n_date=$(date +'%T %F') time=$(echo "$time_n_date" |cut -f 1 -d \ ) date=$(echo "$time_n_date" |cut -f 2 -d \ ) # Concatenating db host string case $type in mysql) str="HOST='$host' USER='$dbuser' PASSWORD='$dbpass'"; 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'"; 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 #----------------------------------------------------------# # Vesta # #----------------------------------------------------------# # Adding host to conf echo "$str" >> $VESTA/conf/$type.conf chmod 660 $VESTA/conf/$type.conf # Updating vesta.conf if [ -z "$(grep DB_SYSTEM $VESTA/conf/vesta.conf)" ]; then echo "DB_SYSTEM='$type'" >> $VESTA/conf/vesta.conf else db=$(echo "$DB_SYSTEM,$type" |\ sed "s/,/\n/g"|\ sort -r -u |\ sed "/^$/d"|\ sed ':a;N;$!ba;s/\n/,/g') sed -i "s/DB_SYSTEM=.*/DB_SYSTEM='$db'/g" $VESTA/conf/vesta.conf fi # Logging log_event "$OK" "$ARGUMENTS" exit