Guest User

Untitled

a guest
Sep 20th, 2018
1,659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.19 KB | None | 0 0
  1. #!/bin/bash -x
  2. #  README
  3. :'
  4. Credit to redditor /u/jvinch76  https://www.reddit.com/user/jvinch76 for creating the basis for this modification.
  5. I had been thinking of a script like his to keep my primary and secondary pihole raspberry pis in sync, but could not find the motivation to create it.
  6. /u/jvinch76 did the heavy lifting and I made changes I hope you find useful.
  7.  
  8. I modified the code to increase the frequency of the sync to every 5 minutes and reduce the file writes by using rsync to compare the files and only transfer changes.
  9. Furthermore, gravity will be updated and services restarted only if files are modified and a sync occurs.
  10.  
  11. I am unsure of the performance cost, but it is likely there is a trade-off with rsync being more cpu heavy, but this script reduces the disk write to minimal amounts if no sync is necessary.
  12.  
  13. Why run dual piholes?
  14. If you are not, you really, really should be.  If the primary pihole is being updated, undergoing maintenance, running a backup, or simply failed you will not have a backup pihole available.
  15. This will happen on your network.  Your only other option during an outage (usually unexpected) is to configure your DHCP server to forward to a non-pihole, public DNS, thusly defeating why you have pihole installed in the first place.
  16. Furthermore, DNS is load balanced by design and the secondary\tertiary DNS always receives 10%-20% of the DNS traffic and if configured with a public DNS IP, your devices will be bypassing the safety of pihole blocking.
  17. If you are running a single pihole and have that pihole listed as the only DNS entry in your DHCP setting, all devices on your network will immediately be unable to resolve DNS if that pihole goes offline.
  18. I recommend running a PI3 as your primary and a PI3/PI2/ZeroW as your secondary.  PI2/ZeroW is more than sufficient as a secondary and emergency failover.
  19.  
  20. What about using my pihole for DHCP?
  21. I still prefer to use my router for DHCP, if you need help refer to /u/jvinch76 post https://www.reddit.com/r/pihole/comments/9gw6hx/sync_two_piholes_bash_script/
  22. or other docs about using pihole for DHCP with this script.
  23.  
  24. /u/LandlordTiberius
  25. '
  26.  
  27. # INSTALLATION STEPS ON PRIMARY PIHOLE
  28. : '
  29. 1. Login to pihole
  30. 2. type "SUDO NANO ~/piholesync.rsync.sh" to create file
  31. 3. cut and paste all information in this code snippet
  32. 4. edit PIHOLE2, HAUSER, HAPASS to match your SECONDARY pihole settings
  33. 5. save and exit
  34. 6. type "chmod +x ~/piholesync.rsync.sh" to make file executable
  35.  
  36. # CREATE SSH file transfer permissions
  37. 7. type "ssh-keygen"
  38. 8. type "ssh-copy-id user@192.168.1.3" <- type the same IP as PIHOLE2, this IP is specific to your network, 192.168.1.3 is an example only
  39. 9. type "yes" - YOU MUST TYPE "yes", not "y"
  40. 10. type the password of your secondary pihole
  41.  
  42. # INSTALL CRON Job
  43. 11. type "crontab -e"
  44. 12. scroll to the bottom of the editor, and on a new blank line,
  45. 13. type "*/5 * * * * /bin/bash /root/piholesync.rsync.sh" <- this will run rsync every 5 minutes, edit per your preferences\tolerence, see https://crontab.guru/every-5-minutes for help
  46. 14. save and exit
  47.  
  48. # DONE
  49. '
  50.  
  51. #VARS
  52. FILES=(black.list blacklist.txt regex.list whitelist.txt) #list of files you want to sync
  53. PIHOLEDIR=/etc/pihole #working dir of pihole
  54. PIHOLE2=192.168.1.3 #IP of 2nd PiHole
  55. HAUSER=pi #user of second pihole
  56. HAPASS=raspberry #password of second pihole
  57.  
  58. #LOOP FOR FILE TRANSFER
  59. RESTART=0 # flag determine if service restart is needed
  60. for FILE in ${FILES[@]}
  61. do
  62.   RSYNC_COMMAND=$(rsync -ai $PIHOLEDIR/$FILE $HAUSER@$PIHOLE2:$PIHOLEDIR)
  63.   if [[ -n "${RSYNC_COMMAND}" ]]; then
  64.     # rsync copied changes
  65.     RESTART=1 # restart flagged
  66.    # else
  67.      # no changes
  68.    fi
  69. done
  70.  
  71. FILE="adlists.list"
  72. RSYNC_COMMAND=$(rsync -ai $PIHOLEDIR/$FILE $HAUSER@$PIHOLE2:$PIHOLEDIR)
  73. if [[ -n "${RSYNC_COMMAND}" ]]; then
  74.   # rsync copied changes, update GRAVITY
  75.   ssh $HAUSER@$PIHOLE2 "echo $HAPASS | sudo -S pihole -g"
  76. # else
  77.   # no changes
  78. fi
  79.  
  80. if [ $RESTART == "1" ]; then
  81.   # INSTALL FILES AND RESTART pihole
  82.   ssh $HAUSER@$PIHOLE2 "echo $HAPASS | sudo -S service pihole-FTL stop"
  83.   ssh $HAUSER@$PIHOLE2 "echo $HAPASS | sudo -S pkill pihole-FTL"
  84.   ssh $HAUSER@$PIHOLE2 "echo $HAPASS | sudo -S service pihole-FTL start"
  85. fi
Add Comment
Please, Sign In to add comment