#!/bin/bash # info: add database # options: USER DATABASE DBUSER DBPASS [TYPE] [HOST] [CHARSET] # # example: v-add-database admin wordpress_db matt qwerty123 # # This function creates the database concatenating username and user_db. # Supported types of databases you can get using v-list-sys-config script. # If the host isn't stated and there are few hosts configured on the server, # then the host will be defined by one of three algorithms. "First" will choose # the first host in the list. "Random" will chose the host by a chance. # "Weight" will distribute new database through hosts evenly. Algorithm and # types of supported databases is designated in the main configuration file. #----------------------------------------------------------# # Variables & Functions # #----------------------------------------------------------# # Argument definition user=$1 database="$user"_"$2" dbuser="$user"_"$3" password=$4 HIDE=4 type=${5-mysql} host=$6 charset=${7-UTF8MB4} charset=$(echo "$charset" | tr '[:lower:]' '[:upper:]') # Includes # shellcheck source=/etc/hestiacp/hestia.conf source /etc/hestiacp/hestia.conf # shellcheck source=/usr/local/hestia/func/main.sh source $HESTIA/func/main.sh # shellcheck source=/usr/local/hestia/func/db.sh source $HESTIA/func/db.sh # load config file source_conf "$HESTIA/conf/hestia.conf" if [ "$type" = "pgsql" ]; then database=$(echo "$user"_"$2" | tr '[:upper:]' '[:lower:]') dbuser=$(echo "$user"_"$3" | tr '[:upper:]' '[:lower:]') fi #----------------------------------------------------------# # Verifications # #----------------------------------------------------------# check_args '4' "$#" 'USER DATABASE DBUSER DBPASS [TYPE] [HOST] [CHARSET]' is_format_valid 'user' 'database' 'dbuser' 'charset' is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM' is_type_valid "$DB_SYSTEM" "$type" is_object_valid 'user' 'USER' "$user" is_object_unsuspended 'user' 'USER' "$user" is_object_new 'db' 'DB' "$database" is_object_new 'db' 'DBUSER' "$dbuser" get_next_dbhost is_object_valid "../../../conf/$type" 'HOST' "$host" is_object_unsuspended "../../../conf/$type" 'DBHOST' "$host" #is_charset_valid is_package_full 'DATABASES' is_password_valid if [ "$type" = "pgsql" ]; then exclude="-" if [[ "$dbuser" =~ $exclude ]]; then check_result "$E_INVALID" "invalid database user format" fi if [[ "$database" =~ $exclude ]]; then check_result "$E_INVALID" "invalid database format" fi fi dbpass="$password" # Perform verification if read-only mode is enabled check_hestia_demo_mode #----------------------------------------------------------# # Action # #----------------------------------------------------------# # Switching on db type case $type in mysql) add_mysql_database ;; pgsql) add_pgsql_database ;; esac #----------------------------------------------------------# # Hestia # #----------------------------------------------------------# # 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 \ ) # Adding db to db conf str="DB='$database' DBUSER='$dbuser' MD5='$md5' 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 # Increasing counters increase_dbhost_values increase_user_value "$user" '$U_DATABASES' # Logging $BIN/v-log-action "$user" "Info" "Databases" "Added new database $database ($type)." log_event "$OK" "$ARGUMENTS" exit