From e08022cfe18d38412997acecdfe1a46a80cb6ca7 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 16:21:10 -0500 Subject: [PATCH 01/13] Moving each status type to a function --- README.md | 10 +++--- gravity-sync.sh | 82 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 81a317ca..fb6185bb 100644 --- a/README.md +++ b/README.md @@ -126,13 +126,13 @@ Gravity Sync will validate that the `sshpass` utility is installed on your syste Save. Keep calm, carry on. ## Execution -Now test the script. You can run a comparison between the two which will be non-distruptive and see if everything has been configured correctly. +Now, test Gravity Sync. You can run a comparison between primary and secondary databases, which will be non-distruptive, and see if everything has been configured correctly. ```bash ./gravity-sync.sh compare ``` -Assuming Gravity Sync runs successfully, it'll indicate if there are changes pending between the two databases. If not, I suggest making a subtle change to a whitelist/blacklist on your primary PH, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly. +Assuming Gravity Sync runs successfully, it will indicate if there are changes pending between the two databases. If not, make a subtle change to a whitelist/blacklist on your primary PH, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly. ### The Pull Function @@ -142,7 +142,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 PH 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 master, 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 on the secondary PH 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. 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. @@ -224,4 +224,6 @@ chmod +x gravity-sync.sh - If you use a non-standard SSH port to connect to your primary Pi-hole, you can add `SSH_PORT='123'` to the bottom of your `gravity-sync.conf` file. (Subsitute 123 for your non-standard port.) This will overwrite the `SSH_PORT=22` at the top of the script as it is imported later in the execution. - If you'd like to know what version of the script you have running by running `./gravity-sync.sh version` - If the update script fails, make sure you did your original deployment via `git clone` and not a manual install. -- If it doesn't kick off, you can manually execute a `git pull` while in the `gravity-sync` directory. \ No newline at end of file +- If it doesn't kick off, you can manually execute a `git pull` while in the `gravity-sync` directory. + +If all else fails, delete the entire `gravity-sync` folder from your system and re-deploy. This will have no impact on your replicated databases. \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index 8747eb66..c9dbe425 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -73,12 +73,12 @@ function import_gs { error_validate MESSAGE="Using ${REMOTE_USER}@${REMOTE_HOST}" - echo -e "${INFO} ${MESSAGE}" + echo_info else echo -e "\r${FAIL} ${MESSAGE}" MESSAGE="${CONFIG_FILE} Missing" - echo -e "${INFO} ${MESSAGE}" + echo_info TASKTYPE='CONFIG' config_generate @@ -93,7 +93,7 @@ function update_gs { # logs_export # dumps log prior to execution because script stops after successful pull MESSAGE="Requires GitHub Installation" - echo -e "${INFO} ${MESSAGE}" + echo_info git reset --hard git pull exit @@ -105,7 +105,7 @@ function beta_gs { # logs_export # dumps log prior to execution because script stops after successful pull MESSAGE="Requires GitHub Installation" - echo -e "${INFO} ${MESSAGE}" + echo_info git reset --hard git fetch origin git pull origin development @@ -117,10 +117,13 @@ function beta_gs { function pull_gs { TASKTYPE='PULL' - echo -e "${INFO} ${TASKTYPE} Requested" + MESSAGE="${TASKTYPE} Requested" + echo_info + md5_compare - echo -e "${INFO} ${TASKTYPE} Commencing" + MESSAGE="${TASKTYPE} Starting" + echo_info MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME" echo -en "${STAT} ${MESSAGE}" @@ -176,7 +179,7 @@ function pull_gs { fi MESSAGE="Inverting Tachyon Pulse" - echo -e "${INFO} ${MESSAGE}" + echo_info sleep 1 MESSAGE="Updating FTLDNS Configuration" @@ -226,7 +229,7 @@ function push_gs { error_validate MESSAGE="Contacting Borg Collective" - echo -e "${INFO} ${MESSAGE}" + echo_info sleep 1 MESSAGE="Updating FTLDNS Configuration" @@ -343,7 +346,7 @@ function validate_ph_folders { ## Validate SSHPASS function validate_os_sshpass { MESSAGE="Checking SSH Configuration" - echo -e "${INFO} ${MESSAGE}" + echo_info if hash sshpass 2>/dev/null then @@ -367,7 +370,7 @@ function validate_os_sshpass { MESSAGE="Using SSH Key-Pair Authentication" fi - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Testing SSH Connection" echo -en "${STAT} ${MESSAGE}" @@ -437,7 +440,7 @@ function config_generate { if hash sshpass 2>/dev/null then MESSAGE="SSHPASS Utility Detected" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Do you want to configure password based SSH authentication?" echo -e "${WARN} ${MESSAGE}" @@ -455,10 +458,10 @@ function config_generate { else MESSAGE="SSHPASS Not Installed" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Defaulting to SSH Key-Pair Authentication" - echo -e "${INFO} ${MESSAGE}" + echo_info fi if [ -z $INPUT_REMOTE_PASS ] @@ -466,10 +469,10 @@ function config_generate { if [ -f $HOME/${SSH_PKIF} ] then MESSAGE="Using Existing ~/${SSH_PKIF}" - echo -e "${INFO} ${MESSAGE}" + echo_info else MESSAGE="Generating ~/${SSH_PKIF}" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Accept All Defaults" echo -e "${WARN} ${MESSAGE}" @@ -495,7 +498,7 @@ function config_generate { if [ -f $HOME/${SSH_PKIF} ] then MESSAGE="Registering Key-Pair on ${REMOTE_HOST}" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Enter ${REMOTE_USER}@${REMOTE_HOST} Password Below" echo -e "${NEED} ${MESSAGE}" @@ -558,7 +561,7 @@ function exit_nochange { function exit_withchange { SCRIPT_END=$SECONDS MESSAGE="${PROGRAM} ${TASKTYPE} Completed in $((SCRIPT_END-SCRIPT_START)) seconds" - echo -e "${INFO} ${MESSAGE}" + echo_info exit 0 } @@ -605,14 +608,14 @@ function task_automate { if [ ${CRON_CHECK} == 1 ] then MESSAGE="Automation Task Already Exists" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="Use 'crontab -e' to manually remove/edit" - echo -e "${INFO} ${MESSAGE}" + echo_info exit_nochange fi MESSAGE="Set Automation Frequency Per Hour" - echo -e "${INFO} ${MESSAGE}" + echo_info MESSAGE="1 = Every 60 Minutes" echo -e "++++++ ${MESSAGE}" @@ -658,6 +661,39 @@ function task_automate { exit_withchange } +# Echo Stack +## Informative +function echo_info { + echo -e "${INFO} ${MESSAGE}" +} + +## Warning +function echo_warn { + echo -e "${WARN} ${MESSAGE}" +} + +## Executing +function echo_stat { + echo -en "${STAT} ${MESSAGE}" +} + +## Success +function echo_good { + echo -e "\r${GOOD} ${MESSAGE}" +} + +## Failure +function echo_fail { + echo -e "\r${FAIL} ${MESSAGE}" +} + +## Request +function echo_need { + echo -en "${NEED} ${MESSAGE}: " +} + + + # SCRIPT EXECUTION ########################### SCRIPT_START=$SECONDS @@ -679,7 +715,7 @@ case $# in import_gs MESSAGE="Validating Folder Configuration" - echo -e "${INFO} ${MESSAGE}" + echo_info validate_gs_folders validate_ph_folders validate_os_sshpass @@ -734,7 +770,7 @@ case $# in echo -e "\r${GOOD} ${MESSAGE}" MESSAGE="Logs Requested" - echo -e "${INFO} ${MESSAGE}" + echo_info logs_gs ;; @@ -770,7 +806,7 @@ case $# in else MESSAGE="${CONFIG_FILE} Missing" - echo -e "${INFO} ${MESSAGE}" + echo_info config_generate fi From 6dfd8359889fbec7c7be0275b8ed17587f42a4bb Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 16:50:25 -0500 Subject: [PATCH 02/13] Replace all the echos --- gravity-sync.sh | 253 ++++++++++++++++++++++++++++-------------------- 1 file changed, 148 insertions(+), 105 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index c9dbe425..45bf6949 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -75,7 +75,7 @@ function import_gs { MESSAGE="Using ${REMOTE_USER}@${REMOTE_HOST}" echo_info else - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail MESSAGE="${CONFIG_FILE} Missing" echo_info @@ -126,54 +126,54 @@ function pull_gs { echo_info MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME" - echo -en "${STAT} ${MESSAGE}" + echo_stat cp ${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup >/dev/null 2>&1 error_validate MESSAGE="Pulling ${GRAVITY_FI} from ${REMOTE_HOST}" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull >/dev/null 2>&1 error_validate MESSAGE="Replacing ${GRAVITY_FI} on $HOSTNAME" - echo -en "${STAT} ${MESSAGE}" + echo_stat sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate MESSAGE="Validating Ownership on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}') if [ $GRAVDB_OWN == "piholepihole" ] then - echo -e "\r${GOOD} ${MESSAGE}" + echo_good else - echo -e "\r${FAIL} $MESSAGE" + echo_fail - MESSAGE2="Attempting to Compensate" - echo -e "${INFO} ${MESSAGE2}" + MESSAGE="Attempting to Compensate" + echo_info MESSAGE="Setting Ownership on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate fi MESSAGE="Validating Permissions on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${GRAVITY_FI} | grep -v f: | grep ${GRAVITY_FI} | awk '{print $1}') if [ $GRAVDB_RWE = "-rw-rw-r--" ] then - echo -e "\r${GOOD} ${MESSAGE}" + echo_good else - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail - MESSAGE2="Attempting to Compensate" - echo -e "${INFO} ${MESSAGE2}" + MESSAGE="Attempting to Compensate" + echo_info MESSAGE="Setting Ownership on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate fi @@ -183,12 +183,12 @@ function pull_gs { sleep 1 MESSAGE="Updating FTLDNS Configuration" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1 error_validate MESSAGE="Reloading FTLDNS Services" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${PIHOLE_BIN} restartdns >/dev/null 2>&1 error_validate @@ -198,33 +198,31 @@ function pull_gs { ## Push Function function push_gs { - TASKTYPE='PUSH' - - echo -e "${INFO} ${TASKTYPE} Requested" md5_compare - echo -e "${WARN} Are you sure you want to overwrite the primary PH configuration on ${REMOTE_HOST}?" + MESSAGE="Are you sure you want to overwrite ${GRAVITY_DB} on ${REMOTE_HOST}?" + echo_warn select yn in "Yes" "No"; do case $yn in Yes ) MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.push >/dev/null 2>&1 error_validate MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 error_validate MESSAGE="Setting Permissions on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1 error_validate MESSAGE="Setting Ownership on ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${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 @@ -233,12 +231,12 @@ function push_gs { sleep 1 MESSAGE="Updating FTLDNS Configuration" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1 error_validate MESSAGE="Reloading FTLDNS Services" - echo -en "${STAT} ${MESSAGE}" + echo_stat ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1 error_validate @@ -257,7 +255,9 @@ function push_gs { ## Core Logging ### Write Logs Out function logs_export { - echo -e "${INFO} Logging Timestamps to ${SYNCING_LOG}" + MESSAGE="Logging Timestamps to ${SYNCING_LOG}" + echo_info + echo -e $(date) "[${TASKTYPE}]" >> ${LOG_PATH}/${SYNCING_LOG} } @@ -265,13 +265,16 @@ function logs_export { function logs_gs { import_gs - echo -e "${INFO} Tailing ${LOG_PATH}/${SYNCING_LOG}" + MESSAGE="Tailing ${LOG_PATH}/${SYNCING_LOG}" + echo_info + echo -e "========================================================" echo -e "Recent Complete ${YELLOW}PULL${NC} Executions" 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 "========================================================" + exit_nochange } @@ -281,28 +284,37 @@ function show_crontab { import_gs MESSAGE="Replaying Last Cronjob" - echo -en "${STAT} ${MESSAGE}" + echo_stat if [ -f ${LOG_PATH}/${CRONJOB_LOG} ] then if [ -s ${LOG_PATH}/${CRONJOB_LOG} ] - echo -e "\r${GOOD} ${MESSAGE}" + echo_good - echo -e "${INFO} Tailing ${LOG_PATH}/${CRONJOB_LOG}" + MESSAGE="Tailing ${LOG_PATH}/${CRONJOB_LOG}" + echo_info + echo -e "========================================================" date -r ${LOG_PATH}/${CRONJOB_LOG} cat ${LOG_PATH}/${CRONJOB_LOG} echo -e "========================================================" - exit_nochange + + exit_nochange then - echo -e "\r${FAIL} ${MESSAGE}" - echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} appears empty" - exit_nochange + echo_fail + + MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Empty" + echo_info + + exit_nochange fi else - echo -e "\r${FAIL} ${MESSAGE}" - echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} not yet created" - exit_nochange + echo_fail + + MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Missing" + echo_info + + exit_nochange fi } @@ -310,22 +322,22 @@ function show_crontab { ## Validate GS Folders function validate_gs_folders { MESSAGE="Locating $HOME/${LOCAL_FOLDR}" - echo -en "${STAT} ${MESSAGE}" + echo_stat if [ -d $HOME/${LOCAL_FOLDR} ] then - echo -e "\r${GOOD} ${MESSAGE}" + echo_good else - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail exit_nochange fi MESSAGE="Locating $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}" - echo -en "${STAT} ${MESSAGE}" + echo_stat if [ -d $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD} ] then - echo -e "\r${GOOD} ${MESSAGE}" + echo_good else - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail exit_nochange fi } @@ -333,12 +345,12 @@ function validate_gs_folders { ## Validate PH Folders function validate_ph_folders { MESSAGE="Locating ${PIHOLE_DIR}" - echo -en "${STAT} ${MESSAGE}" + echo_stat if [ -d ${PIHOLE_DIR} ] then - echo -e "\r${GOOD} ${MESSAGE}" + echo_good else - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail exit_nochange fi } @@ -373,7 +385,7 @@ function validate_os_sshpass { echo_info MESSAGE="Testing SSH Connection" - echo -en "${STAT} ${MESSAGE}" + echo_stat timeout 5 ${SSHPASSWORD} ssh -p ${SSH_PORT} -i '$HOME/${SSH_PKIF}' -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1 error_validate } @@ -381,33 +393,36 @@ function validate_os_sshpass { ## Error Validation function error_validate { if [ "$?" != "0" ]; then - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail exit 1 else - echo -e "\r${GOOD} ${MESSAGE}" + echo_good fi } ## Validate Sync Required function md5_compare { - echo -e "${INFO} Comparing ${GRAVITY_FI} Changes" + MESSAGE="Comparing ${GRAVITY_FI} Changes" + echo_info MESSAGE="Analyzing Remote ${GRAVITY_FI}" - echo -en "${STAT} ${MESSAGE}" + echo_stat primaryMD5=$(${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 -en "${STAT} ${MESSAGE}" + echo_stat secondMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI}) error_validate if [ "$primaryMD5" == "$secondMD5" ] then - echo -e "${INFO} No Differences in ${GRAVITY_FI}" + MESSAGE="No Differences in ${GRAVITY_FI}" + echo_info exit_nochange else - echo -e "${INFO} Changes Detected in ${GRAVITY_FI}" + MESSAGE="Changes Detected in ${GRAVITY_FI}" + echo_info fi } @@ -415,25 +430,25 @@ function md5_compare { ## Generate New Configuration function config_generate { MESSAGE="Creating ${CONFIG_FILE} from Template" - echo -en "${STAT} ${MESSAGE}" + echo_stat cp $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}.example $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate - MESSAGE="Enter IP or DNS of primary Pi-hole server: " - echo -en "${NEED} ${MESSAGE}" + MESSAGE="Enter IP or DNS of primary Pi-hole server" + echo_need read INPUT_REMOTE_HOST MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server: " - echo -en "${NEED} ${MESSAGE}" + echo_need read INPUT_REMOTE_USER MESSAGE="Saving Host to ${CONFIG_FILE}" - echo -en "${STAT} ${MESSAGE}" + echo_stat sed -i "/REMOTE_HOST='192.168.1.10'/c\REMOTE_HOST='${INPUT_REMOTE_HOST}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate MESSAGE="Saving User to ${CONFIG_FILE}" - echo -en "${STAT} ${MESSAGE}" + echo_stat sed -i "/REMOTE_USER='pi'/c\REMOTE_USER='${INPUT_REMOTE_USER}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate @@ -443,16 +458,16 @@ function config_generate { echo_info MESSAGE="Do you want to configure password based SSH authentication?" - echo -e "${WARN} ${MESSAGE}" + echo_warn MESSAGE="Your password will be stored clear-text in the ${CONFIG_FILE}!" - echo -e "${WARN} ${MESSAGE}" + echo_warn - MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication: " - echo -en "${NEED} ${MESSAGE}" + MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication" + echo_need read INPUT_REMOTE_PASS MESSAGE="Saving Password to ${CONFIG_FILE}" - echo -en "${STAT} ${MESSAGE}" + echo_stat sed -i "/REMOTE_PASS=''/c\REMOTE_PASS='${INPUT_REMOTE_PASS}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate @@ -475,7 +490,7 @@ function config_generate { echo_info MESSAGE="Accept All Defaults" - echo -e "${WARN} ${MESSAGE}" + echo_warn MESSAGE="Complete Key-Pair Creation" echo -e "${NEED} ${MESSAGE}" @@ -489,7 +504,7 @@ function config_generate { fi MESSAGE="Importing New ${CONFIG_FILE}" - echo -en "${STAT} ${MESSAGE}" + echo_stat source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate @@ -523,20 +538,20 @@ function config_generate { function config_delete { source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} MESSAGE="Configuration File Exists" - echo -e "${WARN} ${MESSAGE}" + echo_warn echo -e "========================================================" cat $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} echo -e "========================================================" MESSAGE="Are you sure you want to erase this configuration?" - echo -e "${WARN} ${MESSAGE}" + echo_warn select yn in "Yes" "No"; do case $yn in Yes ) MESSAGE="Erasing Existing Configuration" - echo -en "${STAT} ${MESSAGE}" + echo_stat rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} error_validate @@ -553,7 +568,8 @@ function config_delete { # Exit Codes ## No Changes Made function exit_nochange { - echo -e "${INFO} ${PROGRAM} ${TASKTYPE} Aborting" + MESSAGE="${PROGRAM} ${TASKTYPE} Aborting" + echo_info exit 0 } @@ -593,14 +609,15 @@ function list_gs_arguments { # Output Version function show_version { - echo -e "${INFO} ${PROGRAM} ${VERSION}" + MESSAGE="${PROGRAM} ${VERSION}" + echo_info } # Task Stack ## Automate Task function task_automate { TASKTYPE='AUTOMATE' - echo -e "\r${GOOD} ${MESSAGE}" + echo_good import_gs @@ -628,8 +645,8 @@ function task_automate { MESSAGE="12 = Every 05 Minutes" echo -e "++++++ ${MESSAGE}" - MESSAGE="Input Automation Frequency: " - echo -en "${NEED} ${MESSAGE}" + MESSAGE="Input Automation Frequency" + echo_need read INPUT_AUTO_FREQ if [ $INPUT_AUTO_FREQ == 1 ] @@ -654,7 +671,7 @@ function task_automate { fi MESSAGE="Saving to Crontab" - echo -en "${STAT} ${MESSAGE}" + echo_stat (crontab -l 2>/dev/null; echo "*/${AUTO_FREQ} * * * * ${BASH_PATH} $HOME/${LOCAL_FOLDR}/${GS_FILENAME} pull > ${LOG_PATH}/${CRONJOB_LOG}") | crontab - error_validate @@ -698,19 +715,23 @@ function echo_need { SCRIPT_START=$SECONDS MESSAGE="Evaluating Script Arguments" - echo -en "${STAT} ${MESSAGE}" + echo_stat case $# in 0) - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail list_gs_arguments ;; 1) case $1 in pull) - echo -e "\r${GOOD} ${MESSAGE}" + TASKTYPE='PULL' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info import_gs @@ -725,11 +746,16 @@ case $# in ;; push) - echo -e "\r${GOOD} ${MESSAGE}" + TASKTYPE='PUSH' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info import_gs - echo -e "${INFO} Validating Folder Configuration" + MESSAGE="Validating Folder Configuration" + echo_info validate_gs_folders validate_ph_folders validate_os_sshpass @@ -740,47 +766,59 @@ case $# in version) TASKTYPE='VERSION' - echo -e "\r${GOOD} ${MESSAGE}" + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info show_version exit_nochange ;; update) - # TASKTYPE='UPDATE' - echo -e "\r${GOOD} ${MESSAGE}" + TASKTYPE='UPDATE' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info - echo -e "${INFO} Update Requested" - update_gs + update_gs exit_nochange ;; beta) - # TASKTYPE='BETA' - echo -e "\r${GOOD} ${MESSAGE}" + TASKTYPE='BETA' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info - echo -e "${INFO} Beta Update Requested" - beta_gs + beta_gs exit_nochange ;; logs) TASKTYPE='LOGS' + echo_good - echo -e "\r${GOOD} ${MESSAGE}" - - MESSAGE="Logs Requested" + MESSAGE="${TASKTYPE} Requested" echo_info - logs_gs + + logs_gs ;; compare) TASKTYPE='COMPARE' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info - echo -e "\r${GOOD} ${MESSAGE}" - import_gs + import_gs - echo -e "${INFO} Validating Folder Configuration" + MESSAGE="Validating OS Configuration" + echo_info + validate_gs_folders validate_ph_folders validate_os_sshpass @@ -790,15 +828,20 @@ case $# in cron) TASKTYPE='CRON' - echo -e "\r${GOOD} ${MESSAGE}" + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info show_crontab ;; config) TASKTYPE='CONFIGURE' - echo -e "\r${GOOD} ${MESSAGE}" - echo -e "${INFO} Entering ${TASKTYPE} Mode" + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info if [ -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} ] then @@ -821,14 +864,14 @@ case $# in ;; *) - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail list_gs_arguments ;; esac ;; *) - echo -e "\r${FAIL} ${MESSAGE}" + echo_fail list_gs_arguments ;; esac \ No newline at end of file From 6a9ab25613aad1a0d1177b0b6ecde172f9e7a553 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 16:51:41 -0500 Subject: [PATCH 03/13] Fix --- gravity-sync.sh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 45bf6949..41255352 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -115,16 +115,8 @@ function beta_gs { # Gravity Core Functions ## Pull Function function pull_gs { - TASKTYPE='PULL' - - MESSAGE="${TASKTYPE} Requested" - echo_info - md5_compare - MESSAGE="${TASKTYPE} Starting" - echo_info - MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME" echo_stat cp ${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup >/dev/null 2>&1 From 263f1ce67de4afa43fec04a5758d5eedd08f8ab6 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:09:58 -0500 Subject: [PATCH 04/13] Add yank function --- gravity-sync.sh | 99 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 41255352..a815cf48 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -243,6 +243,83 @@ function push_gs { done } +function yank_gs { + MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!" + echo_warn + + MESSAGE="Enter WARP-CORE-EJECT at this prompt to confirm" + echo_need + + read INPUT_WARPCORE + + if [ $INPUT_WARPCORE != "WARP-CORE-EJECT" ] + then + MESSAGE="${TASKTYPE} Aborted" + echo_info + exit_nochange + fi + + MESSAGE="Restoring ${GRAVITY_FI} on $HOSTNAME" + echo_stat + sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 + error_validate + + MESSAGE="Validating Ownership on ${GRAVITY_FI}" + echo_stat + + GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}') + if [ $GRAVDB_OWN == "piholepihole" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${GRAVITY_FI}" + echo_stat + sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 + error_validate + fi + + MESSAGE="Validating Permissions on ${GRAVITY_FI}" + echo_stat + + GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${GRAVITY_FI} | grep -v f: | grep ${GRAVITY_FI} | awk '{print $1}') + if [ $GRAVDB_RWE = "-rw-rw-r--" ] + then + echo_good + else + echo_fail + + MESSAGE="Attempting to Compensate" + echo_info + + MESSAGE="Setting Ownership on ${GRAVITY_FI}" + echo_stat + sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 + error_validate + fi + + MESSAGE="Evacuating Turbolifts" + echo_info + sleep 1 + + MESSAGE="Updating FTLDNS Configuration" + echo_stat + ${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1 + error_validate + + MESSAGE="Reloading FTLDNS Services" + echo_stat + ${PIHOLE_BIN} restartdns >/dev/null 2>&1 + error_validate + + logs_export + exit_withchange +} + # Logging Functions ## Core Logging ### Write Logs Out @@ -430,7 +507,7 @@ function config_generate { echo_need read INPUT_REMOTE_HOST - MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server: " + MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server" echo_need read INPUT_REMOTE_USER @@ -585,6 +662,7 @@ function list_gs_arguments { echo -e "Replication Options:" echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary PH to this server" echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH" + echo -e " ${YELLOW}yank${NC} Restore ${GRAVITY_FI} on this server from previous copy" echo -e " ${YELLOW}compare${NC} Just check for differences between primary and secondary" echo -e "" echo -e "Update Options:" @@ -755,6 +833,25 @@ case $# in push_gs exit ;; + + push) + TASKTYPE='YANK' + echo_good + + MESSAGE="${TASKTYPE} Requested" + echo_info + + import_gs + + MESSAGE="Validating Folder Configuration" + echo_info + validate_gs_folders + validate_ph_folders + # validate_os_sshpass + + yank_gs + exit + ;; version) TASKTYPE='VERSION' From f9127fdcd66fd1de74309cf8c48ee38901d58342 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:10:51 -0500 Subject: [PATCH 05/13] Yank --- gravity-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index a815cf48..07a2643b 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -834,7 +834,7 @@ case $# in exit ;; - push) + yank) TASKTYPE='YANK' echo_good From 24662dd16d31303cdc0a250d71bfc9cb0c0704f0 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:12:05 -0500 Subject: [PATCH 06/13] variable name --- gravity-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 07a2643b..849a1354 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -252,7 +252,7 @@ function yank_gs { read INPUT_WARPCORE - if [ $INPUT_WARPCORE != "WARP-CORE-EJECT" ] + if [ "${INPUT_WARPCORE}" != "WARP-CORE-EJECT" ] then MESSAGE="${TASKTYPE} Aborted" echo_info From 88e30377e3717967dfc1bd469b83dbcf5eed43a0 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:24:31 -0500 Subject: [PATCH 07/13] Change push confirmation method --- gravity-sync.sh | 95 ++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 849a1354..d8dda27c 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -192,55 +192,54 @@ function pull_gs { function push_gs { md5_compare - MESSAGE="Are you sure you want to overwrite ${GRAVITY_DB} on ${REMOTE_HOST}?" - echo_warn - select yn in "Yes" "No"; do - case $yn in - Yes ) - - MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}" - echo_stat - ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.push >/dev/null 2>&1 - error_validate - - MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}" - echo_stat - ${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 - error_validate - - MESSAGE="Setting Permissions on ${GRAVITY_FI}" - echo_stat - ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1 - error_validate - - MESSAGE="Setting Ownership on ${GRAVITY_FI}" - echo_stat - ${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 + MESSAGE="Enter FIRE-PHOTON-TORPEDOS at this prompt to confirm" + echo_need + + read INPUT_TORPEDOS + + if [ "${INPUT_TORPEDOS}" != "FIRE-PHOTON-TORPEDOS" ] + then + MESSAGE="${TASKTYPE} Aborted" + echo_info + exit_nochange + fi + + MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.push >/dev/null 2>&1 + error_validate + + MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}" + echo_stat + ${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 + error_validate + + MESSAGE="Setting Permissions on ${GRAVITY_FI}" + echo_stat + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1 + error_validate + + MESSAGE="Setting Ownership on ${GRAVITY_FI}" + echo_stat + ${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 + + MESSAGE="Contacting Borg Collective" + echo_info + sleep 1 + + MESSAGE="Updating FTLDNS Configuration" + echo_stat + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1 + error_validate - MESSAGE="Contacting Borg Collective" - echo_info - sleep 1 + MESSAGE="Reloading FTLDNS Services" + echo_stat + ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1 + error_validate - MESSAGE="Updating FTLDNS Configuration" - echo_stat - ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1 - error_validate - - MESSAGE="Reloading FTLDNS Services" - echo_stat - ${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1 - error_validate - - logs_export - exit_withchange - ;; - - No ) - exit_nochange - ;; - esac - done + logs_export + exit_withchange } function yank_gs { @@ -258,7 +257,7 @@ function yank_gs { echo_info exit_nochange fi - + MESSAGE="Restoring ${GRAVITY_FI} on $HOSTNAME" echo_stat sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1 From ad45d1779105f7187df9715f6764d49c7035d08d Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:29:41 -0500 Subject: [PATCH 08/13] Redo config delete dialog --- gravity-sync.sh | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index d8dda27c..be29e267 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -242,16 +242,16 @@ function push_gs { exit_withchange } -function yank_gs { +function restore_gs { MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!" echo_warn - MESSAGE="Enter WARP-CORE-EJECT at this prompt to confirm" + MESSAGE="Enter FIRE-ALL-PHASERS at this prompt to confirm" echo_need - read INPUT_WARPCORE + read INPUT_PHASER - if [ "${INPUT_WARPCORE}" != "WARP-CORE-EJECT" ] + if [ "${INPUT_PHASER}" != "FIRE-ALL-PHASERS" ] then MESSAGE="${TASKTYPE} Aborted" echo_info @@ -614,23 +614,24 @@ function config_delete { MESSAGE="Are you sure you want to erase this configuration?" echo_warn - - select yn in "Yes" "No"; do - case $yn in - Yes ) - MESSAGE="Erasing Existing Configuration" - echo_stat - rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} - error_validate - - config_generate - ;; - No ) - exit_nochange - ;; - esac - done + MESSAGE="Enter EJECT-THE-WARPCORE at this prompt to confirm" + echo_need + + read INPUT_WARPCORE + + if [ "${INPUT_WARPCORE}" != "EJECT-THE-WARPCORE" ] + then + MESSAGE="${TASKTYPE} Aborted" + echo_info + exit_nochange + else + MESSAGE="Erasing Existing Configuration" + echo_stat + rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} + error_validate + config_generate + fi } # Exit Codes @@ -661,7 +662,7 @@ function list_gs_arguments { echo -e "Replication Options:" echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary PH to this server" echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH" - echo -e " ${YELLOW}yank${NC} Restore ${GRAVITY_FI} on this server from previous copy" + echo -e " ${YELLOW}restore${NC} Restore ${GRAVITY_FI} on this server from previous copy" echo -e " ${YELLOW}compare${NC} Just check for differences between primary and secondary" echo -e "" echo -e "Update Options:" @@ -833,8 +834,8 @@ case $# in exit ;; - yank) - TASKTYPE='YANK' + restore) + TASKTYPE='RESTORE' echo_good MESSAGE="${TASKTYPE} Requested" @@ -848,7 +849,7 @@ case $# in validate_ph_folders # validate_os_sshpass - yank_gs + restore_gs exit ;; From bea46fa3fb0e2bbda47f5f74ed93bbaab6c6b278 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 17:51:48 -0500 Subject: [PATCH 09/13] remove dashes --- gravity-sync.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index be29e267..cf2eec4b 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -192,12 +192,12 @@ function pull_gs { function push_gs { md5_compare - MESSAGE="Enter FIRE-PHOTON-TORPEDOS at this prompt to confirm" + MESSAGE="Enter FIRE PHOTON TORPEDOS at this prompt to confirm" echo_need read INPUT_TORPEDOS - if [ "${INPUT_TORPEDOS}" != "FIRE-PHOTON-TORPEDOS" ] + if [ "${INPUT_TORPEDOS}" != "FIRE PHOTON TORPEDOS" ] then MESSAGE="${TASKTYPE} Aborted" echo_info @@ -246,12 +246,12 @@ function restore_gs { MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!" echo_warn - MESSAGE="Enter FIRE-ALL-PHASERS at this prompt to confirm" + MESSAGE="Enter FIRE ALL PHASERS at this prompt to confirm" echo_need read INPUT_PHASER - if [ "${INPUT_PHASER}" != "FIRE-ALL-PHASERS" ] + if [ "${INPUT_PHASER}" != "FIRE ALL PHASERS" ] then MESSAGE="${TASKTYPE} Aborted" echo_info @@ -615,12 +615,12 @@ function config_delete { MESSAGE="Are you sure you want to erase this configuration?" echo_warn - MESSAGE="Enter EJECT-THE-WARPCORE at this prompt to confirm" + MESSAGE="Enter EJECT THE WARPCORE at this prompt to confirm" echo_need read INPUT_WARPCORE - if [ "${INPUT_WARPCORE}" != "EJECT-THE-WARPCORE" ] + if [ "${INPUT_WARPCORE}" != "EJECT THE WARPCORE" ] then MESSAGE="${TASKTYPE} Aborted" echo_info From f86b85799776b38bb3b04849fee3f7558dd804c9 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 21:28:14 -0500 Subject: [PATCH 10/13] Moved intent validation to a function --- gravity-sync.sh | 75 +++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index cf2eec4b..01f5f0fe 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -192,17 +192,7 @@ function pull_gs { function push_gs { md5_compare - MESSAGE="Enter FIRE PHOTON TORPEDOS at this prompt to confirm" - echo_need - - read INPUT_TORPEDOS - - if [ "${INPUT_TORPEDOS}" != "FIRE PHOTON TORPEDOS" ] - then - MESSAGE="${TASKTYPE} Aborted" - echo_info - exit_nochange - fi + intent_validate MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}" echo_stat @@ -246,17 +236,7 @@ function restore_gs { MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!" echo_warn - MESSAGE="Enter FIRE ALL PHASERS at this prompt to confirm" - echo_need - - read INPUT_PHASER - - if [ "${INPUT_PHASER}" != "FIRE ALL PHASERS" ] - then - MESSAGE="${TASKTYPE} Aborted" - echo_info - exit_nochange - fi + intent_validate MESSAGE="Restoring ${GRAVITY_FI} on $HOSTNAME" echo_stat @@ -494,6 +474,36 @@ function md5_compare { fi } +## Validate Intent +function intent_validate { + PHASER=$(( ( RANDOM % 4 ) + 1 )) + if [ "$PHASER" = "1" ] + then + INTENT="FIRE PHOTON TORPEDOS" + elif [ "$PHASER" = "2" ] + then + INTENT="FIRE ALL PHASERS" + elif [ "$PHASER" = "3" ] + then + INTENT="EJECT THE WARPCORE" + elif [ "$PHASER" = "4" ] + then + INTENT="ENGAGE TRACTOR BEAM" + fi + + MESSAGE="Enter ${INTENT} at this prompt to confirm" + echo_need + + read INPUT_INTENT + + if [ "${INPUT_INTENT}" != "${INTENT}" ] + then + MESSAGE="${TASKTYPE} Aborted" + echo_info + exit_nochange + fi +} + # Configuration Management ## Generate New Configuration function config_generate { @@ -615,23 +625,14 @@ function config_delete { MESSAGE="Are you sure you want to erase this configuration?" echo_warn - MESSAGE="Enter EJECT THE WARPCORE at this prompt to confirm" - echo_need + intent_validate - read INPUT_WARPCORE + MESSAGE="Erasing Existing Configuration" + echo_stat + rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} + error_validate - if [ "${INPUT_WARPCORE}" != "EJECT THE WARPCORE" ] - then - MESSAGE="${TASKTYPE} Aborted" - echo_info - exit_nochange - else - MESSAGE="Erasing Existing Configuration" - echo_stat - rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} - error_validate - config_generate - fi + config_generate } # Exit Codes From bb453c0ce96d37565f2bd2e80af2bfb1d83e9dcd Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 21:43:58 -0500 Subject: [PATCH 11/13] Verification bypass mechanism --- gravity-sync.conf.example | 3 ++- gravity-sync.sh | 55 +++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/gravity-sync.conf.example b/gravity-sync.conf.example index 62c679eb..a48c0589 100644 --- a/gravity-sync.conf.example +++ b/gravity-sync.conf.example @@ -25,4 +25,5 @@ REMOTE_PASS='' # SSH_PKIF='' # LOG_PATH='' # SYNCING_LOG='' -# CRONJOB_LOG='' \ No newline at end of file +# CRONJOB_LOG='' +# VERIFY_PASS='0' \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index 01f5f0fe..cc0fc49c 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -2,7 +2,7 @@ # GRAVITY SYNC BY VMSTAN ##################### PROGRAM='Gravity Sync' -VERSION='1.5.0' +VERSION='1.6.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 @@ -24,6 +24,9 @@ LOG_PATH="$HOME/${LOCAL_FOLDR}" # replace in gravity-sync.conf to overwrite SYNCING_LOG='gravity-sync.log' # replace in gravity-sync.conf to overwrite CRONJOB_LOG='gravity-sync.cron' # replace in gravity-sync.conf to overwrite +# Interaction Customization +VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite + # PH Folder/File Locations PIHOLE_DIR='/etc/pihole' # default PH data directory GRAVITY_FI='gravity.db' # default PH database file @@ -476,31 +479,37 @@ function md5_compare { ## Validate Intent function intent_validate { - PHASER=$(( ( RANDOM % 4 ) + 1 )) - if [ "$PHASER" = "1" ] - then - INTENT="FIRE PHOTON TORPEDOS" - elif [ "$PHASER" = "2" ] - then - INTENT="FIRE ALL PHASERS" - elif [ "$PHASER" = "3" ] + if [ "$VERIFY_PASS" == "0" ] then - INTENT="EJECT THE WARPCORE" - elif [ "$PHASER" = "4" ] - then - INTENT="ENGAGE TRACTOR BEAM" - fi - - MESSAGE="Enter ${INTENT} at this prompt to confirm" - echo_need + PHASER=$((( RANDOM % 4 ) + 1 )) + if [ "$PHASER" = "1" ] + then + INTENT="FIRE PHOTON TORPEDOS" + elif [ "$PHASER" = "2" ] + then + INTENT="FIRE ALL PHASERS" + elif [ "$PHASER" = "3" ] + then + INTENT="EJECT THE WARPCORE" + elif [ "$PHASER" = "4" ] + then + INTENT="ENGAGE TRACTOR BEAM" + fi + + MESSAGE="Enter ${INTENT} at this prompt to confirm" + echo_need - read INPUT_INTENT + read INPUT_INTENT - if [ "${INPUT_INTENT}" != "${INTENT}" ] - then - MESSAGE="${TASKTYPE} Aborted" - echo_info - exit_nochange + if [ "${INPUT_INTENT}" != "${INTENT}" ] + then + MESSAGE="${TASKTYPE} Aborted" + echo_info + exit_nochange + fi + else + MESSAGE="Verification Bypassed" + echo_warn fi } From b18af38addb822c8e19449f115044ea16acc01a9 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 21:50:29 -0500 Subject: [PATCH 12/13] cleanup --- gravity-sync.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index cc0fc49c..e9cdf93a 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -284,7 +284,7 @@ function restore_gs { error_validate fi - MESSAGE="Evacuating Turbolifts" + MESSAGE="Evacuating Saucer Section" echo_info sleep 1 From a38afd0d3a52c01776af2c55ab3fcd40307526c8 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Thu, 28 May 2020 22:01:08 -0500 Subject: [PATCH 13/13] 1.6.0 --- README.md | 35 +++++++++++++++++++++++------------ gravity-sync.conf.example | 2 +- gravity-sync.sh | 14 +++++++------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fb6185bb..4ee53ff8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Gravity Sync ## Background -What is better than a [Pi-hole](https://github.com/pi-hole/pi-hole) blocking ads via DNS on your network? That's right, Two Pi-hole! But if you have more than one Pi-hole (PH) in your network you'll want a simple way to keep the list configurations identical between the two. +What is better than a [Pi-hole](https://github.com/pi-hole/pi-hole) blocking ads via DNS on your network? That's right, Two Pi-hole! But if you have more than one Pi-hole in your network you'll want a simple way to keep the list configurations identical between the two. That's Gravity Sync. @@ -13,7 +13,7 @@ Gravity Sync will **not** overwrite device specific settings such as local netwo ## Prerequisites Gravity Sync **requires** Pi-hole 5.0 or higher. -You will need to designate one Pi-Hole as primary and one as secondary. This is where you'll make all your configuration changes through the Web UI, doing things such as; manual whitelisting, adding blocklists, device/group management, and other list settings. Gravity Sync will pull the configuration of the primary PH to the secondary. It will also bring over the downloaded blocklist files after a `pihole -g` update on the primary, so you do not need to reach out to all your blocklist hosts for updates after syncing. +You will need to designate one Pi-Hole as primary and one as secondary. This is where you'll make all your configuration changes through the Web UI, doing things such as; manual whitelisting, adding blocklists, device/group management, and other list settings. Gravity Sync will pull the configuration of the primary Pi-hole to the secondary. It will also bring over the downloaded blocklist files after a `pihole -g` update on the primary, so you do not need to reach out to all your blocklist hosts for updates after syncing. The designation of primary and secondary is purely at your discretion and depends on your desired use case. @@ -44,9 +44,9 @@ Download the latest release from [GitHub](https://github.com/vmstan/gravity-sync ```bash cd ~ -wget https://github.com/vmstan/gravity-sync/archive/v1.5.0zip -unzip v1.5.0.zip -mv ~/gravity-sync-1.5.0 ~/gravity-sync +wget https://github.com/vmstan/gravity-sync/archive/v1.6.0zip +unzip v1.6.0.zip +mv ~/gravity-sync-1.6.0 ~/gravity-sync cd gravity-sync ``` @@ -95,7 +95,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 PH user and copy it to your primary PH. 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 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. *Note: If you already have this setup on your systems for other purposes, you can skip this step.* @@ -104,12 +104,12 @@ ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub REMOTE_USER@REMOTE_HOST ``` -Subsitute REMOTE_USER for the account on the primary PH with sudo permissions, and REMOTE_HOST for the IP or DNS name of the PH you have designated as the primary. +Subsitute REMOTE_USER for the account on the primary Pi-hole with sudo permissions, and REMOTE_HOST for the IP or DNS name of the Pi-hole you have designated as the primary. Make sure to leave the `REMOTE_PASS` variable set to nothing in `gravity-sync.conf` if you want to use key-pair authentication. #### Password Authentication -This is the non-preferred option, as it depends on an non-standard utility called `sshpass` which must be installed on your secondary PH. Install it using your package manager of choice. The example below is for Raspberry Pi OS (previously Raspbian) or Ubuntu. +This is the non-preferred option, as it depends on an non-standard utility called `sshpass` which must be installed on your secondary Pi-hole. Install it using your package manager of choice. The example below is for Raspberry Pi OS (previously Raspbian) or Ubuntu. ```bash sudo apt install sshpass @@ -132,7 +132,7 @@ Now, test Gravity Sync. You can run a comparison between primary and secondary d ./gravity-sync.sh compare ``` -Assuming Gravity Sync runs successfully, it will indicate if there are changes pending between the two databases. If not, make a subtle change to a whitelist/blacklist on your primary PH, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly. +Assuming Gravity Sync runs successfully, it will indicate if there are changes pending between the two databases. If not, make a subtle change to a whitelist/blacklist on your primary Pi-hole, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly. ### The Pull Function @@ -142,20 +142,31 @@ 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 PH 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 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. 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. You can check for successful pull attempts by running: `./gravity-sync.sh logs` ### The Push Function -Gravity Sync includes the ability to `push` from the secondary PH back to the primary. This would be useful in a situation where your primary PH is down for an extended period of time, and you have made list changes on the secondary PH that you want to force back to the primary, when it comes online. +Gravity Sync includes the ability to `push` from the secondary Pi-hole back to the primary. This would be useful in a situation where your primary Pi-hole is down for an extended period of time, and you have made list changes on the secondary Pi-hole that you want to force back to the primary, when it comes online. ```bash ./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 PH. +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. + +This function purposefuly asks for user interaction to avoid being accidentally automated. + +### The Restore Function +Graviy Sync can also `restore` the database on the secondary Pi-hole in the event you've overwritten it accidentally. This might happen in the above scenario where you've had your primary Pi-hole down for an extended period, made changes to the secondary, but perhaps didn't get a chance to perform a `push` of the changes back to the primary, before your automated sync ran. + +```bash +./gravity-sync.sh restore +``` + +This will copy your last `gravity.db.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.conf.example b/gravity-sync.conf.example index a48c0589..a56cdc65 100644 --- a/gravity-sync.conf.example +++ b/gravity-sync.conf.example @@ -26,4 +26,4 @@ REMOTE_PASS='' # LOG_PATH='' # SYNCING_LOG='' # CRONJOB_LOG='' -# VERIFY_PASS='0' \ No newline at end of file +# VERIFY_PASS='' # 0=Verify 1=Bypass \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index e9cdf93a..4f6de427 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -27,10 +27,10 @@ CRONJOB_LOG='gravity-sync.cron' # replace in gravity-sync.conf to overwrite # Interaction Customization VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite -# PH Folder/File Locations -PIHOLE_DIR='/etc/pihole' # default PH data directory -GRAVITY_FI='gravity.db' # default PH database file -PIHOLE_BIN='/usr/local/bin/pihole' # default PH binary directory +# Pi-hole Folder/File Locations +PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory +GRAVITY_FI='gravity.db' # default Pi-hole database file +PIHOLE_BIN='/usr/local/bin/pihole' # default Pi-hole binary directory # OS Settings BASH_PATH='/bin/bash' # default OS bash path @@ -393,7 +393,7 @@ function validate_gs_folders { fi } -## Validate PH Folders +## Validate Pi-hole Folders function validate_ph_folders { MESSAGE="Locating ${PIHOLE_DIR}" echo_stat @@ -670,8 +670,8 @@ function list_gs_arguments { echo -e " ${YELLOW}automate${NC} Add scheduled task to run sync" echo -e "" echo -e "Replication Options:" - echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary PH to this server" - echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH" + echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary Pi-hole to this server" + echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary Pi-hole" echo -e " ${YELLOW}restore${NC} Restore ${GRAVITY_FI} on this server from previous copy" echo -e " ${YELLOW}compare${NC} Just check for differences between primary and secondary" echo -e ""