2020-05-21 18:00:42 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Gravity Sync by vmstan
|
2020-05-22 16:50:42 +00:00
|
|
|
VERSION='1.2.1'
|
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)
|
|
|
|
# Configure certificate based SSH authentication between the Pihole HA nodes - it does not use passwords
|
2020-05-21 18:44:01 +00:00
|
|
|
# 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
|
2020-05-21 18:44:01 +00:00
|
|
|
# For the latest version please visit https://github.com/vmstan/gravity-sync under Releases
|
2020-05-21 18:00:42 +00:00
|
|
|
|
2020-05-21 21:23:46 +00:00
|
|
|
# You must define REMOTE_HOST and REMOTE_USER in a file called 'gravity-sync.conf' -- see above documentation
|
2020-05-21 18:00:42 +00:00
|
|
|
|
2020-05-22 15:44:38 +00:00
|
|
|
# CUSTOMIZATION ##############################
|
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
|
|
|
|
SYNCING_LOG='gravity-sync.log' # will be created in above folder
|
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 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 {
|
|
|
|
echo -e "${CYAN}Importing gravity-sync.conf settings${NC}"
|
|
|
|
if [ -f ~/${LOCAL_FOLDR}/gravity-sync.conf ]
|
|
|
|
then
|
|
|
|
source gravity-sync.conf
|
|
|
|
echo -e "${GREEN}Success${NC}: Configured for ${REMOTE_USER}@${REMOTE_HOST}"
|
|
|
|
else
|
|
|
|
echo -e "${RED}Failure${NC}: Required file gravity-sync.conf is missing!"
|
|
|
|
echo -e "Please review installation documentation for more information"
|
|
|
|
exit
|
|
|
|
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 {
|
2020-05-22 16:50:42 +00:00
|
|
|
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 15:57:41 +00:00
|
|
|
echo -e "${YELLOW}This update will fail if Gravity Sync was not installed via GitHub${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
git reset --hard
|
|
|
|
git pull
|
2020-05-22 16:50:42 +00:00
|
|
|
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 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Copying ${GRAVITY_FI} from remote server ${REMOTE_HOST}${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
rsync -v --progress -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}
|
2020-05-22 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Backing up the running ${GRAVITY_FI} on this server${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
sudo mv -v ${PIHOLE_DIR}/${GRAVITY_FI} ${PIHOLE_DIR}/${GRAVITY_FI}.backup
|
2020-05-22 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Replacing the ${GRAVITY_FI} configuration on this server${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
sudo cp -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ${PIHOLE_DIR}
|
|
|
|
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"
|
|
|
|
echo -e "${CYAN}Reloading FTLDNS with configuration from new ${GRAVITY_FI}${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
pihole restartdns reloadlists
|
|
|
|
pihole restartdns
|
2020-05-22 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Retaining additional copy of remote ${GRAVITY_FI}${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
mv -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}.last
|
2020-05-22 15:32:02 +00:00
|
|
|
echo -e "${GREEN}gravity.db pull completed${NC}"
|
2020-05-22 16:50:42 +00:00
|
|
|
logs_export
|
|
|
|
exit
|
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 {
|
2020-05-22 16:50:42 +00:00
|
|
|
TASKTYPE='PUSH'
|
2020-05-22 15:32:02 +00:00
|
|
|
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"
|
|
|
|
echo -e "No backup copies are made on the primary Pihole before or after executing this command!"
|
|
|
|
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 )
|
|
|
|
echo "Replacing gravity.db on primary"
|
|
|
|
echo -e "${CYAN}Copying local ${GRAVITY_FI} to ${REMOTE_HOST}${NC}"
|
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 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Applying permissions to remote gravity.db${NC}"
|
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 15:32:02 +00:00
|
|
|
echo -e "${CYAN}Reloading configuration on remote FTLDNS${NC}"
|
2020-05-22 16:06:59 +00:00
|
|
|
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns reloadlists'
|
|
|
|
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns'
|
2020-05-22 15:32:02 +00:00
|
|
|
echo -e "${GREEN}gravity.db push completed${NC}"
|
2020-05-22 16:50:42 +00:00
|
|
|
logs_export
|
2020-05-22 15:32:02 +00:00
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
No )
|
|
|
|
echo "No changes have been made to the system"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
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 16:59:36 +00:00
|
|
|
echo -e "Recent ${PURPLE}PULL${NC} attempts"
|
2020-05-22 16:54:12 +00:00
|
|
|
tail -n 10 ${SYNCING_LOG} | grep PULL
|
2020-05-22 16:59:36 +00:00
|
|
|
echo -e "Recent ${PURPLE}UPDATE${NC} attempts"
|
2020-05-22 16:54:12 +00:00
|
|
|
tail -n 10 ${SYNCING_LOG} | grep UPDATE
|
2020-05-22 16:59:36 +00:00
|
|
|
echo -e "Recent ${PURPLE}PUSH${NC} attempts"
|
|
|
|
tail -n 10 ${SYNCING_LOG} | grep PUSH
|
2020-05-22 15:44:38 +00:00
|
|
|
}
|
2020-05-22 01:25:06 +00:00
|
|
|
|
2020-05-22 16:39:31 +00:00
|
|
|
## Log Out
|
|
|
|
function logs_export {
|
|
|
|
echo -e "Logging timestamps to ${SYNCING_LOG}"
|
2020-05-22 16:45:33 +00:00
|
|
|
# date >> ~/${LOCAL_FOLDR}/${SYNCING_LOG}
|
2020-05-22 16:50:42 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
2020-05-21 18:44:01 +00:00
|
|
|
|
2020-05-22 15:44:38 +00:00
|
|
|
## 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
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:06:59 +00:00
|
|
|
## Validate GS Argument Used
|
2020-05-22 15:44:38 +00:00
|
|
|
function validate_gs_arguments {
|
|
|
|
echo "Usage: $0 {pull|push}"
|
2020-05-22 15:55:23 +00:00
|
|
|
echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on a remote host to this server"
|
2020-05-22 15:44:38 +00:00
|
|
|
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 ###########################
|
|
|
|
|
2020-05-22 01:35:53 +00:00
|
|
|
echo -e "${CYAN}Evaluating $0 script arguments${NC}"
|
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)
|
|
|
|
echo -e "${RED}Failure${NC}: ${GRAVITY_FI} replication direction required"
|
2020-05-22 15:44:38 +00:00
|
|
|
validate_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)
|
|
|
|
echo -e "${GREEN}Success${NC}: Pull Requested"
|
2020-05-22 15:55:23 +00:00
|
|
|
import_gs
|
|
|
|
|
|
|
|
echo -e "${CYAN}Validating sync folder configuration${NC}"
|
|
|
|
validate_gs_folders
|
|
|
|
validate_ph_folders
|
|
|
|
|
|
|
|
pull_gs
|
2020-05-22 02:42:37 +00:00
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
push)
|
|
|
|
echo -e "${GREEN}Success${NC}: Push Requested"
|
2020-05-22 15:55:23 +00:00
|
|
|
import_gs
|
|
|
|
|
|
|
|
echo -e "${CYAN}Validating sync folder configuration${NC}"
|
|
|
|
validate_gs_folders
|
|
|
|
validate_ph_folders
|
|
|
|
|
|
|
|
push_gs
|
2020-05-22 02:42:37 +00:00
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
version)
|
|
|
|
echo -e "${PURPLE}Info:${NC} Gravity Sync ${VERSION}"
|
|
|
|
echo -e "No changes have been made to the system"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
|
|
|
|
update)
|
|
|
|
echo -e "${GREEN}Success:${NC} Update Requested"
|
2020-05-22 15:28:02 +00:00
|
|
|
update_gs
|
2020-05-22 02:42:37 +00:00
|
|
|
exit
|
|
|
|
;;
|
2020-05-22 02:07:01 +00:00
|
|
|
|
2020-05-22 02:42:37 +00:00
|
|
|
logs)
|
|
|
|
echo -e "${GREEN}Success:${NC} Logs Requested"
|
2020-05-22 15:44:38 +00:00
|
|
|
logs_gs
|
2020-05-22 02:42:37 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo -e "${RED}'$1' is not a valid argument${NC}"
|
|
|
|
echo "Usage: $0 {pull|push}"
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
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
|
|
|
*)
|
|
|
|
echo -e "${RED}Too many arguments provided ($#)${NC}"
|
|
|
|
echo "Usage: $0 {pull|push}"
|
|
|
|
exit 3
|
2020-05-22 16:39:31 +00:00
|
|
|
;;
|
2020-05-21 18:00:42 +00:00
|
|
|
esac
|