restore 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. source ../settings/scripts/_common.sh
  8. source /usr/share/yunohost/helpers
  9. # Load common variables for all scripts.
  10. source ../settings/scripts/_variables
  11. #=================================================
  12. # MANAGE SCRIPT FAILURE
  13. #=================================================
  14. # Exit if an error occurs during the execution of the script
  15. ynh_abort_if_errors
  16. #=================================================
  17. # LOAD SETTINGS
  18. #=================================================
  19. ynh_script_progression --message="Loading settings..." --weight=2
  20. app=$YNH_APP_INSTANCE_NAME
  21. domain=$(ynh_app_setting_get --app=$app --key=domain)
  22. path_url=$(ynh_app_setting_get --app=$app --key=path)
  23. final_path=$(ynh_app_setting_get --app=$app --key=final_path)
  24. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  25. admin=$(ynh_app_setting_get --app=$app --key=admin)
  26. #=================================================
  27. # CHECK IF THE APP CAN BE RESTORED
  28. #=================================================
  29. ynh_script_progression --message="Validating restoration parameters..."
  30. ynh_webpath_available --domain=$domain --path_url=$path_url \
  31. || ynh_die --message="Path not available: ${domain}${path_url}"
  32. test ! -d $final_path \
  33. || ynh_die --message="There is already a directory: $final_path "
  34. #=================================================
  35. # ACTIVATE MAINTENANCE MODE
  36. #=================================================
  37. ynh_script_progression --message="Activating maintenance mode..." --weight=2
  38. ynh_maintenance_mode_ON
  39. #=================================================
  40. # STANDARD RESTORE STEPS
  41. #=================================================
  42. # RESTORE THE NGINX CONFIGURATION
  43. #=================================================
  44. ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
  45. #=================================================
  46. # RESTORE THE MAIN DIRECTORIES OF THE APP
  47. #=================================================
  48. ynh_script_progression --message="Restoring the app main directory..."
  49. ynh_restore_file --origin_path="$final_path"
  50. ynh_restore_file --origin_path="/etc/.pihole"
  51. ynh_restore_file --origin_path="/etc/pihole"
  52. ynh_restore_file --origin_path="/opt/pihole"
  53. #=================================================
  54. # RECREATE THE DEDICATED USER
  55. #=================================================
  56. ynh_script_progression --message="Recreating the dedicated system user..." --weight=2
  57. # Create the dedicated user (if not existing)
  58. ynh_system_user_create --username=$app
  59. #=================================================
  60. # RESTORE USER RIGHTS
  61. #=================================================
  62. # Restore permissions on app files
  63. chown $app: -R "/etc/pihole"
  64. # /etc/pihole/logrotate have to belong to root, otherwise logrotate will failed silently...
  65. chown root: -R "/etc/pihole/logrotate"
  66. #=================================================
  67. # RESTORE THE PHP-FPM CONFIGURATION
  68. #=================================================
  69. ynh_restore_file --origin_path="/etc/php/7.0/fpm/pool.d/$app.conf"
  70. #=================================================
  71. # SPECIFIC RESTORE
  72. #=================================================
  73. # REINSTALL DEPENDENCIES
  74. #=================================================
  75. ynh_script_progression --message="Reinstalling dependencies..." --weight=12
  76. ynh_install_app_dependencies $app_depencencies
  77. #=================================================
  78. # ADVERTISE SERVICE IN ADMIN PANEL
  79. #=================================================
  80. yunohost service add pihole-FTL --description "PiHole backend service" --log "/var/log/pihole-FTL.log"
  81. #=================================================
  82. # RESTORE THE CRON FILE
  83. #=================================================
  84. ynh_restore_file --origin_path="/etc/cron.d/pihole"
  85. #=================================================
  86. # RECREATE LOG FILES
  87. #=================================================
  88. touch /var/log/pihole.log
  89. chmod 644 /var/log/pihole.log
  90. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  91. chown $dnsmasq_user:root /var/log/pihole.log
  92. #=================================================
  93. # RESTORE SPECIFIC FILES
  94. #=================================================
  95. ynh_script_progression --message="Restoring specific files..."
  96. ynh_restore_file --origin_path="/usr/local/bin/pihole"
  97. ynh_restore_file --origin_path="/etc/bash_completion.d/pihole"
  98. ynh_restore_file --origin_path="/etc/sudoers.d/pihole"
  99. ynh_restore_file --origin_path="/etc/init.d/pihole-FTL"
  100. ynh_restore_file --origin_path="/usr/bin/pihole-FTL"
  101. ynh_restore_file --origin_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  102. #=================================================
  103. # RESTORE DNSMASQ CONFIG
  104. #=================================================
  105. ynh_script_progression --message="Restoring Dnsmasq config..."
  106. ynh_systemd_action --action=stop --service_name=dnsmasq
  107. ynh_restore_file --origin_path="/etc/dnsmasq.d/01-pihole.conf"
  108. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
  109. ynh_restore_file --origin_path="/etc/dnsmasq.d/02-pihole-dhcp.conf"
  110. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
  111. ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf"
  112. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
  113. ynh_restore_file --origin_path="/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  114. # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config.
  115. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf
  116. #=================================================
  117. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  118. #=================================================
  119. ynh_script_progression --message="Configuring dns for the local domains..." --weight=2
  120. # Get the default network interface
  121. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  122. # Find the IP associated to the network interface
  123. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  124. # List all YunoHost domains
  125. while read perdomain
  126. do
  127. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  128. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  129. # And add a resolution on the local IP instead
  130. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  131. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  132. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  133. #=================================================
  134. # RESTART DNSMASQ
  135. #=================================================
  136. ynh_script_progression --message="Restarting Dnsmasq..."
  137. ynh_systemd_action --action=restart --service_name=dnsmasq
  138. #=================================================
  139. # UPDATE VARIABLES FILE
  140. #=================================================
  141. setupVars="/etc/pihole/setupVars.conf"
  142. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  143. ynh_replace_string --match_string="^PIHOLE_INTERFACE=.*" --replace_string="PIHOLE_INTERFACE=$main_iface" --target_file=$setupVars
  144. ynh_replace_string --match_string="^IPV4_ADDRESS=.*" --replace_string="IPV4_ADDRESS=127.0.0.1" --target_file=$setupVars
  145. # Recalculate and store the checksum of the file for the next upgrade.
  146. ynh_store_file_checksum --file="$setupVars"
  147. #=================================================
  148. # START PIHOLE-FTL
  149. #=================================================
  150. ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2
  151. ynh_exec_warn_less systemctl enable pihole-FTL
  152. ynh_systemd_action --action=restart --service_name=pihole-FTL
  153. #=================================================
  154. # GENERIC FINALISATION
  155. #=================================================
  156. # RELOAD NGINX AND PHP-FPM
  157. #=================================================
  158. ynh_script_progression --message="Reloading nginx web server and php-fpm..."
  159. ynh_systemd_action --action=reload --service_name=php7.0-fpm
  160. ynh_systemd_action --action=reload --service_name=nginx
  161. #=================================================
  162. # DEACTIVE MAINTENANCE MODE
  163. #=================================================
  164. ynh_script_progression --message="Disabling maintenance mode..." --weight=4
  165. ynh_maintenance_mode_OFF
  166. #=================================================
  167. # SEND A README FOR THE ADMIN
  168. #=================================================
  169. # Get main domain and buid the url of the admin panel of the app.
  170. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  171. if [ $enable_dhcp -eq 1 ]
  172. then
  173. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  174. 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__
  175. "
  176. else
  177. dhcp_alert=""
  178. fi
  179. 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__.
  180. 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__.
  181. 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
  182. ynh_send_readme_to_admin --app_message="mail_to_send" --recipients="$admin" --type=restore
  183. #=================================================
  184. # END OF SCRIPT
  185. #=================================================
  186. ynh_script_progression --message="Restoration completed for $app" --last