restore 7.2 KB

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