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

apply example_ynh on backup, restore and upgrade

This commit is contained in:
Yalh 2019-01-27 23:16:09 +01:00
parent 814da2ff4f
commit 30645d06e0
3 changed files with 352 additions and 150 deletions

View file

@ -1,31 +1,78 @@
#!/bin/bash #!/bin/bash
# Exit on command errors and treat unset variables as an error #=================================================
set -eu # GENERIC START
app=$YNH_APP_INSTANCE_NAME #=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Source app helpers source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# Retrieve app settings #=================================================
domain=$(ynh_app_setting_get "$app" domain) # MANAGE SCRIPT FAILURE
path=$(ynh_app_setting_get "$app" path) #=================================================
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
# Copy the app files ynh_clean_setup () {
DESTDIR="/var/www/$app" ### Remove this function if there's nothing to clean before calling the remove script.
ynh_backup "$DESTDIR" "sources" true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
# Copy the conf files #=================================================
ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" "nginx.conf" # LOAD SETTINGS
#=================================================
# Copy dedicated php-fpm process to backup folder app=$YNH_APP_INSTANCE_NAME
ynh_backup "/etc/php5/fpm/pool.d/${app}.conf" "php-fpm.conf"
# Set app specific variables final_path=$(ynh_app_setting_get $app final_path)
dbname=$app domain=$(ynh_app_setting_get $app domain)
dbuser=$app db_name=$(ynh_app_setting_get $app db_name)
#=================================================
# STANDARD BACKUP STEPS
#=================================================
# BACKUP THE APP MAIN DIR
#=================================================
ynh_backup "$final_path"
#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================
ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# BACKUP THE PHP-FPM CONFIGURATION
#=================================================
ynh_backup "/etc/php5/fpm/pool.d/$app.conf"
#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================
ynh_mysql_dump_db "$db_name" > db.sql
#=================================================
# SPECIFIC BACKUP
#=================================================
# BACKUP LOGROTATE
#=================================================
#ynh_backup "/etc/logrotate.d/$app"
#=================================================
# BACKUP SYSTEMD
#=================================================
#ynh_backup "/etc/systemd/system/$app.service"
#=================================================
# BACKUP A CRON FILE
#=================================================
#ynh_backup "/etc/cron.d/$app"
# Dump the database
mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./db.sql
ynh_backup "db.sql" "dump.sql"

View file

@ -1,56 +1,127 @@
#!/bin/bash #!/bin/bash
# Exit on command errors and treat unset variables as an error #=================================================
set -eu # GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Get multi-instances specific variables source ../settings/scripts/_common.sh
app=$YNH_APP_INSTANCE_NAME source /usr/share/yunohost/helpers
# Source app helpers #=================================================
source /usr/share/yunohost/helpers # MANAGE SCRIPT FAILURE
#=================================================
# Retrieve old app settings ynh_clean_setup () {
domain=$(ynh_app_setting_get "$app" domain) #### Remove this function if there's nothing to clean before calling the remove script.
path=$(ynh_app_setting_get "$app" path) true
dbname=$app }
dbuser=$app # Exit if an error occurs during the execution of the script
dbpass=$(ynh_app_setting_get "$app" mysqlpwd) ynh_abort_if_errors
# Check domain/path availability #=================================================
sudo yunohost app checkurl "${domain}${path}" -a "$app" \ # LOAD SETTINGS
|| ynh_die #=================================================
# Check destination directory app=$YNH_APP_INSTANCE_NAME
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 domain=$(ynh_app_setting_get $app domain)
sudo cp -a ./sources "$DESTDIR" path_url=$(ynh_app_setting_get $app path)
final_path=$(ynh_app_setting_get $app final_path)
db_name=$(ynh_app_setting_get $app db_name)
# Create and restore the database #=================================================
ynh_mysql_create_db $dbname $dbuser $dbpass # CHECK IF THE APP CAN BE RESTORED
ynh_mysql_connect_as $dbuser $dbpass $dbname < ./dump.sql #=================================================
# Fix installation directories and permissions ynh_webpath_available $domain $path_url \
sudo mkdir -p "${DESTDIR}/logs" "${DESTDIR}/temp" || ynh_die "Path not available: ${domain}${path_url}"
sudo chown -R www-data: "$DESTDIR" test ! -d $final_path \
|| ynh_die "There is already a directory: $final_path "
# Restore configuration files #=================================================
sudo cp -a ./nginx.conf "$nginx_conf" # STANDARD RESTORATION STEPS
sudo cp -a ./php-fpm.conf "$phpfpm_conf" #=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
# Reload services ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
sudo service php5-fpm reload
sudo service nginx reload #=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_restore_file "$final_path"
#=================================================
# RESTORE THE MYSQL DATABASE
#=================================================
db_pwd=$(ynh_app_setting_get $app mysqlpwd)
ynh_mysql_setup_db $db_name $db_name $db_pwd
ynh_mysql_connect_as $db_name $db_pwd $db_name < ./db.sql
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
# Create the dedicated user (if not existing)
#ynh_system_user_create $app
#=================================================
# RESTORE USER RIGHTS
#=================================================
# Restore permissions on app files
chown -R www-data: $final_path
#=================================================
# RESTORE THE PHP-FPM CONFIGURATION
#=================================================
ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf"
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
# Define and install dependencies
#ynh_install_app_dependencies deb1 deb2
#=================================================
# RESTORE SYSTEMD
#=================================================
#ynh_restore_file "/etc/systemd/system/$app.service"
#systemctl enable $app.service
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
#yunohost service add $app --log "/var/log/$app/APP.log"
#=================================================
# RESTORE THE CRON FILE
#=================================================
#ynh_restore_file "/etc/cron.d/$app"
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
#ynh_restore_file "/etc/logrotate.d/$app"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
systemctl reload php5-fpm
systemctl reload nginx

