v_add_database 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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. dbuser="$user"_"$3"
  19. dbpass=$4
  20. type=$5
  21. host=$6
  22. charset=${7-UTF8}
  23. charset=$(echo "$charset" |tr '[:lower:]' '[:upper:]')
  24. # Includes
  25. source $VESTA/conf/vesta.conf
  26. source $VESTA/func/shared.sh
  27. source $VESTA/func/db.sh
  28. #----------------------------------------------------------#
  29. # Verifications #
  30. #----------------------------------------------------------#
  31. check_args '5' "$#" 'user database dbuser dbpass type [host] [charset]'
  32. validate_format 'user' 'database' 'dbuser' 'dbpass' 'charset'
  33. is_system_enabled "$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_free 'db' 'DB' "$database"
  38. get_next_dbhost
  39. is_object_valid "../../../conf/$type" 'HOST' "$host"
  40. is_object_unsuspended "../../../conf/$type" 'HOST' "$host"
  41. is_charset_valid
  42. is_package_full 'DATABASES'
  43. #----------------------------------------------------------#
  44. # Action #
  45. #----------------------------------------------------------#
  46. # Switching on db type
  47. case $type in
  48. mysql) add_mysql_database ;;
  49. pgsql) add_pgsql_database ;;
  50. esac
  51. #----------------------------------------------------------#
  52. # Vesta #
  53. #----------------------------------------------------------#
  54. # Increasing counters
  55. increase_dbhost_values
  56. increase_user_value "$user" '$U_DATABASES'
  57. # Adding db to db conf
  58. str="DB='$database' DBUSER='$dbuser' HOST='$host' TYPE='$type'"
  59. str="$str CHARSET='$charset' U_DISK='0' SUSPENDED='no' TIME='$TIME'"
  60. str="$str DATE='$DATE'"
  61. echo "$str" >> $USER_DATA/db.conf
  62. chmod 660 $USER_DATA/db.conf
  63. # Hiding password
  64. EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT'"
  65. EVENT="$EVENT ARGUMENTS='$user $database $dbuser ***** $type $host $charset'"
  66. # Logging
  67. log_history "$EVENT"
  68. log_event "$OK" "$EVENT"
  69. exit