1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/baikal_ynh.git synced 2024-09-03 18:16:11 +02:00
baikal_ynh/scripts/restore

62 lines
1.9 KiB
Bash

#!/bin/bash
# Exit on command errors and treat unset variables as an error
set -eu
# Retrieve app id
app=$YNH_APP_INSTANCE_NAME
# Source app helpers
. /usr/share/yunohost/helpers
# Retrieve old app settings
domain=$(ynh_app_setting_get "$app" domain)
path=$(ynh_app_setting_get "$app" path)
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
# Set app specific variables
dbname=$app
dbuser=$app
# Check domain/path availability
sudo yunohost app checkurl "${domain}${path}" -a "$app" \
|| exit 1
# Check destination directory
DESTDIR="/var/www/$app"
[[ -d $DESTDIR ]] && ynh_die \
"The destination directory '$DESTDIR' already exists.\
You should safely delete it before restoring this app."
# Check configuration files
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
[[ -f $nginx_conf ]] && ynh_die \
"The NGINX configuration already exists at '${nginx_conf}'.
You should safely delete it before restoring this app."
phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf"
[[ -f $phpfpm_conf ]] && ynh_die \
"The PHP FPM configuration already exists at '${phpfpm_conf}'.
You should safely delete it before restoring this app."
# Restore the app files and set permissions
sudo cp -a ./sources "$DESTDIR"
if ! id -u $app > /dev/null 2>&1 ; then
sudo useradd -c "$app system account" \
-d /var/www/$app --system --user-group $app --shell /usr/sbin/nologin \
|| ynh_die "Unable to create $app system account"
fi
sudo chown -hR root: "$DESTDIR"
sudo chown $app:root "$DESTDIR/Specific/"{config.php,config.system.php}
sudo chmod 640 "$DESTDIR/Specific/"{config.php,config.system.php}
# Create and restore the database
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"
ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./dump.sql
# Restore configuration files
sudo cp -a ./nginx.conf "$nginx_conf"
sudo cp -a ./php-fpm.conf "$phpfpm_conf"
# Reload services
sudo service php5-fpm restart || true
sudo service nginx reload || true