Merge branch 'main' of https://git.schroedercity.com/voltron/my-pihole-sync
This commit is contained in:
207
pihole-sync
207
pihole-sync
@@ -4,11 +4,25 @@
|
|||||||
# pihole-cloudsync
|
# pihole-cloudsync
|
||||||
# Helper script to keep multiple Pi-holes' lists synchronized via Git
|
# Helper script to keep multiple Pi-holes' lists synchronized via Git
|
||||||
|
|
||||||
|
# ORIGINAL AUTHOR
|
||||||
# Steve Jenkins (stevejenkins.com)
|
# Steve Jenkins (stevejenkins.com)
|
||||||
# https://github.com/stevejenkins/pihole-cloudsync
|
# https://github.com/stevejenkins/pihole-cloudsync
|
||||||
|
|
||||||
version='5.0'
|
# Joel Goguen
|
||||||
update='December 26, 2020'
|
# https://github.com/jgoguen/pihole-cloudsync
|
||||||
|
# * Provide docker support
|
||||||
|
# * Allow non-master branches
|
||||||
|
|
||||||
|
# Jon Stephens
|
||||||
|
# https://github.com/wetnun/pihole-cloudsync
|
||||||
|
# * Remove collection of csv files in favor of dump
|
||||||
|
# * Move import/export to helper functions
|
||||||
|
# * Allow ENV file so you don't have to edit git files
|
||||||
|
# * Fix issue of custom whitelist/domains don't work in agents because of groups missing
|
||||||
|
# * Removed hard tabs, I just don't like them
|
||||||
|
|
||||||
|
version='6.0'
|
||||||
|
update='December 9, 2021'
|
||||||
|
|
||||||
# SETUP
|
# SETUP
|
||||||
# Follow the instructions in the README to set up your own private Git
|
# Follow the instructions in the README to set up your own private Git
|
||||||
@@ -30,6 +44,11 @@ update='December 26, 2020'
|
|||||||
# 'pihole-cloudsync --pull' will pull (download) your lists from a remote Git repo
|
# 'pihole-cloudsync --pull' will pull (download) your lists from a remote Git repo
|
||||||
|
|
||||||
# Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
# Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
||||||
|
|
||||||
|
# Create env.sh file with presets if you don't want to edit this file
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
|
||||||
|
[ -f "${SCRIPT_DIR}/env.sh" ] && source "${SCRIPT_DIR}/env.sh"
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# CONSTANTS
|
# CONSTANTS
|
||||||
if [ -f "/opt/my-pihole-sync/pihole-sync" ]; then
|
if [ -f "/opt/my-pihole-sync/pihole-sync" ]; then
|
||||||
@@ -39,55 +58,84 @@ elif [ -f "/etc/pihole/my-pihole-sync/pihole-sync" ]; then
|
|||||||
else
|
else
|
||||||
personal_git_dir='/usr/local/bin/my-pihole-sync'
|
personal_git_dir='/usr/local/bin/my-pihole-sync'
|
||||||
fi
|
fi
|
||||||
|
git_branch="${GIT_BRANCH:-master}"
|
||||||
|
pihole_dir="${PIHOLE_DIR:-/etc/pihole}"
|
||||||
# if [ -d "/opt/docker-pi-hole/etc-pihole" ]; then
|
gravity_db="${GRAVITY_DB:-/etc/pihole/gravity.db}"
|
||||||
# pihole_dir='/opt/docker-pi-hole/etc-pihole'
|
dnsmasq_dir="${DNSMASQ_DIR:-/etc/dnsmasq.d}"
|
||||||
# gravity_db='/opt/docker-pi-hole/etc-pihole/gravity.db'
|
custom_list="${CUSTOM_LIST:-custom.list}"
|
||||||
# dnsmasq_dir='/opt/docker-pi-hole/etc-dnsmasq.d/'
|
cname_list="${CNAME_LIST:-05-pihole-custom-cname.conf}"
|
||||||
# else
|
|
||||||
pihole_dir='/etc/pihole'
|
|
||||||
gravity_db='/etc/pihole/gravity.db'
|
|
||||||
dnsmasq_dir='/etc/dnsmasq.d/'
|
|
||||||
# fi
|
|
||||||
|
|
||||||
ad_list='adlist.csv'
|
|
||||||
custom_list='custom.list'
|
|
||||||
domain_list='domainlist.csv'
|
|
||||||
cname_list='05-pihole-custom-cname.conf'
|
|
||||||
localdomains_list='02-local-domains.conf'
|
localdomains_list='02-local-domains.conf'
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# SHOULDN'T NEED TO EDIT BELOW THIS LINE
|
# SHOULDN'T NEED TO EDIT BELOW THIS LINE
|
||||||
|
|
||||||
|
# List of DB tables we need to migrate between instances
|
||||||
|
DB_TABLES="${SYNC_TABLES:-adlist domainlist group domainlist_by_group}"
|
||||||
|
DB_DUMP_FILE="db_dump.sql"
|
||||||
|
|
||||||
# Force sudo if not running with root privileges
|
# Force sudo if not running with root privileges
|
||||||
SUDO=''
|
SUDO=''
|
||||||
if [ "$EUID" -ne 0 ]
|
if [ "$EUID" -ne 0 ]; then
|
||||||
then SUDO='sudo'
|
SUDO='sudo'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Attempt to detect pihole running in Docker
|
||||||
|
DOCKER=''
|
||||||
|
DOCKER_CMD="$(command -v docker)"
|
||||||
|
JQ_CMD="$(command -v jq)"
|
||||||
|
if [ -n "${DOCKER_CMD}" ]; then
|
||||||
|
CONTAINER="$(${DOCKER_CMD} ps -f "ancestor=pihole/pihole" --format "{{.Names}}")"
|
||||||
|
if [ -n "${CONTAINER}" ]; then
|
||||||
|
if [ -n "${JQ_CMD}" ]; then
|
||||||
|
echo "Found pihole running under Docker container '${CONTAINER}'"
|
||||||
|
|
||||||
|
pihole_dir="$(${DOCKER_CMD} inspect -f "{{json .Mounts}}" "${CONTAINER}" | ${JQ_CMD} -r --arg dir "${pihole_dir}" '.[] | select(.Destination==$dir) | .Source')"
|
||||||
|
gravity_db="${pihole_dir}/gravity.db"
|
||||||
|
dnsmasq_dir="$(${DOCKER_CMD} inspect -f "{{json .Mounts}}" "${CONTAINER}" | ${JQ_CMD} -r --arg dir "${dnsmasq_dir}" '.[] | select(.Destination==$dir) | .Source')"
|
||||||
|
|
||||||
|
echo "Found pihole directory mapped to '${pihole_dir}'"
|
||||||
|
echo "Found dnsmasq directory mapped to '${dnsmasq_dir}'"
|
||||||
|
|
||||||
|
DOCKER="${DOCKER_CMD} exec -i ${CONTAINER}"
|
||||||
|
else
|
||||||
|
echo "Found Docker container '${CONTAINER}' but jq is not installed"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
export_tables () {
|
||||||
|
$SUDO sqlite3 $gravity_db ".dump --preserve-rowids $DB_TABLES" > $DB_DUMP_FILE
|
||||||
|
# sqlite3 doesn't have a drop option, so we inject it after the transaction is generated
|
||||||
|
for t in $DB_TABLES; do
|
||||||
|
sed -i "/BEGIN TRAN/a DROP TABLE IF EXISTS '$t';" $DB_DUMP_FILE
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
import_tables () {
|
||||||
|
$SUDO sqlite3 $gravity_db ".read $DB_DUMP_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
push_initialize () {
|
push_initialize () {
|
||||||
# Go to Pi-hole directory, exit if doesn't exist
|
# Go to Pi-hole directory, exit if doesn't exist
|
||||||
cd $pihole_dir || exit
|
cd $pihole_dir || exit
|
||||||
|
|
||||||
# Verify Custom and CNAME lists exist
|
# Verify Custom and CNAME lists exist
|
||||||
touch $custom_list
|
$SUDO touch $custom_list
|
||||||
touch $dnsmasq_dir/$cname_list
|
$SUDO touch $dnsmasq_dir/$cname_list
|
||||||
# touch $dnsmasq_dir/$localdomains_list
|
# touch $dnsmasq_dir/$localdomains_list
|
||||||
|
|
||||||
# Copy local Custom and CNAME lists to local Git repo
|
# Copy local Custom and CNAME lists to local Git repo
|
||||||
cp $custom_list $personal_git_dir
|
$SUDO cp $custom_list $personal_git_dir
|
||||||
cp $dnsmasq_dir/$cname_list $personal_git_dir
|
$SUDO cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||||
|
|
||||||
# Go to local Git repo directory
|
# Go to local Git repo directory
|
||||||
cd $personal_git_dir || exit
|
$SUDO cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Export Ad and Domain lists from Gravity database
|
# Export Ad and Domain lists from Gravity database
|
||||||
sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
export_tables
|
||||||
sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
|
||||||
|
|
||||||
# Add all lists to local Git repo
|
# Add all lists to local Git repo
|
||||||
git add .
|
$SUDO git add .
|
||||||
echo "Local Pi-hole initialized in Push mode and local lists were added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo.";
|
echo "Local Pi-hole initialized in Push mode and local lists were added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo.";
|
||||||
}
|
}
|
||||||
pull_initialize () {
|
pull_initialize () {
|
||||||
@@ -95,30 +143,26 @@ pull_initialize () {
|
|||||||
cd $personal_git_dir || exit
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Update local Git repo from remote Git repo
|
# Update local Git repo from remote Git repo
|
||||||
git remote update > /dev/null
|
$SUDO git remote update > /dev/null
|
||||||
|
|
||||||
# Remove -q option if you don't want to run in "quiet" mode
|
# Remove -q option if you don't want to run in "quiet" mode
|
||||||
git fetch --all -q
|
$SUDO git fetch --all -q
|
||||||
git reset --hard origin/main -q
|
$SUDO git reset --hard "origin/${git_branch}" -q
|
||||||
|
|
||||||
# Stop DNS server
|
# Stop DNS server
|
||||||
$SUDO service pihole-FTL stop
|
$SUDO ${DOCKER} service pihole-FTL stop
|
||||||
|
|
||||||
# Overwrite local files
|
# Overwrite local files
|
||||||
cp $custom_list $pihole_dir
|
$SUDO cp $custom_list $pihole_dir
|
||||||
cp $cname_list $dnsmasq_dir
|
$SUDO cp $cname_list $dnsmasq_dir
|
||||||
# if [[ $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) != *"10.10.10.11"* ]]; then
|
# if [[ $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) != *"10.10.10.11"* ]]; then
|
||||||
# cp $localdomains_list $dnsmasq_dir
|
# $SUDO cp $localdomains_list $dnsmasq_dir
|
||||||
# fi
|
# fi
|
||||||
|
|
||||||
# Overwrite local database tables
|
# Overwrite local database tables
|
||||||
sqlite3 $gravity_db "DROP TABLE adlist;"
|
import_tables
|
||||||
sqlite3 $gravity_db -header -csv ".import adlist.csv adlist"
|
|
||||||
sqlite3 $gravity_db "DROP TABLE domainlist;"
|
|
||||||
sqlite3 $gravity_db -header -csv ".import domainlist.csv domainlist"
|
|
||||||
|
|
||||||
# Restart Pi-hole to pick up changes
|
# Restart Pi-hole to pick up changes
|
||||||
$SUDO pihole -g
|
$SUDO ${DOCKER} pihole -g
|
||||||
|
|
||||||
# Display success messages
|
# Display success messages
|
||||||
echo "Local Pi-hole initialized in Pull mode and first pull successfully completed.";
|
echo "Local Pi-hole initialized in Pull mode and first pull successfully completed.";
|
||||||
@@ -129,28 +173,27 @@ push () {
|
|||||||
cd $pihole_dir || exit
|
cd $pihole_dir || exit
|
||||||
|
|
||||||
# Copy local Custom and CNAME lists to local Git repo
|
# Copy local Custom and CNAME lists to local Git repo
|
||||||
cp $custom_list $personal_git_dir
|
$SUDO cp $custom_list $personal_git_dir
|
||||||
cp $dnsmasq_dir/$cname_list $personal_git_dir
|
$SUDO cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||||
# cp $dnsmasq_dir/$localdomains_list $personal_git_dir
|
# $SUDO cp $dnsmasq_dir/$localdomains_list $personal_git_dir
|
||||||
|
|
||||||
# Go to local Git repo directory
|
# Go to local Git repo directory
|
||||||
cd $personal_git_dir || exit
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Export Ad and Domain lists from Gravity database
|
# Export Ad and Domain lists from Gravity database
|
||||||
sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
export_tables
|
||||||
sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
|
||||||
|
|
||||||
# Compare local files to remote Git repo
|
# Compare local files to remote Git repo
|
||||||
git remote update > /dev/null
|
$SUDO git remote update > /dev/null
|
||||||
|
|
||||||
# If local files are different than remote, update remote Git repo
|
# If local files are different than remote, update remote Git repo
|
||||||
CHANGED=$(git --work-tree=$personal_git_dir status --porcelain)
|
CHANGED=$($SUDO git --work-tree=$personal_git_dir status --porcelain)
|
||||||
if [ -n "${CHANGED}" ]; then
|
if [ -n "${CHANGED}" ]; then
|
||||||
echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...';
|
echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...';
|
||||||
rightnow=$(date +"%B %e, %Y %l:%M%p")
|
rightnow=$(date +"%B %e, %Y %l:%M%p")
|
||||||
# Remove -q option if you don't want to run in "quiet" mode
|
# Remove -q option if you don't want to run in "quiet" mode
|
||||||
git commit -a -m "Updated $rightnow" -q
|
$SUDO git commit -a -m "Updated $rightnow" -q
|
||||||
git push -q
|
$SUDO git push -q
|
||||||
echo 'Done!';
|
echo 'Done!';
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
@@ -164,23 +207,18 @@ pull () {
|
|||||||
cd $personal_git_dir || exit
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Update local Git repo from remote Git repo
|
# Update local Git repo from remote Git repo
|
||||||
git remote update > /dev/null
|
$SUDO git remote update > /dev/null
|
||||||
CHANGED=$(git log HEAD..origin/main --oneline)
|
CHANGED=$($SUDO git log HEAD..origin/${git_branch} --oneline)
|
||||||
if [ -n "${CHANGED}" ]; then
|
if [ -n "${CHANGED}" ]; then
|
||||||
echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...';
|
echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...';
|
||||||
git fetch --all -q
|
# Remove -q option if you don't want to run in "quiet" mode
|
||||||
git reset --hard origin/main -q
|
$SUDO git fetch --all -q
|
||||||
$SUDO service pihole-FTL stop
|
$SUDO git reset --hard "origin/${git_branch}" -q
|
||||||
cp $custom_list $pihole_dir
|
$SUDO ${DOCKER} service pihole-FTL stop
|
||||||
cp $cname_list $dnsmasq_dir
|
$SUDO cp $custom_list $pihole_dir
|
||||||
# if [[ $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) != *"10.10.10.11"* ]]; then
|
$SUDO cp $cname_list $dnsmasq_dir
|
||||||
# cp $localdomains_list $dnsmasq_dir
|
import_tables
|
||||||
# fi
|
$SUDO ${DOCKER} pihole -g
|
||||||
sqlite3 $gravity_db "DROP TABLE adlist;"
|
|
||||||
sqlite3 $gravity_db -header -csv ".import adlist.csv adlist"
|
|
||||||
sqlite3 $gravity_db "DROP TABLE domainlist;"
|
|
||||||
sqlite3 $gravity_db -header -csv ".import domainlist.csv domainlist"
|
|
||||||
$SUDO pihole -g
|
|
||||||
echo 'Done!';
|
echo 'Done!';
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
@@ -188,66 +226,61 @@ pull () {
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# Check to see whether a command line option was provided
|
# Check to see whether a command line option was provided
|
||||||
if [ -z "$1" ]
|
if [ -z "$1" ]; then
|
||||||
then
|
|
||||||
echo "Missing command line option. Try --push, --pull, or --help."
|
echo "Missing command line option. Try --push, --pull, or --help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# Determine which action to perform (InitPush, InitPull, Push, Pull, or Help)
|
# Determine which action to perform (InitPush, InitPull, Push, Pull, or Help)
|
||||||
for arg in "$@"
|
for arg in "$@"; do
|
||||||
do
|
|
||||||
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
||||||
if [ "$arg" == "--initpush" ]
|
if [ "$arg" == "--initpush" ]; then
|
||||||
then
|
|
||||||
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
|
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
|
||||||
push_initialize
|
push_initialize
|
||||||
exit 0
|
exit 0
|
||||||
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
||||||
elif [ "$arg" == "--initpull" ]
|
elif [ "$arg" == "--initpull" ]; then
|
||||||
then
|
|
||||||
echo "$arg option detected. Initializing local Git repo for Pull/Download.";
|
echo "$arg option detected. Initializing local Git repo for Pull/Download.";
|
||||||
|
shift
|
||||||
|
[ -n "$1" ] && git_branch="$1"
|
||||||
pull_initialize
|
pull_initialize
|
||||||
exit 0
|
exit 0
|
||||||
# Push / Upload - Pushes updated local Pi-hole lists to remote Git repo
|
# Push / Upload - Pushes updated local Pi-hole lists to remote Git repo
|
||||||
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]
|
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]; then
|
||||||
then
|
|
||||||
echo "$arg option detected. Running in Push/Upload mode."
|
echo "$arg option detected. Running in Push/Upload mode."
|
||||||
push
|
push
|
||||||
exit 0
|
exit 0
|
||||||
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
|
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
|
||||||
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]
|
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]; then
|
||||||
then
|
|
||||||
echo "$arg option detected. Running in Pull/Download mode."
|
echo "$arg option detected. Running in Pull/Download mode."
|
||||||
|
shift
|
||||||
|
[ -n "$1" ] && git_branch="$1"
|
||||||
pull
|
pull
|
||||||
exit 0
|
exit 0
|
||||||
# Help - Displays help dialog
|
# Help - Displays help dialog
|
||||||
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]
|
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]; then
|
||||||
then
|
cat <<- EOF
|
||||||
cat << EOF
|
|
||||||
Usage: pihole-cloudsync <option>
|
Usage: pihole-cloudsync <option>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo
|
--push, --upload, --up, -u Push (upload) your Pi-hole lists to a remote Git repo
|
||||||
--pull, --download, --down, -d Pull (download) your lists from a remote Git repo
|
--pull, --download, --down, -d [branch] Pull (download) your lists from a remote Git repo
|
||||||
--initpush Initialize Primary Pi-hole in "Push" mode
|
--initpush Initialize Primary Pi-hole in "Push" mode
|
||||||
--initpull Initialize Secondary Pi-hole in "Pull" mode
|
--initpull [branch] Initialize Secondary Pi-hole in "Pull" mode with optional branch (default: master)
|
||||||
--help, -h, -? Show this help dialog
|
--help, -h, -? Show this help dialog
|
||||||
--version, -v Show the current version of pihole-cloudsync
|
--version, -v Show the current version of pihole-cloudsync
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
'pihole-cloudsync --push' will push (upload) your lists to a Git repo
|
'pihole-cloudsync --push' will push (upload) your lists to a Git repo
|
||||||
'pihole-cloudsync --pull' will pull (download) your lists from a Git repo
|
'pihole-cloudsync --pull' will pull (download) your lists from a Git repo from origin/master
|
||||||
|
'pihole-cloudsync --pull main' will pull (download) your lists from a Git repo from origin/main
|
||||||
|
|
||||||
Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Version - Displays version number
|
# Version - Displays version number
|
||||||
elif [ "$arg" == "--version" ] || [ "$arg" == "-v" ]
|
elif [ "$arg" == "--version" ] || [ "$arg" == "-v" ]; then
|
||||||
then
|
|
||||||
echo 'pihole-cloudsync v'$version' - Updated '"$update";
|
echo 'pihole-cloudsync v'$version' - Updated '"$update";
|
||||||
echo 'https://github.com/stevejenkins/pihole-cloudsync';
|
echo 'https://github.com/stevejenkins/pihole-cloudsync';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user