View file

@ -1,105 +1,189 @@
#!/bin/bash #!/bin/bash
app=$YNH_APP_INSTANCE_NAME
rainloop_version=$(cat ../sources/rainloop_version)
# Source app helpers #=================================================
source /usr/share/yunohost/helpers # GENERIC START
source .fonctions #=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Backup the current version of the app, restore it if the upgrade fails source _common.sh
# Check if old backup exists source /usr/share/yunohost/helpers
if sudo yunohost backup list | grep -q $app-before-upgrade > /dev/null 2>&1;
then
sudo yunohost backup delete $app-before-upgrade
else
echo "no old backup to delete"
fi
sudo yunohost backup create --ignore-hooks --apps $app --name $app-before-upgrade --quiet
EXIT_PROPERLY () {
trap '' ERR
set +eu
sudo yunohost backup restore --ignore-hooks $app-before-upgrade --apps $app --force --quiet # Restore the backup if upgrade failed
ynh_die "Upgrade failed. The app was restored to the way it was before the failed upgrade."
}
set -eu
trap EXIT_PROPERLY ERR
# Retrieve arguments #=================================================
domain=$(ynh_app_setting_get "$app" domain) # LOAD SETTINGS
path=$(ynh_app_setting_get "$app" path) #=================================================
is_public=$(ynh_app_setting_get "$app" is_public)
password=$(ynh_app_setting_get "$app" password)
ldap=$(ynh_app_setting_get "$app" ldap)
language=$(ynh_app_setting_get "$app" language)
dp_pwd=$(ynh_app_setting_get "$app" mysqlpwd)
db_user=$app
plugins=$(ynh_app_setting_get "$app" plugins)
# Correct path using .fonctions app=$YNH_APP_INSTANCE_NAME
CHECK_PATH
# no update for db now... domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
#admin=$(ynh_app_setting_get $app admin)
is_public=$(ynh_app_setting_get $app is_public)
final_path=$(ynh_app_setting_get $app final_path)
language=$(ynh_app_setting_get $app language)
db_name=$(ynh_app_setting_get $app db_name)
password=$(ynh_app_setting_get "$app" password)
ldap=$(ynh_app_setting_get "$app" ldap)
plugins=$(ynh_app_setting_get "$app" plugins)
# Copy the new sources lang=$(ynh_app_setting_get $app lang)
language=$lang
ynh_app_setting_set $app language $language
rainloop_path=${final_path}/app
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set $app is_public 0
is_public=0
fi
# If db_name doesn't exist, create it
if [ -z $db_name ]; then
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set $app db_name $db_name
fi
# If final_path doesn't exist, create it
if [ -z $final_path ]; then
final_path=/var/www/$app final_path=/var/www/$app
final_path=${final_path}/app ynh_app_setting_set $app final_path $final_path
sudo rm -rf $final_path/rainloop # Remove the previous Rainloop files except data fi
# Download sources and keys #=================================================
sudo wget -q https://github.com/RainLoop/rainloop-webmail/releases/download/v${rainloop_version}/rainloop-community-${rainloop_version}.zip # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
sudo wget -q https://github.com/RainLoop/rainloop-webmail/releases/download/v${rainloop_version}/rainloop-community-${rainloop_version}.zip.asc #=================================================
sudo wget -q https://repository.rainloop.net/RainLoop.asc
# Verify the integrity of sources
sudo gpg --import --quiet RainLoop.asc
sudo gpg --verify --quiet rainloop-community-${rainloop_version}.zip.asc rainloop-community-${rainloop_version}.zip
sudo gpg --batch --delete-key --yes Rainloop
# Unzip and overwrite
sudo unzip -qq -o rainloop-community-${rainloop_version}.zip -d $final_path/
# Update ynh plugins: # Backup the current version of the app
sudo mkdir -p $final_path/data/_data_/_default_/plugins ynh_backup_before_upgrade
sudo cp -rf ../sources/plugins/auto-domain-grab $final_path/data/_data_/_default_/plugins/. ynh_clean_setup () {
sudo cp -rf ../sources/plugins/ynh-login-mapping $final_path/data/_data_/_default_/plugins/. # restore it if the upgrade fails
sudo cp -rf ../sources/plugins/ynh-ldap-suggestions $final_path/data/_data_/_default_/plugins/. ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path $path_url)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source "$rainloop_path"
#=================================================
# NGINX CONFIGURATION
#=================================================
# Create a dedicated nginx config
ynh_add_nginx_config
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
#ynh_install_app_dependencies deb1 deb2
#=================================================
# CREATE DEDICATED USER
#=================================================
# Create a dedicated user (if not existing)
#ynh_system_user_create $app
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
# Create a dedicated php-fpm config
ynh_add_fpm_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
# ...
#=================================================
application_file=$rainloop_path/data/_data_/_default_/configs/application.ini
# Update ynh plugins:
sudo mkdir -p $rainloop_path/data/_data_/_default_/plugins
sudo cp -rf ../sources/plugins/auto-domain-grab $rainloop_path/data/_data_/_default_/plugins/.
sudo cp -rf ../sources/plugins/ynh-login-mapping $rainloop_path/data/_data_/_default_/plugins/.
sudo cp -rf ../sources/plugins/ynh-ldap-suggestions $rainloop_path/data/_data_/_default_/plugins/.
# update SSO # update SSO
sudo cp ../sources/sso/sso.php $final_path/index.php sudo cp ../sources/sso/sso.php $final_path/index.php
sudo sed -i "s@domain.tld@$domain@g" $final_path/index.php
sudo sed -i "s@ALIASTOCHANGE@$final_path@g" $final_path/index.php ynh_replace_string "__URL__" $domain$path_url $final_path/index.php
if [ $path = "/" ]; then ynh_replace_string "__FINAL_PATH__" $final_path $final_path/index.php
sudo sed -i "s@ROOTTOCHANGE@@g" $final_path/index.php
else
sudo sed -i "s@ROOTTOCHANGE@$path@g" $final_path/index.php
fi
# Install PGPback by chtixof to allow users to backup/restore their PGP private keys on the server # Install PGPback by chtixof to allow users to backup/restore their PGP private keys on the server
sudo cp -rf ../sources/pgpback $final_path/. sudo cp -rf ../sources/pgpback $final_path/.
# Set permissions to rainloop directory ### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
sudo find $final_path/. -type d -exec chmod 755 {} \; ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it.
sudo find $final_path/. -type f -exec chmod 644 {} \; ynh_backup_if_checksum_is_different "$final_path/index.php"
sudo chown -R www-data:www-data $final_path ynh_backup_if_checksum_is_different "$application_file"
# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum "$final_path/index.php"
ynh_store_file_checksum "$application_file"
# Update Nginx configuration file #=================================================
nginx_conf_file=/etc/nginx/conf.d/$domain.d/$app.conf # SETUP LOGROTATE
sudo cp ../conf/nginx.conf $nginx_conf_file #=================================================
if [ $path = "/" ]; then
sudo sed -i "s@ROOTTOCHANGE@@g" $nginx_conf_file
else
sudo sed -i "s@ROOTTOCHANGE@$path@g" $nginx_conf_file
fi
sudo sed -i "s@PATHTOCHANGE@$path@g" $nginx_conf_file
sudo sed -i "s@ALIASTOCHANGE@$final_path/@g" $nginx_conf_file
sudo sed -i "s@NAMETOCHANGE@$app@g" $nginx_conf_file
sudo chown root: $nginx_conf_file
sudo chmod 644 $nginx_conf_file
finalphpconf=/etc/php5/fpm/pool.d/$app.conf # Use logrotate to manage app-specific logfile(s)
sudo cp ../conf/php-fpm.conf $finalphpconf #ynh_use_logrotate --non-append
sudo sed -i "s@NAMETOCHANGE@$app@g" $finalphpconf
sudo chown root: $finalphpconf
sudo chmod 644 $finalphpconf
# Reload services #=================================================
sudo service php5-fpm reload # SETUP SYSTEMD
sudo service nginx reload #=================================================
# Create a dedicated systemd config
#ynh_add_systemd_config
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# Set permissions on app files
chown -R www-data:www-data $final_path
sudo find $final_path/. -type d -exec chmod 755 {} \;
sudo find $final_path/. -type f -exec chmod 644 {} \;
#=================================================
# SETUP SSOWAT
#=================================================
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway
ynh_app_setting_set $app unprotected_uris "/"
fi
#=================================================
# RELOAD NGINX
#=================================================
systemctl reload nginx
systemctl reload php5-fpm