restore 7.9 KB

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