restore 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC START
  4. #=================================================
  5. # IMPORT GENERIC HELPERS
  6. #=================================================
  7. if [ ! -e _common.sh ]; then
  8. # Get the _common.sh file if it's not in the current directory
  9. cp ../settings/scripts/_common.sh ./_common.sh
  10. chmod a+rx _common.sh
  11. fi
  12. source _common.sh
  13. source /usr/share/yunohost/helpers
  14. # Load common variables for all scripts.
  15. source ../settings/scripts/_variables
  16. #=================================================
  17. # MANAGE SCRIPT FAILURE
  18. #=================================================
  19. # Exit if an error occurs during the execution of the script
  20. ynh_abort_if_errors
  21. #=================================================
  22. # LOAD SETTINGS
  23. #=================================================
  24. app=$YNH_APP_INSTANCE_NAME
  25. domain=$(ynh_app_setting_get $app domain)
  26. path_url=$(ynh_app_setting_get $app path)
  27. final_path=$(ynh_app_setting_get $app final_path)
  28. enable_dhcp=$(ynh_app_setting_get $app enable_dhcp)
  29. admin=$(ynh_app_setting_get $app admin)
  30. #=================================================
  31. # CHECK IF THE APP CAN BE RESTORED
  32. #=================================================
  33. ynh_webpath_available $domain $path_url \
  34. || ynh_die "Path not available: ${domain}${path_url}"
  35. test ! -d $final_path \
  36. || ynh_die "There is already a directory: $final_path "
  37. #=================================================
  38. # ACTIVATE MAINTENANCE MODE
  39. #=================================================
  40. ynh_maintenance_mode_ON
  41. #=================================================
  42. # STANDARD RESTORE STEPS
  43. #=================================================
  44. # RESTORE OF THE NGINX CONFIGURATION
  45. #=================================================
  46. ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
  47. #=================================================
  48. # RESTORE OF THE MAIN DIRECTORIES OF THE APP
  49. #=================================================
  50. ynh_restore_file "$final_path"
  51. ynh_restore_file "/etc/.pihole"
  52. ynh_restore_file "/etc/pihole"
  53. ynh_restore_file "/opt/pihole"
  54. #=================================================
  55. # RECREATE OF THE DEDICATED USER
  56. #=================================================
  57. ynh_system_user_create $app # Recreate the dedicated user, if not exist
  58. #=================================================
  59. # RESTORE USER RIGHTS
  60. #=================================================
  61. # Les fichiers appartiennent à root
  62. chown $app: -R "/etc/pihole"
  63. #=================================================
  64. # RESTORE OF THE PHP-FPM CONFIGURATION
  65. #=================================================
  66. ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf"
  67. ynh_restore_file "/etc/php5/fpm/conf.d/20-$app.ini"
  68. #=================================================
  69. # SPECIFIC RESTORE
  70. #=================================================
  71. # REINSTALL DEPENDENCIES
  72. #=================================================
  73. ynh_install_app_dependencies $app_depencencies
  74. #=================================================
  75. # ENABLE SERVICE IN ADMIN PANEL
  76. #=================================================
  77. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  78. #=================================================
  79. # RESTORE OF 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 OF SPECIFIC FILES
  91. #=================================================
  92. ynh_restore_file "/usr/local/bin/pihole"
  93. ynh_restore_file "/etc/bash_completion.d/pihole"
  94. ynh_restore_file "/etc/sudoers.d/pihole"
  95. ynh_restore_file "/etc/init.d/pihole-FTL"
  96. ynh_restore_file "/usr/bin/pihole-FTL"
  97. ynh_restore_file "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  98. #=================================================
  99. # RESTORE OF DNSMASQ CONFIG
  100. #=================================================
  101. ynh_system_reload dnsmasq stop
  102. ynh_restore_file "/etc/dnsmasq.d/01-pihole.conf"
  103. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
  104. ynh_restore_file "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  105. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
  106. ynh_restore_file "/etc/dnsmasq.d/03-pihole-wildcard.conf"
  107. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
  108. ynh_restore_file "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  109. # Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
  110. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  111. #=================================================
  112. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  113. #=================================================
  114. # Trouve l'interface réseau par défaut
  115. main_iface=$(route | grep default | awk '{print $8;}' | head -n1)
  116. # Trouve l'ipv4 associée à l'interface trouvée
  117. localipv4=$(ifconfig | grep -A 1 "$main_iface" | tail -1 | awk '{print $2;}' | cut -d: -f2)
  118. # Liste les domaines de yunohost
  119. while read perdomain
  120. do
  121. # Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
  122. sed -i "s/^127.0.0.1.*$perdomain/#Commented by pihole# &/g" /etc/hosts
  123. # Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
  124. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  125. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  126. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  127. #=================================================
  128. # RESTART DNSMASQ
  129. #=================================================
  130. ynh_system_reload dnsmasq restart
  131. #=================================================
  132. # UPDATE THE VARIABLES FILE
  133. #=================================================
  134. setupVars="/etc/pihole/setupVars.conf"
  135. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  136. ynh_replace_string "^PIHOLE_INTERFACE=.*" "PIHOLE_INTERFACE=$main_iface" $setupVars
  137. ynh_replace_string "^IPV4_ADDRESS=.*" "IPV4_ADDRESS=127.0.0.1" $setupVars
  138. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  139. #=================================================
  140. # START PIHOLE-FTL
  141. #=================================================
  142. ynh_exec_warn_less systemctl enable pihole-FTL
  143. ynh_system_reload pihole-FTL restart
  144. #=================================================
  145. # GENERIC FINALISATION
  146. #=================================================
  147. # RELOAD NGINX AND PHP-FPM
  148. #=================================================
  149. ynh_system_reload php5-fpm
  150. ynh_system_reload nginx
  151. #=================================================
  152. # DEACTIVE MAINTENANCE MODE
  153. #=================================================
  154. ynh_maintenance_mode_OFF
  155. #=================================================
  156. # SEND A README FOR THE ADMIN
  157. #=================================================
  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}If you 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 "$message" "$admin"