v-add-database 3.1 KB

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