1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/nextcloud_ynh.git synced 2024-09-03 19:55:57 +02:00

[fix] Rename MySQL database and user when migrating from ownCloud

This commit is contained in:
Jérôme Lebleu 2016-09-26 15:28:01 +02:00
parent 6adad2ed04
commit 8420807556
2 changed files with 44 additions and 1 deletions

View file

@ -80,3 +80,20 @@ is_url_handled() {
# it's handled if it does not redirect to the SSO nor return 404 # it's handled if it does not redirect to the SSO nor return 404
[[ ! ${OUTPUT[0]} =~ \/yunohost\/sso\/ && ${OUTPUT[1]} != 404 ]] [[ ! ${OUTPUT[0]} =~ \/yunohost\/sso\/ && ${OUTPUT[1]} != 404 ]]
} }
# Rename a MySQL database and user
# usage: rename_mysql_db DBNAME DBUSER DBPASS NEW_DBNAME NEW_DBUSER
rename_mysql_db() {
local DBNAME=$1 DBUSER=$2 DBPASS=$3 NEW_DBNAME=$4 NEW_DBUSER=$5
local SQLPATH="/tmp/${DBNAME}-$(date '+%s').sql"
# dump the old database
mysqldump -u "$DBUSER" -p"$DBPASS" --no-create-db "$DBNAME" > "$SQLPATH"
# create the new database and user
ynh_mysql_create_db "$NEW_DBNAME" "$NEW_DBUSER" "$DBPASS"
ynh_mysql_connect_as "$NEW_DBUSER" "$DBPASS" "$NEW_DBNAME" < "$SQLPATH"
# remove the old database
ynh_mysql_drop_db "$DBNAME"
ynh_mysql_drop_user "$DBUSER"
rm "$SQLPATH"
}

View file

@ -22,9 +22,12 @@ if [[ $YNH_APP_INSTANCE_NAME != $app ]]; then
(sudo yunohost app list --installed -f "$app" | grep -q id) \ (sudo yunohost app list --installed -f "$app" | grep -q id) \
&& ynh_die "Nextcloud is already installed" && ynh_die "Nextcloud is already installed"
# retrieve ownCloud domain setting # retrieve ownCloud app settings
real_app=$YNH_APP_INSTANCE_NAME real_app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get "$real_app" domain) domain=$(ynh_app_setting_get "$real_app" domain)
oc_dbpass=$(ynh_app_setting_get "$real_app" mysqlpwd)
oc_dbname=$real_app
oc_dbuser=$real_app
# remove nginx and php-fpm configuration files # remove nginx and php-fpm configuration files
sudo rm -f \ sudo rm -f \
@ -50,11 +53,34 @@ if [[ $YNH_APP_INSTANCE_NAME != $app ]]; then
sudo sed -ri "s#^(\s*'datadirectory' =>).*,#\1 '${DATADIR}',#" \ sudo sed -ri "s#^(\s*'datadirectory' =>).*,#\1 '${DATADIR}',#" \
"/var/www/${app}/config/config.php" "/var/www/${app}/config/config.php"
# rename the MySQL database
rename_mysql_db "$oc_dbname" "$oc_dbuser" "$oc_dbpass" "$dbname" "$dbuser"
sudo sed -ri "s#^(\s*'dbname' =>).*,#\1 '${dbname}',#" \
"/var/www/${app}/config/config.php"
sudo sed -ri "s#^(\s*'dbuser' =>).*,#\1 '${dbuser}',#" \
"/var/www/${app}/config/config.php"
# rename ownCloud system group and account # rename ownCloud system group and account
sudo groupmod -n "$app" "$real_app" sudo groupmod -n "$app" "$real_app"
sudo usermod -l "$app" "$real_app" sudo usermod -l "$app" "$real_app"
else else
real_app=$app real_app=$app
# handle old migrations from ownCloud
curr_dbname=$(sudo cat "/var/www/${app}/config/config.php" \
| grep dbname | sed "s|.*=> '\(.*\)'.*|\1|g")
if [[ $curr_dbname != $dbname ]]; then
curr_dbuser=$(sudo cat "/var/www/${app}/config/config.php" \
| grep dbuser | sed "s|.*=> '\(.*\)'.*|\1|g")
dbpass=$(ynh_app_setting_get "$real_app" mysqlpwd)
# rename the MySQL database
rename_mysql_db "$curr_dbname" "$curr_dbuser" "$dbpass" "$dbname" "$dbuser"
sudo sed -ri "s#^(\s*'dbname' =>).*,#\1 '${dbname}',#" \
"/var/www/${app}/config/config.php"
sudo sed -ri "s#^(\s*'dbuser' =>).*,#\1 '${dbuser}',#" \
"/var/www/${app}/config/config.php"
fi
fi fi
# Retrieve app settings # Retrieve app settings