restore 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #=================================================
  60. # RESTORE THE PHP-FPM CONFIGURATION
  61. #=================================================
  62. ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf"
  63. #=================================================
  64. # SPECIFIC RESTORE
  65. #=================================================
  66. # REINSTALL DEPENDENCIES
  67. #=================================================
  68. ynh_install_app_dependencies $app_depencencies
  69. #=================================================
  70. # ADVERTISE SERVICE IN ADMIN PANEL
  71. #=================================================
  72. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  73. #=================================================
  74. # RESTORE THE CRON FILE
  75. #=================================================
  76. ynh_restore_file "/etc/cron.d/pihole"
  77. #=================================================
  78. # RECREATE LOG FILES
  79. #=================================================
  80. touch /var/log/pihole.log
  81. chmod 644 /var/log/pihole.log
  82. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  83. chown $dnsmasq_user:root /var/log/pihole.log
  84. #=================================================
  85. # RESTORE SPECIFIC FILES
  86. #=================================================
  87. ynh_restore_file "/usr/local/bin/pihole"
  88. ynh_restore_file "/etc/bash_completion.d/pihole"
  89. ynh_restore_file "/etc/sudoers.d/pihole"
  90. ynh_restore_file "/etc/init.d/pihole-FTL"
  91. ynh_restore_file "/usr/bin/pihole-FTL"
  92. ynh_restore_file "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  93. #=================================================
  94. # RESTORE DNSMASQ CONFIG
  95. #=================================================
  96. ynh_systemd_action --action=stop --service_name=dnsmasq
  97. ynh_restore_file "/etc/dnsmasq.d/01-pihole.conf"
  98. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
  99. ynh_restore_file "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  100. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
  101. ynh_restore_file "/etc/dnsmasq.d/03-pihole-wildcard.conf"
  102. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
  103. ynh_restore_file "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  104. # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config.
  105. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  106. #=================================================
  107. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  108. #=================================================
  109. # Get the default network interface
  110. main_iface=$(ip route | grep default | awk '{print $5;}')
  111. # Find the IP associated to the network interface
  112. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  113. # List all YunoHost domains
  114. while read perdomain
  115. do
  116. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  117. sed -i "s/^127.0.0.1.*$perdomain/#Commented by pihole# &/g" /etc/hosts
  118. # And add a resolution on the local IP instead
  119. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  120. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  121. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  122. #=================================================
  123. # RESTART DNSMASQ
  124. #=================================================
  125. ynh_systemd_action --action=restart --service_name=dnsmasq
  126. #=================================================
  127. # UPDATE VARIABLES FILE
  128. #=================================================
  129. setupVars="/etc/pihole/setupVars.conf"
  130. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  131. ynh_replace_string "^PIHOLE_INTERFACE=.*" "PIHOLE_INTERFACE=$main_iface" $setupVars
  132. ynh_replace_string "^IPV4_ADDRESS=.*" "IPV4_ADDRESS=127.0.0.1" $setupVars
  133. # Recalculate and store the checksum of the file for the next upgrade.
  134. ynh_store_file_checksum "$setupVars"
  135. #=================================================
  136. # START PIHOLE-FTL
  137. #=================================================
  138. ynh_exec_warn_less systemctl enable pihole-FTL
  139. ynh_systemd_action --action=restart --service_name=pihole-FTL
  140. #=================================================
  141. # GENERIC FINALISATION
  142. #=================================================
  143. # RELOAD NGINX AND PHP-FPM
  144. #=================================================
  145. ynh_systemd_action --action=reload --service_name=php5-fpm
  146. ynh_systemd_action --action=reload --service_name=nginx
  147. #=================================================
  148. # DEACTIVE MAINTENANCE MODE
  149. #=================================================
  150. ynh_maintenance_mode_OFF
  151. #=================================================
  152. # SEND A README FOR THE ADMIN
  153. #=================================================
  154. # Get main domain and buid the url of the admin panel of the app.
  155. admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
  156. if [ $enable_dhcp -eq 1 ]
  157. then
  158. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  159. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  160. "
  161. else
  162. dhcp_alert=""
  163. fi
  164. message="${dhcp_alert}You can configure this app easily by using the experimental config-panel feature: $admin_panel/config-panel.
  165. You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
  166. 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"
  167. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="restore"