restore 8.9 KB

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