mirror of
https://github.com/myvesta/vesta
synced 2025-08-14 10:37:42 -07:00
change database owner
This commit is contained in:
parent
cd89023ddf
commit
f34738f04d
1 changed files with 122 additions and 0 deletions
122
bin/v-change-database-owner
Executable file
122
bin/v-change-database-owner
Executable file
|
@ -0,0 +1,122 @@
|
|||
#!/bin/bash
|
||||
# info: change database password
|
||||
# options: DATABASE USER
|
||||
#
|
||||
# The function for changing database owner.
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Variable&Function #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Argument defenition
|
||||
database=$1
|
||||
user=$2
|
||||
|
||||
# Includes
|
||||
source $VESTA/func/main.sh
|
||||
source $VESTA/func/db.sh
|
||||
source $VESTA/func/rebuild.sh
|
||||
source $VESTA/conf/vesta.conf
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Verifications #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
check_args '2' "$#" 'DATABASE USER'
|
||||
validate_format 'database' 'user'
|
||||
is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
|
||||
is_object_valid 'user' 'USER' "$user"
|
||||
is_object_unsuspended 'user' 'USER' "$user"
|
||||
|
||||
# Check owner existance
|
||||
owner=$(echo $database | cut -f 1 -d '_')
|
||||
if [ ! -e "$VESTA/data/users/$owner" ]; then
|
||||
echo "Error: database owner doesn't exist"
|
||||
log_event "$E_NOTEXIST" "$EVENT"
|
||||
exit $E_NOTEXIST
|
||||
fi
|
||||
|
||||
# Check if owner is the same as the dst user
|
||||
if [ "$owner" = "$user" ]; then
|
||||
exit
|
||||
fi
|
||||
|
||||
# Check db existance
|
||||
db_data=$(grep "DB='$database'" $VESTA/data/users/$owner/db.conf)
|
||||
if [ -z "$db_data" ]; then
|
||||
echo "Error: database $database doesn't exist"
|
||||
log_event "$E_NOTEXIST" "$EVENT"
|
||||
exit $E_NOTEXIST
|
||||
fi
|
||||
|
||||
# Check if datbase name is uniq
|
||||
new_db=$(echo $database | sed "s/^${owner}_/${user}_/")
|
||||
check_db=$(grep "DB='$new_db'" $VESTA/data/users/$user/db.conf)
|
||||
if [ ! -z "$check_db" ]; then
|
||||
echo "Error: $new_db database exists"
|
||||
log_event "$E_EXISTS" "$EVENT"
|
||||
exit $E_EXISTS
|
||||
fi
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Action #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Creating temporary directory
|
||||
tmpdir=$(mktemp -p $BACKUP -d)
|
||||
if [ "$?" -ne 0 ]; then
|
||||
echo "Error: can't create $tmpdir"
|
||||
log_event "$E_NOTEXIST" "$EVENT"
|
||||
exit $E_NOTEXIST
|
||||
fi
|
||||
|
||||
# Suspend database
|
||||
$BIN/v-suspend-database $owner $database > /dev/null 2>&1
|
||||
|
||||
# Dump database
|
||||
eval $db_data
|
||||
dump="$tmpdir/$database.$TYPE.sql"
|
||||
grants="$tmpdir/$database.$TYPE.$DBUSER"
|
||||
send_mail='/bin/true'
|
||||
case $TYPE in
|
||||
mysql) dump_mysql_database ;;
|
||||
pgsql) dump_pgsql_database ;;
|
||||
esac
|
||||
|
||||
# Import configuration
|
||||
db_data=$(echo "$db_data" | sed "s/'${owner}_/'${user}_/g")
|
||||
echo "$db_data" >> $VESTA/data/users/$user/db.conf
|
||||
eval $db_data
|
||||
|
||||
# Unsuspend db
|
||||
$BIN/v-unsuspend-database $user $new_db > /dev/null 2>&1
|
||||
|
||||
# Rebuild databases
|
||||
$BIN/v-rebuild-databases $user
|
||||
|
||||
# Import dump
|
||||
case $TYPE in
|
||||
mysql) import_mysql_database $dump ;;
|
||||
pgsql) import_pgsql_database $dump ;;
|
||||
esac
|
||||
|
||||
# Remove old database
|
||||
$BIN/v-unsuspend-database $owner $database > /dev/null 2>&1
|
||||
$BIN/v-delete-database $owner $database > /dev/null 2>&1
|
||||
|
||||
# Update counters
|
||||
$BIN/v-update-user-counters $owner
|
||||
$BIN/v-update-user-counters $user
|
||||
|
||||
|
||||
#----------------------------------------------------------#
|
||||
# Vesta #
|
||||
#----------------------------------------------------------#
|
||||
|
||||
# Logging
|
||||
log_event "$OK" "$EVENT"
|
||||
|
||||
exit
|
Loading…
Add table
Add a link
Reference in a new issue