gravity-sync/gravity-sync.sh

1354 lines
31 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-06-01 15:00:01 +00:00
VERSION='1.7.6'
2020-05-21 18:00:42 +00:00
2020-05-28 01:51:21 +00:00
# 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
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-28 18:05:22 +00:00
GS_FILENAME='gravity-sync.sh' # must exist because it's this script
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-29 02:43:58 +00:00
# Interaction Customization
VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite
2020-05-29 19:43:36 +00:00
SKIP_CUSTOM='0' # replace in gravity-sync.conf to overwrite
DATE_OUTPUT='0' # replace in gravity-sync.conf to overwrite
2020-05-29 02:43:58 +00:00
2020-05-29 03:01:08 +00:00
# Pi-hole Folder/File Locations
PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory
GRAVITY_FI='gravity.db' # default Pi-hole database file
2020-05-29 15:58:06 +00:00
CUSTOM_DNS='custom.list' # default Pi-hole local DNS lookups
2020-05-29 03:01:08 +00:00
PIHOLE_BIN='/usr/local/bin/pihole' # default Pi-hole binary directory
2020-05-21 18:00:42 +00:00
2020-05-28 15:54:32 +00:00
# OS Settings
BASH_PATH='/bin/bash' # default OS bash path
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-30 02:09:37 +00:00
BOLD='\033[1m'
2020-05-21 18:00:42 +00:00
NC='\033[0m'
2020-05-23 22:58:06 +00:00
# Message Codes
2020-05-31 02:58:25 +00:00
FAIL="[ ${RED}FAIL${NC} ]"
WARN="[ ${PURPLE}WARN${NC} ]"
GOOD="[ ${GREEN} OK ${NC} ]"
STAT="[ ${CYAN}EXEC${NC} ]"
INFO="[ ${YELLOW}INFO${NC} ]"
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-31 02:23:02 +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
2020-05-31 02:01:24 +00:00
MESSAGE="Targeting ${REMOTE_USER}@${REMOTE_HOST}"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-31 01:40:33 +00:00
2020-05-31 01:39:28 +00:00
detect_ssh
2020-05-22 15:46:24 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-25 00:56:55 +00:00
MESSAGE="${CONFIG_FILE} Missing"
2020-05-28 21:21:10 +00:00
echo_info
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
2020-05-31 01:20:06 +00:00
if [ -f "$HOME/${LOCAL_FOLDR}/dev" ]
then
BRANCH='development'
else
BRANCH='master'
fi
2020-05-31 03:37:06 +00:00
GIT_CHECK=$(git status | awk '{print $1}')
2020-05-31 03:43:59 +00:00
if [ "$GIT_CHECK" == "fatal:" ]
2020-05-31 03:37:06 +00:00
then
MESSAGE="Requires GitHub Installation"
echo_warn
exit_nochange
else
2020-05-31 03:44:30 +00:00
# MESSAGE="This might break..."
2020-05-31 03:46:22 +00:00
# echo_warn
2020-05-31 03:42:37 +00:00
MESSAGE="Updating Cache"
echo_stat
git fetch --all >/dev/null 2>&1
error_validate
2020-05-31 03:42:57 +00:00
MESSAGE="Applying Update"
echo_stat
2020-05-31 03:42:37 +00:00
git reset --hard origin/${BRANCH} >/dev/null 2>&1
error_validate
2020-05-31 03:37:06 +00:00
fi
2020-05-31 03:47:03 +00:00
exit_withchange
2020-05-22 15:28:02 +00:00
}
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-25 01:54:02 +00:00
md5_compare
2020-05-25 00:38:43 +00:00
2020-05-23 23:26:12 +00:00
MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME"
2020-05-28 21:50:25 +00:00
echo_stat
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}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync -e "${SSH_CMD} -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"
2020-05-28 21:50:25 +00:00
echo_stat
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-28 21:50:25 +00:00
echo_stat
2020-05-26 16:32:30 +00:00
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
2020-05-28 21:50:25 +00:00
echo_good
2020-05-26 15:32:52 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-26 16:32:30 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="Attempting to Compensate"
echo_info
2020-05-26 16:32:30 +00:00
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-26 16:32:30 +00:00
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}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-26 16:32:30 +00:00
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-28 21:50:25 +00:00
echo_good
2020-05-26 16:43:12 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-26 16:43:12 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="Attempting to Compensate"
echo_info
2020-05-26 16:43:12 +00:00
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
2020-05-28 21:50:25 +00:00
echo_stat
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
if [ "$SKIP_CUSTOM" != '1' ]
then
2020-05-29 19:20:24 +00:00
if [ "$REMOTE_CUSTOM_DNS" == "1" ]
then
if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ]
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
fi
2020-05-29 18:37:35 +00:00
MESSAGE="Pulling ${CUSTOM_DNS} from ${REMOTE_HOST}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync -e "${SSH_CMD} -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
2020-05-29 18:37:35 +00:00
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
2020-05-29 18:37:35 +00:00
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
2020-05-29 18:37:35 +00:00
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
2020-05-29 18:37:35 +00:00
2020-05-29 18:51:12 +00:00
CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}')
2020-05-29 18:51:59 +00:00
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
2020-05-29 18:37:35 +00:00
fi
MESSAGE="Inverting Tachyon Pulse"
2020-05-28 21:21:10 +00:00
echo_info
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-28 21:50:25 +00:00
echo_stat
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-28 21:50:25 +00:00
echo_stat
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 {
2020-05-25 01:54:02 +00:00
md5_compare
2020-05-29 02:28:14 +00:00
intent_validate
2020-05-28 22:24:31 +00:00
MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync -e "${SSH_CMD} -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-28 22:24:31 +00:00
error_validate
MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "${SSH_CMD} -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-28 22:24:31 +00:00
error_validate
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
2020-05-28 22:24:31 +00:00
error_validate
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -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-28 22:24:31 +00:00
error_validate
2020-05-29 19:03:18 +00:00
if [ "$SKIP_CUSTOM" != '1' ]
then
2020-05-29 19:20:24 +00:00
if [ "$REMOTE_CUSTOM_DNS" == "1" ]
2020-05-29 19:03:18 +00:00
then
MESSAGE="Backing Up ${CUSTOM_DNS} from ${REMOTE_HOST}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync -e "${SSH_CMD} -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
2020-05-29 19:03:18 +00:00
error_validate
MESSAGE="Pushing ${CUSTOM_DNS} to ${REMOTE_HOST}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${CUSTOM_DNS} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
2020-05-29 19:03:18 +00:00
error_validate
MESSAGE="Setting Permissions on ${CUSTOM_DNS}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1
2020-05-29 19:03:18 +00:00
error_validate
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS}" >/dev/null 2>&1
2020-05-29 19:19:07 +00:00
error_validate
fi
2020-05-29 19:03:18 +00:00
fi
2020-05-28 22:24:31 +00:00
MESSAGE="Contacting Borg Collective"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1
2020-05-28 22:24:31 +00:00
error_validate
2020-05-23 23:50:29 +00:00
2020-05-28 22:24:31 +00:00
MESSAGE="Reloading FTLDNS Services"
echo_stat
2020-06-01 15:00:01 +00:00
${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1
2020-05-28 22:24:31 +00:00
error_validate
2020-05-25 01:54:02 +00:00
2020-05-28 22:24:31 +00:00
logs_export
exit_withchange
2020-05-29 19:19:07 +00:00
2020-05-22 15:32:02 +00:00
}
2020-05-22 15:28:02 +00:00
2020-05-28 22:29:41 +00:00
function restore_gs {
2020-05-28 22:09:58 +00:00
MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!"
echo_warn
2020-05-29 02:28:14 +00:00
intent_validate
2020-05-28 22:24:31 +00:00
2020-05-28 22:09:58 +00:00
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
2020-05-29 19:08:56 +00:00
if [ "$SKIP_CUSTOM" != '1' ]
then
2020-05-31 03:59:03 +00:00
if [ -f $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup ]
2020-05-29 19:08:56 +00:00
then
MESSAGE="Restoring ${CUSTOM_DNS} on $HOSTNAME"
echo_stat
2020-05-31 04:02:21 +00:00
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
2020-05-29 19:08:56 +00:00
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
2020-05-29 02:50:29 +00:00
MESSAGE="Evacuating Saucer Section"
2020-05-28 22:09:58 +00:00
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
}
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 {
2020-05-28 21:50:25 +00:00
MESSAGE="Logging Timestamps to ${SYNCING_LOG}"
echo_info
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
2020-05-28 21:50:25 +00:00
MESSAGE="Tailing ${LOG_PATH}/${SYNCING_LOG}"
echo_info
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-29 19:08:56 +00:00
echo -e "Recent Complete ${YELLOW}RESTORE${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep RESTORE
2020-05-25 02:15:08 +00:00
echo -e "========================================================"
2020-05-28 21:50:25 +00:00
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-28 21:50:25 +00:00
echo_stat
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-28 21:50:25 +00:00
echo_good
2020-05-27 15:18:58 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="Tailing ${LOG_PATH}/${CRONJOB_LOG}"
echo_info
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-28 21:50:25 +00:00
exit_nochange
2020-05-26 02:16:31 +00:00
then
2020-05-28 21:50:25 +00:00
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Empty"
echo_info
exit_nochange
2020-05-26 02:16:31 +00:00
fi
else
2020-05-28 21:50:25 +00:00
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Missing"
echo_info
exit_nochange
2020-05-26 02:16:31 +00:00
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-31 03:28:57 +00:00
MESSAGE="Validating $HOSTNAME:$HOME/${LOCAL_FOLDR}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-25 00:38:43 +00:00
if [ -d $HOME/${LOCAL_FOLDR} ]
then
2020-05-28 21:50:25 +00:00
echo_good
2020-05-25 00:38:43 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-25 00:38:43 +00:00
exit_nochange
fi
2020-05-22 20:44:15 +00:00
2020-05-31 03:28:57 +00:00
MESSAGE="Validating $HOSTNAME:$HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-25 00:38:43 +00:00
if [ -d $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD} ]
then
2020-05-28 21:50:25 +00:00
echo_good
2020-05-25 00:38:43 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-25 00:38:43 +00:00
exit_nochange
fi
2020-05-22 15:44:38 +00:00
}
2020-05-29 03:01:08 +00:00
## Validate Pi-hole Folders
2020-05-22 15:44:38 +00:00
function validate_ph_folders {
2020-05-31 03:28:57 +00:00
MESSAGE="Validating $HOSTNAME:${PIHOLE_DIR}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-25 00:38:43 +00:00
if [ -d ${PIHOLE_DIR} ]
then
2020-05-28 21:50:25 +00:00
echo_good
2020-05-25 00:38:43 +00:00
else
2020-05-28 21:50:25 +00:00
echo_fail
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-31 03:06:31 +00:00
# MESSAGE="Checking SSH Configuration"
# echo_info
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-28 21:21:10 +00:00
echo_info
2020-05-25 18:18:23 +00:00
2020-05-31 03:28:57 +00:00
MESSAGE="Validating SSH Connection to ${REMOTE_HOST}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-06-01 18:08:18 +00:00
if hash ssh 2>/dev/null
then
2020-06-01 18:53:12 +00:00
timeout 5 ${SSHPASSWORD} ${SSH_CMD} -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-06-01 18:29:50 +00:00
elif hash dbclient 2>/dev/null
2020-06-01 18:08:18 +00:00
then
2020-06-01 18:36:47 +00:00
# if [ "$SSH_CMD" = "dbclient" ]; then echo ''; fi;
2020-06-01 18:50:58 +00:00
timeout 5 ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1
2020-06-01 18:08:18 +00:00
error_validate
fi
2020-05-25 02:15:08 +00:00
}
2020-05-31 02:01:24 +00:00
## Detect SSH-KEYGEN
function detect_sshkeygen {
2020-05-31 03:28:57 +00:00
MESSAGE="Validating SSH-KEYGEN install on $HOSTNAME"
2020-05-31 02:01:24 +00:00
echo_stat
2020-05-31 02:29:01 +00:00
if hash ssh-keygen >/dev/null 2>&1
2020-05-31 02:01:24 +00:00
then
echo_good
else
echo_fail
MESSAGE="SSH-KEYGEN is Required"
echo_info
MESSAGE="Attempting to Compensate"
echo_info
2020-05-31 02:29:01 +00:00
if hash dropbearkey >/dev/null 2>&1
2020-05-31 02:01:24 +00:00
then
MESSAGE="Using DROPBEARKEY Instead"
echo_info
if [ -d $HOME/.ssh ]
then
KEYGEN_COMMAND="dropbearkey -t rsa -f"
else
mkdir $HOME/.ssh >/dev/null 2>&1
KEYGEN_COMMAND="dropbearkey -t rsa -f $HOME/${SSH_PKIF}"
fi
2020-05-31 02:01:24 +00:00
else
MESSAGE="No Alternatives Located"
echo_info
exit_nochange
fi
fi
}
## Detect Package Manager
function distro_check {
2020-05-31 02:35:23 +00:00
if hash apt-get 2>/dev/null
2020-05-31 02:01:24 +00:00
then
2020-06-01 19:20:50 +00:00
PKG_MANAGER="apt-get"
2020-05-31 02:29:01 +00:00
PKG_INSTALL="sudo apt-get --yes --no-install-recommends --quiet install"
2020-05-31 02:35:23 +00:00
elif hash rpm 2>/dev/null
2020-05-31 02:01:24 +00:00
then
2020-05-31 02:35:23 +00:00
if hash dnf 2>/dev/null
2020-05-31 02:01:24 +00:00
then
PKG_MANAGER="dnf"
2020-05-31 02:35:23 +00:00
elif hash yum 2>/dev/null
2020-05-31 02:01:24 +00:00
then
PKG_MANAGER="yum"
else
MESSAGE="Unable to find OS Package Manager"
echo_info
exit_nochange
fi
PKG_INSTALL="sudo ${PKG_MANAGER} install -y"
else
MESSAGE="Unable to find OS Package Manager"
echo_info
exit_nochange
fi
}
## Detect SSH & RSYNC
function detect_ssh {
2020-06-01 18:50:58 +00:00
MESSAGE="Validating SSH on $HOSTNAME"
2020-05-31 02:01:24 +00:00
echo_stat
2020-05-31 02:35:23 +00:00
if hash ssh 2>/dev/null
2020-05-31 02:01:24 +00:00
then
2020-06-01 18:50:58 +00:00
MESSAGE="Validating SSH on $HOSTNAME (OpenSSH)"
2020-05-31 02:01:24 +00:00
echo_good
2020-06-01 15:00:01 +00:00
SSH_CMD='ssh'
2020-06-01 18:45:16 +00:00
elif hash dbclient 2>/dev/null
then
2020-06-01 18:50:58 +00:00
MESSAGE="Validating SSH on $HOSTNAME (Dropbear)"
2020-06-01 18:45:16 +00:00
echo_good
SSH_CMD='dbclient'
2020-05-31 02:01:24 +00:00
else
echo_fail
2020-06-01 18:45:16 +00:00
2020-05-31 03:16:42 +00:00
MESSAGE="Attempting to Compensate"
2020-05-31 02:01:24 +00:00
echo_info
2020-06-01 18:45:16 +00:00
MESSAGE="Installing SSH Client with ${PKG_MANAGER}"
echo_stat
${PKG_INSTALL} ssh-client >/dev/null 2>&1
error_validate
2020-05-31 02:01:24 +00:00
fi
2020-06-01 18:50:58 +00:00
MESSAGE="Validating RSYNC on $HOSTNAME"
2020-05-31 02:01:24 +00:00
echo_stat
2020-05-31 02:35:23 +00:00
if hash rsync 2>/dev/null
2020-05-31 02:01:24 +00:00
then
echo_good
else
echo_fail
2020-05-31 03:16:42 +00:00
MESSAGE="RSYNC is Required"
2020-05-31 02:01:24 +00:00
echo_info
2020-05-31 02:33:54 +00:00
distro_check
2020-05-31 03:16:42 +00:00
MESSAGE="Attempting to Compensate"
echo_info
MESSAGE="Installing RSYNC with ${PKG_MANAGER}"
2020-05-31 02:01:24 +00:00
echo_stat
2020-05-31 02:29:01 +00:00
${PKG_INSTALL} rsync >/dev/null 2>&1
2020-05-31 02:01:24 +00:00
error_validate
fi
}
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-28 21:50:25 +00:00
echo_fail
2020-05-23 22:39:02 +00:00
exit 1
2020-05-23 23:10:18 +00:00
else
2020-05-28 21:50:25 +00:00
echo_good
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-29 15:58:06 +00:00
HASHMARK='0'
2020-05-31 03:28:57 +00:00
MESSAGE="Analyzing ${REMOTE_HOST} ${GRAVITY_FI}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-06-01 20:17:01 +00:00
primaryDBMD5=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}" | sed 's/\s.*$//')
2020-05-25 01:48:18 +00:00
error_validate
2020-05-31 03:28:57 +00:00
MESSAGE="Analyzing $HOSTNAME ${GRAVITY_FI}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-06-01 20:17:01 +00:00
secondDBMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI} | sed 's/\s.*$//')
2020-05-25 01:48:18 +00:00
error_validate
2020-05-25 01:19:40 +00:00
2020-05-29 15:58:06 +00:00
if [ "$primaryDBMD5" == "$secondDBMD5" ]
2020-05-25 01:19:40 +00:00
then
2020-05-31 04:09:21 +00:00
MESSAGE="${GRAVITY_FI} Identical"
2020-05-28 21:50:25 +00:00
echo_info
2020-05-29 15:58:06 +00:00
HASHMARK=$((HASHMARK+0))
2020-05-25 01:19:40 +00:00
else
2020-05-31 04:20:34 +00:00
MESSAGE="${GRAVITY_FI} Differenced"
2020-06-01 20:27:36 +00:00
echo_warn
2020-05-29 15:58:06 +00:00
HASHMARK=$((HASHMARK+1))
fi
if [ "$SKIP_CUSTOM" != '1' ]
2020-05-29 15:58:06 +00:00
then
if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ]
2020-05-29 16:03:20 +00:00
then
2020-06-01 15:00:01 +00:00
if ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS}
2020-05-29 16:31:38 +00:00
then
REMOTE_CUSTOM_DNS="1"
2020-05-31 03:28:57 +00:00
MESSAGE="Analyzing ${REMOTE_HOST} ${CUSTOM_DNS}"
echo_stat
2020-06-01 20:17:01 +00:00
primaryCLMD5=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS} | sed 's/\s.*$//'")
error_validate
2020-05-31 03:28:57 +00:00
MESSAGE="Analyzing $HOSTNAME ${CUSTOM_DNS}"
echo_stat
2020-06-01 20:17:01 +00:00
secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS} | sed 's/\s.*$//')
error_validate
if [ "$primaryCLMD5" == "$secondCLMD5" ]
then
2020-05-31 04:09:21 +00:00
MESSAGE="${CUSTOM_DNS} Identical"
echo_info
HASHMARK=$((HASHMARK+0))
else
2020-05-31 04:20:34 +00:00
MESSAGE="${CUSTOM_DNS} Differenced"
2020-06-01 20:27:36 +00:00
echo_warn
HASHMARK=$((HASHMARK+1))
fi
2020-05-29 16:31:38 +00:00
else
2020-05-31 03:28:57 +00:00
MESSAGE="No ${CUSTOM_DNS} Detected on ${REMOTE_HOST}"
2020-05-29 16:31:38 +00:00
echo_info
fi
2020-05-29 16:03:20 +00:00
else
2020-06-01 20:38:07 +00:00
if ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS}
then
REMOTE_CUSTOM_DNS="1"
MESSAGE="${REMOTE_HOST} has ${CUSTOM_DNS}"
echo_info
fi
2020-05-31 03:28:57 +00:00
MESSAGE="No ${CUSTOM_DNS} Detected on $HOSTNAME"
2020-05-29 16:03:20 +00:00
echo_info
fi
2020-05-29 15:58:06 +00:00
fi
if [ "$HASHMARK" != "0" ]
then
2020-06-01 20:27:36 +00:00
MESSAGE="Replication Required"
echo_warn
2020-05-29 15:58:06 +00:00
HASHMARK=$((HASHMARK+0))
else
2020-06-01 20:27:36 +00:00
MESSAGE="No Changes to Replicate"
2020-05-29 15:58:06 +00:00
echo_info
exit_nochange
2020-05-25 01:19:40 +00:00
fi
}
2020-05-29 02:28:14 +00:00
## Validate Intent
function intent_validate {
2020-05-29 02:43:58 +00:00
if [ "$VERIFY_PASS" == "0" ]
2020-05-29 02:28:14 +00:00
then
2020-05-29 02:43:58 +00:00
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
2020-05-29 02:28:14 +00:00
2020-05-29 02:43:58 +00:00
read INPUT_INTENT
2020-05-29 02:28:14 +00:00
2020-05-29 02:43:58 +00:00
if [ "${INPUT_INTENT}" != "${INTENT}" ]
then
MESSAGE="${TASKTYPE} Aborted"
echo_info
exit_nochange
fi
else
MESSAGE="Verification Bypassed"
echo_warn
2020-05-29 02:28:14 +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 {
2020-05-31 01:37:59 +00:00
detect_ssh
2020-05-27 02:21:02 +00:00
MESSAGE="Creating ${CONFIG_FILE} from Template"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-27 02:21:02 +00:00
cp $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}.example $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
2020-05-28 21:50:25 +00:00
MESSAGE="Enter IP or DNS of primary Pi-hole server"
echo_need
2020-05-27 02:21:02 +00:00
read INPUT_REMOTE_HOST
2020-05-28 22:09:58 +00:00
MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server"
2020-05-28 21:50:25 +00:00
echo_need
2020-05-27 02:21:02 +00:00
read INPUT_REMOTE_USER
MESSAGE="Saving Host to ${CONFIG_FILE}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-27 02:21:02 +00:00
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}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-27 02:21:02 +00:00
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"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
2020-05-27 04:02:41 +00:00
MESSAGE="Do you want to configure password based SSH authentication?"
2020-05-28 21:50:25 +00:00
echo_warn
2020-05-27 04:02:41 +00:00
MESSAGE="Your password will be stored clear-text in the ${CONFIG_FILE}!"
2020-05-28 21:50:25 +00:00
echo_warn
2020-05-28 16:27:14 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication"
echo_need
2020-05-27 02:21:02 +00:00
read INPUT_REMOTE_PASS
MESSAGE="Saving Password to ${CONFIG_FILE}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-27 02:21:02 +00:00
sed -i "/REMOTE_PASS=''/c\REMOTE_PASS='${INPUT_REMOTE_PASS}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
else
MESSAGE="SSHPASS Not Installed"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
MESSAGE="Defaulting to SSH Key-Pair Authentication"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
fi
2020-05-31 01:20:06 +00:00
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}"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
else
2020-05-31 01:20:06 +00:00
KEYGEN_COMMAND="ssh-keygen -t rsa -f"
detect_sshkeygen
2020-05-27 02:21:02 +00:00
MESSAGE="Generating ~/${SSH_PKIF}"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
2020-05-27 02:33:00 +00:00
MESSAGE="Accept All Defaults"
2020-05-28 21:50:25 +00:00
echo_warn
2020-05-27 02:21:02 +00:00
MESSAGE="Complete Key-Pair Creation"
echo -e "${NEED} ${MESSAGE}"
echo -e "========================================================"
echo -e "========================================================"
2020-05-31 01:20:06 +00:00
${KEYGEN_COMMAND} $HOME/${SSH_PKIF}
2020-05-27 02:21:02 +00:00
echo -e "========================================================"
echo -e "========================================================"
fi
fi
MESSAGE="Importing New ${CONFIG_FILE}"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-27 02:21:02 +00:00
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}"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 02:21:02 +00:00
2020-05-28 16:36:55 +00:00
MESSAGE="Enter ${REMOTE_USER}@${REMOTE_HOST} Password Below"
2020-05-27 02:21:02 +00:00
echo -e "${NEED} ${MESSAGE}"
echo -e "========================================================"
echo -e "========================================================"
if hash ssh-copy-id 2>/dev/null
then
ssh-copy-id -f -i $HOME/${SSH_PKIF}.pub ${REMOTE_USER}@${REMOTE_HOST}
2020-06-01 15:51:38 +00:00
elif hash dbclient 2>/dev/null
then
2020-06-01 17:33:50 +00:00
dropbearkey -y -f $HOME/${SSH_PKIF} | grep "^ssh-rsa " > $HOME/${SSH_PKIF}.pub
cat $HOME/${SSH_PKIF}.pub | dbclient ${REMOTE_USER}@${REMOTE_HOST} 'cat - >> .ssh/authorized_keys'
2020-06-01 15:45:37 +00:00
fi
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"
2020-05-28 21:50:25 +00:00
echo_warn
2020-05-27 02:21:02 +00:00
echo -e "========================================================"
cat $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
echo -e "========================================================"
MESSAGE="Are you sure you want to erase this configuration?"
2020-05-28 21:50:25 +00:00
echo_warn
2020-05-27 02:21:02 +00:00
2020-05-29 02:28:14 +00:00
intent_validate
2020-05-28 22:29:41 +00:00
2020-05-29 02:28:14 +00:00
MESSAGE="Erasing Existing Configuration"
echo_stat
rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
2020-05-28 22:29:41 +00:00
2020-05-29 02:28:14 +00:00
config_generate
2020-05-27 02:21:02 +00:00
}
2020-05-27 03:20:36 +00:00
# Exit Codes
## No Changes Made
function exit_nochange {
2020-05-31 04:22:37 +00:00
MESSAGE="${PROGRAM} ${TASKTYPE} Aborting"
2020-05-28 21:50:25 +00:00
echo_info
2020-05-27 03:20:36 +00:00
exit 0
}
## Changes Made
function exit_withchange {
SCRIPT_END=$SECONDS
2020-05-31 04:22:37 +00:00
MESSAGE="${PROGRAM} ${TASKTYPE} Completed in $((SCRIPT_END-SCRIPT_START)) seconds"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-27 03:20:36 +00:00
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"
2020-05-30 05:06:54 +00:00
echo -e " ${YELLOW}automate${NC} Add a scheduled pull task to crontab"
echo -e " ${YELLOW}update${NC} Update ${PROGRAM} to the latest version"
echo -e " ${YELLOW}version${NC} Display installed version of ${PROGRAM}"
2020-05-27 04:02:41 +00:00
echo -e ""
2020-05-27 03:20:36 +00:00
echo -e "Replication Options:"
2020-05-30 05:06:54 +00:00
echo -e " ${YELLOW}pull${NC} Sync remote ${GRAVITY_FI} to this server"
echo -e " ${YELLOW}push${NC} Force changes made on this server back"
echo -e " ${YELLOW}restore${NC} Restore ${GRAVITY_FI} on this server"
echo -e " ${YELLOW}compare${NC} Just check for differences"
2020-05-27 03:20:36 +00:00
echo -e ""
# echo -e "Update Options:"
# 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"
# echo -e ""
2020-05-27 03:20:36 +00:00
echo -e "Debug Options:"
2020-05-27 04:02:41 +00:00
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
}
2020-05-30 02:01:52 +00:00
# Version Control
## Show Version
2020-05-27 03:20:36 +00:00
function show_version {
2020-05-30 02:06:07 +00:00
echo -e "========================================================"
2020-05-30 02:09:37 +00:00
MESSAGE="${BOLD}${PROGRAM}${NC} by ${CYAN}@vmstan${NC}"
2020-05-30 02:04:11 +00:00
echo_info
MESSAGE="${BLUE}https://github.com/vmstan/gravity-sync${NC}"
2020-05-29 23:09:48 +00:00
echo_info
2020-05-30 02:01:52 +00:00
MESSAGE="Running Version: ${GREEN}${VERSION}${NC}"
2020-05-28 21:50:25 +00:00
echo_info
2020-05-29 23:07:38 +00:00
2020-05-30 02:39:01 +00:00
GITVERSION=$(curl -sf https://raw.githubusercontent.com/vmstan/gravity-sync/master/VERSION)
2020-05-30 01:53:28 +00:00
if [ -z "$GITVERSION" ]
then
2020-05-30 02:01:52 +00:00
MESSAGE="Latest Version: ${RED}Unknown${NC}"
2020-05-30 01:53:28 +00:00
else
2020-05-30 02:01:52 +00:00
if [ "$GITVERSION" != "$VERSION" ]
then
2020-05-31 04:20:34 +00:00
MESSAGE="Upgrade Available: ${PURPLE}${GITVERSION}${NC}"
2020-05-30 02:01:52 +00:00
else
MESSAGE="Latest Version: ${GREEN}${GITVERSION}${NC}"
fi
2020-05-30 01:53:28 +00:00
fi
2020-05-29 23:07:38 +00:00
echo_info
2020-05-30 02:06:07 +00:00
echo -e "========================================================"
2020-05-27 03:20:36 +00:00
}
2020-05-28 01:51:21 +00:00
# Task Stack
## Automate Task
function task_automate {
TASKTYPE='AUTOMATE'
2020-05-28 21:50:25 +00:00
echo_good
2020-05-28 01:51:21 +00:00
import_gs
2020-05-28 18:05:22 +00:00
CRON_CHECK=$(crontab -l | grep -q "${GS_FILENAME}" && echo '1' || echo '0')
2020-05-28 16:27:14 +00:00
if [ ${CRON_CHECK} == 1 ]
then
MESSAGE="Automation Task Already Exists"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-28 16:41:57 +00:00
MESSAGE="Use 'crontab -e' to manually remove/edit"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-28 16:27:14 +00:00
exit_nochange
fi
2020-05-28 15:54:32 +00:00
MESSAGE="Set Automation Frequency Per Hour"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-28 15:54:32 +00:00
2020-05-28 16:48:34 +00:00
MESSAGE="1 = Every 60 Minutes"
2020-05-28 16:46:54 +00:00
echo -e "++++++ ${MESSAGE}"
2020-05-28 16:48:34 +00:00
MESSAGE="2 = Every 30 Minutes"
2020-05-28 16:46:54 +00:00
echo -e "++++++ ${MESSAGE}"
2020-05-28 16:48:34 +00:00
MESSAGE="4 = Every 15 Minutes"
2020-05-28 16:46:54 +00:00
echo -e "++++++ ${MESSAGE}"
2020-05-28 16:48:34 +00:00
MESSAGE="6 = Every 10 Minutes"
2020-05-28 16:46:54 +00:00
echo -e "++++++ ${MESSAGE}"
2020-05-28 16:50:39 +00:00
MESSAGE="12 = Every 05 Minutes"
2020-05-28 16:46:54 +00:00
echo -e "++++++ ${MESSAGE}"
2020-05-28 15:54:32 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="Input Automation Frequency"
echo_need
2020-05-28 15:54:32 +00:00
read INPUT_AUTO_FREQ
2020-05-28 15:54:57 +00:00
if [ $INPUT_AUTO_FREQ == 1 ]
2020-05-28 15:54:32 +00:00
then
AUTO_FREQ='60'
2020-05-28 15:54:57 +00:00
elif [ $INPUT_AUTO_FREQ == 2 ]
2020-05-28 15:59:04 +00:00
then
2020-05-28 15:54:32 +00:00
AUTO_FREQ='30'
2020-05-28 15:54:57 +00:00
elif [ $INPUT_AUTO_FREQ == 4 ]
2020-05-28 15:59:04 +00:00
then
2020-05-28 15:54:32 +00:00
AUTO_FREQ='15'
2020-05-28 15:54:57 +00:00
elif [ $INPUT_AUTO_FREQ == 6 ]
2020-05-28 15:59:04 +00:00
then
2020-05-28 15:54:32 +00:00
AUTO_FREQ='10'
2020-05-28 15:54:57 +00:00
elif [ $INPUT_AUTO_FREQ == 12 ]
2020-05-28 15:59:04 +00:00
then
2020-05-28 15:54:32 +00:00
AUTO_FREQ='5'
else
MESSAGE="Invalid Input"
echo -e "${FAIL} ${MESSAGE}"
2020-05-28 16:43:24 +00:00
exit_nochange
2020-05-28 15:54:32 +00:00
fi
MESSAGE="Saving to Crontab"
2020-05-28 21:50:25 +00:00
echo_stat
2020-05-28 18:05:22 +00:00
(crontab -l 2>/dev/null; echo "*/${AUTO_FREQ} * * * * ${BASH_PATH} $HOME/${LOCAL_FOLDR}/${GS_FILENAME} pull > ${LOG_PATH}/${CRONJOB_LOG}") | crontab -
2020-05-28 15:54:32 +00:00
error_validate
2020-05-28 15:19:50 +00:00
exit_withchange
2020-05-28 01:51:21 +00:00
}
2020-05-28 21:21:10 +00:00
# Echo Stack
## Informative
function echo_info {
2020-05-31 04:17:36 +00:00
echo -e "${INFO} ${YELLOW}${MESSAGE}${NC}"
2020-05-28 21:21:10 +00:00
}
## Warning
function echo_warn {
2020-06-01 20:27:36 +00:00
echo -e "${WARN} ${PURPLE}${MESSAGE}${NC}"
2020-05-28 21:21:10 +00:00
}
## 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}: "
}
2020-05-22 15:44:38 +00:00
# SCRIPT EXECUTION ###########################
2020-05-26 02:35:25 +00:00
SCRIPT_START=$SECONDS
2020-05-31 03:06:31 +00:00
2020-05-31 04:22:37 +00:00
MESSAGE="${PROGRAM} Executing"
2020-05-31 03:06:31 +00:00
echo_info
2020-05-24 18:39:21 +00:00
2020-05-31 03:06:31 +00:00
MESSAGE="Evaluating Arguments"
2020-05-28 21:50:25 +00:00
echo_stat
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-28 21:50:25 +00:00
echo_fail
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-28 21:50:25 +00:00
TASKTYPE='PULL'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-24 18:39:21 +00:00
2020-05-25 00:38:43 +00:00
import_gs
2020-05-31 03:06:31 +00:00
# MESSAGE="Validating Folder Configuration"
# echo_info
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-28 21:50:25 +00:00
TASKTYPE='PUSH'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-25 00:38:43 +00:00
import_gs
2020-05-31 03:06:31 +00:00
# MESSAGE="Validating Folder Configuration"
# echo_info
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
;;
2020-05-28 22:09:58 +00:00
2020-05-28 22:29:41 +00:00
restore)
TASKTYPE='RESTORE'
2020-05-28 22:09:58 +00:00
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
import_gs
2020-05-31 03:28:57 +00:00
# MESSAGE="Validating Folder Configuration"
# echo_info
2020-05-28 22:09:58 +00:00
validate_gs_folders
validate_ph_folders
# validate_os_sshpass
2020-05-28 22:29:41 +00:00
restore_gs
2020-05-28 22:09:58 +00:00
exit
;;
2020-05-22 02:42:37 +00:00
version)
2020-05-25 01:06:11 +00:00
TASKTYPE='VERSION'
2020-05-28 21:50:25 +00:00
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-28 18:05:22 +00:00
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-28 21:50:25 +00:00
TASKTYPE='UPDATE'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-25 01:48:18 +00:00
2020-05-28 21:50:25 +00:00
update_gs
2020-05-22 02:42:37 +00:00
;;
2020-05-25 01:23:19 +00:00
2020-05-30 21:23:02 +00:00
dev)
TASKTYPE='DEV'
echo_good
2020-05-25 01:48:18 +00:00
2020-05-30 21:23:02 +00:00
if [ -f $HOME/${LOCAL_FOLDR}/dev ]
then
2020-05-30 21:28:22 +00:00
MESSAGE="Disabling ${TASKTYPE}"
2020-05-30 21:23:02 +00:00
echo_stat
2020-05-30 21:28:22 +00:00
rm -f $HOME/${LOCAL_FOLDR}/dev
2020-05-30 21:23:02 +00:00
error_validate
else
2020-05-30 21:28:22 +00:00
MESSAGE="Enabling ${TASKTYPE}"
2020-05-30 21:23:02 +00:00
echo_stat
2020-05-30 21:28:22 +00:00
touch $HOME/${LOCAL_FOLDR}/dev
2020-05-30 21:23:02 +00:00
error_validate
fi
2020-05-31 04:20:34 +00:00
MESSAGE="Run UPDATE to apply changes"
2020-05-30 21:23:02 +00:00
echo_info
exit_withchange
;;
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-28 21:50:25 +00:00
echo_good
2020-05-26 02:05:28 +00:00
2020-05-28 21:50:25 +00:00
MESSAGE="${TASKTYPE} Requested"
2020-05-28 21:21:10 +00:00
echo_info
2020-05-28 21:50:25 +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-28 21:50:25 +00:00
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-25 01:39:06 +00:00
2020-05-28 21:50:25 +00:00
import_gs
2020-05-25 02:51:04 +00:00
2020-05-31 03:28:57 +00:00
# MESSAGE="Validating OS Configuration"
# echo_info
2020-05-28 21:50:25 +00:00
2020-05-25 02:51:04 +00:00
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-28 21:50:25 +00:00
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
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)
2020-05-28 01:51:21 +00:00
TASKTYPE='CONFIGURE'
2020-05-28 21:50:25 +00:00
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
2020-05-26 19:01:01 +00:00
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"
2020-05-28 21:21:10 +00:00
echo_info
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-28 01:51:21 +00:00
auto)
task_automate
;;
automate)
task_automate
;;
2020-05-22 02:42:37 +00:00
*)
2020-05-28 21:50:25 +00:00
echo_fail
2020-05-22 18:31:42 +00:00
list_gs_arguments
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-28 21:50:25 +00:00
echo_fail
2020-05-24 18:39:21 +00:00
list_gs_arguments
2020-05-22 16:39:31 +00:00
;;
2020-05-21 18:00:42 +00:00
esac