restore 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. #=================================================
  3. # IMPORT GENERIC HELPERS
  4. #=================================================
  5. # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
  6. source ../settings/scripts/_common.sh
  7. source /usr/share/yunohost/helpers
  8. #=================================================
  9. # INITIALIZE AND STORE SETTINGS
  10. #=================================================
  11. dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
  12. #=================================================
  13. # CHECK AVAILABLE PORT
  14. #=================================================
  15. _configure_ports
  16. #=================================================
  17. # ACTIVATE MAINTENANCE MODE
  18. #=================================================
  19. ynh_script_progression --message="Activating maintenance mode..." --weight=2
  20. ynh_maintenance_mode_ON
  21. #=================================================
  22. # RESTORE THE APP MAIN DIR
  23. #=================================================
  24. ynh_script_progression --message="Restoring the app main directory..." --weight=1
  25. ynh_restore_file --origin_path="$install_dir"
  26. chmod -R o-rwx "$install_dir"
  27. chown -R "$app:www-data" "$install_dir"
  28. touch /var/log/{pihole,pihole-FTL}.log
  29. chmod 644 /var/log/{pihole,pihole-FTL}.log
  30. chown "$dnsmasq_user:root" /var/log/{pihole,pihole-FTL}.log
  31. #=================================================
  32. # RESTORE SPECIFIC FILES
  33. #=================================================
  34. ynh_script_progression --message="Restoring specific files..." --weight=1
  35. ynh_restore_file --origin_path="$PI_HOLE_LOCAL_REPO"
  36. ynh_restore_file --origin_path="$PI_HOLE_CONFIG_DIR"
  37. chown "$app:" -R "$PI_HOLE_CONFIG_DIR"
  38. # $PI_HOLE_CONFIG_DIR/logrotate have to belong to root, otherwise logrotate will failed silently...
  39. chown root: -R "$PI_HOLE_CONFIG_DIR/logrotate"
  40. ynh_restore_file --origin_path="$PI_HOLE_INSTALL_DIR"
  41. ynh_restore_file --origin_path="$PI_HOLE_BIN_DIR/pihole"
  42. ynh_restore_file --origin_path="/etc/bash_completion.d/pihole"
  43. ynh_restore_file --origin_path="/usr/bin/pihole-FTL"
  44. #=================================================
  45. # FINAL EXPORTS
  46. #=================================================
  47. ynh_script_progression --message="Final exports..." --weight=1
  48. setupVars="$PI_HOLE_CONFIG_DIR/setupVars.conf"
  49. # Get the default network interface
  50. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  51. echo "PIHOLE_INTERFACE=$main_iface" > "$setupVars"
  52. ynh_replace_string --target_file="$setupVars" --match_string="^PIHOLE_INTERFACE=.*" --replace_string="PIHOLE_INTERFACE=$main_iface"
  53. ynh_replace_string --target_file="$setupVars" --match_string="^IPV4_ADDRESS=.*" --replace_string="IPV4_ADDRESS=127.0.0.1"
  54. # Calculate and store the config file checksum into the app settings
  55. ynh_store_file_checksum --file="$setupVars"
  56. #=================================================
  57. # CONFIGURE DNS FOR THE LOCAL DOMAINS
  58. #=================================================
  59. ynh_script_progression --message="Configuring DNS for the local domains..." --weight=2
  60. # Find the IP associated to the network interface
  61. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  62. # List all YunoHost domains
  63. while read -r perdomain; do
  64. # Comment domain resolution in /etc/hosts on 127.0.0.1, because they can interfere with the local network resolution.
  65. ynh_replace_string --match_string="^127.0.0.1.*$perdomain" --replace_string="#Commented by pihole# &" --target_file=/etc/hosts
  66. # And add a resolution on the local IP instead
  67. grep -q "^$localipv4.*$perdomain" /etc/hosts || \
  68. echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
  69. done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
  70. #=================================================
  71. # DISABLING DNSMASQ
  72. #=================================================
  73. ynh_script_progression --message="Disabling Dnsmasq in system and yunohost..." --weight=1
  74. # Stopped dnsmasq to replace it by pihole-FTL
  75. ynh_systemd_action --service_name=dnsmasq --action=stop
  76. # Replace the service dnsmasq by pihole-FTL
  77. # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL
  78. #ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service
  79. systemctl mask dnsmasq.service
  80. # Disable the real dnsmasq service
  81. #ynh_exec_warn_less systemctl disable dnsmasq --quiet
  82. # Reload systemd config
  83. systemctl daemon-reload
  84. test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf"
  85. ynh_restore_file --origin_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app"
  86. ynh_exec_warn_less yunohost tools regen-conf dnsmasq
  87. #=================================================
  88. # RESTORE SYSTEM CONFIGURATIONS
  89. #=================================================
  90. ynh_script_progression --message="Restoring system configurations related to $app..." --weight=1
  91. ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
  92. ynh_restore_file --origin_path="/etc/cron.d/pihole"
  93. ynh_restore_file --origin_path="/etc/sudoers.d/pihole"
  94. ynh_restore_file --origin_path="/etc/init.d/pihole-FTL"
  95. # install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
  96. systemctl daemon-reload
  97. ynh_exec_warn_less systemctl enable pihole-FTL --quiet
  98. yunohost service add pihole-FTL --description="PiHole backend service" --log="/var/log/pihole/FTL.log" --needs_exposed_ports 53 67
  99. #=================================================
  100. # RELOAD NGINX AND PHP-FPM OR THE APP SERVICE
  101. #=================================================
  102. ynh_script_progression --message="Reloading NGINX web server and $app's service..." --weight=1
  103. ynh_systemd_action --service_name="pihole-FTL" --action="restart" --line_match="Web server ports" --log_path="/var/log/pihole/FTL.log"
  104. ynh_systemd_action --service_name="nginx" --action=reload
  105. #=================================================
  106. # DEACTIVE MAINTENANCE MODE
  107. #=================================================
  108. ynh_script_progression --message="Disabling maintenance mode..." --weight=4
  109. ynh_maintenance_mode_OFF
  110. #=================================================
  111. # END OF SCRIPT
  112. #=================================================
  113. ynh_script_progression --message="Restoration completed for $app" --last