v_add_db_host 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # info: add new database server
  3. # options: type host port db_user db_password [max_db] [tpl]
  4. #
  5. # The function add new database server to the server pool. It supports local
  6. # and remote database servers, which is useful for clusters. By adding a host
  7. # you can set limit for number of databases on a host. Template parameter is
  8. # used only for PostgreSQL and has an default value "template1". You can read
  9. # more about templates in official PostgreSQL documentation.
  10. #----------------------------------------------------------#
  11. # Variable&Function #
  12. #----------------------------------------------------------#
  13. # Argument defenition
  14. type=$1
  15. host=$2
  16. port=$3
  17. db_user=$4
  18. db_password=$5
  19. max_db=${6-300}
  20. template=${7-template1}
  21. # Importing variables
  22. source $VESTA/conf/vars.conf
  23. source $V_CONF/vesta.conf
  24. source $V_FUNC/shared.func
  25. source $V_FUNC/db.func
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. # Checking arg number
  30. args_usage='type host port db_user db_password [max_db] [tpl]'
  31. check_args '5' "$#" "$args_usage"
  32. # Checking argument format
  33. format_validation 'host' 'port' 'db_user' 'db_password' 'max_db'
  34. format_validation 'template'
  35. # Checking db system is enabled
  36. is_system_enabled 'db'
  37. # Checking db type
  38. is_type_valid 'db' "$type"
  39. # Checking host existance
  40. is_db_host_new
  41. # Checking host connection
  42. case $type in
  43. mysql) is_mysql_host_alive ;;
  44. pgsql) is_pgsql_host_alive ;;
  45. esac
  46. #----------------------------------------------------------#
  47. # Action #
  48. #----------------------------------------------------------#
  49. # Concatentating db host string
  50. case $type in
  51. mysql) new_str="HOST='$host' USER='$db_user' PASSWORD='$db_password'";
  52. new_str="$new_str PORT='$port' MAX_DB='$max_db' U_SYS_USERS=''";
  53. new_str="$new_str U_DB_BASES='0' ACTIVE='yes' DATE='$V_DATE'";;
  54. pgsql) new_str="HOST='$host' USER='$db_user' PASSWORD='$db_password'";
  55. new_str="$new_str PORT='$port' TPL='$template'";
  56. new_str="$new_str MAX_DB='$max_db' U_SYS_USERS=''";
  57. new_str="$new_str U_DB_BASES='0' ACTIVE='yes' DATE='$V_DATE'";;
  58. esac
  59. # Adding host to conf
  60. echo "$new_str" >> $V_DB/$type.conf
  61. chmod 640 $V_DB/$type.conf
  62. #----------------------------------------------------------#
  63. # Vesta #
  64. #----------------------------------------------------------#
  65. # Hidding db pass
  66. V_EVENT=$(echo $V_EVENT | sed -e "s/$db_password/xxxxxx/g")
  67. # Logging
  68. log_event 'system' "$V_EVENT"
  69. exit