v-add-database 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/main.sh
  27. source $VESTA/func/db.sh
  28. # Hiding password
  29. A4='******'
  30. EVENT="DATE='$DATE' TIME='$TIME' CMD='$SCRIPT' A1='$A1' A2='$A2' A3='$A3'"
  31. EVENT="$EVENT A4='$A4' A5='$A5' A6='$A6' A7='$A7' A8='$A8' A9='$A9'"
  32. #----------------------------------------------------------#
  33. # Verifications #
  34. #----------------------------------------------------------#
  35. check_args '5' "$#" 'USER DATABASE DBUSER DBPASS TYPE [HOST] [CHARSET]'
  36. validate_format 'user' 'database' 'dbuser' 'dbpass' 'charset'
  37. is_system_enabled "$DB_SYSTEM"
  38. is_type_valid "$DB_SYSTEM" "$type"
  39. is_object_valid 'user' 'USER' "$user"
  40. is_object_unsuspended 'user' 'USER' "$user"
  41. is_object_free 'db' 'DB' "$database"
  42. get_next_dbhost
  43. is_object_valid "../../../conf/$type" 'HOST' "$host"
  44. is_object_unsuspended "../../../conf/$type" 'HOST' "$host"
  45. #is_charset_valid
  46. is_package_full 'DATABASES'
  47. #----------------------------------------------------------#
  48. # Action #
  49. #----------------------------------------------------------#
  50. # Switching on db type
  51. case $type in
  52. mysql) add_mysql_database ;;
  53. pgsql) add_pgsql_database ;;
  54. esac
  55. #----------------------------------------------------------#
  56. # Vesta #
  57. #----------------------------------------------------------#
  58. # Increasing counters
  59. increase_dbhost_values
  60. increase_user_value "$user" '$U_DATABASES'
  61. # Adding db to db conf
  62. str="DB='$database' DBUSER='$dbuser' MD5='$md5' HOST='$host' TYPE='$type'"
  63. str="$str CHARSET='$charset' U_DISK='0' SUSPENDED='no' TIME='$TIME'"
  64. str="$str DATE='$DATE'"
  65. echo "$str" >> $USER_DATA/db.conf
  66. chmod 660 $USER_DATA/db.conf
  67. # Logging
  68. log_history "added $type database $database"
  69. log_event "$OK" "$EVENT"
  70. exit