gravity-sync/gravity-sync.sh

728 lines
19 KiB
Bash
Raw Normal View History

2020-05-21 18:00:42 +00:00
#!/bin/bash
2020-05-22 17:18:16 +00:00
# GRAVITY SYNC BY VMSTAN #####################
2020-05-23 23:37:08 +00:00
PROGRAM='Gravity Sync'
2020-05-27 20:50:47 +00:00
VERSION='1.4.3'
2020-05-21 18:00:42 +00:00
2020-05-27 03:20:36 +00:00
# Execute from the home folder of the user who own's it (ex: 'cd ~/gravity-sync')
# For documentation or download updates visit https://github.com/vmstan/gravity-sync
2020-05-21 18:00:42 +00:00
2020-05-22 17:18:16 +00:00
# REQUIRED SETTINGS ##########################
2020-05-21 18:00:42 +00:00
2020-05-27 03:20:36 +00:00
# Run './gravity-sync.sh config' to get started
2020-05-22 17:18:16 +00:00
2020-05-22 17:25:30 +00:00
# STANDARD VARIABLES #########################
2020-05-21 18:00:42 +00:00
2020-05-22 15:44:38 +00:00
# GS Folder/File Locations
2020-05-27 03:20:36 +00:00
LOCAL_FOLDR='gravity-sync' # must exist in running user home folder
2020-05-27 15:00:30 +00:00
CONFIG_FILE='gravity-sync.conf' # must exist with primary host/user configured
2020-05-27 03:20:36 +00:00
BACKUP_FOLD='backup' # must exist as subdirectory in LOCAL_FOLDR
2020-05-21 18:00:42 +00:00
2020-05-27 15:00:30 +00:00
# Logging Folder/File Locations
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
2020-05-22 15:44:38 +00:00
# PH Folder/File Locations
2020-05-27 03:20:36 +00:00
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
2020-05-21 18:00:42 +00:00
2020-05-27 03:20:36 +00:00
# SSH CONFIGURATION ##########################
# Suggested not to replace these values here
# Add replacement variables to gravity-sync.conf
SSH_PORT='22' # default SSH port
2020-05-27 21:09:41 +00:00
SSH_PKIF='.ssh/id_rsa' # default local SSH key
2020-05-24 18:39:21 +00:00
##############################################
### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
##############################################
2020-05-22 15:44:38 +00:00
# Script Colors
2020-05-21 19:09:55 +00:00
RED='\033[0;91m'
GREEN='\033[0;92m'
CYAN='\033[0;96m'
2020-05-21 19:10:49 +00:00
YELLOW='\033[0;93m'
PURPLE='\033[0;95m'
2020-05-26 19:39:55 +00:00
BLUE='\033[0;94m'
2020-05-21 18:00:42 +00:00
NC='\033[0m'
2020-05-23 22:58:06 +00:00
# Message Codes
FAIL="[${RED}FAIL${NC}]"
WARN="[${PURPLE}WARN${NC}]"
2020-05-25 15:25:54 +00:00
GOOD="[${GREEN}DONE${NC}]"
2020-05-24 18:39:21 +00:00
STAT="[${CYAN}EXEC${NC}]"
2020-05-23 23:37:08 +00:00
INFO="[${YELLOW}INFO${NC}]"
2020-05-26 19:39:55 +00:00
NEED="[${BLUE}NEED${NC}]"
2020-05-23 22:58:06 +00:00
2020-05-22 15:46:24 +00:00
# FUNCTION DEFINITIONS #######################
2020-05-22 15:44:38 +00:00
2020-05-22 15:46:24 +00:00
# Import Settings
function import_gs {
2020-05-25 00:38:43 +00:00
MESSAGE="Importing ${CONFIG_FILE} Settings"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} $MESSAGE"
2020-05-23 03:35:46 +00:00
if [ -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} ]
2020-05-22 15:46:24 +00:00
then
2020-05-23 04:00:59 +00:00
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
2020-05-25 00:40:09 +00:00
error_validate
2020-05-25 00:38:43 +00:00
MESSAGE="Using ${REMOTE_USER}@${REMOTE_HOST}"
echo -e "${INFO} ${MESSAGE}"
2020-05-22 15:46:24 +00:00
else
2020-05-26 02:18:31 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-25 00:56:55 +00:00
MESSAGE="${CONFIG_FILE} Missing"
echo -e "${INFO} ${MESSAGE}"
2020-05-27 17:06:39 +00:00
TASKTYPE='CONFIG'
config_generate
2020-05-27 17:09:50 +00:00
# echo -e "Please run ${YELLOW}$#${NC} again."
2020-05-22 15:46:24 +00:00
fi
}
2020-05-22 15:28:02 +00:00
2020-05-27 03:20:36 +00:00
# GS Update Functions
## Master Branch
2020-05-22 15:28:02 +00:00
function update_gs {
TASKTYPE='UPDATE'
2020-05-27 04:02:41 +00:00
# logs_export # dumps log prior to execution because script stops after successful pull
2020-05-25 00:38:43 +00:00
MESSAGE="Requires GitHub Installation"
2020-05-25 00:56:55 +00:00
echo -e "${INFO} ${MESSAGE}"
2020-05-22 16:06:59 +00:00
git reset --hard
git pull
exit
2020-05-22 15:28:02 +00:00
}
2020-05-27 03:20:36 +00:00
## Developer Branch
2020-05-25 01:23:19 +00:00
function beta_gs {
2020-05-25 01:29:32 +00:00
TASKTYPE='BETA'
2020-05-27 04:02:41 +00:00
# logs_export # dumps log prior to execution because script stops after successful pull
2020-05-25 01:23:19 +00:00
MESSAGE="Requires GitHub Installation"
echo -e "${INFO} ${MESSAGE}"
git reset --hard
git pull
git checkout origin/development
exit
}
2020-05-27 03:20:36 +00:00
# Gravity Core Functions
## Pull Function
2020-05-22 15:32:02 +00:00
function pull_gs {
2020-05-22 16:45:33 +00:00
TASKTYPE='PULL'
2020-05-23 22:54:24 +00:00
2020-05-25 00:38:43 +00:00
echo -e "${INFO} ${TASKTYPE} Requested"
2020-05-25 01:54:02 +00:00
md5_compare
2020-05-25 00:38:43 +00:00
2020-05-26 16:32:30 +00:00
echo -e "${INFO} ${TASKTYPE} Commencing"
2020-05-23 23:26:12 +00:00
MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-26 17:30:00 +00:00
cp ${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup >/dev/null 2>&1
2020-05-23 23:26:12 +00:00
error_validate
MESSAGE="Pulling ${GRAVITY_FI} from ${REMOTE_HOST}"
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${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}"
2020-05-26 17:30:00 +00:00
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
2020-05-23 23:26:12 +00:00
error_validate
2020-05-26 15:32:52 +00:00
MESSAGE="Validating Ownership on ${GRAVITY_FI}"
2020-05-26 16:32:30 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-26 15:43:56 +00:00
GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}')
2020-05-26 15:51:26 +00:00
if [ $GRAVDB_OWN == "piholepihole" ]
2020-05-26 15:32:52 +00:00
then
echo -e "\r${GOOD} ${MESSAGE}"
else
2020-05-26 16:32:30 +00:00
echo -e "\r${FAIL} $MESSAGE"
MESSAGE2="Attempting to Compensate"
echo -e "${INFO} ${MESSAGE2}"
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
2020-05-26 15:32:52 +00:00
fi
2020-05-26 16:32:30 +00:00
MESSAGE="Validating Permissions on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
2020-05-26 16:52:28 +00:00
GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${GRAVITY_FI} | grep -v f: | grep ${GRAVITY_FI} | awk '{print $1}')
2020-05-27 03:44:30 +00:00
if [ $GRAVDB_RWE = "-rw-rw-r--" ]
2020-05-26 16:32:30 +00:00
then
2020-05-26 16:43:12 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
else
2020-05-26 16:47:04 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-26 16:43:12 +00:00
2020-05-26 16:32:30 +00:00
MESSAGE2="Attempting to Compensate"
2020-05-26 16:47:04 +00:00
echo -e "${INFO} ${MESSAGE2}"
2020-05-26 16:43:12 +00:00
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
2020-05-27 03:44:30 +00:00
sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
2020-05-26 16:32:30 +00:00
error_validate
fi
MESSAGE="Inverting Tachyon Pulse"
2020-05-26 04:58:26 +00:00
echo -e "${INFO} ${MESSAGE}"
2020-05-26 17:32:11 +00:00
sleep 1
2020-05-23 23:26:12 +00:00
2020-05-25 00:59:03 +00:00
MESSAGE="Updating FTLDNS Configuration"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-26 05:09:45 +00:00
${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1
2020-05-25 00:59:03 +00:00
error_validate
MESSAGE="Reloading FTLDNS Services"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-26 05:09:45 +00:00
${PIHOLE_BIN} restartdns >/dev/null 2>&1
2020-05-23 23:26:12 +00:00
error_validate
logs_export
2020-05-22 18:59:09 +00:00
exit_withchange
2020-05-22 15:32:02 +00:00
}
2020-05-22 15:28:02 +00:00
2020-05-27 03:20:36 +00:00
## Push Function
2020-05-22 15:32:02 +00:00
function push_gs {
TASKTYPE='PUSH'
2020-05-25 01:54:02 +00:00
echo -e "${INFO} ${TASKTYPE} Requested"
md5_compare
2020-05-25 17:06:37 +00:00
echo -e "${WARN} Are you sure you want to overwrite the primary PH configuration on ${REMOTE_HOST}?"
2020-05-22 15:32:02 +00:00
select yn in "Yes" "No"; do
case $yn in
Yes )
2020-05-23 23:50:29 +00:00
MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${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
2020-05-23 23:50:29 +00:00
error_validate
MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${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
2020-05-23 23:50:29 +00:00
error_validate
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
2020-05-23 23:50:29 +00:00
error_validate
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${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
2020-05-23 23:50:29 +00:00
error_validate
2020-05-26 17:32:11 +00:00
MESSAGE="Contacting Borg Collective"
2020-05-26 04:58:26 +00:00
echo -e "${INFO} ${MESSAGE}"
2020-05-26 17:32:11 +00:00
sleep 1
2020-05-25 01:54:02 +00:00
MESSAGE="Updating FTLDNS Configuration"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1
2020-05-25 01:54:02 +00:00
error_validate
MESSAGE="Reloading FTLDNS Services"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1
2020-05-23 23:50:29 +00:00
error_validate
logs_export
2020-05-22 18:59:09 +00:00
exit_withchange
2020-05-22 15:32:02 +00:00
;;
No )
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:32:02 +00:00
;;
esac
done
}
2020-05-22 15:28:02 +00:00
2020-05-22 16:39:31 +00:00
# Logging Functions
2020-05-27 03:20:36 +00:00
## Core Logging
### Write Logs Out
function logs_export {
echo -e "${INFO} Logging Timestamps to ${SYNCING_LOG}"
2020-05-27 15:00:30 +00:00
echo -e $(date) "[${TASKTYPE}]" >> ${LOG_PATH}/${SYNCING_LOG}
2020-05-27 03:20:36 +00:00
}
### Output Sync Logs
2020-05-22 15:47:39 +00:00
function logs_gs {
2020-05-27 16:09:07 +00:00
import_gs
2020-05-27 16:13:04 +00:00
echo -e "${INFO} Tailing ${LOG_PATH}/${SYNCING_LOG}"
2020-05-25 02:15:08 +00:00
echo -e "========================================================"
echo -e "Recent Complete ${YELLOW}PULL${NC} Executions"
2020-05-27 16:06:35 +00:00
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PULL
2020-05-25 02:15:08 +00:00
echo -e "Recent Complete ${YELLOW}PUSH${NC} Executions"
2020-05-27 16:06:35 +00:00
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PUSH
2020-05-25 02:15:08 +00:00
echo -e "========================================================"
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:44:38 +00:00
}
2020-05-27 03:20:36 +00:00
## Crontab Logs
### Core Crontab Logs
2020-05-26 02:16:31 +00:00
function show_crontab {
2020-05-27 15:00:30 +00:00
import_gs
2020-05-26 02:16:31 +00:00
MESSAGE="Replaying Last Cronjob"
2020-05-26 04:58:26 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-26 02:16:31 +00:00
2020-05-27 15:00:30 +00:00
if [ -f ${LOG_PATH}/${CRONJOB_LOG} ]
2020-05-26 02:16:31 +00:00
then
2020-05-27 15:00:30 +00:00
if [ -s ${LOG_PATH}/${CRONJOB_LOG} ]
2020-05-26 04:58:26 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-27 15:18:58 +00:00
2020-05-27 16:20:29 +00:00
echo -e "${INFO} Tailing ${LOG_PATH}/${CRONJOB_LOG}"
2020-05-27 15:18:58 +00:00
echo -e "========================================================"
2020-05-27 15:20:14 +00:00
date -r ${LOG_PATH}/${CRONJOB_LOG}
2020-05-27 15:18:58 +00:00
cat ${LOG_PATH}/${CRONJOB_LOG}
echo -e "========================================================"
2020-05-26 02:16:31 +00:00
exit_nochange
then
2020-05-26 04:58:26 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-27 15:00:30 +00:00
echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} appears empty"
2020-05-26 02:16:31 +00:00
exit_nochange
fi
else
2020-05-26 04:58:26 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-27 15:00:30 +00:00
echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} cannot be located"
2020-05-26 02:16:31 +00:00
exit_nochange
fi
}
2020-05-22 16:39:31 +00:00
# Validate Functions
2020-05-22 15:44:38 +00:00
## Validate GS Folders
function validate_gs_folders {
2020-05-25 00:38:43 +00:00
MESSAGE="Locating $HOME/${LOCAL_FOLDR}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
if [ -d $HOME/${LOCAL_FOLDR} ]
then
2020-05-26 02:18:31 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
else
2020-05-26 02:18:31 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
exit_nochange
fi
2020-05-22 20:44:15 +00:00
2020-05-25 00:38:43 +00:00
MESSAGE="Locating $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
if [ -d $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD} ]
then
2020-05-26 02:18:31 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
else
2020-05-26 02:18:31 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
exit_nochange
fi
2020-05-22 15:44:38 +00:00
}
2020-05-22 15:44:38 +00:00
## Validate PH Folders
function validate_ph_folders {
2020-05-25 00:38:43 +00:00
MESSAGE="Locating ${PIHOLE_DIR}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
if [ -d ${PIHOLE_DIR} ]
then
2020-05-26 02:18:31 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
else
2020-05-26 02:18:31 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
exit_nochange
fi
2020-05-22 15:44:38 +00:00
}
2020-05-25 02:15:08 +00:00
## Validate SSHPASS
function validate_os_sshpass {
2020-05-27 03:20:36 +00:00
MESSAGE="Checking SSH Configuration"
echo -e "${INFO} ${MESSAGE}"
2020-05-25 19:31:46 +00:00
if hash sshpass 2>/dev/null
2020-05-25 02:15:08 +00:00
then
2020-05-25 02:25:07 +00:00
if test -z "$REMOTE_PASS"
2020-05-25 02:15:08 +00:00
then
2020-05-25 17:06:37 +00:00
SSHPASSWORD=''
2020-05-25 02:29:23 +00:00
MESSAGE="Using SSH Key-Pair Authentication"
2020-05-25 02:21:22 +00:00
else
2020-05-27 21:02:44 +00:00
timeout 5 ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1
2020-05-25 17:06:37 +00:00
if [ "$?" != "0" ]; then
2020-05-25 19:03:15 +00:00
SSHPASSWORD="sshpass -p ${REMOTE_PASS}"
2020-05-25 17:06:37 +00:00
MESSAGE="Using SSH Password Authentication"
else
2020-05-25 19:07:38 +00:00
SSHPASSWORD=''
2020-05-25 17:06:37 +00:00
MESSAGE="Using SSH Key-Pair Authentication"
fi
2020-05-25 02:15:08 +00:00
fi
else
2020-05-25 19:07:38 +00:00
SSHPASSWORD=''
2020-05-25 02:29:23 +00:00
MESSAGE="Using SSH Key-Pair Authentication"
2020-05-25 02:15:08 +00:00
fi
2020-05-25 02:29:23 +00:00
2020-05-25 19:19:56 +00:00
echo -e "${INFO} ${MESSAGE}"
2020-05-25 18:18:23 +00:00
MESSAGE="Testing SSH Connection"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
timeout 5 ${SSHPASSWORD} ssh -p ${SSH_PORT} -i '$HOME/${SSH_PKIF}' -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1
2020-05-25 19:19:56 +00:00
error_validate
2020-05-25 18:18:23 +00:00
2020-05-25 02:15:08 +00:00
}
2020-05-27 03:20:36 +00:00
## Error Validation
2020-05-23 23:26:12 +00:00
function error_validate {
2020-05-23 22:39:02 +00:00
if [ "$?" != "0" ]; then
2020-05-26 02:18:31 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-23 22:39:02 +00:00
exit 1
2020-05-23 23:10:18 +00:00
else
2020-05-26 02:18:31 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-23 22:39:02 +00:00
fi
}
2020-05-27 03:20:36 +00:00
## Validate Sync Required
2020-05-25 01:19:40 +00:00
function md5_compare {
2020-05-25 01:54:02 +00:00
echo -e "${INFO} Comparing ${GRAVITY_FI} Changes"
2020-05-25 01:48:18 +00:00
MESSAGE="Analyzing Remote ${GRAVITY_FI}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-27 21:02:44 +00:00
primaryMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}")
2020-05-25 01:48:18 +00:00
error_validate
MESSAGE="Analyzing Local ${GRAVITY_FI}"
2020-05-26 02:16:31 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-25 01:19:40 +00:00
secondMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI})
2020-05-25 01:48:18 +00:00
error_validate
2020-05-25 01:19:40 +00:00
if [ "$primaryMD5" == "$secondMD5" ]
then
2020-05-25 01:48:18 +00:00
echo -e "${INFO} No Changes in ${GRAVITY_FI}"
exit_nochange
2020-05-25 01:19:40 +00:00
else
2020-05-25 01:48:18 +00:00
echo -e "${INFO} Changes Detected in ${GRAVITY_FI}"
2020-05-25 01:19:40 +00:00
fi
}
2020-05-27 03:20:36 +00:00
# Configuration Management
## Generate New Configuration
2020-05-27 02:21:02 +00:00
function config_generate {
MESSAGE="Creating ${CONFIG_FILE} from Template"
echo -en "${STAT} ${MESSAGE}"
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 -e "${NEED} ${MESSAGE}"
read INPUT_REMOTE_HOST
MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server"
echo -e "${NEED} ${MESSAGE}"
read INPUT_REMOTE_USER
MESSAGE="Saving Host to ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
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}"
sed -i "/REMOTE_USER='pi'/c\REMOTE_USER='${INPUT_REMOTE_USER}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
if hash sshpass 2>/dev/null
then
MESSAGE="SSHPASS Utility Detected"
echo -e "${INFO} ${MESSAGE}"
2020-05-27 04:02:41 +00:00
MESSAGE="Do you want to configure password based SSH authentication?"
2020-05-27 02:21:02 +00:00
echo -e "${WARN} ${MESSAGE}"
2020-05-27 04:02:41 +00:00
MESSAGE="Your password will be stored clear-text in the ${CONFIG_FILE}!"
2020-05-27 02:21:02 +00:00
echo -e "${WARN} ${MESSAGE}"
2020-05-27 04:02:41 +00:00
MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication:"
2020-05-27 02:21:02 +00:00
echo -e "${NEED} ${MESSAGE}"
read INPUT_REMOTE_PASS
MESSAGE="Saving Password to ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
sed -i "/REMOTE_PASS=''/c\REMOTE_PASS='${INPUT_REMOTE_PASS}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
else
MESSAGE="SSHPASS Not Installed"
echo -e "${INFO} ${MESSAGE}"
MESSAGE="Defaulting to SSH Key-Pair Authentication"
echo -e "${INFO} ${MESSAGE}"
fi
2020-05-27 02:26:54 +00:00
if [ -z $INPUT_REMOTE_PASS ]
2020-05-27 02:21:02 +00:00
then
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Using Existing ~/${SSH_PKIF}"
echo -e "${INFO} ${MESSAGE}"
else
MESSAGE="Generating ~/${SSH_PKIF}"
echo -e "${INFO} ${MESSAGE}"
2020-05-27 02:33:00 +00:00
MESSAGE="Accept All Defaults"
2020-05-27 02:21:02 +00:00
echo -e "${WARN} ${MESSAGE}"
MESSAGE="Complete Key-Pair Creation"
echo -e "${NEED} ${MESSAGE}"
echo -e "========================================================"
echo -e "========================================================"
ssh-keygen -t rsa
echo -e "========================================================"
echo -e "========================================================"
fi
fi
MESSAGE="Importing New ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
2020-05-27 02:26:54 +00:00
if [ -z $REMOTE_PASS ]
2020-05-27 02:21:02 +00:00
then
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Registering Key-Pair on ${REMOTE_HOST}"
echo -e "${INFO} ${MESSAGE}"
2020-05-27 03:20:36 +00:00
MESSAGE="Enter ${REMOTE_USER}@${REMOTE_HOST} Password"
2020-05-27 02:21:02 +00:00
echo -e "${NEED} ${MESSAGE}"
echo -e "========================================================"
echo -e "========================================================"
2020-05-27 21:09:41 +00:00
ssh-copy-id -f -i $HOME/${SSH_PKIF}.pub ${REMOTE_USER}@${REMOTE_HOST}
2020-05-27 02:21:02 +00:00
echo -e "========================================================"
echo -e "========================================================"
2020-05-27 02:33:00 +00:00
else
2020-05-27 02:21:02 +00:00
MESSAGE="Error Creating Key-Pair"
echo -e "${FAIL} ${MESSAGE}"
2020-05-27 02:33:00 +00:00
fi
2020-05-27 02:21:02 +00:00
fi
validate_os_sshpass
exit_withchange
}
2020-05-27 03:20:36 +00:00
## Delete Existing Configuration
2020-05-27 02:21:02 +00:00
function config_delete {
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
MESSAGE="Configuration File Exists"
echo -e "${WARN} ${MESSAGE}"
echo -e "========================================================"
echo -e "========================================================"
echo -e ""
cat $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
echo -e ""
echo -e "========================================================"
echo -e "========================================================"
MESSAGE="Are you sure you want to erase this configuration?"
echo -e "${WARN} ${MESSAGE}"
select yn in "Yes" "No"; do
case $yn in
Yes )
MESSAGE="Erasing Existing Configuration"
echo -en "${STAT} ${MESSAGE}"
rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
config_generate
;;
No )
exit_nochange
;;
esac
done
}
2020-05-27 03:20:36 +00:00
# Exit Codes
## No Changes Made
function exit_nochange {
echo -e "${INFO} ${PROGRAM} ${TASKTYPE} Aborting"
exit 0
}
## Changes Made
function exit_withchange {
SCRIPT_END=$SECONDS
echo -e "${INFO} ${PROGRAM} ${TASKTYPE} Completed in $((SCRIPT_END-SCRIPT_START)) seconds"
exit 0
}
## List GS Arguments
function list_gs_arguments {
echo -e "Usage: $0 [options]"
echo -e "Example: '$0 pull'"
echo -e ""
2020-05-27 04:02:41 +00:00
echo -e "Setup Options:"
echo -e " ${YELLOW}config${NC} Create a new ${CONFIG_FILE} file"
echo -e ""
2020-05-27 03:20:36 +00:00
echo -e "Replication Options:"
2020-05-27 04:02:41 +00:00
echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary PH to this server"
2020-05-27 03:20:36 +00:00
echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH"
2020-05-27 04:02:41 +00:00
echo -e " ${YELLOW}compare${NC} Just check for differences between primary and secondary"
2020-05-27 03:20:36 +00:00
echo -e ""
echo -e "Update Options:"
2020-05-27 04:02:41 +00:00
echo -e " ${YELLOW}update${NC} Use GitHub to update this script to the latest version"
echo -e " ${YELLOW}beta${NC} Use GitHub to update this script to the latest beta version"
2020-05-27 03:20:36 +00:00
echo -e ""
echo -e "Debug Options:"
2020-05-27 04:02:41 +00:00
echo -e " ${YELLOW}version${NC} Display your version of ${PROGRAM}"
echo -e " ${YELLOW}logs${NC} Show recent successful replication jobs"
2020-05-27 03:20:36 +00:00
echo -e " ${YELLOW}cron${NC} Display output of last crontab execution"
echo -e ""
exit_nochange
}
# Output Version
function show_version {
echo -e "${INFO} ${PROGRAM} ${VERSION}"
}
2020-05-22 15:44:38 +00:00
# SCRIPT EXECUTION ###########################
2020-05-26 02:35:25 +00:00
SCRIPT_START=$SECONDS
2020-05-24 18:39:21 +00:00
MESSAGE="Evaluating Script Arguments"
2020-05-26 01:53:32 +00:00
echo -en "${STAT} ${MESSAGE}"
2020-05-21 19:15:24 +00:00
2020-05-21 18:00:42 +00:00
case $# in
2020-05-22 01:30:20 +00:00
2020-05-22 02:42:37 +00:00
0)
2020-05-26 02:05:28 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-22 18:59:09 +00:00
list_gs_arguments
2020-05-22 01:30:20 +00:00
;;
2020-05-22 01:39:14 +00:00
2020-05-22 02:42:37 +00:00
1)
case $1 in
pull)
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-24 18:39:21 +00:00
2020-05-25 00:38:43 +00:00
import_gs
MESSAGE="Validating Folder Configuration"
echo -e "${INFO} ${MESSAGE}"
validate_gs_folders
validate_ph_folders
2020-05-25 02:51:04 +00:00
validate_os_sshpass
pull_gs
2020-05-22 02:42:37 +00:00
exit
;;
push)
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 00:38:43 +00:00
import_gs
2020-05-25 00:38:43 +00:00
echo -e "${INFO} Validating Folder Configuration"
validate_gs_folders
validate_ph_folders
2020-05-25 02:51:04 +00:00
validate_os_sshpass
push_gs
2020-05-22 02:42:37 +00:00
exit
;;
version)
2020-05-25 01:06:11 +00:00
TASKTYPE='VERSION'
2020-05-24 22:33:42 +00:00
show_version
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 02:42:37 +00:00
;;
update)
2020-05-25 01:29:32 +00:00
# TASKTYPE='UPDATE'
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 01:48:18 +00:00
echo -e "${INFO} Update Requested"
2020-05-22 15:28:02 +00:00
update_gs
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 02:42:37 +00:00
;;
2020-05-25 01:23:19 +00:00
beta)
2020-05-25 01:29:32 +00:00
# TASKTYPE='BETA'
2020-05-26 02:16:31 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 01:48:18 +00:00
echo -e "${INFO} Beta Update Requested"
2020-05-25 01:23:19 +00:00
beta_gs
exit_nochange
;;
2020-05-22 02:07:01 +00:00
2020-05-22 02:42:37 +00:00
logs)
2020-05-25 01:06:11 +00:00
TASKTYPE='LOGS'
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-24 18:39:21 +00:00
MESSAGE="Logs Requested"
2020-05-26 02:05:28 +00:00
echo -e "${INFO} ${MESSAGE}"
2020-05-22 15:44:38 +00:00
logs_gs
2020-05-22 02:42:37 +00:00
;;
2020-05-24 18:39:21 +00:00
2020-05-25 01:19:40 +00:00
compare)
TASKTYPE='COMPARE'
2020-05-25 01:39:06 +00:00
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 01:48:18 +00:00
import_gs
2020-05-25 02:51:04 +00:00
echo -e "${INFO} Validating Folder Configuration"
validate_gs_folders
validate_ph_folders
validate_os_sshpass
2020-05-25 02:53:20 +00:00
md5_compare
2020-05-25 01:20:52 +00:00
;;
2020-05-25 01:19:40 +00:00
2020-05-24 18:39:21 +00:00
cron)
2020-05-25 01:06:11 +00:00
TASKTYPE='CRON'
2020-05-26 02:05:28 +00:00
echo -e "\r${GOOD} ${MESSAGE}"
2020-05-25 00:45:05 +00:00
2020-05-26 02:16:31 +00:00
show_crontab
2020-05-24 18:39:21 +00:00
;;
2020-05-26 19:01:01 +00:00
config)
TASKTYPE='CONFIG'
echo -e "\r${GOOD} ${MESSAGE}"
echo -e "${INFO} Entering ${TASKTYPE} Mode"
if [ -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} ]
then
2020-05-27 02:21:02 +00:00
config_delete
2020-05-26 19:01:01 +00:00
else
MESSAGE="${CONFIG_FILE} Missing"
echo -e "${INFO} ${MESSAGE}"
2020-05-26 19:05:28 +00:00
2020-05-27 02:21:02 +00:00
config_generate
2020-05-26 19:01:01 +00:00
fi
;;
2020-05-22 02:42:37 +00:00
*)
2020-05-26 02:05:28 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-22 18:31:42 +00:00
list_gs_arguments
2020-05-24 18:39:21 +00:00
exit_nochange
2020-05-22 02:42:37 +00:00
;;
esac
2020-05-22 02:08:31 +00:00
;;
2020-05-22 16:39:31 +00:00
2020-05-21 18:00:42 +00:00
*)
2020-05-26 02:05:28 +00:00
echo -e "\r${FAIL} ${MESSAGE}"
2020-05-24 18:39:21 +00:00
list_gs_arguments
exit_nochange
2020-05-22 16:39:31 +00:00
;;
2020-05-21 18:00:42 +00:00
esac