v_add_db_base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # info: add database
  3. # options: user db db_user db_password type [host] [encoding]
  4. #
  5. # The function creates the database concatenating username and user_db.
  6. # Supported yypes 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 defenition
  16. user=$1
  17. database="$user"_"$2"
  18. db_user="$user"_"$3"
  19. db_password=$4
  20. type=$5
  21. host=$6
  22. encoding=${7-UTF8}
  23. encoding=$(echo "$encoding" |tr '[:lower:]' '[:upper:]')
  24. # Importing variables
  25. source $VESTA/conf/vars.conf
  26. source $V_CONF/vesta.conf
  27. source $V_FUNC/shared.func
  28. source $V_FUNC/db.func
  29. #----------------------------------------------------------#
  30. # Verifications #
  31. #----------------------------------------------------------#
  32. # Checking arg number
  33. check_args '5' "$#" 'user db db_user db_password type [host] [encoding]'
  34. # Checking argument format
  35. format_validation 'user' 'database' 'db_user' 'db_password' 'encoding'
  36. # Checking db system is enabled
  37. is_system_enabled 'db'
  38. # Checking db type
  39. is_type_valid 'db' "$type"
  40. # Checking user
  41. is_user_valid
  42. # Checking user is active
  43. is_user_suspended
  44. # Checking db existance
  45. is_db_new
  46. # Checking db host
  47. if [ -z "$host" ]; then
  48. host=$(get_next_db_host)
  49. fi
  50. is_db_host_valid
  51. # Checking package
  52. is_package_full 'db_base'
  53. #----------------------------------------------------------#
  54. # Action #
  55. #----------------------------------------------------------#
  56. # Switching on db type
  57. case $type in
  58. mysql) create_db_mysql ;;
  59. pgsql) create_db_pgsql ;;
  60. esac
  61. #----------------------------------------------------------#
  62. # Vesta #
  63. #----------------------------------------------------------#
  64. # Increasing db value
  65. increase_db_value
  66. # Increasing domain value
  67. increase_user_value "$user" '$U_DATABASES'
  68. # Adding db to db conf
  69. v_str="DB='$database' USER='$db_user' HOST='$host' TYPE='$type'"
  70. v_str="$v_str CHARSET='$encoding' U_DISK='0' SUSPEND='no' DATE='$V_DATE'"
  71. echo "$v_str">>$V_USERS/$user/db.conf
  72. # Hiding password
  73. V_EVENT="$V_DATE $V_SCRIPT $user $database $db_user ***** $type $host"
  74. V_EVENT="$V_EVENT $encoding"
  75. # Logging
  76. log_history "$V_EVENT" "v_delete_db_base $user $database"
  77. log_event 'system' "$V_EVENT"
  78. exit