PiHole HA Sync

Script to run on a primary PiHole server to sync blocklist database and custom local DNS entries to a secondary server.

Make sure to setup sshkeys to allow user on primary server to login to secondary server.

Then run this script in the cron every 15-30 minutes.

# HA Sync from PiHolePri to PiHoleSec
*/15 * * * * /root/pi-ha-sync.sh >> /var/log/pi-ha-sync.log 2>&1
#!/usr/bin/env bash

sshuser=pi
pisecondary=10.10.10.10
sshkey=/root/.ssh/id.pi
filelist="gravity.db custom.list"

piping=$(timeout 0.2 ping -c1 ${pisecondary} &> /dev/null && echo "0" || echo "1")
if [ "${piping}" -eq 0 ]; then
  cd /etc/pihole/
  ssh -i ${sshkey} ${sshuser}@${pisecondary} 'if [ ! -d ~/piholepri ]; then mkdir ~/piholepri; fi'
  scp -q -i ${sshkey} ${filelist} ${sshuser}@${pisecondary}:~/piholepri
  ssh -i ${sshkey} ${sshuser}@${pisecondary} "sudo cp ~/piholepri/* /etc/pihole"
  ssh -i ${sshkey} ${sshuser}@${pisecondary} "pihole restartdns reload-lists"
else
  echo "${pisecondary} is not available."
fi