restore 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. true
  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..." --weight=2
  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. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  27. admin=$(ynh_app_setting_get --app=$app --key=admin)
  28. # Get variables from ynh_add_fpm_config
  29. fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint)
  30. fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage)
  31. fpm_config_dir=$(ynh_app_setting_get --app=$app --key=fpm_config_dir)
  32. fpm_service=$(ynh_app_setting_get --app=$app --key=fpm_service)
  33. #=================================================
  34. # CHECK IF THE APP CAN BE RESTORED
  35. #=================================================
  36. ynh_script_progression --message="Validating restoration parameters..." --weight=1
  37. test ! -d $final_path \
  38. || ynh_die --message="There is already a directory: $final_path "
  39. #=================================================
  40. # FIND AND OPEN A PORT
  41. #=================================================
  42. ynh_script_progression --message="Finding an available port..." --weight=12
  43. # Find an available port
  44. port=$(ynh_find_port --port=4711)
  45. if [ $port -gt 4720 ]
  46. then
  47. ynh_die --message="The ports 4711 to 4720 are already in use. Pi-hole can't work on another port. Please try to free one of these ports."
  48. fi
  49. ynh_app_setting_set --app=$app --key=port --value=$port
  50. # Disable the port 53 for upnp
  51. ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
  52. ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
  53. # Open the UDP port 67 for dhcp
  54. ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
  55. #=================================================
  56. # ACTIVATE MAINTENANCE MODE
  57. #=================================================
  58. ynh_script_progression --message="Activating maintenance mode..." --weight=2
  59. ynh_maintenance_mode_ON
  60. #=================================================
  61. # STANDARD RESTORATION STEPS
  62. #=================================================
  63. # RECREATE THE DEDICATED USER
  64. #=================================================
  65. ynh_script_progression --message="Recreating the dedicated system user..." --weight=2
  66. # Create the dedicated user (if not existing)
  67. ynh_system_user_create --username=$app --home_dir="$final_path"
  68. #=================================================
  69. # RESTORE THE APP MAIN DIR
  70. #=================================================
  71. ynh_script_progression --message="Restoring the app main directory..." --weight=1
  72. ynh_restore_file --origin_path="$final_path"
  73. chmod 750 "$final_path"
  74. chmod -R o-rwx "$final_path"
  75. chown -R $app:www-data "$final_path"
  76. #=================================================
  77. # SPECIFIC RESTORATION
  78. #=================================================
  79. # REINSTALL DEPENDENCIES
  80. #=================================================
  81. ynh_script_progression --message="Reinstalling dependencies..." --weight=12
  82. # Define and install dependencies
  83. ynh_install_app_dependencies $pkg_dependencies
  84. #=================================================
  85. # RESTORE THE PHP-FPM CONFIGURATION
  86. #=================================================
  87. ynh_script_progression --message="Restoring the PHP-FPM configuration..." --weight=7
  88. # Restore the file first, so it can have a backup if different
  89. ynh_restore_file --origin_path="$fpm_config_dir/php-fpm-$app.conf"
  90. ynh_restore_file --origin_path="$fpm_config_dir/pool.d/$app.conf"
  91. # Recreate a dedicated php-fpm config
  92. ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --dedicated_service
  93. #=================================================
  94. # RESTORE THE NGINX CONFIGURATION
  95. #=================================================
  96. ynh_script_progression --message="Restoring the NGINX web server configuration..." --weight=1
  97. ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
  98. #=================================================
  99. # RESTORE SPECIFIC FILES
  100. #=================================================
  101. ynh_script_progression --message="Restoring specific files..." --weight=1
  102. ynh_restore_file --origin_path="$PI_HOLE_LOCAL_REPO"
  103. ynh_restore_file --origin_path="$PI_HOLE_CONFIG_DIR"
  104. # Restore permissions on app files
  105. chown $app: -R "$PI_HOLE_CONFIG_DIR"
  106. # $PI_HOLE_CONFIG_DIR/logrotate have to belong to root, otherwise logrotate will failed silently...
  107. chown root: -R "$PI_HOLE_CONFIG_DIR/logrotate"
  108. ynh_restore_file --origin_path="$PI_HOLE_INSTALL_DIR"
  109. ynh_restore_file --origin_path="$PI_HOLE_BIN_DIR/pihole"
  110. ynh_restore_file --origin_path="/etc/bash_completion.d/pihole"
  111. ynh_restore_file --origin_path="/etc/init.d/pihole-FTL"
  112. ynh_restore_file --origin_path="/usr/bin/pihole-FTL"
  113. install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  114. ynh_restore_file --origin_path="/etc/sudoers.d/pihole"
  115. #=================================================
  116. # RESTORE THE CRON FILE
  117. #=================================================
  118. ynh_script_progression --message="Restoring the cron file..." --weight=1
  119. ynh_restore_file --origin_path="/etc/cron.d/pihole"
  120. #=================================================
  121. # DISABLING DNSMASQ
  122. #=================================================
  123. ynh_script_progression --message="Disabling DNSMASQ..." --weight=1
  124. # Last version available
  125. # Stopped dnsmasq to replace it by pihole-FTL
  126. ynh_systemd_action --service_name=dnsmasq --action=stop
  127. # Disable the real dnsmasq service
  128. #ynh_exec_warn_less systemctl disable dnsmasq --quiet
  129. #=================================================
  130. # FINAL EXPORTS
  131. #=================================================
  132. ynh_script_progression --message="Final exports..." --weight=1
  133. setupVars="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  134. # Get the default network interface
  135. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  136. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  137. ynh_replace_string --match_string="^PIHOLE_INTERFACE=.*" --replace_string="PIHOLE_INTERFACE=$main_iface" --target_file=$setupVars
  138. ynh_replace_string --match_string="^IPV4_ADDRESS=.*" --replace_string="IPV4_ADDRESS=127.0.0.1" --target_file=$setupVars
  139. # Calculate and store the config file checksum into the app settings
  140. ynh_store_file_checksum --file="$setupVars"
  141. #=================================================
  142. # ENABLING FTL
  143. #=================================================
  144. ynh_script_progression --message="Enable FTL..." --weight=1
  145. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  146. # Replace the service dnsmasq by pihole-FTL
  147. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  148. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  149. systemctl mask dnsmasq.service
  150. # Reload systemd config
  151. systemctl daemon-reload
  152. #=================================================
  153. # RECREATE LOG FILES
  154. #=================================================
  155. ynh_script_progression --message="Recreate log files..." --weight=1
  156. touch /var/log/{pihole,pihole-FTL}.log
  157. chmod 644 /var/log/{pihole,pihole-FTL}.log
  158. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  159. chown $dnsmasq_user:root /var/log/{pihole,pihole-FTL}.log
  160. #=================================================
  161. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  162. #=================================================
  163. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=2
  164. # Find the IP associated to the network interface
  165. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  166. # List all YunoHost domains
  167. while read perdomain
  168. do
  169. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  170. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  171. # And add a resolution on the local IP instead
  172. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  173. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  174. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  175. #=================================================
  176. # SET UP CONF_REGEN HOOK
  177. #=================================================
  178. ynh_script_progression --message="Setting up conf_regen hook..." --weight=1
  179. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf"
  180. ynh_restore_file --origin_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  181. systemctl daemon-reload
  182. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  183. #=================================================
  184. # INTEGRATE SERVICE IN YUNOHOST
  185. #=================================================
  186. ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
  187. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole-FTL.log" --needs_exposed_ports 53 67
  188. #=================================================
  189. # START SYSTEMD SERVICE
  190. #=================================================
  191. ynh_script_progression --message="Starting a systemd service..." --weight=2
  192. ynh_systemd_action --service_name=pihole-FTL --action=restart --log_path="/var/log/pihole-FTL.log"
  193. #=================================================
  194. # GENERIC FINALIZATION
  195. #=================================================
  196. # RELOAD NGINX AND PHP-FPM
  197. #=================================================
  198. ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." --weight=1
  199. ynh_systemd_action --service_name=$fpm_service --action=reload
  200. ynh_systemd_action --service_name=nginx --action=reload
  201. #=================================================
  202. # DEACTIVE MAINTENANCE MODE
  203. #=================================================
  204. ynh_script_progression --message="Disabling maintenance mode..." --weight=4
  205. ynh_maintenance_mode_OFF
  206. #=================================================
  207. # SEND A README FOR THE ADMIN
  208. #=================================================
  209. # Get main domain and buid the url of the admin panel of the app.
  210. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  211. if [ $enable_dhcp -eq 1 ]
  212. then
  213. dhcp_alert="You asked to use the internal DHCP server of Dnsmasq with PiHole.
  214. You should really read the __URL_TAG1__documentation about that__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md__URL_TAG3__
  215. "
  216. else
  217. dhcp_alert=""
  218. fi
  219. echo "${dhcp_alert}You can configure this app easily by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__.
  220. You can also find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__.
  221. If you're facing an issue or want to improve this app, please open a new issue in this __URL_TAG1__project__URL_TAG2__https://github.com/YunoHost-Apps/pihole_ynh__URL_TAG3__." > mail_to_send
  222. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=restore
  223. #=================================================
  224. # END OF SCRIPT
  225. #=================================================
  226. ynh_script_progression --message="Restoration completed for $app" --last