Skip to content

Commit f34738f

Browse files
committed
change database owner
1 parent cd89023 commit f34738f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

bin/v-change-database-owner

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

0 commit comments

Comments
 (0)