From d6c40328dd6070a2d31dffcd36036bb26f8252a8 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 09:43:26 -0500 Subject: [PATCH 01/16] Explain sudo requirement --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 493c922..0f7c292 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The designation of primary and secondary is purely at your discretion and depend Additionally, some things to consider: - Gravity Sync is regularly tested during development with Ubuntu and Raspberry Pi OS (previously, Raspbian). As Gravity Sync is just an (admittedly) long bash script, it will likely work on other Linux distributions that have the `bash` shell installed. But please file an Issue if you're unable to run it on another platform. +- Gravity Sync uses SUDO to elevate permissions for itself during execution. You will need to make sure that you have passwordless SUDO enabled for the accounts on both the primary and secondary Pi-hole that will be performing the work. Most of the pre-built images available for the Raspberry Pi already have this configured, but if you have your Pi-hole running in a virtual machine, you may need to adjust this manually. [This tutorial](https://linuxize.com/post/how-to-run-sudo-command-without-password/) may be helpful in this respect. - Gravity Sync has not been tested with Docker container deployments of Pi-hole, and is not expected to work there without major modifications. You will need Pi-hole setup with a "traditional" install directly in the base operating system. ## Installation From e1adcd11059fd38fe1ebf152a73c29ad208e79c0 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 10:58:06 -0500 Subject: [PATCH 02/16] Compare custom.list --- gravity-sync.sh | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 4f6de42..151f6ea 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -30,6 +30,7 @@ VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite # Pi-hole Folder/File Locations PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory GRAVITY_FI='gravity.db' # default Pi-hole database file +CUSTOM_DNS='custom.list' # default Pi-hole local DNS lookups PIHOLE_BIN='/usr/local/bin/pihole' # default Pi-hole binary directory # OS Settings @@ -456,24 +457,62 @@ function md5_compare { MESSAGE="Comparing ${GRAVITY_FI} Changes" echo_info + HASHMARK='0' + MESSAGE="Analyzing Remote ${GRAVITY_FI}" echo_stat - primaryMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}") + primaryDBMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}") error_validate MESSAGE="Analyzing Local ${GRAVITY_FI}" echo_stat - secondMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI}) + secondDBMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI}) error_validate - if [ "$primaryMD5" == "$secondMD5" ] + if [ "$primaryDBMD5" == "$secondDBMD5" ] then MESSAGE="No Differences in ${GRAVITY_FI}" echo_info - exit_nochange + HASHMARK=$((HASHMARK+0)) else MESSAGE="Changes Detected in ${GRAVITY_FI}" echo_info + HASHMARK=$((HASHMARK+1)) + fi + + MESSAGE="Comparing ${CUSTOM_DNS} Changes" + echo_info + + MESSAGE="Analyzing Remote ${CUSTOM_DNS}" + echo_stat + primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") + error_validate + + MESSAGE="Analyzing Local ${CUSTOM_DNS}" + echo_stat + secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) + error_validate + + if [ "$primaryCLMD5" == "$secondCLMD5" ] + then + MESSAGE="No Differences in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+0)) + else + MESSAGE="Changes Detected in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+1)) + fi + + if [ "$HASHMARK" != "0" ] + then + MESSAGE="Replication Required" + echo_info + HASHMARK=$((HASHMARK+0)) + else + MESSAGE="No Replication Required" + echo_info + exit_nochange fi } From 3e0d06617ec9f802522517389e2b6e7ff6c5e757 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 11:03:20 -0500 Subject: [PATCH 03/16] Verify existance of custom.list --- gravity-sync.sh | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 151f6ea..812a87b 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -480,28 +480,34 @@ function md5_compare { HASHMARK=$((HASHMARK+1)) fi - MESSAGE="Comparing ${CUSTOM_DNS} Changes" - echo_info - - MESSAGE="Analyzing Remote ${CUSTOM_DNS}" - echo_stat - primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") - error_validate - - MESSAGE="Analyzing Local ${CUSTOM_DNS}" - echo_stat - secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) - error_validate - - if [ "$primaryCLMD5" == "$secondCLMD5" ] + if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ] then - MESSAGE="No Differences in ${CUSTOM_DNS}" + MESSAGE="Comparing ${CUSTOM_DNS} Changes" echo_info - HASHMARK=$((HASHMARK+0)) + + MESSAGE="Analyzing Remote ${CUSTOM_DNS}" + echo_stat + primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") + error_validate + + MESSAGE="Analyzing Local ${CUSTOM_DNS}" + echo_stat + secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) + error_validate + + if [ "$primaryCLMD5" == "$secondCLMD5" ] + then + MESSAGE="No Differences in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+0)) + else + MESSAGE="Changes Detected in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+1)) + fi else - MESSAGE="Changes Detected in ${CUSTOM_DNS}" + MESSAGE="No Local ${CUSTOM_DNS} Detected" echo_info - HASHMARK=$((HASHMARK+1)) fi if [ "$HASHMARK" != "0" ] From ca4e99cb1e1cebd272969966ea9572adf8e30a04 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 11:31:38 -0500 Subject: [PATCH 04/16] Check existance of remote custom.list --- gravity-sync.sh | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 812a87b..b51150f 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -485,25 +485,32 @@ function md5_compare { MESSAGE="Comparing ${CUSTOM_DNS} Changes" echo_info - MESSAGE="Analyzing Remote ${CUSTOM_DNS}" - echo_stat - primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") - error_validate - - MESSAGE="Analyzing Local ${CUSTOM_DNS}" - echo_stat - secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) - error_validate - - if [ "$primaryCLMD5" == "$secondCLMD5" ] + if ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS} then - MESSAGE="No Differences in ${CUSTOM_DNS}" - echo_info - HASHMARK=$((HASHMARK+0)) + MESSAGE="Analyzing Remote ${CUSTOM_DNS}" + echo_stat + + primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") + error_validate + + MESSAGE="Analyzing Local ${CUSTOM_DNS}" + echo_stat + secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) + error_validate + + if [ "$primaryCLMD5" == "$secondCLMD5" ] + then + MESSAGE="No Differences in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+0)) + else + MESSAGE="Changes Detected in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+1)) + fi else - MESSAGE="Changes Detected in ${CUSTOM_DNS}" + MESSAGE="No Remote ${CUSTOM_DNS} Detected" echo_info - HASHMARK=$((HASHMARK+1)) fi else MESSAGE="No Local ${CUSTOM_DNS} Detected" @@ -512,7 +519,7 @@ function md5_compare { if [ "$HASHMARK" != "0" ] then - MESSAGE="Replication Required" + MESSAGE="Replication Suggested" echo_info HASHMARK=$((HASHMARK+0)) else From 89e0edb456de1b2487b0e750733dd37e420c78f6 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 12:18:40 -0500 Subject: [PATCH 05/16] add skip custom variable --- gravity-sync.conf.example | 4 +++- gravity-sync.sh | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/gravity-sync.conf.example b/gravity-sync.conf.example index a56cdc6..176d977 100644 --- a/gravity-sync.conf.example +++ b/gravity-sync.conf.example @@ -26,4 +26,6 @@ REMOTE_PASS='' # LOG_PATH='' # SYNCING_LOG='' # CRONJOB_LOG='' -# VERIFY_PASS='' # 0=Verify 1=Bypass \ No newline at end of file + +# VERIFY_PASS='' # 0=Verify 1=Bypass +# SKIP_CUSTOM='' # 0=Syncit 1=Skip \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index b51150f..260f4fc 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -26,6 +26,7 @@ CRONJOB_LOG='gravity-sync.cron' # replace in gravity-sync.conf to overwrite # Interaction Customization VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite +SKIP_CUSTOM='0' # Pi-hole Folder/File Locations PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory From 5bf02b9d25af0919a948bcdd8a195e514b14bbcd Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 13:37:35 -0500 Subject: [PATCH 06/16] Merge --- gravity-sync.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/gravity-sync.sh b/gravity-sync.sh index b51150f..13cab26 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -174,6 +174,63 @@ function pull_gs { error_validate fi + if [ $REMOTE_CUSTOM_DNS == "1" ] + then + MESSAGE="Backing Up ${CUSTOM_DNS} on $HOSTNAME" + echo_stat + cp ${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup >/dev/null 2>&1 + error_validate + + MESSAGE="Pulling ${CUSTOM_DNS} from ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull >/dev/null 2>&1 + error_validate + + MESSAGE="Replacing ${CUSTOM_DNS} on $HOSTNAME" + echo_stat + sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + + MESSAGE="Validating Ownership on ${CUSTOM_DNS}" + echo_stat + + CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}') + if [ $CUSTOMLS_OWN == "rootroot" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + + MESSAGE="Validating Permissions on ${CUSTOM_DNS}" + echo_stat + + GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') + if [ $CUSTOMLS_RWE = "-rw-r--r--" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + fi + + MESSAGE="Inverting Tachyon Pulse" echo_info sleep 1 @@ -487,6 +544,7 @@ function md5_compare { if ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS} then + REMOTE_CUSTOM_DNS="1" MESSAGE="Analyzing Remote ${CUSTOM_DNS}" echo_stat From 810e28aa0ef3b6990903279dccff8abb94a652fd Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 13:46:23 -0500 Subject: [PATCH 07/16] Add custom.list bypass to pull function --- gravity-sync.conf.example | 2 +- gravity-sync.sh | 169 ++++++++++++++++++++------------------ 2 files changed, 88 insertions(+), 83 deletions(-) diff --git a/gravity-sync.conf.example b/gravity-sync.conf.example index 176d977..947fc49 100644 --- a/gravity-sync.conf.example +++ b/gravity-sync.conf.example @@ -27,5 +27,5 @@ REMOTE_PASS='' # SYNCING_LOG='' # CRONJOB_LOG='' -# VERIFY_PASS='' # 0=Verify 1=Bypass +# VERIFY_PASS='' # 0=Verify 1=Bypass # SKIP_CUSTOM='' # 0=Syncit 1=Skip \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index 3f71dc0..ad8e388 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -174,63 +174,65 @@ function pull_gs { sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate fi - - if [ $REMOTE_CUSTOM_DNS == "1" ] - then - MESSAGE="Backing Up ${CUSTOM_DNS} on $HOSTNAME" - echo_stat - cp ${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup >/dev/null 2>&1 - error_validate - - MESSAGE="Pulling ${CUSTOM_DNS} from ${REMOTE_HOST}" - echo_stat - ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull >/dev/null 2>&1 - error_validate - - MESSAGE="Replacing ${CUSTOM_DNS} on $HOSTNAME" - echo_stat - sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 - error_validate - - MESSAGE="Validating Ownership on ${CUSTOM_DNS}" - echo_stat - - CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}') - if [ $CUSTOMLS_OWN == "rootroot" ] - then - echo_good - else - echo_fail - - MESSAGE="Attempting to Compensate" - echo_info - - MESSAGE="Setting Ownership on ${CUSTOM_DNS}" - echo_stat - sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 - error_validate - fi - - MESSAGE="Validating Permissions on ${CUSTOM_DNS}" - echo_stat - - GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') - if [ $CUSTOMLS_RWE = "-rw-r--r--" ] - then - echo_good - else - echo_fail - - MESSAGE="Attempting to Compensate" - echo_info - - MESSAGE="Setting Ownership on ${CUSTOM_DNS}" - echo_stat - sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 - error_validate - fi - fi + if [ "$SKIP_CUSTOM" != '1' ] + then + if [ $REMOTE_CUSTOM_DNS == "1" ] + then + MESSAGE="Backing Up ${CUSTOM_DNS} on $HOSTNAME" + echo_stat + cp ${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup >/dev/null 2>&1 + error_validate + + MESSAGE="Pulling ${CUSTOM_DNS} from ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull >/dev/null 2>&1 + error_validate + + MESSAGE="Replacing ${CUSTOM_DNS} on $HOSTNAME" + echo_stat + sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + + MESSAGE="Validating Ownership on ${CUSTOM_DNS}" + echo_stat + + CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}') + if [ $CUSTOMLS_OWN == "rootroot" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + + MESSAGE="Validating Permissions on ${CUSTOM_DNS}" + echo_stat + + GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') + if [ $CUSTOMLS_RWE = "-rw-r--r--" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + fi + fi MESSAGE="Inverting Tachyon Pulse" echo_info @@ -538,42 +540,45 @@ function md5_compare { HASHMARK=$((HASHMARK+1)) fi - if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ] + if [ "$SKIP_CUSTOM" != '1' ] then - MESSAGE="Comparing ${CUSTOM_DNS} Changes" - echo_info - - if ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS} + if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ] then - REMOTE_CUSTOM_DNS="1" - MESSAGE="Analyzing Remote ${CUSTOM_DNS}" - echo_stat - - primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") - error_validate + MESSAGE="Comparing ${CUSTOM_DNS} Changes" + echo_info - MESSAGE="Analyzing Local ${CUSTOM_DNS}" - echo_stat - secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) - error_validate - - if [ "$primaryCLMD5" == "$secondCLMD5" ] + if ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS} then - MESSAGE="No Differences in ${CUSTOM_DNS}" - echo_info - HASHMARK=$((HASHMARK+0)) + REMOTE_CUSTOM_DNS="1" + MESSAGE="Analyzing Remote ${CUSTOM_DNS}" + echo_stat + + primaryCLMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}") + error_validate + + MESSAGE="Analyzing Local ${CUSTOM_DNS}" + echo_stat + secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS}) + error_validate + + if [ "$primaryCLMD5" == "$secondCLMD5" ] + then + MESSAGE="No Differences in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+0)) + else + MESSAGE="Changes Detected in ${CUSTOM_DNS}" + echo_info + HASHMARK=$((HASHMARK+1)) + fi else - MESSAGE="Changes Detected in ${CUSTOM_DNS}" + MESSAGE="No Remote ${CUSTOM_DNS} Detected" echo_info - HASHMARK=$((HASHMARK+1)) fi else - MESSAGE="No Remote ${CUSTOM_DNS} Detected" + MESSAGE="No Local ${CUSTOM_DNS} Detected" echo_info fi - else - MESSAGE="No Local ${CUSTOM_DNS} Detected" - echo_info fi if [ "$HASHMARK" != "0" ] From 569bce284e58b5ff412f680d853b75928287d751 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 13:51:12 -0500 Subject: [PATCH 08/16] Fix permission validation --- gravity-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index ad8e388..053f017 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -216,7 +216,7 @@ function pull_gs { MESSAGE="Validating Permissions on ${CUSTOM_DNS}" echo_stat - GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') + CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') if [ $CUSTOMLS_RWE = "-rw-r--r--" ] then echo_good From 3816e14173d7069873535ff55ef4ff0e720295f9 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 13:51:59 -0500 Subject: [PATCH 09/16] == --- gravity-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 053f017..b4d7d84 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -217,7 +217,7 @@ function pull_gs { echo_stat CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') - if [ $CUSTOMLS_RWE = "-rw-r--r--" ] + if [ $CUSTOMLS_RWE == "-rw-r--r--" ] then echo_good else From 45fd9ed58b629a1aa6c0f20e141467323b427364 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:03:18 -0500 Subject: [PATCH 10/16] Add custom.list to push --- gravity-sync.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gravity-sync.sh b/gravity-sync.sh index b4d7d84..8108b56 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -278,6 +278,31 @@ function push_gs { ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1 error_validate + if [ "$SKIP_CUSTOM" != '1' ] + then + if [ $REMOTE_CUSTOM_DNS == "1" ] + then + MESSAGE="Backing Up ${CUSTOM_DNS} from ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.push >/dev/null 2>&1 + error_validate + + MESSAGE="Pushing ${CUSTOM_DNS} to ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${CUSTOM_DNS} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + + MESSAGE="Setting Permissions on ${CUSTOM_DNS}" + echo_stat + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 + error_validate + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 + error_validate + fi + MESSAGE="Contacting Borg Collective" echo_info sleep 1 From c35642e5e73f199dd4a9ab1bacbe4148f4c4e392 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:08:56 -0500 Subject: [PATCH 11/16] Adding custom.list to restore function --- gravity-sync.sh | 57 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 8108b56..d2629d1 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -294,12 +294,12 @@ function push_gs { MESSAGE="Setting Permissions on ${CUSTOM_DNS}" echo_stat - ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 error_validate MESSAGE="Setting Ownership on ${CUSTOM_DNS}" echo_stat - ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 error_validate fi @@ -369,7 +369,56 @@ function restore_gs { sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate fi - + + if [ "$SKIP_CUSTOM" != '1' ] + then + if [ $REMOTE_CUSTOM_DNS == "1" ] + then + MESSAGE="Restoring ${CUSTOM_DNS} on $HOSTNAME" + echo_stat + cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + + MESSAGE="Validating Ownership on ${CUSTOM_DNS}" + echo_stat + + CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}') + if [ $CUSTOMLS_OWN == "rootroot" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + + MESSAGE="Validating Permissions on ${CUSTOM_DNS}" + echo_stat + + CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}') + if [ $CUSTOMLS_RWE == "-rw-r--r--" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${CUSTOM_DNS}" + echo_stat + sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1 + error_validate + fi + fi + fi + MESSAGE="Evacuating Saucer Section" echo_info sleep 1 @@ -410,6 +459,8 @@ function logs_gs { tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PULL echo -e "Recent Complete ${YELLOW}PUSH${NC} Executions" tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PUSH + echo -e "Recent Complete ${YELLOW}RESTORE${NC} Executions" + tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep RESTORE echo -e "========================================================" exit_nochange From 711ad979795879b91398622240dccca4903609f0 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:15:04 -0500 Subject: [PATCH 12/16] space --- gravity-sync.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/gravity-sync.sh b/gravity-sync.sh index d2629d1..011909b 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -319,6 +319,7 @@ function push_gs { logs_export exit_withchange + } function restore_gs { From affe98e82a2d6756c6593102f37e8ba447f5d760 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:19:07 -0500 Subject: [PATCH 13/16] Fineto --- gravity-sync.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 011909b..e8479df 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -300,7 +300,8 @@ function push_gs { MESSAGE="Setting Ownership on ${CUSTOM_DNS}" echo_stat ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1 - error_validate + error_validate + fi fi MESSAGE="Contacting Borg Collective" @@ -319,7 +320,7 @@ function push_gs { logs_export exit_withchange - + } function restore_gs { From 9c6a8bfa8d7a2b9a180b0a11744ea093bc97b066 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:20:24 -0500 Subject: [PATCH 14/16] "" --- gravity-sync.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index e8479df..c0beb55 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -177,7 +177,7 @@ function pull_gs { if [ "$SKIP_CUSTOM" != '1' ] then - if [ $REMOTE_CUSTOM_DNS == "1" ] + if [ "$REMOTE_CUSTOM_DNS" == "1" ] then MESSAGE="Backing Up ${CUSTOM_DNS} on $HOSTNAME" echo_stat @@ -280,7 +280,7 @@ function push_gs { if [ "$SKIP_CUSTOM" != '1' ] then - if [ $REMOTE_CUSTOM_DNS == "1" ] + if [ "$REMOTE_CUSTOM_DNS" == "1" ] then MESSAGE="Backing Up ${CUSTOM_DNS} from ${REMOTE_HOST}" echo_stat @@ -374,7 +374,7 @@ function restore_gs { if [ "$SKIP_CUSTOM" != '1' ] then - if [ $REMOTE_CUSTOM_DNS == "1" ] + if [ "$REMOTE_CUSTOM_DNS" == "1" ] then MESSAGE="Restoring ${CUSTOM_DNS} on $HOSTNAME" echo_stat From 6157889ac4434061d2cdaf3cb3d0361e4eb41427 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:39:54 -0500 Subject: [PATCH 15/16] Add references to custom.list --- README.md | 18 +++++++++--------- gravity-sync.sh | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0f7c292..c4975e2 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ That's Gravity Sync. ![Pull execution](https://user-images.githubusercontent.com/3002053/82915078-e870a180-9f35-11ea-8b36-271a02acdeaa.gif) -At it's core, Gravity Sync is maybe a handful of core bash commands, that uses rsync to reach out to a remote host, copy the running `gravity.db` file that contains the Pi-hole blocklist, and then replaces the copy on the local system. What Gravity Sync provides is an easy way to keep this happening in the background. Ideally you set it and forget it. In the long term, it would be awesome if the Pi-hole team made this entire script unncessary. +At it's core, Gravity Sync is maybe a handful of core bash commands, that uses rsync to reach out to a remote host, copy the running `gravity.db` and `custom.list` files that contains the Pi-hole blocklist, as well as the `custom.list` file that contains local DNS enteries, and then replaces the copy on the local system. What Gravity Sync provides is an easy way to keep this happening in the background. Ideally you set it and forget it. In the long term, it would be awesome if the Pi-hole team made this entire script unncessary. -Gravity Sync will **not** overwrite device specific settings such as local network configuration, admin/API passwords/keys, local hostfiles, upstream DNS resolvers, etc. It will also **not** keep DHCP settings or device leases synchronized. +Gravity Sync will **not** overwrite device specific settings such as device network configuration, admin/API passwords/keys, upstream DNS resolvers, etc. It will also **not** keep DHCP settings or device leases synchronized. ## Prerequisites Gravity Sync **requires** Pi-hole 5.0 or higher. @@ -45,9 +45,9 @@ Download the latest release from [GitHub](https://github.com/vmstan/gravity-sync ```bash cd ~ -wget https://github.com/vmstan/gravity-sync/archive/v1.6.0zip -unzip v1.6.0.zip -mv ~/gravity-sync-1.6.0 ~/gravity-sync +wget https://github.com/vmstan/gravity-sync/archive/v1.7.0.zip +unzip v1.7.0.zip +mv ~/gravity-sync-1.7.0 ~/gravity-sync cd gravity-sync ``` @@ -96,7 +96,7 @@ Gravity Sync uses SSH to run commands on the primary Pi-hole, and sync the two s #### Key-Pair Authentication This is the preferred option, as it's more reliable and less dependant on third party plugins. -You'll need to generate an SSH key for your secondary Pi-hole user and copy it to your primary Pi-hole. This will allow you to connect to and copy the gravity.db file without needing a password each time. When generating the SSH key, accept all the defaults and do not put a passphrase on your key file. +You'll need to generate an SSH key for your secondary Pi-hole user and copy it to your primary Pi-hole. This will allow you to connect to and copy the necessary files without needing a password each time. When generating the SSH key, accept all the defaults and do not put a passphrase on your key file. *Note: If you already have this setup on your systems for other purposes, you can skip this step.* @@ -143,7 +143,7 @@ The Gravity Sync Pull, is the standard method of sync operation, and will not pr ./gravity-sync.sh pull ``` -If the execution completes, you will now have overwritten your running gravity.db on the secondary Pi-hole after creating a copy of the running database (`gravity.db.backup`) in the `backup` subfolder located with your script. Gravity Sync will also keep a copy of the last sync'd gravity.db from the primary (in the `backup` folder identified as `gravity.db.pull`) for future use. +If the execution completes, you will now have overwritten your running `gravity.db` and `custom.list` on the secondary Pi-hole after creating a copy of the running files (with `.backup` appended) in the `backup` subfolder located with your script. Gravity Sync will also keep a copy of the last sync'd files from the primary (in the `backup` folder appended with `.pull`) for future use. Finally, a file called `gravity-sync.log` will be created in the `gravity-sync` folder along side the script with the date the script was last executed appended to the bottom. @@ -156,7 +156,7 @@ Gravity Sync includes the ability to `push` from the secondary Pi-hole back to t ./gravity-sync.sh push ``` -Before executing, this will make a copy of the remote database under `backup/gravity.db.push` then sync the local configuration to the primary Pi-hole. +Before executing, this will make a copy of the remote database under `backup/gravity.db.push` and `backup/custom.list.push` then sync the local configuration to the primary Pi-hole. This function purposefuly asks for user interaction to avoid being accidentally automated. @@ -167,7 +167,7 @@ Graviy Sync can also `restore` the database on the secondary Pi-hole in the even ./gravity-sync.sh restore ``` -This will copy your last `gravity.db.backup` to the running copy on the secondary Pi-hole. +This will copy your last `gravity.db.backup` and `custom.list.backup` to the running copy on the secondary Pi-hole. This function purposefuly asks for user interaction to avoid being accidentally automated. diff --git a/gravity-sync.sh b/gravity-sync.sh index c0beb55..fc3de2f 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -2,7 +2,7 @@ # GRAVITY SYNC BY VMSTAN ##################### PROGRAM='Gravity Sync' -VERSION='1.6.0' +VERSION='1.7.0' # Execute from the home folder of the user who owns it (ex: 'cd ~/gravity-sync') # For documentation or downloading updates visit https://github.com/vmstan/gravity-sync From 2bf3d0b8bad8a18f8443c2d29f9a4c6fd73469e3 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 29 May 2020 14:43:36 -0500 Subject: [PATCH 16/16] 1.7.0 --- gravity-sync.conf.example | 3 ++- gravity-sync.sh | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/gravity-sync.conf.example b/gravity-sync.conf.example index 947fc49..6d87add 100644 --- a/gravity-sync.conf.example +++ b/gravity-sync.conf.example @@ -28,4 +28,5 @@ REMOTE_PASS='' # CRONJOB_LOG='' # VERIFY_PASS='' # 0=Verify 1=Bypass -# SKIP_CUSTOM='' # 0=Syncit 1=Skip \ No newline at end of file +# SKIP_CUSTOM='' # 0=SyncIt 1=SkipIt +# DATE_OUTPUT='' $ 0=NoShow 1=ShowIt (na) \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index fc3de2f..0ed3038 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -26,7 +26,8 @@ CRONJOB_LOG='gravity-sync.cron' # replace in gravity-sync.conf to overwrite # Interaction Customization VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite -SKIP_CUSTOM='0' +SKIP_CUSTOM='0' # replace in gravity-sync.conf to overwrite +DATE_OUTPUT='0' # replace in gravity-sync.conf to overwrite # Pi-hole Folder/File Locations PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory