gravity-sync/gravity-sync.sh

265 lines
7.9 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-22 19:49:51 +00:00
VERSION='1.2.3'
2020-05-21 18:00:42 +00:00
# Must execute from a location in the home folder of the user who own's it (ex: /home/pi/gravity-sync)
2020-05-22 17:18:16 +00:00
# Configure certificate based SSH authentication between the Pi-hole HA nodes - it does not use passwords
# Tested against Pihole 5.0 GA on Raspbian Buster and Ubuntu 20.04, but it should work on most configs
2020-05-21 18:00:42 +00:00
# More installation instructions available at https://vmstan.com/gravity-sync
# For the latest version please visit https://github.com/vmstan/gravity-sync under Releases
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-22 17:18:16 +00:00
# You MUST define REMOTE_HOST and REMOTE_USER in a file called 'gravity-sync.conf'
# You can copy the 'gravity-sync.conf.example' file in the script directory to get started
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-21 18:53:12 +00:00
LOCAL_FOLDR='gravity-sync' # must exist in running user home folder
2020-05-22 17:25:30 +00:00
CONFIG_FILE='gravity-sync.conf' # must exist as explained above
2020-05-21 18:53:12 +00:00
SYNCING_LOG='gravity-sync.log' # will be created in above folder
BACKUP_FOLD='backup'
2020-05-21 18:00:42 +00:00
2020-05-22 15:44:38 +00:00
# PH Folder/File Locations
2020-05-21 18:00:42 +00:00
PIHOLE_DIR='/etc/pihole' # default install directory
GRAVITY_FI='gravity.db' # this should not change
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-21 18:00:42 +00:00
NC='\033[0m'
2020-05-22 17:18:16 +00:00
##############################################
### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
##############################################
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-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Importing ${CONFIG_FILE} Settings"
2020-05-22 17:29:05 +00:00
if [ -f ~/${LOCAL_FOLDR}/${CONFIG_FILE} ]
2020-05-22 15:46:24 +00:00
then
2020-05-22 17:29:05 +00:00
source ${CONFIG_FILE}
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Using ${REMOTE_USER}@${REMOTE_HOST}"
2020-05-22 15:46:24 +00:00
else
2020-05-22 19:49:51 +00:00
echo -e "[${RED}FAIL${NC}] Required ${CONFIG_FILE} Missing"
2020-05-22 15:46:24 +00:00
echo -e "Please review installation documentation for more information"
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:46:24 +00:00
fi
}
2020-05-22 15:28:02 +00:00
2020-05-22 15:44:38 +00:00
# Update Function
2020-05-22 15:28:02 +00:00
function update_gs {
TASKTYPE='UPDATE'
2020-05-22 16:56:43 +00:00
logs_export # dumps log prior to execution because script stops after successful pull
2020-05-22 19:49:51 +00:00
echo -e "[${PURPLE}WARN${NC}] Requires GitHub Installation"
2020-05-22 16:06:59 +00:00
git reset --hard
git pull
exit
2020-05-22 15:28:02 +00:00
}
2020-05-22 15:44:38 +00:00
# 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-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Copying ${GRAVITY_FI} from ${REMOTE_HOST}"
rsync -v --progress -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.last
echo -e "[${CYAN}STAT${NC}] Backing Up Current ${GRAVITY_FI} on $HOSTNAME"
2020-05-22 20:48:45 +00:00
cp -v ${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Replacing ${GRAVITY_FI} on $HOSTNAME"
sudo cp -v ~/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.last ${PIHOLE_DIR}/${GRAVITY_FI}
2020-05-22 16:06:59 +00:00
sudo chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI}
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}
2020-05-22 15:32:02 +00:00
echo -e "${GRAVITY_FI} ownership and file permissions reset"
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Reloading FTLDNS Configuration"
2020-05-22 16:06:59 +00:00
pihole restartdns reloadlists
pihole restartdns
# echo -e "[${CYAN}STAT${NC}] Archiving Latest ${GRAVITY_FI}"
# mv -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.last
2020-05-22 18:59:09 +00:00
logs_export
exit_withchange
2020-05-22 15:32:02 +00:00
}
2020-05-22 15:28:02 +00:00
2020-05-22 15:44:38 +00:00
# Push Function
2020-05-22 15:32:02 +00:00
function push_gs {
TASKTYPE='PUSH'
2020-05-22 19:49:51 +00:00
echo -e "[${PURPLE}WARN${NC}] DATA LOSS IS POSSIBLE"
2020-05-22 19:58:33 +00:00
echo -e "The standard use of this script is to ${YELLOW}PULL${NC} data from the primary PH server to the secondary."
2020-05-22 20:31:35 +00:00
echo -e "By issuing a ${YELLOW}PUSH${NC} we will overwrite ${GRAVITY_FI} on ${YELLOW}${REMOTE_HOST}${NC} with ${YELLOW}$HOSTNAME${NC} server data."
2020-05-22 15:32:02 +00:00
echo -e "No backup copies are made on the primary Pihole before or after executing this command!"
2020-05-22 19:58:33 +00:00
echo -e ""
2020-05-22 15:32:02 +00:00
echo -e "Are you sure you want to overwrite the primary node configuration on ${REMOTE_HOST}?"
select yn in "Yes" "No"; do
case $yn in
Yes )
2020-05-22 18:59:09 +00:00
# echo "Replacing gravity.db on primary"
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Copying ${GRAVITY_FI} to ${REMOTE_HOST}"
2020-05-22 16:06:59 +00:00
rsync --rsync-path="sudo rsync" -v --progress -e 'ssh -p 22' ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI}
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Applying Rermissions to Remote ${GRAVITY_FI}"
2020-05-22 16:06:59 +00:00
ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI}"
ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}"
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Reloading FTLDNS Configuration"
2020-05-22 16:06:59 +00:00
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns reloadlists'
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns'
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
## Check Log Function
2020-05-22 15:47:39 +00:00
function logs_gs {
2020-05-22 19:49:51 +00:00
echo -e "Recent ${YELLOW}PULL${NC} attempts"
2020-05-22 16:54:12 +00:00
tail -n 10 ${SYNCING_LOG} | grep PULL
2020-05-22 19:49:51 +00:00
echo -e "Recent ${YELLOW}UPDATE${NC} attempts"
2020-05-22 16:54:12 +00:00
tail -n 10 ${SYNCING_LOG} | grep UPDATE
2020-05-22 19:49:51 +00:00
echo -e "Recent ${YELLOW}PUSH${NC} attempts"
2020-05-22 16:59:36 +00:00
tail -n 10 ${SYNCING_LOG} | grep PUSH
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:44:38 +00:00
}
2020-05-22 16:39:31 +00:00
## Log Out
function logs_export {
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Logging Timestamps to ${SYNCING_LOG}"
2020-05-22 16:45:33 +00:00
# date >> ~/${LOCAL_FOLDR}/${SYNCING_LOG}
echo -e $(date) "[${TASKTYPE}]" >> ~/${LOCAL_FOLDR}/${SYNCING_LOG}
2020-05-22 16:39:31 +00:00
}
2020-05-21 18:00:42 +00:00
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 {
if [ -d ~/${LOCAL_FOLDR} ]
then
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Required ~/${LOCAL_FOLDR} Located"
2020-05-22 15:44:38 +00:00
else
2020-05-22 19:49:51 +00:00
echo -e "[${RED}FAIL${NC}] Required ~/${LOCAL_FOLDR} Missing"
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:44:38 +00:00
fi
2020-05-22 20:44:15 +00:00
if [ -d ~/${LOCAL_FOLDR}/${BACKUP_FOLD} ]
then
echo -e "[${GREEN}GOOD${NC}] Required ~/${LOCAL_FOLDR}/${BACKUP_FOLD} Located"
else
echo -e "[${RED}FAIL${NC}] Required ~/${LOCAL_FOLDR}/${BACKUP_FOLD} Missing"
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 {
if [ -d ${PIHOLE_DIR} ]
then
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Required ${PIHOLE_DIR} Located"
2020-05-22 15:44:38 +00:00
else
2020-05-22 19:49:51 +00:00
echo -e "[${RED}FAIL${NC}] Required ${PIHOLE_DIR} Missing"
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 15:44:38 +00:00
fi
}
2020-05-22 18:18:19 +00:00
# List GS Arguments
function list_gs_arguments {
echo -e "Usage: $0 [options]"
echo -e "Example: '$0 pull'"
echo -e ""
echo -e "Replication Options:"
2020-05-22 19:51:41 +00:00
echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} configuration on primary PH to this server"
echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH"
2020-05-22 18:18:19 +00:00
echo -e ""
echo -e "Debugging Options:"
2020-05-22 19:51:41 +00:00
echo -e " ${YELLOW}update${NC} Use GitHub to update this script to the latest version available"
echo -e " ${YELLOW}version${NC} Display the version of the current installed script"
echo -e " ${YELLOW}logs${NC} Show recent successful jobs"
2020-05-22 18:18:19 +00:00
echo -e ""
2020-05-22 18:59:09 +00:00
exit_nochange
}
# Exit Codes
## No Changes Made
function exit_nochange {
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Exiting Without Making Changes"
2020-05-22 18:59:09 +00:00
exit
}
## Changes Made
function exit_withchange {
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] ${GRAVITY_FI} ${TASKTYPE} Completed"
2020-05-22 18:59:09 +00:00
exit
2020-05-22 15:44:38 +00:00
}
# SCRIPT EXECUTION ###########################
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Evaluating Script Arguments"
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-22 19:49:51 +00:00
echo -e "[${RED}FAIL${NC}] Missing Required Arguments"
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-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Pull Requested"
import_gs
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Validating Folder Configuration"
validate_gs_folders
validate_ph_folders
pull_gs
2020-05-22 02:42:37 +00:00
exit
;;
push)
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Push Requested"
import_gs
2020-05-22 19:49:51 +00:00
echo -e "[${CYAN}STAT${NC}] Validating Folder Configuration"
validate_gs_folders
validate_ph_folders
push_gs
2020-05-22 02:42:37 +00:00
exit
;;
version)
2020-05-22 18:31:42 +00:00
echo -e "[ ${PURPLE}INFO${NC} ] Gravity Sync ${VERSION}"
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 02:42:37 +00:00
;;
update)
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] 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-22 02:07:01 +00:00
2020-05-22 02:42:37 +00:00
logs)
2020-05-22 19:49:51 +00:00
echo -e "[${GREEN}GOOD${NC}] Logs Requested"
2020-05-22 15:44:38 +00:00
logs_gs
2020-05-22 02:42:37 +00:00
;;
*)
2020-05-22 19:53:01 +00:00
echo -e "[${RED}FAIL${NC}] '${YELLOW}$1${NC}' is Invalid Argument"
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-22 19:49:51 +00:00
echo -e "[${RED}FAIL${NC}] Too Many Arguments"
2020-05-22 18:31:42 +00:00
list_gs_arguments
2020-05-22 18:59:09 +00:00
exit_nochange
2020-05-22 16:39:31 +00:00
;;
2020-05-21 18:00:42 +00:00
esac