v-change-database-owner 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/bash
  2. # info: change database owner
  3. # options: DATABASE USER
  4. #
  5. # The function for changing database owner.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. database=$1
  11. user=$2
  12. # Includes
  13. source $VESTA/func/main.sh
  14. source $VESTA/func/db.sh
  15. source $VESTA/func/rebuild.sh
  16. source $VESTA/conf/vesta.conf
  17. #----------------------------------------------------------#
  18. # Verifications #
  19. #----------------------------------------------------------#
  20. check_args '2' "$#" 'DATABASE USER'
  21. is_format_valid 'database' 'user'
  22. is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
  23. is_object_valid 'user' 'USER' "$user"
  24. is_object_unsuspended 'user' 'USER' "$user"
  25. # Check owner existance
  26. owner=$(echo $database | cut -f 1 -d '_')
  27. if [ ! -e "$VESTA/data/users/$owner" ]; then
  28. echo "Error: database owner doesn't exist"
  29. log_event "$E_NOTEXIST" "$ARGUMENTS"
  30. exit $E_NOTEXIST
  31. fi
  32. # Check if owner is the same as the dst user
  33. if [ "$owner" = "$user" ]; then
  34. exit
  35. fi
  36. # Check db existance
  37. db_data=$(grep "DB='$database'" $VESTA/data/users/$owner/db.conf)
  38. if [ -z "$db_data" ]; then
  39. echo "Error: database $database doesn't exist"
  40. log_event "$E_NOTEXIST" "$ARGUMENTS"
  41. exit $E_NOTEXIST
  42. fi
  43. # Check if datbase name is uniq
  44. new_db=$(echo $database | sed "s/^${owner}_/${user}_/")
  45. check_db=$(grep "DB='$new_db'" $VESTA/data/users/$user/db.conf)
  46. if [ ! -z "$check_db" ]; then
  47. echo "Error: $new_db database exists"
  48. log_event "$E_EXISTS" "$ARGUMENTS"
  49. exit $E_EXISTS
  50. fi
  51. #----------------------------------------------------------#
  52. # Action #
  53. #----------------------------------------------------------#
  54. # Creating temporary directory
  55. tmpdir=$(mktemp -p $BACKUP -d)
  56. if [ "$?" -ne 0 ]; then
  57. echo "Error: can't create $tmpdir"
  58. log_event "$E_NOTEXIST" "$ARGUMENTS"
  59. exit $E_NOTEXIST
  60. fi
  61. # Suspend database
  62. $BIN/v-suspend-database $owner $database > /dev/null 2>&1
  63. # Dump database
  64. eval $db_data
  65. dump="$tmpdir/$database.$TYPE.sql"
  66. grants="$tmpdir/$database.$TYPE.$DBUSER"
  67. case $TYPE in
  68. mysql) dump_mysql_database ;;
  69. pgsql) dump_pgsql_database ;;
  70. esac
  71. # Import configuration
  72. db_data=$(echo "$db_data" | sed "s/'${owner}_/'${user}_/g")
  73. echo "$db_data" >> $VESTA/data/users/$user/db.conf
  74. eval $db_data
  75. # Unsuspend db
  76. $BIN/v-unsuspend-database $user $new_db > /dev/null 2>&1
  77. # Rebuild databases
  78. $BIN/v-rebuild-databases $user
  79. # Import dump
  80. case $TYPE in
  81. mysql) import_mysql_database $dump ;;
  82. pgsql) import_pgsql_database $dump ;;
  83. esac
  84. # Deleting tmpdir
  85. rm -rf $tmpdir
  86. # Remove old database
  87. $BIN/v-unsuspend-database $owner $database > /dev/null 2>&1
  88. $BIN/v-delete-database $owner $database > /dev/null 2>&1
  89. # Update counters
  90. $BIN/v-update-user-counters $owner
  91. $BIN/v-update-user-counters $user
  92. #----------------------------------------------------------#
  93. # Vesta #
  94. #----------------------------------------------------------#
  95. # Logging
  96. log_event "$OK" "$ARGUMENTS"
  97. exit