v-add-database 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # info: add database
  3. # options: USER DATABASE DBUSER DBPASS [TYPE] [HOST] [CHARSET]
  4. #
  5. # The function creates the database concatenating username and user_db.
  6. # Supported types of databases you can get using v-list-sys-config script.
  7. # If the host isn't stated and there are few hosts configured on the server,
  8. # then the host will be defined by one of three algorithms. "First" will choose
  9. # the first host in the list. "Random" will chose the host by a chance.
  10. # "Weight" will distribute new database through hosts evenly. Algorithm and
  11. # types of supported databases is designated in the main configuration file.
  12. #----------------------------------------------------------#
  13. # Variable&Function #
  14. #----------------------------------------------------------#
  15. # Argument definition
  16. user=$1
  17. database="$user"_"$2"
  18. dbuser="$user"_"$3"
  19. password=$4; HIDE=4
  20. type=${5-mysql}
  21. host=$6
  22. charset=${7-UTF8}
  23. charset=$(echo "$charset" |tr '[:lower:]' '[:upper:]')
  24. # Includes
  25. source $VESTA/func/main.sh
  26. source $VESTA/func/db.sh
  27. source $VESTA/conf/vesta.conf
  28. #----------------------------------------------------------#
  29. # Verifications #
  30. #----------------------------------------------------------#
  31. check_args '4' "$#" 'USER DATABASE DBUSER DBPASS [TYPE] [HOST] [CHARSET]'
  32. is_format_valid 'user' 'database' 'dbuser' 'charset'
  33. is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
  34. is_type_valid "$DB_SYSTEM" "$type"
  35. is_object_valid 'user' 'USER' "$user"
  36. is_object_unsuspended 'user' 'USER' "$user"
  37. is_object_new 'db' 'DB' "$database"
  38. get_next_dbhost
  39. is_object_valid "../../../conf/$type" 'HOST' "$host"
  40. is_object_unsuspended "../../../conf/$type" 'DBHOST' "$host"
  41. #is_charset_valid
  42. is_package_full 'DATABASES'
  43. is_password_valid
  44. dbpass="$password"
  45. #----------------------------------------------------------#
  46. # Action #
  47. #----------------------------------------------------------#
  48. # Switching on db type
  49. case $type in
  50. mysql) add_mysql_database ;;
  51. pgsql) add_pgsql_database ;;
  52. esac
  53. #----------------------------------------------------------#
  54. # Vesta #
  55. #----------------------------------------------------------#
  56. # Generating timestamp
  57. time_n_date=$(date +'%T %F')
  58. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  59. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  60. # Adding db to db conf
  61. str="DB='$database' DBUSER='$dbuser' MD5='$md5' HOST='$host' TYPE='$type'"
  62. str="$str CHARSET='$charset' U_DISK='0' SUSPENDED='no' TIME='$time'"
  63. str="$str DATE='$date'"
  64. echo "$str" >> $USER_DATA/db.conf
  65. chmod 660 $USER_DATA/db.conf
  66. # Increasing counters
  67. increase_dbhost_values
  68. increase_user_value "$user" '$U_DATABASES'
  69. # Logging
  70. log_history "added $type database $database"
  71. log_event "$OK" "$ARGUMENTS"
  72. exit