mirror of
https://github.com/stevejenkins/pihole-cloudsync.git
synced 2024-08-30 18:22:11 +00:00
Normalize indentation and style
This commit is contained in:
parent
e039298355
commit
0f8a428365
191
pihole-cloudsync
191
pihole-cloudsync
@ -18,12 +18,12 @@ update='December 26, 2020'
|
|||||||
# USAGE: pihole-cloudsync <option>
|
# USAGE: pihole-cloudsync <option>
|
||||||
|
|
||||||
# OPTIONS:
|
# OPTIONS:
|
||||||
# --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 Initialize Secondary Pi-hole in "Pull" mode
|
||||||
# --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 Pull (download) your lists from a remote Git repo
|
||||||
# --help, -h, -? Show the current version of pihole-cloudsync
|
# --help, -h, -? Show the current version of pihole-cloudsync
|
||||||
# --version, -v Show version number
|
# --version, -v Show version number
|
||||||
|
|
||||||
# EXAMPLES:
|
# EXAMPLES:
|
||||||
# 'pihole-cloudsync --push' will push (upload) your lists to a remote Git repo
|
# 'pihole-cloudsync --push' will push (upload) your lists to a remote Git repo
|
||||||
@ -45,21 +45,18 @@ cname_list='05-pihole-custom-cname.conf'
|
|||||||
|
|
||||||
# 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
|
# Attempt to detect pihole running in Docker
|
||||||
DOCKER=''
|
DOCKER=''
|
||||||
DOCKER_CMD="$(command -v docker)"
|
DOCKER_CMD="$(command -v docker)"
|
||||||
JQ_CMD="$(command -v jq)"
|
JQ_CMD="$(command -v jq)"
|
||||||
if [ -n "${DOCKER_CMD}" ]
|
if [ -n "${DOCKER_CMD}" ]; then
|
||||||
then
|
|
||||||
CONTAINER="$(${DOCKER_CMD} ps -f "ancestor=pihole/pihole" --format "{{.Names}}")"
|
CONTAINER="$(${DOCKER_CMD} ps -f "ancestor=pihole/pihole" --format "{{.Names}}")"
|
||||||
if [ -n "${CONTAINER}" ]
|
if [ -n "${CONTAINER}" ]; then
|
||||||
then
|
if [ -n "${JQ_CMD}" ]; then
|
||||||
if [ -n "${JQ_CMD}" ]
|
|
||||||
then
|
|
||||||
echo "Found pihole running under Docker container '${CONTAINER}'"
|
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')"
|
pihole_dir="$(${DOCKER_CMD} inspect -f "{{json .Mounts}}" "${CONTAINER}" | ${JQ_CMD} -r --arg dir "${pihole_dir}" '.[] | select(.Destination==$dir) | .Source')"
|
||||||
@ -135,127 +132,119 @@ push () {
|
|||||||
# 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
|
||||||
|
|
||||||
# Copy local Custom and CNAME lists to local Git repo
|
# Copy local Custom and CNAME lists to local Git repo
|
||||||
$SUDO cp $custom_list $personal_git_dir
|
$SUDO cp $custom_list $personal_git_dir
|
||||||
$SUDO 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
|
cd $personal_git_dir || exit
|
||||||
|
|
||||||
# Export Ad and Domain lists from Gravity database
|
# 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 adlist" >$ad_list
|
||||||
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
$SUDO sqlite3 $gravity_db -header -csv "SELECT * FROM domainlist" >$domain_list
|
||||||
|
|
||||||
# Compare local files to remote Git repo
|
# Compare local files to remote Git repo
|
||||||
$SUDO 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=$($SUDO 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
|
||||||
$SUDO git commit -a -m "Updated $rightnow" -q
|
$SUDO git commit -a -m "Updated $rightnow" -q
|
||||||
$SUDO git push -q
|
$SUDO git push -q
|
||||||
echo 'Done!';
|
echo 'Done!';
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
# If local files are the same as remote, do nothing and exit
|
# 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.';
|
echo 'Remote Git repo matches local Pi-hole lists. No further action required.';
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
pull () {
|
pull () {
|
||||||
# Go to Pi-hole directory, exit if doesn't exist
|
# Go to Pi-hole directory, exit if doesn't exist
|
||||||
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
|
||||||
$SUDO git remote update > /dev/null
|
$SUDO git remote update > /dev/null
|
||||||
CHANGED=$($SUDO git log HEAD..origin/master --oneline)
|
CHANGED=$($SUDO git log HEAD..origin/master --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...';
|
||||||
# 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
|
||||||
$SUDO git fetch --all -q
|
$SUDO git fetch --all -q
|
||||||
$SUDO git reset --hard origin/master -q
|
$SUDO git reset --hard origin/master -q
|
||||||
$SUDO ${DOCKER} service pihole-FTL stop
|
$SUDO ${DOCKER} service pihole-FTL stop
|
||||||
$SUDO cp $custom_list $pihole_dir
|
$SUDO cp $custom_list $pihole_dir
|
||||||
$SUDO cp $cname_list $dnsmasq_dir
|
$SUDO cp $cname_list $dnsmasq_dir
|
||||||
$SUDO sqlite3 $gravity_db "DROP TABLE adlist;"
|
$SUDO sqlite3 $gravity_db "DROP TABLE adlist;"
|
||||||
$SUDO sqlite3 $gravity_db -header -csv ".import adlist.csv adlist"
|
$SUDO sqlite3 $gravity_db -header -csv ".import adlist.csv adlist"
|
||||||
$SUDO sqlite3 $gravity_db "DROP TABLE domainlist;"
|
$SUDO sqlite3 $gravity_db "DROP TABLE domainlist;"
|
||||||
$SUDO sqlite3 $gravity_db -header -csv ".import domainlist.csv domainlist"
|
$SUDO sqlite3 $gravity_db -header -csv ".import domainlist.csv domainlist"
|
||||||
$SUDO ${DOCKER} pihole -g
|
$SUDO ${DOCKER} pihole -g
|
||||||
echo 'Done!';
|
echo 'Done!';
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
echo 'Local Pi-hole lists match remote Git repo. No further action required.';
|
echo 'Local Pi-hole lists match remote Git repo. No further action required.';
|
||||||
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" ]; then
|
||||||
if [ "$arg" == "--initpush" ]
|
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
|
||||||
then
|
push_initialize
|
||||||
echo "$arg option detected. Initializing local Git repo for Push/Upload.";
|
exit 0
|
||||||
push_initialize
|
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
||||||
exit 0
|
elif [ "$arg" == "--initpull" ]; then
|
||||||
# Initialize - adds primary Pi-hole's lists to local Git repo before first push/upload
|
echo "$arg option detected. Initializing local Git repo for Pull/Download.";
|
||||||
elif [ "$arg" == "--initpull" ]
|
pull_initialize
|
||||||
then
|
exit 0
|
||||||
echo "$arg option detected. Initializing local Git repo for Pull/Download.";
|
# Push / Upload - Pushes updated local Pi-hole lists to remote Git repo
|
||||||
pull_initialize
|
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]; then
|
||||||
exit 0
|
echo "$arg option detected. Running in Push/Upload mode."
|
||||||
# Push / Upload - Pushes updated local Pi-hole lists to remote Git repo
|
push
|
||||||
elif [ "$arg" == "--push" ] || [ "$arg" == "--upload" ] || [ "$arg" == "--up" ] || [ "$arg" == "-u" ]
|
exit 0
|
||||||
then
|
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
|
||||||
echo "$arg option detected. Running in Push/Upload mode."
|
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]; then
|
||||||
push
|
echo "$arg option detected. Running in Pull/Download mode."
|
||||||
exit 0
|
pull
|
||||||
# Pull / Download - Pulls updated Pi-hole lists from remote Git repo
|
exit 0
|
||||||
elif [ "$arg" == "--pull" ] || [ "$arg" == "--download" ] || [ "$arg" == "--down" ]|| [ "$arg" == "-d" ]
|
# Help - Displays help dialog
|
||||||
then
|
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]; then
|
||||||
echo "$arg option detected. Running in Pull/Download mode."
|
cat <<- EOF
|
||||||
pull
|
Usage: pihole-cloudsync <option>
|
||||||
exit 0
|
|
||||||
# Help - Displays help dialog
|
|
||||||
elif [ "$arg" == "--help" ] || [ "$arg" == "-h" ] || [ "$arg" == "-?" ]
|
|
||||||
then
|
|
||||||
cat << EOF
|
|
||||||
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 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 Initialize Secondary Pi-hole in "Pull" mode
|
||||||
--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
|
||||||
|
|
||||||
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';
|
|
||||||
|
|
||||||
# Invalid command line option was passed
|
# Invalid command line option was passed
|
||||||
else
|
else
|
||||||
echo "Invalid command line option. Try --push, --pull, or --help."
|
echo "Invalid command line option. Try --push, --pull, or --help."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user