2020-05-21 18:00:42 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Gravity Sync by vmstan
|
2020-05-21 18:44:01 +00:00
|
|
|
VERSION='1.1.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
|
|
|
|
|
|
|
# You must change the REMOTE_HOST and REMOTE_USER variables below to match your configuration
|
|
|
|
|
|
|
|
# STANDARD SETTINGS
|
|
|
|
REMOTE_HOST='192.168.7.5'
|
|
|
|
REMOTE_USER='ubuntu'
|
|
|
|
|
|
|
|
# PUSH SETTINGS
|
|
|
|
|
|
|
|
|
|
|
|
# CUSTOMIZATION
|
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
|
|
|
|
|
|
|
# PIHOLE DEFAULTS
|
|
|
|
PIHOLE_DIR='/etc/pihole' # default install directory
|
|
|
|
GRAVITY_FI='gravity.db' # this should not change
|
|
|
|
|
|
|
|
# SCRIPT COLORS
|
2020-05-21 19:09:55 +00:00
|
|
|
RED='\033[0;91m'
|
|
|
|
GREEN='\033[0;92m'
|
|
|
|
CYAN='\033[0;96m'
|
|
|
|
YELLOW='033[0;93m'
|
|
|
|
PURPLE='033[0;95m'
|
2020-05-21 18:00:42 +00:00
|
|
|
NC='\033[0m'
|
|
|
|
|
|
|
|
##############################################
|
|
|
|
|
|
|
|
# print title
|
|
|
|
# echo -e "${GREEN}Gravity Sync ${VERSION}${NC}"
|
|
|
|
|
2020-05-21 19:04:10 +00:00
|
|
|
echo -e "${CYAN}Validating sync folder configuration${NC}"
|
2020-05-21 18:48:23 +00:00
|
|
|
|
|
|
|
# check to see if logging/backup directory is available
|
2020-05-21 18:44:01 +00:00
|
|
|
if [ -d ~/${LOCAL_FOLDR} ]
|
|
|
|
then
|
2020-05-21 18:58:58 +00:00
|
|
|
echo -e "${GREEN}Success${NC}: Required directory ~/${LOCAL_FOLDR} is present"
|
2020-05-21 18:44:01 +00:00
|
|
|
else
|
2020-05-21 18:58:58 +00:00
|
|
|
echo -e "${RED}Failure${NC}: Required directory ~/${LOCAL_FOLDR} is missing"
|
2020-05-21 18:44:01 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-05-21 18:53:12 +00:00
|
|
|
# check to see if current pihole directory is correct
|
2020-05-21 18:56:58 +00:00
|
|
|
if [ -f ${PIHOLE_DIR}/${GRAVITY_FI} ]
|
2020-05-21 18:53:12 +00:00
|
|
|
then
|
|
|
|
echo -e "${GREEN}Success${NC}: Required file ${PIHOLE_DIR}/${GRAVITY_FI} is present"
|
|
|
|
else
|
|
|
|
echo -e "${RED}Failure${NC}: Required file ${PIHOLE_DIR}/${GRAVITY_FI} is missing"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-05-21 18:00:42 +00:00
|
|
|
case $# in
|
|
|
|
0)
|
2020-05-21 19:04:10 +00:00
|
|
|
echo -e "${RED}Failure${NC}: ${GRAVITY_FI} replication direction required"
|
2020-05-21 18:00:42 +00:00
|
|
|
echo "Usage: $0 {pull|push}"
|
2020-05-21 19:10:23 +00:00
|
|
|
echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on $REMOTE_HOST to this server"
|
|
|
|
echo -e "> ${YELLOW}Push${NC} will force any changes made on this server to the primary"
|
2020-05-21 18:00:42 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
1)
|
|
|
|
case $1 in
|
|
|
|
pull)
|
|
|
|
|
|
|
|
echo -e "${CYAN}Copying remote ${GRAVITY_FI} from ${REMOTE_HOST}${NC}"
|
|
|
|
rsync -v -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}
|
|
|
|
echo -e "${CYAN}Replacing gravity.db on HA secondary${NC}"
|
|
|
|
sudo mv -v ${PIHOLE_DIR}/${GRAVITY_FI} ${PIHOLE_DIR}/${GRAVITY_FI}.backup
|
|
|
|
sudo cp -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ${PIHOLE_DIR}
|
|
|
|
sudo chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI}
|
|
|
|
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}
|
|
|
|
echo -e "${CYAN}Reloading configuration of HA secondary FTLDNS from new gravity.db${NC}"
|
|
|
|
pihole restartdns reloadlists
|
|
|
|
pihole restartdns
|
|
|
|
echo -e "${CYAN}Cleaning up things${NC}"
|
|
|
|
mv -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}.last
|
|
|
|
date >> ~/${LOCAL_FOLDR}/${SYNCING_LOG}
|
|
|
|
echo -e "${GREEN}gravity.db pull completed${NC}"
|
|
|
|
;;
|
|
|
|
|
|
|
|
push)
|
|
|
|
echo -e "${RED}WARNING - DATA LOSS POSSIBLE${NC}"
|
|
|
|
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}"
|
|
|
|
rsync --rsync-path="sudo rsync" -v -e 'ssh -p 22' ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI}
|
|
|
|
echo -e "${CYAN}Applying permissions to remote gravity.db${NC}"
|
|
|
|
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}"
|
|
|
|
echo -e "${CYAN}Reloading configuration on remote FTLDNS${NC}"
|
|
|
|
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns reloadlists'
|
|
|
|
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns'
|
|
|
|
echo -e "${GREEN}gravity.db push completed${NC}"
|
|
|
|
break;;
|
|
|
|
No )
|
|
|
|
echo "No changes have been made"
|
|
|
|
exit;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo -e "${RED}'$1' is not a valid argument${NC}"
|
|
|
|
echo "Usage: $0 {pull|push}"
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo -e "${RED}Too many arguments provided ($#)${NC}"
|
|
|
|
echo "Usage: $0 {pull|push}"
|
|
|
|
exit 3
|
|
|
|
;;
|
|
|
|
esac
|