This commit is contained in:
Michael Stanclift 2020-05-21 21:42:37 -05:00
parent 7e53996df8
commit 8c1c38f962

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# Gravity Sync by vmstan # Gravity Sync by vmstan
VERSION='1.1.5' VERSION='1.1.6'
# Must execute from a location in the home folder of the user who own's it (ex: /home/pi/gravity-sync) # 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 # Configure certificate based SSH authentication between the Pihole HA nodes - it does not use passwords
@ -71,106 +71,113 @@ fi
echo -e "${CYAN}Evaluating $0 script arguments${NC}" echo -e "${CYAN}Evaluating $0 script arguments${NC}"
case $# in case $# in
0)
echo -e "${RED}Failure${NC}: ${GRAVITY_FI} replication direction required" 0)
echo "Usage: $0 {pull|push}" echo -e "${RED}Failure${NC}: ${GRAVITY_FI} replication direction required"
echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on $REMOTE_HOST to this server" echo "Usage: $0 {pull|push}"
echo -e "> ${YELLOW}Push${NC} will force any changes made on this server to the primary" echo -e "> ${YELLOW}Pull${NC} will copy the ${GRAVITY_FI} configuration on $REMOTE_HOST to this server"
echo -e "No changes have been made to the system" 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 exit 1
;; ;;
1)
case $1 in
pull)
echo -e "${GREEN}Success${NC}: Pull Requested" 1)
echo -e "${CYAN}Copying ${GRAVITY_FI} from remote server ${REMOTE_HOST}${NC}" case $1 in
rsync -v --progress -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI} pull)
echo -e "${CYAN}Backing up the running ${GRAVITY_FI} on this server${NC}" echo -e "${GREEN}Success${NC}: Pull Requested"
sudo mv -v ${PIHOLE_DIR}/${GRAVITY_FI} ${PIHOLE_DIR}/${GRAVITY_FI}.backup echo -e "${CYAN}Copying ${GRAVITY_FI} from remote server ${REMOTE_HOST}${NC}"
echo -e "${CYAN}Replacing the ${GRAVITY_FI} configuration on this server${NC}" rsync -v --progress -e 'ssh -p 22' ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}
sudo cp -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ${PIHOLE_DIR} echo -e "${CYAN}Backing up the running ${GRAVITY_FI} on this server${NC}"
sudo chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI} sudo mv -v ${PIHOLE_DIR}/${GRAVITY_FI} ${PIHOLE_DIR}/${GRAVITY_FI}.backup
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} echo -e "${CYAN}Replacing the ${GRAVITY_FI} configuration on this server${NC}"
echo -e "${GRAVITY_FI} ownership and file permissions reset" sudo cp -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ${PIHOLE_DIR}
echo -e "${CYAN}Reloading FTLDNS with configuration from new ${GRAVITY_FI}${NC}" sudo chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI}
pihole restartdns reloadlists sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}
pihole restartdns echo -e "${GRAVITY_FI} ownership and file permissions reset"
echo -e "${CYAN}Retaining additional copy of remote ${GRAVITY_FI}${NC}" echo -e "${CYAN}Reloading FTLDNS with configuration from new ${GRAVITY_FI}${NC}"
mv -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}.last pihole restartdns reloadlists
date >> ~/${LOCAL_FOLDR}/${SYNCING_LOG} pihole restartdns
echo -e "${GREEN}gravity.db pull completed${NC}" echo -e "${CYAN}Retaining additional copy of remote ${GRAVITY_FI}${NC}"
exit mv -v ~/${LOCAL_FOLDR}/${GRAVITY_FI} ~/${LOCAL_FOLDR}/${GRAVITY_FI}.last
;; date >> ~/${LOCAL_FOLDR}/${SYNCING_LOG}
echo -e "${GREEN}gravity.db pull completed${NC}"
exit
;;
push) push)
echo -e "${GREEN}Success${NC}: Push Requested" echo -e "${GREEN}Success${NC}: Push Requested"
echo -e "${YELLOW}WARNING: DATA LOSS IS POSSIBLE${NC}" 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 "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 "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}?" echo -e "Are you sure you want to overwrite the primary node configuration on ${REMOTE_HOST}?"
select yn in "Yes" "No"; do select yn in "Yes" "No"; do
case $yn in case $yn in
Yes ) Yes )
echo "Replacing gravity.db on primary" echo "Replacing gravity.db on primary"
echo -e "${CYAN}Copying local ${GRAVITY_FI} to ${REMOTE_HOST}${NC}" echo -e "${CYAN}Copying local ${GRAVITY_FI} to ${REMOTE_HOST}${NC}"
rsync --rsync-path="sudo rsync" -v --progress -e 'ssh -p 22' ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} rsync --rsync-path="sudo rsync" -v --progress -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}" 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 chmod 644 ${PIHOLE_DIR}/${GRAVITY_FI}"
ssh ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${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}" echo -e "${CYAN}Reloading configuration on remote FTLDNS${NC}"
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns reloadlists' ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns reloadlists'
ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns' ssh ${REMOTE_USER}@${REMOTE_HOST} 'pihole restartdns'
echo -e "${GREEN}gravity.db push completed${NC}" echo -e "${GREEN}gravity.db push completed${NC}"
break;; exit
No ) ;;
echo "No changes have been made to the system"
exit;; No )
esac echo "No changes have been made to the system"
done exit
exit ;;
esac
done
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"
# echo -e "${PURPLE}Info:${NC} Gravity Sync ${VERSION}"
# echo -e "${YELLOW}UPDATING REQUIRES ORIGINAL INSTALL VIA GIT${NC}"
echo -e "Are you sure you want to update?"
select yn in "Yes" "No"; do
case $yn in
Yes )
git pull
exit
;;
No )
echo "No changes have been made to the system"
exit
;;
esac
done
exit
;;
logs)
echo -e "${GREEN}Success:${NC} Logs Requested"
echo -e "These are the last three valid PULL timestamps"
tail -n 3 ${SYNCING_LOG}
;;
*)
echo -e "${RED}'$1' is not a valid argument${NC}"
echo "Usage: $0 {pull|push}"
exit 2
;;
esac
;; ;;
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"
# echo -e "${PURPLE}Info:${NC} Gravity Sync ${VERSION}"
# echo -e "${YELLOW}UPDATING REQUIRES ORIGINAL INSTALL VIA GIT${NC}"
echo -e "Are you sure you want to update?"
select yn in "Yes" "No"; do
case $yn in
Yes )
git pull
break;;
No )
echo "No changes have been made to the system"
exit;;
esac
done
exit
;;
logs)
echo -e "${GREEN}Success:${NC} Logs Requested"
echo -e "These are the last three valid PULL timestamps"
tail -n 3 ${SYNCING_LOG}
;;
*) *)
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 -e "${RED}Too many arguments provided ($#)${NC}"
echo "Usage: $0 {pull|push}" echo "Usage: $0 {pull|push}"
exit 3 exit 3
;; ;;
esac esac