restore 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/bin/bash
  2. #=================================================
  3. # GENERIC STARTING
  4. #=================================================
  5. # MANAGE FAILURE OF THE SCRIPT
  6. #=================================================
  7. # Exit on command errors and treat unset variables as an error
  8. set -eu
  9. #=================================================
  10. # IMPORT GENERIC HELPERS
  11. #=================================================
  12. if [ ! -e _common.sh ]; then
  13. # Rapatrie le fichier de fonctions si il n'est pas dans le dossier courant
  14. sudo cp ../settings/scripts/_common.sh ./_common.sh
  15. sudo chmod a+rx _common.sh
  16. fi
  17. source _common.sh
  18. source /usr/share/yunohost/helpers
  19. #=================================================
  20. # LOAD SETTINGS
  21. #=================================================
  22. app=$YNH_APP_INSTANCE_NAME
  23. domain=$(ynh_app_setting_get $app domain)
  24. path_url=$(ynh_app_setting_get $app path)
  25. final_path=$(ynh_app_setting_get $app final_path)
  26. #=================================================
  27. # CHECK IF THE APP CAN BE RESTORED
  28. #=================================================
  29. sudo yunohost app checkurl "${domain}${path_url}" -a "$app" \
  30. || ynh_die "Path not available: ${domain}${path_url}"
  31. test ! -d $final_path \
  32. || ynh_die "There is already a directory: $final_path "
  33. #=================================================
  34. # STANDARD RESTORE STEPS
  35. #=================================================
  36. # RESTORE OF THE NGINX CONFIGURATION
  37. #=================================================
  38. ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
  39. #=================================================
  40. # RESTORE OF THE MAIN DIRECTORIES OF THE APP
  41. #=================================================
  42. ynh_restore_file "$final_path"
  43. ynh_restore_file "/etc/.pihole"
  44. ynh_restore_file "/etc/pihole"
  45. ynh_restore_file "/opt/pihole"
  46. #=================================================
  47. # RECREATE OF THE DEDICATED USER
  48. #=================================================
  49. ynh_system_user_create $app # Recreate the dedicated user, if not exist
  50. #=================================================
  51. # RESTORE USER RIGHTS
  52. #=================================================
  53. # Les fichiers appartiennent à root
  54. chown $app: -R "/etc/pihole"
  55. #=================================================
  56. # RESTORE OF THE PHP-FPM CONFIGURATION
  57. #=================================================
  58. ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf"
  59. ynh_restore_file "/etc/php5/fpm/conf.d/20-$app.ini"
  60. #=================================================
  61. # SPECIFIC RESTORE
  62. #=================================================
  63. # REINSTALL DEPENDENCIES
  64. #=================================================
  65. ynh_install_app_dependencies dhcpcd5
  66. #=================================================
  67. # ENABLE SERVICE IN ADMIN PANEL
  68. #=================================================
  69. sudo yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
  70. #=================================================
  71. # RESTORE OF THE CRON FILE
  72. #=================================================
  73. ynh_restore_file "/etc/cron.d/pihole"
  74. #=================================================
  75. # RECREATE LOG FILES
  76. #=================================================
  77. touch /var/log/pihole.log
  78. chmod 644 /var/log/pihole.log
  79. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  80. chown $dnsmasq_user:root /var/log/pihole.log
  81. #=================================================
  82. # RESTORE OF SPECIFIC FILES
  83. #=================================================
  84. ynh_restore_file "/usr/local/bin/pihole"
  85. ynh_restore_file "/etc/bash_completion.d/pihole"
  86. ynh_restore_file "/etc/sudoers.d/pihole"
  87. ynh_restore_file "/etc/init.d/pihole-FTL"
  88. ynh_restore_file "/usr/bin/pihole-FTL"
  89. ynh_restore_file "/var/run/pihole-FTL.pid"
  90. ynh_restore_file "/var/run/pihole-FTL.port"
  91. ynh_restore_file "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  92. #=================================================
  93. # RESTORE OF DNSMASQ CONFIG
  94. #=================================================
  95. systemctl stop 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=$(route | grep default | awk '{print $8;}' | head -n1)
  110. # Trouve l'ipv4 associée à l'interface trouvée
  111. localipv4=$(ifconfig | grep -A 1 "$main_iface" | tail -1 | awk '{print $2;}' | cut -d: -f2)
  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 <<< "$(sudo yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  121. #=================================================
  122. # RESTART DNSMASQ
  123. #=================================================
  124. systemctl start 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=$localipv4" $setupVars
  132. ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
  133. #=================================================
  134. # START PIHOLE-FTL
  135. #=================================================
  136. SUPPRESS_WARNING systemctl enable pihole-FTL
  137. systemctl start pihole-FTL
  138. #=================================================
  139. # GENERIC FINALISATION
  140. #=================================================
  141. # RELOAD NGINX AND PHP-FPM
  142. #=================================================
  143. sudo systemctl reload php5-fpm
  144. sudo systemctl reload nginx