restore 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #=================================================
  63. # SPECIFIC RESTORE
  64. #=================================================
  65. # REINSTALL DEPENDENCIES
  66. #=================================================
  67. ynh_install_app_dependencies $app_depencencies
  68. #=================================================
  69. # ENABLE SERVICE IN ADMIN PANEL
  70. #=================================================
  71. yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  72. #=================================================
  73. # RESTORE OF THE CRON FILE
  74. #=================================================
  75. ynh_restore_file "/etc/cron.d/pihole"
  76. #=================================================
  77. # RECREATE LOG FILES
  78. #=================================================
  79. touch /var/log/pihole.log
  80. chmod 644 /var/log/pihole.log
  81. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  82. chown $dnsmasq_user:root /var/log/pihole.log
  83. #=================================================
  84. # RESTORE OF SPECIFIC FILES
  85. #=================================================
  86. ynh_restore_file "/usr/local/bin/pihole"
  87. ynh_restore_file "/etc/bash_completion.d/pihole"
  88. ynh_restore_file "/etc/sudoers.d/pihole"
  89. ynh_restore_file "/etc/init.d/pihole-FTL"
  90. ynh_restore_file "/usr/bin/pihole-FTL"
  91. ynh_restore_file "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  92. #=================================================
  93. # RESTORE OF DNSMASQ CONFIG
  94. #=================================================
  95. ynh_systemd_action --action=stop --service_name=dnsmasq
  96. ynh_restore_file "/etc/dnsmasq.d/01-pihole.conf"
  97. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \
  98. ynh_restore_file "/etc/dnsmasq.d/02-pihole-dhcp.conf"
  99. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \
  100. ynh_restore_file "/etc/dnsmasq.d/03-pihole-wildcard.conf"
  101. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \
  102. ynh_restore_file "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  103. # Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
  104. ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
  105. #=================================================
  106. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  107. #=================================================
  108. # Trouve l'interface réseau par défaut
  109. main_iface=$(ip route | grep default | awk '{print $5;}')
  110. # Trouve l'ipv4 associée à l'interface trouvée
  111. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  112. # Liste les domaines de yunohost
  113. while read perdomain
  114. do
  115. # Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
  116. sed -i "s/^127.0.0.1.*$perdomain/#Commented by pihole# &/g" /etc/hosts
  117. # Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
  118. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  119. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  120. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  121. #=================================================
  122. # RESTART DNSMASQ
  123. #=================================================
  124. ynh_systemd_action --action=restart --service_name=dnsmasq
  125. #=================================================
  126. # UPDATE THE VARIABLES FILE
  127. #=================================================
  128. setupVars="/etc/pihole/setupVars.conf"
  129. echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
  130. ynh_replace_string "^PIHOLE_INTERFACE=.*" "PIHOLE_INTERFACE=$main_iface" $setupVars
  131. ynh_replace_string "^IPV4_ADDRESS=.*" "IPV4_ADDRESS=127.0.0.1" $setupVars
  132. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  133. #=================================================
  134. # START PIHOLE-FTL
  135. #=================================================
  136. ynh_exec_warn_less systemctl enable pihole-FTL
  137. ynh_systemd_action --action=restart --service_name=pihole-FTL
  138. #=================================================
  139. # GENERIC FINALISATION
  140. #=================================================
  141. # RELOAD NGINX AND PHP-FPM
  142. #=================================================
  143. ynh_systemd_action --action=reload --service_name=php5-fpm
  144. ynh_systemd_action --action=reload --service_name=nginx
  145. #=================================================
  146. # DEACTIVE MAINTENANCE MODE
  147. #=================================================
  148. ynh_maintenance_mode_OFF
  149. #=================================================
  150. # SEND A README FOR THE ADMIN
  151. #=================================================
  152. if [ $enable_dhcp -eq 1 ]
  153. then
  154. dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
  155. You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
  156. "
  157. else
  158. dhcp_alert=""
  159. fi
  160. 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"
  161. ynh_send_readme_to_admin --app_message="$message" --recipients="$admin"