Last active
June 17, 2022 18:32
-
-
Save kernelkaribou/3699d7794cf2dee7fe33c41cdd922ece to your computer and use it in GitHub Desktop.
Pihole Instance Sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
###################################################################### | |
# Bash script for syncing pihole servers to a secondary | |
# | |
# This tool will allow you sync common pihole settings, either | |
# full server (DNS Records and Gravity DB for adlists) or select | |
# specific syncs to perform. It does NOT sync the core config settings | |
# of the instance found in setupVars.conf | |
# | |
# This is assuming you have two pihole instances running on docker. | |
# This script will pull from the executing server to the remote server. | |
# I prefer to run this on my secondary so I can make changes, test | |
# then push to the primary DNS server. | |
# | |
# Last update: 2022-06-14 | |
# Author: kernelkaribou@github | |
###################################################################### | |
# Config settings | |
local_dir="" # Location of Pihole volumes. Example I use docker/pihole/app & docker/pihole/dnsmasq.d | |
identity_path="" # Path to ssh key, you need one if you dont have one | |
remote_server="" # Remote server IP/hostname | |
remote_port= # Remote Server SSH Port | |
remote_user="" # Remote Server user | |
remote_dir="" #Remote server base pihole directory | |
Help() | |
{ | |
# Display Help | |
echo | |
echo "Script for syncing common primary PiHole server settings to Secondary instances" | |
echo | |
echo "Syntax: scriptName.sh [-f|d|g|h]" | |
echo | |
echo "options:" | |
echo "-f Full server sync" | |
echo "-d Sync only DNS records" | |
echo "-g Sync only Gravity DB" | |
echo "-h Display this help information" | |
echo | |
} | |
getTimestamp() | |
{ | |
echo $(date +"%Y-%m-%d %T") | |
} | |
startSync() | |
{ | |
echo "$(getTimestamp) - Beginning Pihole sync" | |
TYPE=$1; | |
if [ $TYPE == "f" ] | |
then | |
echo "$(getTimestamp) - Performing full Pihole server sync" | |
dnsSync | |
gravitySync | |
elif [ $TYPE == "d" ] | |
then | |
echo "$(getTimestamp) - Performing custom DNS records sync only" | |
dnsSync | |
elif [ $TYPE == "g" ] | |
then | |
echo "$(getTimestamp) - Performing Gravity Database sync only" | |
gravitySync | |
fi | |
echo "$(getTimestamp) - Pihole sync complete" | |
} | |
dnsSync() | |
{ | |
# Copy A record and CNAME record list | |
rsync -avpP -e "ssh -p 8622 -i $identity_path" --rsync-path="sudo rsync" $local_dir/app/custom.list $remote_user@$remote_server:$remote_dir/app/custom.list | |
rsync -avpP -e "ssh -p 8622 -i $identity_path" --rsync-path="sudo rsync" $local_dir/dnsmasq.d/05-pihole-custom-cname.conf $remote_user@$remote_server:$remote_dir/dnsmasq.d/05-pihole-custom-cname.conf | |
#Restart DNS after copying files | |
echo "$(getTimestamp) - Restarting DNS server" | |
ssh -p $remote_port -i $identity_path $remote_user@$remote_server "sudo docker exec pihole pihole restartdns" | |
echo "$(getTimestamp) - DNS files sync completed" | |
} | |
gravitySync() | |
{ | |
# Copy Gravity DB | |
rsync -avpP -e "ssh -p 8622 -i $identity_path" --rsync-path="sudo rsync" $local_dir/app/gravity.db $remote_user@$remote_server:$remote_dir/gravity.db | |
echo "$(getTimestamp) - Gravity Database sync completed" | |
} | |
# Get the options | |
while getopts "hfdg" option; do | |
case $option in | |
h) # display Help | |
Help | |
exit;; | |
f) # Full pihole sync | |
startSync "f" | |
exit;; | |
d) # Custom DNS sync | |
startSync "d" | |
exit;; | |
g) # Gravity Database sync | |
startSync "g" | |
exit;; | |
\?) # Invalid option | |
echo "Error: Invalid option, -h for help" | |
exit;; | |
esac | |
done | |
echo "Missing option, -h for help" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment