From 9b60ea604aa080396bca6463263a86708878a6d5 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Fri, 22 May 2020 10:44:38 -0500 Subject: [PATCH] Function all the things --- gravity-sync.sh | 88 ++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/gravity-sync.sh b/gravity-sync.sh index 2611b31..9924e98 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -11,17 +11,17 @@ VERSION='1.2.0' # You must define REMOTE_HOST and REMOTE_USER in a file called 'gravity-sync.conf' -- see above documentation -# PUSH SETTINGS +# CUSTOMIZATION ############################## -# CUSTOMIZATION +# GS Folder/File Locations LOCAL_FOLDR='gravity-sync' # must exist in running user home folder SYNCING_LOG='gravity-sync.log' # will be created in above folder -# PIHOLE DEFAULTS +# PH Folder/File Locations PIHOLE_DIR='/etc/pihole' # default install directory GRAVITY_FI='gravity.db' # this should not change -# SCRIPT COLORS +# Script Colors RED='\033[0;91m' GREEN='\033[0;92m' CYAN='\033[0;96m' @@ -32,6 +32,7 @@ NC='\033[0m' ############################################## # IMPORT SETTINGS + echo -e "${CYAN}Importing gravity-sync.conf settings${NC}" if [ -f ~/${LOCAL_FOLDR}/gravity-sync.conf ] then @@ -43,15 +44,17 @@ else exit fi -############################################## +# FUNCTION DEFINITIONS ####################### -# FUNCTIONS +# Update Function function update_gs { git reset --hard git pull } +# Pull Function + function pull_gs { echo -e "${CYAN}Copying ${GRAVITY_FI} from remote server ${REMOTE_HOST}${NC}" rsync -v --progress -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI} @@ -71,6 +74,8 @@ function pull_gs { echo -e "${GREEN}gravity.db pull completed${NC}" } +# Push Function + function push_gs { echo -e "${YELLOW}WARNING: DATA LOSS IS POSSIBLE${NC}" echo -e "This will send the running ${GRAVITY_FI} from this server to your primary Pihole" @@ -100,30 +105,50 @@ function push_gs { done } -############################################## +function logs_gs{ + echo -e "These are the last three valid PULL timestamps" + tail -n 3 ${SYNCING_LOG} +} -# print title -# echo -e "${GREEN}Gravity Sync ${VERSION}${NC}" +# Validate Functions + +## Validate GS Folders + +function validate_gs_folders { + if [ -d ~/${LOCAL_FOLDR} ] + then + echo -e "${GREEN}Success${NC}: Required directory ~/${LOCAL_FOLDR} is present" + else + echo -e "${RED}Failure${NC}: Required directory ~/${LOCAL_FOLDR} is missing" + exit + fi +} + +## Validate PH Folders + +function validate_ph_folders { + if [ -d ${PIHOLE_DIR} ] + then + echo -e "${GREEN}Success${NC}: Required directory ${PIHOLE_DIR} is present" + else + echo -e "${RED}Failure${NC}: Required directory ${PIHOLE_DIR} is missing" + exit + fi +} + +function validate_gs_arguments { + echo "Usage: $0 {pull|push}" + echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on $REMOTE_HOST to this server" + echo -e "> ${YELLOW}Push${NC} will force any changes made on this server to the primary" + echo -e "No changes have been made to the system" + exit 1 +} + +# SCRIPT EXECUTION ########################### echo -e "${CYAN}Validating sync folder configuration${NC}" - -# check to see if logging/backup directory is available -if [ -d ~/${LOCAL_FOLDR} ] -then - echo -e "${GREEN}Success${NC}: Required directory ~/${LOCAL_FOLDR} is present" -else - echo -e "${RED}Failure${NC}: Required directory ~/${LOCAL_FOLDR} is missing" - exit -fi - -# check to see if current pihole directory is correct -if [ -d ${PIHOLE_DIR} ] -then - echo -e "${GREEN}Success${NC}: Required directory ${PIHOLE_DIR} is present" -else - echo -e "${RED}Failure${NC}: Required directory ${PIHOLE_DIR} is missing" - exit -fi + validate_gs_folders + validate_ph_folders echo -e "${CYAN}Evaluating $0 script arguments${NC}" @@ -131,11 +156,7 @@ case $# in 0) echo -e "${RED}Failure${NC}: ${GRAVITY_FI} replication direction required" - echo "Usage: $0 {pull|push}" - echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on $REMOTE_HOST to this server" - echo -e "> ${YELLOW}Push${NC} will force any changes made on this server to the primary" - echo -e "No changes have been made to the system" - exit 1 + validate_gs_arguments ;; 1) @@ -166,8 +187,7 @@ case $# in logs) echo -e "${GREEN}Success:${NC} Logs Requested" - echo -e "These are the last three valid PULL timestamps" - tail -n 3 ${SYNCING_LOG} + logs_gs ;; *)