restore 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
  8. source ../settings/scripts/_common.sh
  9. source /usr/share/yunohost/helpers
  10. #=================================================
  11. # MANAGE SCRIPT FAILURE
  12. #=================================================
  13. ynh_clean_setup () {
  14. ynh_clean_check_starting
  15. }
  16. # Exit if an error occurs during the execution of the script
  17. ynh_abort_if_errors
  18. #=================================================
  19. # LOAD SETTINGS
  20. #=================================================
  21. ynh_script_progression --message="Loading installation settings..."
  22. app=$YNH_APP_INSTANCE_NAME
  23. domain=$(ynh_app_setting_get --app=$app --key=domain)
  24. path_url=$(ynh_app_setting_get --app=$app --key=path)
  25. final_path=$(ynh_app_setting_get --app=$app --key=final_path)
  26. db_name=$(ynh_app_setting_get --app=$app --key=db_name)
  27. db_user=$db_name
  28. phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
  29. language=$(ynh_app_setting_get --app=$app --key=language)
  30. #=================================================
  31. # CHECK IF THE APP CAN BE RESTORED
  32. #=================================================
  33. ynh_script_progression --message="Validating restoration parameters..."
  34. remove_previous_zabbix
  35. ynh_webpath_available --domain=$domain --path_url=$path_url \
  36. || ynh_die --message="Path not available: ${domain}${path_url}"
  37. test ! -d $final_path \
  38. || ynh_die --message="There is already a directory: $final_path "
  39. #=================================================
  40. # STANDARD RESTORATION STEPS
  41. #=================================================
  42. # RESTORE THE NGINX CONFIGURATION
  43. #=================================================
  44. ynh_script_progression --message="Restoring the NGINX web server configuration..."
  45. ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
  46. #=================================================
  47. # SPECIFIC RESTORATION
  48. #=================================================
  49. # REINSTALL DEPENDENCIES
  50. #=================================================
  51. ynh_script_progression --message="Reinstalling dependencies..."
  52. install_zabbix_repo
  53. # Define and install dependencies
  54. ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
  55. DEBIAN_FRONTEND=noninteractive apt-mark hold zabbix-server-mysql zabbix-frontend-php
  56. ynh_replace_string --match_string="# $language.UTF-8 UTF-8" --replace_string="$language.UTF-8 UTF-8" --target_file=/etc/locale.gen
  57. locale-gen
  58. #=================================================
  59. # RESTORE THE APP MAIN DIR
  60. #=================================================
  61. ynh_script_progression --message="Restoring the app main directory..."
  62. ln -s /usr/share/zabbix "$final_path"
  63. chmod 750 "/usr/share/zabbix"
  64. chmod -R o-rwx "/usr/share/zabbix"
  65. chown -R $app:www-data "/usr/share/zabbix"
  66. #=================================================
  67. # RESTORE THE PHP-FPM CONFIGURATION
  68. #=================================================
  69. ynh_script_progression --message="Restoring the PHP-FPM configuration..."
  70. # Restore the file first, so it can have a backup if different
  71. ynh_restore_file --origin_path="/etc/php/$phpversion/fpm/pool.d/$app.conf"
  72. # Recreate a dedicated php-fpm config
  73. ynh_add_fpm_config --package="$extra_php_dependencies"
  74. phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
  75. #=================================================
  76. # RESTORE THE MYSQL DATABASE
  77. #=================================================
  78. ynh_script_progression --message="Restoring the MySQL database..."
  79. db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
  80. ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
  81. ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql
  82. export mysqlconn="mysql --user=$db_user --password=$db_pwd --database=$db_name"
  83. convert_ZabbixDB
  84. #=================================================
  85. # RESTORE VARIOUS FILES
  86. #=================================================
  87. ynh_script_progression --message="Restoring various files..."
  88. if [ -f "/etc/zabbix/web/zabbix.conf.php" ]
  89. then
  90. ynh_secure_remove --file="/etc/zabbix/web/zabbix.conf.php"
  91. fi
  92. ynh_restore_file --origin_path="/etc/zabbix/web/zabbix.conf.php"
  93. chmod 400 "/etc/zabbix/web/zabbix.conf.php"
  94. chown $app:www-data "/etc/zabbix/web/zabbix.conf.php"
  95. ynh_restore_file --origin_path="/etc/apt/apt.conf.d/100update_force_init_zabbix_frontend_config"
  96. ynh_restore_file --origin_path="/etc/zabbix/zabbix_server.conf"
  97. ynh_restore_file --origin_path="/etc/zabbix/zabbix_agentd.conf"
  98. if [ ! -L /etc/zabbix/zabbix_agentd.d ]
  99. then
  100. ln -s /etc/zabbix/zabbix_agentd.conf.d /etc/zabbix/zabbix_agentd.d
  101. fi
  102. # Restore sudo file
  103. ynh_restore_file --origin_path="/etc/sudoers.d/zabbix"
  104. #=================================================
  105. # RESTORE SYSTEMD
  106. #=================================================
  107. ynh_script_progression --message="Restoring the systemd configuration..."
  108. change_timeoutAgent
  109. systemctl enable zabbix-agent --quiet
  110. systemctl enable zabbix-server --quiet
  111. #=================================================
  112. # INTEGRATE SERVICE IN YUNOHOST
  113. #=================================================
  114. ynh_script_progression --message="Integrating service in YunoHost..."
  115. yunohost service add snmpd --description="Management of SNMP Daemon"
  116. yunohost service add zabbix-server --description="Management Zabbix server daemon : collect, agregate, compute and notify" --log="/var/log/$app/${app}_server.log"
  117. yunohost service add zabbix-agent --description="Management Zabbix agent daemon : send informations about this host to the server" --log="/var/log/$app/${app}_agent.log"
  118. #=================================================
  119. # START SYSTEMD SERVICE
  120. #=================================================
  121. ynh_script_progression --message="Starting a systemd service..."
  122. # Start a systemd service
  123. ynh_systemd_action --service_name=$app-server --action="restart" --log_path="/var/log/$app/${app}_server.log"
  124. ynh_systemd_action --service_name=$app-agent --action="restart" --log_path="/var/log/$app/${app}_agent.log"
  125. #=================================================
  126. # GENERIC FINALIZATION
  127. #=================================================
  128. # RELOAD NGINX AND PHP-FPM
  129. #=================================================
  130. ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..."
  131. ynh_systemd_action --service_name=php$phpversion-fpm --action=reload
  132. ynh_systemd_action --service_name=nginx --action=reload
  133. #=================================================
  134. # END OF SCRIPT
  135. #=================================================
  136. ynh_script_progression --message="Restoration completed for $app"