diff --git a/scripts/_common.sh b/scripts/_common.sh index 68b0aa9..8a1646a 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,3 +1,14 @@ +### Constants + +nginx_conf_path="/etc/nginx/conf.d/${domain}.d/ihatemoney.conf" +supervisor_conf_path="/etc/supervisor/conf.d/ihatemoney.conf" +gunicorn_conf_path="/etc/ihatemoney/gunicorn.conf.py" +ihatemoney_conf_path="/etc/ihatemoney/settings.py" +INSTALL_DIR="/opt/yunohost/ihatemoney" + + +### Functions + fetch_and_extract() { local DESTDIR=$1 local OWNER_USER=${2:-admin} diff --git a/scripts/backup b/scripts/backup new file mode 100644 index 0000000..0a8fb3d --- /dev/null +++ b/scripts/backup @@ -0,0 +1,41 @@ +#!/bin/bash +set -eu + +# Source YunoHost helpers +source /usr/share/yunohost/helpers + +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME + +# Set app specific variables +dbname=$app +dbuser=$app + +INSTALL_DIR=/opt/yunohost/ihatemoney + +# Retrieve app settings +domain=$(ynh_app_setting_get "$app" domain) +path=$(ynh_app_setting_get "$app" path) +dbpass=$(ynh_app_setting_get "$app" mysqlpwd) + +# Source local utils +if [ ! -e _common.sh ]; then + # Fetch the local helpers files, because unavail to restore script + sudo cp ../settings/scripts/_common.sh ./_common.sh + sudo chmod a+rx _common.sh +fi + +source _common.sh + +# Backup conf files +mkdir ./conf +ynh_backup "$nginx_conf_path" "conf/nginx.conf" +ynh_backup "$gunicorn_conf_path" "conf/gunicorn.conf.py" +ynh_backup "$supervisor_conf_path" "conf/supervisor.conf" +ynh_backup "$ihatemoney_conf_path" "conf/settings.py" + +# Dump the database +mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./db.sql + +# Backup code and venv +ynh_backup "$INSTALL_DIR" "install_dir" diff --git a/scripts/install b/scripts/install index 102d406..ce9b2e5 100755 --- a/scripts/install +++ b/scripts/install @@ -4,15 +4,15 @@ set -eu # Source YunoHost helpers source /usr/share/yunohost/helpers -# Source local utils -source _common.sh - # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN path=$YNH_APP_ARG_PATH is_public=$YNH_APP_ARG_IS_PUBLIC app=ihatemoney +# Source local utils +source _common.sh + # Database settings db_pwd=$(ynh_string_random) db_name=$app diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..31bc7f9 --- /dev/null +++ b/scripts/restore @@ -0,0 +1,76 @@ +#!/bin/bash +set -eu + +# Source app helpers +source /usr/share/yunohost/helpers + +# Get multi-instances specific variables +app=$YNH_APP_INSTANCE_NAME + +# Set app specific variables +dbname=$app +dbuser=$app + +# 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) + + +# Source local utils +if [ ! -e _common.sh ]; then + # Fetch the local helpers files, because unavail to restore script + sudo cp ../settings/scripts/_common.sh ./_common.sh + sudo chmod a+rx _common.sh +fi + +source _common.sh + + +sudo yunohost app checkurl "${domain}${path}" -a "$app" \ + || ynh_die "The path ${domain}${path} is not available for app installation." + +test -d $INSTALL_DIR && ynh_die \ +"The destination directory '$INSTALL_DIR' already exists.\ + You should safely delete it before restoring this app." + +test -f $nginx_conf_path && ynh_die \ +"The NGINX configuration already exists at '${nginx_conf_path}'. + You should safely delete it before restoring this app." + +test -f $supervisor_conf_path && ynh_die \ +"The Supervisor configuration already exists at '${supervisor_conf_path}'. + You should safely delete it before restoring this app." + +test -f $gunicorn_conf_path && ynh_die \ +"The Gunicorn configuration already exists at '${gunicorn_conf_path}'. + You should safely delete it before restoring this app." + +# Create the dedicated user +sudo useradd ihatemoney -d /opt/yunohost/ihatemoney/ --create-home + +# Restore the app files +sudo cp -a ./install_dir/. "$INSTALL_DIR" +fix_permissions ${INSTALL_DIR}/src + +# Create and restore the database +ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" +ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./db.sql + +# Create various dirs +sudo install -o ihatemoney -g ihatemoney -m 755 \ + -d /var/log/ihatemoney /etc/ihatemoney + +# Restore settings +sudo cp -a "conf/nginx.conf" "$nginx_conf_path" +sudo cp -a "conf/gunicorn.conf.py" "$gunicorn_conf_path" +sudo cp -a "conf/supervisor.conf" "$supervisor_conf_path" +sudo cp -a "conf/settings.py" "$ihatemoney_conf_path" + +# Install debian packages dependencies +sudo apt-get install -y -qq python-dev python-virtualenv supervisor libmysqlclient-dev + +# Reload +sudo systemctl reload nginx +sudo systemctl restart supervisor +sudo supervisorctl restart budget