pihole-cloudsync/pihole-cloudsync.sh

152 lines
5.1 KiB
Bash
Raw Normal View History

2019-07-09 03:49:01 +00:00
#!/bin/bash
###########################################################################
2019-07-10 03:57:13 +00:00
# pihole-cloudsync
# Helper script to keep multiple Pi-holes' lists synchronized via Git
2019-07-09 03:49:01 +00:00
2019-07-10 14:09:29 +00:00
# Version 1.3 - July 10, 2019 - Steve Jenkins (stevejenkins.com)
2019-07-09 03:49:01 +00:00
2019-07-10 03:57:13 +00:00
# SETUP
# Follow the instructions in the README to set up your own private Git
# repository BEFORE running this script for the first time. This script
# will not work without a properly configured Git repo and credentials.
2019-07-10 02:01:53 +00:00
2019-07-10 03:57:13 +00:00
# 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
# --initialize, --init, -i Add local Pi-hole lists to local Git repo before first push
# --help, -h, -? Show this help dialog
# EXAMPLES:
# 'pihole-cloudsync --push' will push (upload) your lists to 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
2019-07-09 03:49:01 +00:00
###########################################################################
2019-07-10 03:57:13 +00:00
# CONSTANTS
2019-07-10 05:22:06 +00:00
personal_git_dir='/usr/local/bin/my-pihole-lists'
2019-07-09 03:49:01 +00:00
pihole_dir='/etc/pihole'
ad_list='adlists.list'
black_list='black.list'
blacklist_list='blacklist.txt'
whitelist_list='whitelist.txt'
regex_list='regex.list'
2019-07-10 03:57:13 +00:00
# Force sudo if not running with root privileges
SUDO=''
if [ "$EUID" -ne 0 ]
then SUDO='sudo'
2019-07-09 03:49:01 +00:00
fi
2019-07-10 03:57:13 +00:00
# FUNCTIONS
initialize () {
2019-07-10 02:01:53 +00:00
cd $pihole_dir || exit
2019-07-10 03:57:13 +00:00
$SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
2019-07-10 02:01:53 +00:00
cd $personal_git_dir || exit
2019-07-10 03:57:13 +00:00
$SUDO git add .
echo "Local Pi-hole lists added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo.";
}
2019-07-10 02:01:53 +00:00
2019-07-10 03:57:13 +00:00
push () {
2019-07-09 03:49:01 +00:00
cd $pihole_dir || exit
2019-07-10 03:57:13 +00:00
$SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
2019-07-09 03:49:01 +00:00
cd $personal_git_dir || exit
2019-07-09 04:28:26 +00:00
2019-07-10 14:09:29 +00:00
CHANGED=$($SUDO git --work-tree=$personal_git_dir status --porcelain > /dev/null)
2019-07-09 03:49:01 +00:00
if [ -n "${CHANGED}" ]; then
2019-07-10 03:57:13 +00:00
echo 'Local Pi-hole lists are different than remote Git repo. Updating remote repo...';
2019-07-10 01:01:03 +00:00
rightnow=$(date +"%B %e, %Y %l:%M%p")
2019-07-10 02:01:53 +00:00
# Remove -q option if you don't want to run in "quiet" mode
2019-07-10 03:57:13 +00:00
$SUDO git commit -a -m "Updated $rightnow" -q
$SUDO git push -q
2019-07-10 02:01:53 +00:00
echo 'Done!';
2019-07-10 01:01:03 +00:00
exit 0
2019-07-09 03:49:01 +00:00
else
2019-07-10 03:57:13 +00:00
echo 'Remote Git repo matches local Pi-hole lists. No further action required.';
2019-07-09 04:28:26 +00:00
exit 0
2019-07-09 03:49:01 +00:00
fi
2019-07-10 03:57:13 +00:00
}
2019-07-09 03:49:01 +00:00
2019-07-10 03:57:13 +00:00
pull () {
2019-07-09 04:28:26 +00:00
cd $personal_git_dir || exit
2019-07-10 14:09:29 +00:00
CHANGED=$($SUDO git remote update && git --work-tree=$personal_git_dir status --porcelain > /dev/null)
2019-07-10 02:01:53 +00:00
if [ -n "${CHANGED}" ]; then
2019-07-10 03:57:13 +00:00
echo 'Remote Git repo is different than local Pi-hole lists. Updating local lists...';
2019-07-10 02:01:53 +00:00
# Remove -q option if you don't want to run in "quiet" mode
2019-07-10 03:57:13 +00:00
$SUDO git fetch --all -q
$SUDO git reset --hard origin/master -q
$SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir
$SUDO pihole -g
2019-07-10 02:01:53 +00:00
echo 'Done!';
exit 0
2019-07-09 04:28:26 +00:00
else
2019-07-10 03:57:13 +00:00
echo 'Local Pi-hole lists match remote Git repo. No further action required.';
2019-07-09 04:28:26 +00:00
exit 0
fi
2019-07-10 03:57:13 +00:00
}
2019-07-09 03:49:01 +00:00
2019-07-10 03:57:13 +00:00
#######################################################
# Check to see whether command line option was provided
if [ -z "$1" ]
then
echo "Missing command line option. Try --push, --pull, or --help."
exit 1
fi
# Determine which action to perform (Push, Pull, or Help)
for arg in "$@"
do
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
if [ "$arg" == "--initialize" ] || [ "$arg" == "--init" ] || [ "$arg" == "-i" ]
then
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
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
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]
then
echo "$arg option detected. Running in Pull/Download mode."
pull
exit 0
# Help - Displays help dialog
2019-07-10 02:01:53 +00:00
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]
2019-07-09 03:49:01 +00:00
then
2019-07-09 04:28:26 +00:00
cat << EOF
Usage: pihole-cloudsync <option>
Options:
2019-07-10 03:57:13 +00:00
--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
--initialize, --init, -i Add local Pi-hole lists to local Git repo before first push
2019-07-10 02:01:53 +00:00
--help, -h, -? Show this help dialog
2019-07-09 04:28:26 +00:00
Examples:
2019-07-10 03:57:13 +00:00
'pihole-cloudsync --push' will push (upload) your lists to a Git repo
'pihole-cloudsync --pull' will pull (download) your lists from a Git repo
2019-07-09 04:28:26 +00:00
Project Home: https://github.com/stevejenkins/pihole-cloudsync
EOF
2019-07-09 03:49:01 +00:00
2019-07-10 02:01:53 +00:00
# Invalid commang line option was passed
2019-07-09 03:49:01 +00:00
else
2019-07-10 02:01:53 +00:00
echo "Invalid command line option. Try --push, --pull, or --help."
2019-07-10 03:57:13 +00:00
exit 1
2019-07-09 03:49:01 +00:00
fi
done