1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/synapse_ynh.git synced 2024-09-03 20:26:38 +02:00
synapse_ynh/scripts/restore

310 lines
13 KiB
Text
Raw Normal View History

2017-02-13 20:43:41 +01:00
#!/bin/bash
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2019-04-30 19:15:33 +02:00
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source ../settings/scripts/experimental_helper.sh
source /usr/share/yunohost/helpers
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
# Clean installation remainings that are not handled by the remove script.
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading settings..."
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=special_domain)
path_url=$(ynh_app_setting_get --app=$app --key=special_path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
synapse_db_pwd=$(ynh_app_setting_get --app=$app --key=synapse_db_pwd)
synapse_tls_port=$(ynh_app_setting_get --app=$app --key=synapse_tls_port)
turnserver_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_tls_port)
turnserver_alt_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_alt_tls_port)
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
# SET ALL CONSTANT
#=================================================
synapse_user="matrix-$app"
synapse_db_name="matrix_$app"
synapse_db_user="matrix_$app"
upstream_version=$(ynh_app_upstream_version)
#=================================================
2019-04-30 19:15:33 +02:00
# CHECK IF THE APP CAN BE RESTORED
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Validating restoration parameters..." --weight=2
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_webpath_available --domain=$domain --path_url=$path_url \
|| ynh_die --message="Path not available: ${domain}${path_url}"
test ! -d $final_path \
|| ynh_die --message="There is already a directory: $final_path "
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
2017-02-13 20:43:41 +01:00
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# RESTORE THE APP MAIN DIR
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restoring the app main directory..."
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_restore_file --origin_path="$final_path"
2018-01-30 23:44:49 +01:00
2019-02-08 11:24:08 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# RESTORE SYNAPSE LOG
2019-02-08 11:24:08 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restoring synapse log..."
2019-02-08 11:24:08 +01:00
2019-04-30 19:15:33 +02:00
ynh_restore_file --origin_path="/var/log/matrix-$app"
2019-02-08 11:24:08 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# RESTORE FAIL2BAN CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the fail2ban configuration..." --weight=6
ynh_restore_file --origin_path="/etc/fail2ban/jail.d/$app.conf"
ynh_restore_file --origin_path="/etc/fail2ban/filter.d/$app.conf"
ynh_systemd_action --action=restart --service_name=fail2ban
2019-02-08 11:24:08 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# SPECIFIC RESTORATION
2018-01-30 23:44:49 +01:00
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Reinstalling dependencies..." --weight=70
2017-02-13 20:43:41 +01:00
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
2018-01-30 23:44:49 +01:00
# For any update do it in all files
2019-04-30 19:15:33 +02:00
ynh_install_app_dependencies $dependances
2018-01-30 23:44:49 +01:00
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Recreating the dedicated system user..." --weight=3
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
# Create the dedicated user (if not existing)
ynh_system_user_create --username=$synapse_user --home_dir=/var/lib/matrix-$app
2017-12-30 15:59:05 +01:00
adduser $synapse_user ssl-cert
adduser turnserver ssl-cert
2017-02-13 20:43:41 +01:00
2019-04-30 19:15:33 +02:00
#=================================================
# RESTORE THE POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=13
ynh_psql_test_if_first_run
ynh_psql_create_user $synapse_db_user $synapse_db_pwd
ynh_psql_execute_as_root \
--sql="CREATE DATABASE $synapse_db_name
ENCODING 'UTF8'
LC_COLLATE='C'
LC_CTYPE='C'
template=template0
OWNER $synapse_db_user;"
ynh_psql_execute_file_as_root --file="${YNH_CWD}/dump.sql" --database="$synapse_db_name"
#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..." --weight=2
ynh_restore_file --origin_path="/etc/default/matrix-$app"
ynh_restore_file --origin_path="/etc/systemd/system/matrix-$app.service"
ynh_restore_file --origin_path="/etc/default/coturn-$app"
ynh_restore_file --origin_path="/etc/systemd/system/coturn-$app.service"
# systemctl daemon-reload
systemctl enable matrix-$app.service
systemctl enable coturn-$app.service
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add matrix-$app -l "/var/log/matrix-$app/homeserver.log"
yunohost service add coturn-$app
#=================================================
# CREATE A DH FILE
#=================================================
ynh_script_progression --message="Creating a dh file..." --weight=40
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE
# For any update do it in all files
# Make dh cert for synapse if it doesn't exist
if [ ! -e /etc/ssl/private/dh2048.pem ]
then
ynh_exec_warn_less openssl dhparam -out /etc/ssl/private/dh2048.pem -outform PEM -2 2048 -dsaparam
chown root:ssl-cert /etc/ssl/private/dh2048.pem
chmod 640 /etc/ssl/private/dh2048.pem
fi
#=================================================
# RESTORE SYNAPSE CONFIG
#=================================================
ynh_script_progression --message="Restoring synapse configuration..."
ynh_restore_file --origin_path="/etc/matrix-$app"
#=================================================
# RECONFIGURE THE TURNSERVER
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Reconfiguring coturn..." --weight=23
2018-07-05 21:46:24 +02:00
# To be sure that at the restoration the IP address in coturn config is the same as the real address we remake the coturn config
2018-07-05 21:46:24 +02:00
# Retrieve specific settings
2019-04-30 19:15:33 +02:00
turnserver_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_tls_port)
turnserver_alt_tls_port=$(ynh_app_setting_get --app=$app --key=turnserver_alt_tls_port)
turnserver_pwd=$(ynh_app_setting_get --app=$app --key=turnserver_pwd)
cli_port=$(ynh_app_setting_get --app=$app --key=cli_port)
2018-07-05 21:46:24 +02:00
# WARNING : these commands are used in INSTALL, UPGRADE
# For any update do it in all files
coturn_config_path="/etc/matrix-$app/coturn.conf"
cp ../settings/conf/turnserver.conf "$coturn_config_path"
2019-04-30 19:15:33 +02:00
ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file="$coturn_config_path"
ynh_replace_string --match_string=__TURNPWD__ --replace_string=$turnserver_pwd --target_file="$coturn_config_path"
ynh_replace_string --match_string=__DOMAIN__ --replace_string=$domain --target_file="$coturn_config_path"
ynh_replace_string --match_string=__TLS_PORT__ --replace_string=$turnserver_tls_port --target_file="$coturn_config_path"
ynh_replace_string --match_string=__TLS_ALT_PORT__ --replace_string=$turnserver_alt_tls_port --target_file="$coturn_config_path"
ynh_replace_string --match_string=__CLI_PORT__ --replace_string=$cli_port --target_file="$coturn_config_path"
# Get public IP and set as external IP for coturn
# note : '|| true' is used to ignore the errors if we can't get the public ipv4 or ipv6
public_ip4="$(curl ip.yunohost.org)" || true
public_ip6="$(curl ipv6.yunohost.org)" || true
2019-04-30 19:15:33 +02:00
if [[ -n "$public_ip4" ]] && ynh_validate_ip4 --ip_address="$public_ip4"
then
2019-04-30 19:15:33 +02:00
ynh_replace_string --match_string='__IPV4__' --replace_string="$public_ip4" --target_file="$coturn_config_path"
else
2019-04-30 19:15:33 +02:00
ynh_replace_string --match_string='__IPV4__,' --replace_string="" --target_file="$coturn_config_path"
fi
2019-04-30 19:15:33 +02:00
if [[ -n "$public_ip6" ]] && ynh_valide_ip6 --ip_address="$public_ip6"
then
2019-04-30 19:15:33 +02:00
ynh_replace_string --match_string='__IPV6__' --replace_string="$public_ip6" --target_file="$coturn_config_path"
else
2019-04-30 19:15:33 +02:00
ynh_replace_string --match_string=',__IPV6__' --replace_string="" --target_file="$coturn_config_path"
fi
2019-04-30 19:15:33 +02:00
ynh_store_file_checksum --file="$coturn_config_path"
2018-01-30 23:44:49 +01:00
#=================================================
# OPEN THE PORT
#=================================================
# Ouvre le port dans le firewall
2019-04-30 19:15:33 +02:00
ynh_exec_warn_less yunohost firewall allow TCP $synapse_tls_port
ynh_exec_warn_less yunohost firewall allow Both $turnserver_tls_port
ynh_exec_warn_less yunohost firewall allow Both $turnserver_alt_tls_port
2018-01-30 23:44:49 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring SSOwat..."
2017-02-13 20:43:41 +01:00
2017-10-21 01:31:04 +02:00
# Open access to server without a button the home
# The script "add_sso_conf.py" will just add en entry for the path "/_matrix" in the sso conf.json.persistent file in the cathegory "skipped_urls".
2019-04-30 19:15:33 +02:00
python3 $final_path/add_sso_conf.py || ynh_die --message="Your file /etc/ssowat/conf.json.persistent don't respect the json synaxe. Please fix the synaxe to install this app. For more information see here : https://github.com/YunoHost-Apps/synapse_ynh/issues/32"
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# SETUP LOGROTATE
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Configuring log rotation..."
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_use_logrotate /var/log/matrix-$app
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
# RESTORE SYNAPSE DATA
2018-01-30 23:44:49 +01:00
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restoring synapse data..."
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_restore_file --origin_path="/var/lib/matrix-$app"
2018-01-30 23:44:49 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
# WARNING : theses command are used in INSTALL, UPGRADE, RESTORE (3 times)
2018-01-30 23:44:49 +01:00
# For any update do it in all files
chown $synapse_user:root -R $final_path
chmod 770 $final_path/Coturn_config_rotate.sh
2018-01-30 23:44:49 +01:00
chown $synapse_user:root -R /var/lib/matrix-$app
chown $synapse_user:root -R /var/log/matrix-$app
chown $synapse_user:root -R /etc/matrix-$app
chmod u=rwX,g=rX,o= -R /etc/matrix-$app
2019-02-08 14:53:18 +01:00
chmod 600 /etc/matrix-$app/$domain.signing.key
2018-01-30 23:44:49 +01:00
setfacl -R -m user:turnserver:rX /etc/matrix-$app
setfacl -R -m user:turnserver:rwX /var/log/matrix-$app
#=================================================
# RELOAD NGINX, SYNAPSE AND COTURN
#=================================================
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restarting synapse services..." --weight=7
2018-01-30 23:44:49 +01:00
2019-04-30 19:15:33 +02:00
ynh_systemd_action --service_name=coturn-$app.service --action=restart
ynh_systemd_action --service_name=matrix-$app --action=restart --line_match="Synapse now listening on TCP port $synapse_tls_port" --log_path="/var/log/matrix-$app/homeserver.log" --timeout=300
2019-02-12 21:24:25 +01:00
2018-03-13 16:50:27 +01:00
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
2018-08-03 15:58:40 +02:00
# WARNING : theses command are used in INSTALL, RESTORE
2018-06-20 19:32:48 +02:00
# For any update do it in all files
2019-04-30 19:15:33 +02:00
echo "To federate this app you need to add this line in your DNS configuration:
2018-03-13 16:50:27 +01:00
_matrix._tcp.$domain. 3600 IN SRV 10 0 $synapse_tls_port $domain.
You also need to open the TCP port $synapse_tls_port on your ISP box if it's not automatically done.
2018-07-05 21:46:24 +02:00
Your synapse server also implements a turnserver (for VoIP), to have this fully functional please read the 'Turnserver' section in the README available here: https://github.com/YunoHost-Apps/synapse_ynh .
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/synapse_ynh" > mail_to_send
ynh_send_readme_to_admin --app_message="mail_to_send" --type="restore"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2018-03-13 16:50:27 +01:00
2019-04-30 19:15:33 +02:00
ynh_script_progression --message="Restoration completed for $app" --last