restore 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 OF THE NGINX CONFIGURATION
  40. #=================================================
  41. ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
  42. #=================================================
  43. # RESTORE OF 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 OF THE DEDICATED USER
  51. #=================================================
  52. ynh_system_user_create $app # Recreate the dedicated user, if not exist
  53. #=================================================
  54. # RESTORE USER RIGHTS
  55. #=================================================
  56. # Les fichiers appartiennent à root
  57. chown $app: -R "/etc/pihole"
  58. #=================================================
  59. # RESTORE OF THE PHP-FPM CONFIGURATION
  60. #=================================================
  61. ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf"
  62. ynh_restore_file "/etc/php5/fpm/conf.d/20-$app.ini"
  63. #=================================================
  64. # SPECIFIC RESTORE
  65. #=================================================
  66. # REINSTALL DEPENDENCIES
  67. #=================================================
  68. ynh_install_app_dependencies $app_depencencies
  69. #=================================================
  70. # ENABLE SERVICE IN ADMIN PANEL
  71. #=================================================
  72. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  73. #=================================================
  74. # RESTORE OF 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 OF 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 OF DNSMASQ CONFIG
  95. #=================================================
  96. ynh_system_reload --service_name=dnsmasq --action=stop
  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. # Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
  105. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  106. #=================================================
  107. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  108. #=================================================
  109. # Trouve l'interface réseau par défaut
  110. main_iface=$(ip route | grep default | awk '{print $5;}')
  111. # Trouve l'ipv4 associée à l'interface trouvée
  112. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  113. # Liste les domaines de yunohost
  114. while read perdomain
  115. do
  116. # Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
  117. sed -i "s/^127.0.0.1.*$perdomain/#Commented by pihole# &/g" /etc/hosts
  118. # Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
  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_system_reload --service_name=dnsmasq --action=restart
  126. #=================================================
  127. # UPDATE THE 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. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  134. #=================================================
  135. # START PIHOLE-FTL
  136. #=================================================
  137. ynh_exec_warn_less systemctl enable pihole-FTL
  138. ynh_system_reload --service_name=pihole-FTL --action=restart
  139. #=================================================
  140. # GENERIC FINALISATION
  141. #=================================================
  142. # RELOAD NGINX AND PHP-FPM
  143. #=================================================
  144. ynh_system_reload --service_name=php5-fpm
  145. ynh_system_reload --service_name=nginx
  146. #=================================================
  147. # DEACTIVE MAINTENANCE MODE
  148. #=================================================
  149. ynh_maintenance_mode_OFF
  150. #=================================================
  151. # SEND A README FOR THE ADMIN
  152. #=================================================
  153. if [ $enable_dhcp -eq 1 ]
  154. then
  155. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  156. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  157. "
  158. else
  159. dhcp_alert=""
  160. fi
  161. 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"
  162. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin"