Update pihole-sync
This commit is contained in:
372
pihole-sync
372
pihole-sync
@@ -6,9 +6,11 @@
|
||||
|
||||
# Steve Jenkins (stevejenkins.com)
|
||||
# https://github.com/stevejenkins/pihole-cloudsync
|
||||
# Extended by Michael Gamlem III (michael@mgamlem3.com)
|
||||
# https://github.com/mgamlem3/pihole-cloudsync
|
||||
|
||||
version='5.0'
|
||||
update='December 26, 2020'
|
||||
version='5.1.2'
|
||||
update='July 2022'
|
||||
|
||||
# SETUP
|
||||
# Follow the instructions in the README to set up your own private Git
|
||||
@@ -18,12 +20,14 @@ update='December 26, 2020'
|
||||
# USAGE: pihole-cloudsync <option>
|
||||
|
||||
# OPTIONS:
|
||||
# --initpush Initialize Primary Pi-hole in "Push" mode
|
||||
# --initpull Initialize Secondary Pi-hole in "Pull" mode
|
||||
# --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
|
||||
# --help, -h, -? Show the current version of pihole-cloudsync
|
||||
# --version, -v Show version number
|
||||
# --initpush Initialize Primary Pi-hole in "Push" mode
|
||||
# --initpull Initialize Secondary Pi-hole in "Pull" mode
|
||||
# --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
|
||||
# --setupcronpush Setup cronjob to push daily
|
||||
# --setupcronpull Setup cronjob to pull daily
|
||||
# --help, -h, -? Show the current version of pihole-cloudsync
|
||||
# --version, -v Show version number
|
||||
|
||||
# EXAMPLES:
|
||||
# 'pihole-cloudsync --push' will push (upload) your lists to a remote Git repo
|
||||
@@ -32,211 +36,210 @@ update='December 26, 2020'
|
||||
# Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
||||
###########################################################################
|
||||
# CONSTANTS
|
||||
if [ -f "/opt/my-pihole-sync/pihole-sync" ]; then
|
||||
personal_git_dir='/opt/my-pihole-sync'
|
||||
elif [ -f "/etc/pihole/my-pihole-sync/pihole-sync" ]; then
|
||||
personal_git_dir='/etc/pihole/my-pihole-sync'
|
||||
else
|
||||
personal_git_dir='/usr/local/bin/my-pihole-sync'
|
||||
fi
|
||||
|
||||
|
||||
# if [ -d "/opt/docker-pi-hole/etc-pihole" ]; then
|
||||
# pihole_dir='/opt/docker-pi-hole/etc-pihole'
|
||||
# gravity_db='/opt/docker-pi-hole/etc-pihole/gravity.db'
|
||||
# dnsmasq_dir='/opt/docker-pi-hole/etc-dnsmasq.d/'
|
||||
# else
|
||||
pihole_dir='/etc/pihole'
|
||||
gravity_db='/etc/pihole/gravity.db'
|
||||
dnsmasq_dir='/etc/dnsmasq.d/'
|
||||
# fi
|
||||
|
||||
personal_git_dir='/usr/local/bin/my-pihole-lists'
|
||||
pihole_dir='/etc/pihole'
|
||||
gravity_db='/etc/pihole/gravity.db'
|
||||
dnsmasq_dir='/etc/dnsmasq.d/'
|
||||
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'
|
||||
###########################################################################
|
||||
# SHOULDN'T NEED TO EDIT BELOW THIS LINE
|
||||
|
||||
# Force sudo if not running with root privileges
|
||||
SUDO=''
|
||||
if [ "$EUID" -ne 0 ]
|
||||
then SUDO='sudo'
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
SUDO='sudo'
|
||||
fi
|
||||
|
||||
# FUNCTIONS
|
||||
push_initialize () {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $pihole_dir || exit
|
||||
|
||||
# Verify Custom and CNAME lists exist
|
||||
touch $custom_list
|
||||
touch $dnsmasq_dir/$cname_list
|
||||
# touch $dnsmasq_dir/$localdomains_list
|
||||
|
||||
# Copy local Custom and CNAME lists to local Git repo
|
||||
cp $custom_list $personal_git_dir
|
||||
cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||
|
||||
# Go to local Git repo directory
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Export Ad and Domain lists from Gravity database
|
||||
sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
||||
sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
||||
|
||||
# Add all lists to local Git repo
|
||||
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.";
|
||||
}
|
||||
pull_initialize () {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Update local Git repo from remote Git repo
|
||||
git remote update > /dev/null
|
||||
|
||||
# Remove -q option if you don't want to run in "quiet" mode
|
||||
git fetch --all -q
|
||||
git reset --hard origin/main -q
|
||||
|
||||
# Stop DNS server
|
||||
$SUDO service pihole-FTL stop
|
||||
|
||||
# Overwrite local files
|
||||
cp $custom_list $pihole_dir
|
||||
cp $cname_list $dnsmasq_dir
|
||||
# if [[ $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) != *"10.10.10.11"* ]]; then
|
||||
# cp $localdomains_list $dnsmasq_dir
|
||||
# fi
|
||||
|
||||
# Overwrite local database tables
|
||||
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"
|
||||
|
||||
# Restart Pi-hole to pick up changes
|
||||
$SUDO pihole -g
|
||||
|
||||
# Display success messages
|
||||
echo "Local Pi-hole initialized in Pull mode and first pull successfully completed.";
|
||||
echo "Future pulls can now be perfomed with 'pihole-cloudsync --pull'.";
|
||||
}
|
||||
push () {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $pihole_dir || exit
|
||||
|
||||
# Copy local Custom and CNAME lists to local Git repo
|
||||
cp $custom_list $personal_git_dir
|
||||
cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||
# cp $dnsmasq_dir/$localdomains_list $personal_git_dir
|
||||
|
||||
# Go to local Git repo directory
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Export Ad and Domain lists from Gravity database
|
||||
sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
||||
sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
||||
|
||||
# Compare local files to remote Git repo
|
||||
git remote update > /dev/null
|
||||
|
||||
# If local files are different than remote, update remote Git repo
|
||||
CHANGED=$(git --work-tree=$personal_git_dir status --porcelain)
|
||||
if [ -n "${CHANGED}" ]; then
|
||||
echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...';
|
||||
rightnow=$(date +"%B %e, %Y %l:%M%p")
|
||||
# Remove -q option if you don't want to run in "quiet" mode
|
||||
git commit -a -m "Updated $rightnow" -q
|
||||
git push -q
|
||||
echo 'Done!';
|
||||
exit 0
|
||||
else
|
||||
# If local files are the same as remote, do nothing and exit
|
||||
echo 'Remote Git repo matches local Pi-hole lists. No further action required.';
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
pull () {
|
||||
push_initialize() {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $personal_git_dir || exit
|
||||
cd $pihole_dir || exit
|
||||
|
||||
# Update local Git repo from remote Git repo
|
||||
git remote update > /dev/null
|
||||
CHANGED=$(git log HEAD..origin/main --oneline)
|
||||
if [ -n "${CHANGED}" ]; then
|
||||
echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...';
|
||||
git fetch --all -q
|
||||
git reset --hard origin/main -q
|
||||
$SUDO service pihole-FTL stop
|
||||
cp $custom_list $pihole_dir
|
||||
cp $cname_list $dnsmasq_dir
|
||||
# if [[ $(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1) != *"10.10.10.11"* ]]; then
|
||||
# cp $localdomains_list $dnsmasq_dir
|
||||
# fi
|
||||
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!';
|
||||
exit 0
|
||||
# Verify Custom and CNAME lists exist
|
||||
$SUDO touch $custom_list
|
||||
$SUDO touch $dnsmasq_dir/$cname_list
|
||||
|
||||
# Copy local Custom and CNAME lists to local Git repo
|
||||
$SUDO cp $custom_list $personal_git_dir
|
||||
$SUDO cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||
|
||||
# Go to local Git repo directory
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Export Ad and Domain lists from Gravity database
|
||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
||||
|
||||
# Add all lists to local Git repo
|
||||
$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."
|
||||
}
|
||||
pull_initialize() {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Update local Git repo from remote Git repo
|
||||
$SUDO git remote update >/dev/null
|
||||
|
||||
# Remove -q option if you don't want to run in "quiet" mode
|
||||
$SUDO git fetch --all -q
|
||||
$SUDO git reset --hard origin/master -q
|
||||
|
||||
# Stop DNS server
|
||||
$SUDO service pihole-FTL stop
|
||||
|
||||
# Overwrite local files
|
||||
$SUDO cp $custom_list $pihole_dir
|
||||
$SUDO cp $cname_list $dnsmasq_dir
|
||||
|
||||
# Overwrite local database tables
|
||||
$SUDO sqlite3 $gravity_db "DELETE FROM adlist;"
|
||||
$SUDO sqlite3 $gravity_db -header -csv ".import --skip 1 adlist.csv adlist"
|
||||
|
||||
if [ -s domainlist.csv ]; then
|
||||
$SUDO sqlite3 $gravity_db "DELETE FROM domainlist;"
|
||||
$SUDO sqlite3 $gravity_db -header -csv ".import --skip 1 domainlist.csv domainlist"
|
||||
fi
|
||||
|
||||
# Restart Pi-hole to pick up changes
|
||||
$SUDO pihole -g
|
||||
|
||||
# Display success messages
|
||||
echo "Local Pi-hole initialized in Pull mode and first pull successfully completed."
|
||||
echo "Future pulls can now be perfomed with 'pihole-cloudsync --pull'."
|
||||
}
|
||||
push() {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $pihole_dir || exit
|
||||
|
||||
# Copy local Custom and CNAME lists to local Git repo
|
||||
$SUDO cp $custom_list $personal_git_dir
|
||||
$SUDO cp $dnsmasq_dir/$cname_list $personal_git_dir
|
||||
|
||||
# Go to local Git repo directory
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Export Ad and Domain lists from Gravity database
|
||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM adlist" >$ad_list
|
||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
||||
|
||||
# Compare local files to remote Git repo
|
||||
$SUDO git remote update >/dev/null
|
||||
|
||||
# If local files are different than remote, update remote Git repo
|
||||
CHANGED=$($SUDO git --work-tree=$personal_git_dir status --porcelain)
|
||||
if [ -n "${CHANGED}" ]; then
|
||||
echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...'
|
||||
rightnow=$(date +"%B %e, %Y %l:%M%p")
|
||||
# Remove -q option if you don't want to run in "quiet" mode
|
||||
$SUDO git commit -a -m "Updated $rightnow" -q
|
||||
$SUDO git push -q
|
||||
echo 'Done!'
|
||||
exit 0
|
||||
else
|
||||
echo 'Local Pi-hole lists match remote Git repo. No further action required.';
|
||||
exit 0
|
||||
# If local files are the same as remote, do nothing and exit
|
||||
echo 'Remote Git repo matches local Pi-hole lists. No further action required.'
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
pull() {
|
||||
# Go to Pi-hole directory, exit if doesn't exist
|
||||
cd $personal_git_dir || exit
|
||||
|
||||
# Update local Git repo from remote Git repo
|
||||
$SUDO git remote update >/dev/null
|
||||
CHANGED=$($SUDO git log HEAD..origin/master --oneline)
|
||||
if [ -n "${CHANGED}" ]; then
|
||||
echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...'
|
||||
# Remove -q option if you don't want to run in "quiet" mode
|
||||
$SUDO git fetch --all -q
|
||||
$SUDO git reset --hard origin/master -q
|
||||
$SUDO service pihole-FTL stop
|
||||
$SUDO cp $custom_list $pihole_dir
|
||||
$SUDO cp $cname_list $dnsmasq_dir
|
||||
$SUDO sqlite3 $gravity_db "DELETE FROM adlist;"
|
||||
$SUDO sqlite3 $gravity_db -header -csv ".import --skip 1 adlist.csv adlist"
|
||||
|
||||
if [ -s domainlist.csv ]; then
|
||||
$SUDO sqlite3 $gravity_db "DELETE FROM domainlist;"
|
||||
$SUDO sqlite3 $gravity_db -header -csv ".import --skip 1 domainlist.csv domainlist"
|
||||
fi
|
||||
|
||||
$SUDO pihole -g
|
||||
echo 'Done!'
|
||||
exit 0
|
||||
else
|
||||
echo 'Local Pi-hole lists match remote Git repo. No further action required.'
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
setup_cron_push() {
|
||||
echo "Setting cron job for push"
|
||||
(
|
||||
crontab -l
|
||||
echo "00 01,07,13,19 * * * sudo /usr/local/bin/pihole-cloudsync/pihole-cloudsync --push > /dev/null 2>&1"
|
||||
) | crontab
|
||||
exit 0
|
||||
}
|
||||
setup_cron_pull() {
|
||||
echo "Setting cron job for pull"
|
||||
(
|
||||
crontab -l
|
||||
echo "05 01,07,13,19 * * * sudo /usr/local/bin/pihole-cloudsync/pihole-cloudsync --pull > /dev/null 2>&1"
|
||||
) | crontab
|
||||
exit 0
|
||||
}
|
||||
###########################################################################
|
||||
# Check to see whether a command line option was provided
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
if [ -z "$1" ]; then
|
||||
echo "Missing command line option. Try --push, --pull, or --help."
|
||||
exit 1
|
||||
fi
|
||||
# Determine which action to perform (InitPush, InitPull, Push, Pull, or Help)
|
||||
for arg in "$@"
|
||||
do
|
||||
for arg in "$@"; do
|
||||
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
||||
if [ "$arg" == "--initpush" ]
|
||||
then
|
||||
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
|
||||
push_initialize
|
||||
exit 0
|
||||
if [ "$arg" == "--initpush" ]; then
|
||||
echo "$arg option detected. Initializing local Git repo for Push/Upload."
|
||||
push_initialize
|
||||
exit 0
|
||||
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
||||
elif [ "$arg" == "--initpull" ]
|
||||
then
|
||||
echo "$arg option detected. Initializing local Git repo for Pull/Download.";
|
||||
pull_initialize
|
||||
exit 0
|
||||
elif [ "$arg" == "--initpull" ]; then
|
||||
echo "$arg option detected. Initializing local Git repo for Pull/Download."
|
||||
pull_initialize
|
||||
exit 0
|
||||
# Push / Upload - Pushes updated local Pi-hole lists to remote Git repo
|
||||
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]
|
||||
then
|
||||
echo "$arg option detected. Running in Push/Upload mode."
|
||||
push
|
||||
exit 0
|
||||
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]; then
|
||||
echo "$arg option detected. Running in Push/Upload mode."
|
||||
push
|
||||
exit 0
|
||||
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
|
||||
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]
|
||||
then
|
||||
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ] || [ "$arg" == "-d" ]; then
|
||||
echo "$arg option detected. Running in Pull/Download mode."
|
||||
pull
|
||||
pull
|
||||
exit 0
|
||||
elif [ "$arg" == "--setupcronpush" ]; then
|
||||
echo "$arg option detected."
|
||||
setup_cron_push
|
||||
exit 0
|
||||
elif [ "$arg" == "--setupcronpull" ]; then
|
||||
echo "$arg option detected."
|
||||
setup_cron_pull
|
||||
exit 0
|
||||
# Help - Displays help dialog
|
||||
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]
|
||||
then
|
||||
cat << EOF
|
||||
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]; then
|
||||
cat <<EOF
|
||||
Usage: pihole-cloudsync <option>
|
||||
|
||||
Options:
|
||||
--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
|
||||
--initpush Initialize Primary Pi-hole in "Push" mode
|
||||
--initpull Initialize Secondary Pi-hole in "Pull" mode
|
||||
--help, -h, -? Show this help dialog
|
||||
--version, -v Show the current version of pihole-cloudsync
|
||||
--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
|
||||
--initpush Initialize Primary Pi-hole in "Push" mode
|
||||
--initpull Initialize Secondary Pi-hole in "Pull" mode
|
||||
--help, -h, -? Show this help dialog
|
||||
--version, -v Show the current version of pihole-cloudsync
|
||||
|
||||
Examples:
|
||||
'pihole-cloudsync --push' will push (upload) your lists to a Git repo
|
||||
@@ -246,14 +249,13 @@ Project Home: https://github.com/stevejenkins/pihole-cloudsync
|
||||
EOF
|
||||
|
||||
# Version - Displays version number
|
||||
elif [ "$arg" == "--version" ] || [ "$arg" == "-v" ]
|
||||
then
|
||||
echo 'pihole-cloudsync v'$version' - Updated '"$update";
|
||||
echo 'https://github.com/stevejenkins/pihole-cloudsync';
|
||||
elif [ "$arg" == "--version" ] || [ "$arg" == "-v" ]; then
|
||||
echo 'pihole-cloudsync v'$version' - Updated '"$update"
|
||||
echo 'https://github.com/stevejenkins/pihole-cloudsync'
|
||||
|
||||
# Invalid command line option was passed
|
||||
else
|
||||
echo "Invalid command line option. Try --push, --pull, or --help."
|
||||
exit 1
|
||||
echo "Invalid command line option. Try --push, --pull, or --help."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
done
|
||||
Reference in New Issue
Block a user