1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/agendav_ynh.git synced 2024-09-03 20:36:12 +02:00

Restructure restore script

This commit is contained in:
Jean-Baptiste Holcroft 2017-10-30 12:48:51 +01:00
parent 4faefcecab
commit 79d530f6bc

View file

@ -1,14 +1,32 @@
#!/bin/bash
# Source local helpers
source ./_common.sh
# Source app helpers
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
if [ ! -e _common.sh ]; then
# Get the _common.sh file if it's not in the current directory
cp ../settings/scripts/_common.sh ./_common.sh
chmod a+rx _common.sh
fi
source _common.sh
source /usr/share/yunohost/helpers
# Abort script if errors
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
# Set app specific variables
app="$YNH_APP_INSTANCE_NAME"
dbname=$app
@ -17,67 +35,92 @@ dbuser=$app
# Retrieve old app settings
domain=$(ynh_app_setting_get "$app" domain)
path_url=$(ynh_app_setting_get "$app" path)
final_path=$(ynh_app_setting_get "$app" final_path)
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
# Check web path availability
ynh_webpath_available "$domain" "$path_url"
LOGDIR=/var/log/$app
# Install dependencies
ynh_install_app_dependencies "php5-cli"
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
# Check destination directory
final_path="/var/www/$app"
ynh_restore_file "$final_path"
ynh_webpath_available "$domain" "$path_url" \
|| ynh_die "Path not available: ${domain}${path_url}"
test ! -d "$final_path" \
|| ynh_die "There is already a directory: $final_path "
# Check whether Baïkal or Radicale is installed
if yunohost app list --installed -f baikal | grep -q id ; then
caldav_app="baikal"
caldav_baseurl="/cal.php/"
elif yunohost app list --installed -f radicale | grep -q id ; then
caldav_app="radicale"
caldav_baseurl="/"
else
ynh_die "You must install Baïkal or Radicale before"
fi
ynh_system_user_create "$app" "$final_path"
# Protect source code against modifications
chown -hR root: "$final_path"
# Only agendav user should write here
chown -hR $app: "${final_path}/web/var/cache/"{profiler,twig}
chmod -R 750 "${final_path}/web/var/cache/"{profiler,twig}
# The agendav user should read here, but does not need to write
# Other users should not be able to read as it stores passwords.
find "${final_path}/web/config" -type f -exec chown root:$app {} \; -exec chmod 640 {} \;
find "${final_path}/web/config" -type d -exec chown root:$app {} \; -exec chmod 750 {} \;
# Clean caches
ynh_secure_remove "${final_path}/web/var/cache/"{profiler,twig}/*
# CalDAV config
caldav_domain=$(ynh_app_setting_get "$caldav_app" domain)
caldav_path=$(ynh_app_setting_get "$caldav_app" path)
caldav_url="https://${caldav_domain}${caldav_path%/}"
conf_path="${final_path}/web/config/settings.php"
sed -i "s@^\(\$app\['caldav.baseurl'\] = \).*\
@\1'${caldav_url}${caldav_baseurl}';@g" "$conf_path"
sed -i "s@^\(\$app\['caldav.baseurl.public'\] = \).*\
@\1'${caldav_domain}';@g" "$conf_path"
# Create log directory
install -m 750 -o www-data -g adm -d "/var/log/${app}"
# Create and restore the database
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql
#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
# Restore configuration files
ynh_restore_file "/etc/nginx/conf.d/${domain}.d/${app}.conf"
#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_restore_file "$final_path"
#=================================================
# RESTORE THE MYSQL DATABASE
#=================================================
# Create and restore the database
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./db.sql
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
ynh_system_user_create "$app" "$final_path"
#=================================================
# RESTORE USER RIGHTS
#=================================================
chown -R root: "$final_path"
# Only agendav user should write here
chown -R "$app" "${final_path}/web/var/cache/"{profiler,twig}
# The agendav user should read here, but does not need to write
chown -R root:"$app" "${final_path}/web/config/"
chmod -R g+rx "${final_path}/web/config/"
# Other users should not be able to read as it stores passwords.
chmod -R o-rwx "${final_path}/web/config/"
#=================================================
# RESTORE THE PHP-FPM CONFIGURATION
#=================================================
ynh_restore_file "/etc/php5/fpm/pool.d/${app}.conf"
# Reload services
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
# Install dependencies
ynh_install_app_dependencies php5-cli
#=================================================
# SETUP LOG directory
#=================================================
mkdir -p "$LOGDIR"
chown -R "$app": "$LOGDIR"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
service php5-fpm restart
service nginx reload