restore 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. # Load common variables for all scripts.
  8. source ../settings/scripts/_variables
  9. source ../settings/scripts/_common.sh
  10. source /usr/share/yunohost/helpers
  11. source ../settings/scripts/_ynh_add_fpm_config.sh
  12. #=================================================
  13. # MANAGE SCRIPT FAILURE
  14. #=================================================
  15. # Exit if an error occurs during the execution of the script
  16. ynh_abort_if_errors
  17. #=================================================
  18. # LOAD SETTINGS
  19. #=================================================
  20. ynh_script_progression --message="Loading settings..." --weight=2
  21. app=$YNH_APP_INSTANCE_NAME
  22. domain=$(ynh_app_setting_get --app=$app --key=domain)
  23. path_url=$(ynh_app_setting_get --app=$app --key=path)
  24. final_path=$(ynh_app_setting_get --app=$app --key=final_path)
  25. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  26. admin=$(ynh_app_setting_get --app=$app --key=admin)
  27. pihole_version="$(ynh_app_setting_get --app=$app --key=pihole_version)"
  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..."
  37. ynh_webpath_available --domain=$domain --path_url=$path_url \
  38. || ynh_die --message="Path not available: ${domain}${path_url}"
  39. test ! -d $final_path \
  40. || ynh_die --message="There is already a directory: $final_path "
  41. #=================================================
  42. # ACTIVATE MAINTENANCE MODE
  43. #=================================================
  44. ynh_script_progression --message="Activating maintenance mode..." --weight=2
  45. ynh_maintenance_mode_ON
  46. #=================================================
  47. # STANDARD RESTORE STEPS
  48. #=================================================
  49. # RESTORE THE NGINX CONFIGURATION
  50. #=================================================
  51. ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
  52. #=================================================
  53. # RESTORE THE MAIN DIRECTORIES OF THE APP
  54. #=================================================
  55. ynh_script_progression --message="Restoring the app main directory..."
  56. ynh_restore_file --origin_path="$final_path"
  57. ynh_restore_file --origin_path="/etc/.pihole"
  58. ynh_restore_file --origin_path="/etc/pihole"
  59. ynh_restore_file --origin_path="/opt/pihole"
  60. #=================================================
  61. # RECREATE THE DEDICATED USER
  62. #=================================================
  63. ynh_script_progression --message="Recreating the dedicated system user..." --weight=2
  64. # Create the dedicated user (if not existing)
  65. ynh_system_user_create --username=$app
  66. #=================================================
  67. # RESTORE USER RIGHTS
  68. #=================================================
  69. # Restore permissions on app files
  70. chown $app: -R "/etc/pihole"
  71. # /etc/pihole/logrotate have to belong to root, otherwise logrotate will failed silently...
  72. chown root: -R "/etc/pihole/logrotate"
  73. #=================================================
  74. # RESTORE THE PHP-FPM CONFIGURATION
  75. #=================================================
  76. ynh_script_progression --message="Reconfiguring php-fpm..." --weight=7
  77. # Restore the file first, so it can have a backup if different
  78. ynh_restore_file --origin_path="$fpm_config_dir/php-fpm-$app.conf"
  79. ynh_restore_file --origin_path="$fpm_config_dir/pool.d/$app.conf"
  80. # Recreate a dedicated php-fpm config
  81. ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --dedicated_service
  82. #=================================================
  83. # SPECIFIC RESTORE
  84. #=================================================
  85. # REINSTALL DEPENDENCIES
  86. #=================================================
  87. ynh_script_progression --message="Reinstalling dependencies..." --weight=12
  88. ynh_install_app_dependencies $app_depencencies
  89. #=================================================
  90. # ADVERTISE SERVICE IN ADMIN PANEL
  91. #=================================================
  92. yunohost service add pihole-FTL --description "PiHole backend service" --log "/var/log/pihole-FTL.log"
  93. #=================================================
  94. # RESTORE THE CRON FILE
  95. #=================================================
  96. ynh_restore_file --origin_path="/etc/cron.d/pihole"
  97. #=================================================
  98. # RECREATE LOG FILES
  99. #=================================================
  100. touch /var/log/pihole.log
  101. chmod 644 /var/log/pihole.log
  102. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  103. chown $dnsmasq_user:root /var/log/pihole.log
  104. #=================================================
  105. # RESTORE SPECIFIC FILES
  106. #=================================================
  107. ynh_script_progression --message="Restoring specific files..."
  108. ynh_restore_file --origin_path="/usr/local/bin/pihole"
  109. ynh_restore_file --origin_path="/etc/bash_completion.d/pihole"
  110. ynh_restore_file --origin_path="/etc/sudoers.d/pihole"
  111. ynh_restore_file --origin_path="/etc/init.d/pihole-FTL"
  112. ynh_restore_file --origin_path="/usr/bin/pihole-FTL"
  113. ynh_restore_file --origin_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  114. #=================================================
  115. # REPLACE THE DEFAULT DNSMASQ BY PIHOLE-FTL
  116. #=================================================
  117. if [ "$pihole_version" == "Last available" ]
  118. then
  119. # Last version available
  120. # Stopped dnsmasq to replace it by pihole-FTL
  121. ynh_systemd_action --action=stop --service_name=dnsmasq
  122. # Disable the real dnsmasq service
  123. ynh_exec_warn_less systemctl disable dnsmasq
  124. # And move the files that make the service available in systemd to really disable it
  125. mv /lib/systemd/system/dnsmasq.service /lib/systemd/system/.dnsmasq.service.backup_by_pihole
  126. mv /etc/init.d/dnsmasq /etc/init.d/.dnsmasq.backup_by_pihole
  127. # Move dnsmasq to preserve the current binary
  128. mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq.backup_by_pihole
  129. # Replace dnsmasq by pihole-FTL
  130. # NOTE: pihole-FTL is actually a modified version of dnsmasq
  131. # https://github.com/pi-hole/FTL/tree/master/dnsmasq
  132. ln -s /usr/bin/pihole-FTL /usr/sbin/dnsmasq
  133. pihole_local_repo="/etc/.pihole"
  134. cp -a $pihole_local_repo/advanced/Templates/pihole-FTL.service /etc/init.d/pihole-FTL
  135. chmod +x /etc/init.d/pihole-FTL
  136. ynh_exec_warn_less systemctl enable pihole-FTL
  137. # Replace the service dnsmasq by pihole-FTL
  138. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  139. ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service
  140. # Reload systemd config
  141. systemctl daemon-reload
  142. fi
  143. #=================================================
  144. # RESTORE DNSMASQ CONFIG
  145. #=================================================
  146. ynh_script_progression --message="Restoring Dnsmasq config..."
  147. ynh_systemd_action --action=stop --service_name=dnsmasq
  148. ynh_restore_file --origin_path="/etc/dnsmasq.d/01-pihole.conf"
  149. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
  150. ynh_restore_file --origin_path="/etc/dnsmasq.d/02-pihole-dhcp.conf"
  151. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
  152. ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf"
  153. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
  154. ynh_restore_file --origin_path="/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  155. # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config.
  156. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf
  157. #=================================================
  158. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  159. #=================================================
  160. ynh_script_progression --message="Configuring dns for the local domains..." --weight=2
  161. # Get the default network interface
  162. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  163. # Find the IP associated to the network interface
  164. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  165. # List all YunoHost domains
  166. while read perdomain
  167. do
  168. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  169. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  170. # And add a resolution on the local IP instead
  171. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  172. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  173. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  174. #=================================================
  175. # RESTART DNSMASQ
  176. #=================================================
  177. # Restart dnsmasq only for the version 3.X, otherwise we're going to restart it twice.
  178. if [ "$pihole_version" == "Last 3.X" ]
  179. then
  180. ynh_script_progression --message="Restarting Dnsmasq..."
  181. ynh_systemd_action --action=restart --service_name=dnsmasq
  182. fi
  183. #=================================================
  184. # UPDATE VARIABLES FILE
  185. #=================================================
  186. setupVars="/etc/pihole/setupVars.conf"
  187. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  188. ynh_replace_string --match_string="^PIHOLE_INTERFACE=.*" --replace_string="PIHOLE_INTERFACE=$main_iface" --target_file=$setupVars
  189. ynh_replace_string --match_string="^IPV4_ADDRESS=.*" --replace_string="IPV4_ADDRESS=127.0.0.1" --target_file=$setupVars
  190. # Recalculate and store the checksum of the file for the next upgrade.
  191. ynh_store_file_checksum --file="$setupVars"
  192. #=================================================
  193. # START PIHOLE-FTL
  194. #=================================================
  195. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  196. ynh_exec_warn_less systemctl enable pihole-FTL
  197. ynh_systemd_action --action=restart --service_name=pihole-FTL
  198. #=================================================
  199. # GENERIC FINALISATION
  200. #=================================================
  201. # RELOAD NGINX AND PHP-FPM
  202. #=================================================
  203. ynh_script_progression --message="Reloading nginx web server and php-fpm..."
  204. ynh_systemd_action --service_name=$fpm_service --action=reload
  205. ynh_systemd_action --service_name=nginx --action=reload
  206. #=================================================
  207. # DEACTIVE MAINTENANCE MODE
  208. #=================================================
  209. ynh_script_progression --message="Disabling maintenance mode..." --weight=4
  210. ynh_maintenance_mode_OFF
  211. #=================================================
  212. # SEND A README FOR THE ADMIN
  213. #=================================================
  214. # Get main domain and buid the url of the admin panel of the app.
  215. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  216. if [ $enable_dhcp -eq 1 ]
  217. then
  218. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  219. 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__
  220. "
  221. else
  222. dhcp_alert=""
  223. fi
  224. 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__.
  225. 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__.
  226. 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
  227. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=restore
  228. #=================================================
  229. # END OF SCRIPT
  230. #=================================================
  231. ynh_script_progression --message="Restoration completed for $app" --last