Initial release

This commit is contained in:
Steve Jenkins 2019-07-09 20:57:13 -07:00
parent 71483270a8
commit 719021d8f5

View File

@ -2,23 +2,34 @@
###########################################################################
# pihole-cloudsync-pull
# Helper script to keep multiple Pi-hole lists synchronized via Git
# pihole-cloudsync
# Helper script to keep multiple Pi-holes' lists synchronized via Git
# Version 1.0 - July 8, 2019 - Steve Jenkins (stevejenkins.com)
# Version 1.1 - July 9, 2019 - Steve Jenkins (stevejenkins.com)
# REQUIREMENTS
# 1. Create a git repo (on Github, GitLab, etc.) with only a master branch.
# 2. In
# 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.
# USAGE
#
# Run ./pihole-cloudsync from the command line
# 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
###########################################################################
# Constants
personal_git_dir='/usr/local/bin/steve-test-1'
# CONSTANTS
personal_git_dir='/usr/local/bin/steve-pihole-lists'
pihole_dir='/etc/pihole'
ad_list='adlists.list'
black_list='black.list'
@ -26,8 +37,60 @@ blacklist_list='blacklist.txt'
whitelist_list='whitelist.txt'
regex_list='regex.list'
# Only used for debugging
# set -x
# Force sudo if not running with root privileges
SUDO=''
if [ "$EUID" -ne 0 ]
then SUDO='sudo'
fi
# FUNCTIONS
initialize () {
cd $pihole_dir || exit
$SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
cd $personal_git_dir || exit
$SUDO git add .
echo "Local Pi-hole lists added to local Git repo. Run 'pihole-cloudsync --push' to push to remote Git repo.";
}
push () {
cd $pihole_dir || exit
$SUDO cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
cd $personal_git_dir || exit
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 'Remote Git repo matches local Pi-hole lists. No further action required.';
exit 0
fi
}
pull () {
cd $personal_git_dir || exit
CHANGED=$($SUDO git --work-tree=$personal_git_dir status --porcelain)
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 cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir
$SUDO pihole -g
echo 'Done!';
exit 0
else
echo 'Local Pi-hole lists match remote Git repo. No further action required.';
exit 0
fi
}
#######################################################
# Check to see whether command line option was provided
if [ -z "$1" ]
@ -40,73 +103,42 @@ fi
for arg in "$@"
do
# Initialize - adds local Pi-hole 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" == "--initialize" ] || [ "$arg" == "--init" ] || [ "$arg" == "-i" ]
then
echo "$arg option detected. Initializing repo for Push/Upload.";
cd $pihole_dir || exit
cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
cd $personal_git_dir || exit
git add .
echo "Local Pi-hole lists added to local repo. Now try 'sudo ./pihole-cloudsync --push'";
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
initialize
exit 0
# Push / Upload
# 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."
cd $pihole_dir || exit
cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $personal_git_dir
cd $personal_git_dir || exit
echo "$arg option detected. Running in Push/Upload mode."
push
exit 0
CHANGED=$(git --work-tree=$personal_git_dir status --porcelain)
if [ -n "${CHANGED}" ]; then
echo 'Local Pi-hole lists are different than remote repo. Updating 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
echo 'Remote repo matches local Pi-hole lists. No further action required.';
exit 0
fi
# Pull / Download
# 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."
cd $personal_git_dir || exit
CHANGED=$(git --work-tree=$personal_git_dir status --porcelain)
if [ -n "${CHANGED}" ]; then
echo 'Remote repo is different than local Pi-hole lists. Updating local lists...';
# Remove -q option if you don't want to run in "quiet" mode
git fetch --all -q
git reset --hard origin/master -q
cp $ad_list $black_list $blacklist_list $whitelist_list $regex_list $pihole_dir
pihole -g
echo 'Done!';
exit 0
else
echo 'Local Pi-hole lists match remote repo. No further action required.';
exit 0
fi
pull
exit 0
# Help
# Help - Displays help dialog
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 Git repo
--pull, --download, --down, -d Pull (download) your lists from a 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
--initialize, --init, -i Add local Pi-hole lists to local Git repo before first push
--help, -h, -? Show this help dialog
Examples:
'pihole --push' will push (upload) your lists to a Git repo
'pihole --pull' will pull (download) your lists from 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
Project Home: https://github.com/stevejenkins/pihole-cloudsync
EOF
@ -114,7 +146,6 @@ EOF
# Invalid commang line option was passed
else
echo "Invalid command line option. Try --push, --pull, or --help."
exit 1
fi
done