Merge pull request #22 from vmstan/development

1.6.0
This commit is contained in:
Michael Stanclift 2020-05-28 22:07:36 -05:00 committed by GitHub
commit f0e3a06c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 394 additions and 202 deletions

View File

@ -1,6 +1,6 @@
# Gravity Sync
## Background
What is better than a [Pi-hole](https://github.com/pi-hole/pi-hole) blocking ads via DNS on your network? That's right, Two Pi-hole! But if you have more than one Pi-hole (PH) in your network you'll want a simple way to keep the list configurations identical between the two.
What is better than a [Pi-hole](https://github.com/pi-hole/pi-hole) blocking ads via DNS on your network? That's right, Two Pi-hole! But if you have more than one Pi-hole in your network you'll want a simple way to keep the list configurations identical between the two.
That's Gravity Sync.
@ -13,7 +13,7 @@ Gravity Sync will **not** overwrite device specific settings such as local netwo
## Prerequisites
Gravity Sync **requires** Pi-hole 5.0 or higher.
You will need to designate one Pi-Hole as primary and one as secondary. This is where you'll make all your configuration changes through the Web UI, doing things such as; manual whitelisting, adding blocklists, device/group management, and other list settings. Gravity Sync will pull the configuration of the primary PH to the secondary. It will also bring over the downloaded blocklist files after a `pihole -g` update on the primary, so you do not need to reach out to all your blocklist hosts for updates after syncing.
You will need to designate one Pi-Hole as primary and one as secondary. This is where you'll make all your configuration changes through the Web UI, doing things such as; manual whitelisting, adding blocklists, device/group management, and other list settings. Gravity Sync will pull the configuration of the primary Pi-hole to the secondary. It will also bring over the downloaded blocklist files after a `pihole -g` update on the primary, so you do not need to reach out to all your blocklist hosts for updates after syncing.
The designation of primary and secondary is purely at your discretion and depends on your desired use case.
@ -44,9 +44,9 @@ Download the latest release from [GitHub](https://github.com/vmstan/gravity-sync
```bash
cd ~
wget https://github.com/vmstan/gravity-sync/archive/v1.5.0.zip
unzip v1.5.0.zip
mv ~/gravity-sync-1.5.0 ~/gravity-sync
wget https://github.com/vmstan/gravity-sync/archive/v1.6.0zip
unzip v1.6.0.zip
mv ~/gravity-sync-1.6.0 ~/gravity-sync
cd gravity-sync
```
@ -95,7 +95,7 @@ Gravity Sync uses SSH to run commands on the primary Pi-hole, and sync the two s
#### Key-Pair Authentication
This is the preferred option, as it's more reliable and less dependant on third party plugins.
You'll need to generate an SSH key for your secondary PH user and copy it to your primary PH. This will allow you to connect to and copy the gravity.db file without needing a password each time. When generating the SSH key, accept all the defaults and do not put a passphrase on your key file.
You'll need to generate an SSH key for your secondary Pi-hole user and copy it to your primary Pi-hole. This will allow you to connect to and copy the gravity.db file without needing a password each time. When generating the SSH key, accept all the defaults and do not put a passphrase on your key file.
*Note: If you already have this setup on your systems for other purposes, you can skip this step.*
@ -104,12 +104,12 @@ ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub REMOTE_USER@REMOTE_HOST
```
Subsitute REMOTE_USER for the account on the primary PH with sudo permissions, and REMOTE_HOST for the IP or DNS name of the PH you have designated as the primary.
Subsitute REMOTE_USER for the account on the primary Pi-hole with sudo permissions, and REMOTE_HOST for the IP or DNS name of the Pi-hole you have designated as the primary.
Make sure to leave the `REMOTE_PASS` variable set to nothing in `gravity-sync.conf` if you want to use key-pair authentication.
#### Password Authentication
This is the non-preferred option, as it depends on an non-standard utility called `sshpass` which must be installed on your secondary PH. Install it using your package manager of choice. The example below is for Raspberry Pi OS (previously Raspbian) or Ubuntu.
This is the non-preferred option, as it depends on an non-standard utility called `sshpass` which must be installed on your secondary Pi-hole. Install it using your package manager of choice. The example below is for Raspberry Pi OS (previously Raspbian) or Ubuntu.
```bash
sudo apt install sshpass
@ -126,13 +126,13 @@ Gravity Sync will validate that the `sshpass` utility is installed on your syste
Save. Keep calm, carry on.
## Execution
Now test the script. You can run a comparison between the two which will be non-distruptive and see if everything has been configured correctly.
Now, test Gravity Sync. You can run a comparison between primary and secondary databases, which will be non-distruptive, and see if everything has been configured correctly.
```bash
./gravity-sync.sh compare
```
Assuming Gravity Sync runs successfully, it'll indicate if there are changes pending between the two databases. If not, I suggest making a subtle change to a whitelist/blacklist on your primary PH, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly.
Assuming Gravity Sync runs successfully, it will indicate if there are changes pending between the two databases. If not, make a subtle change to a whitelist/blacklist on your primary Pi-hole, such as changing a description field or disabling a whitelist item, and then running `./gravity-sync.sh compare` again to validate your installation is working correctly.
### The Pull Function
@ -142,20 +142,31 @@ The Gravity Sync Pull, is the standard method of sync operation, and will not pr
./gravity-sync.sh pull
```
If the execution completes, you will now have overwritten your running gravity.db on the secondary PH after creating a copy of the running database (`gravity.db.backup`) in the `backup` subfolder located with your script. Gravity Sync will also keep a copy of the last sync'd gravity.db from the master, in the `backup` folder identified as `gravity.db.pull` for future use.
If the execution completes, you will now have overwritten your running gravity.db on the secondary Pi-hole after creating a copy of the running database (`gravity.db.backup`) in the `backup` subfolder located with your script. Gravity Sync will also keep a copy of the last sync'd gravity.db from the primary (in the `backup` folder identified as `gravity.db.pull`) for future use.
Finally, a file called `gravity-sync.log` will be created in the `gravity-sync` folder along side the script with the date the script was last executed appended to the bottom.
You can check for successful pull attempts by running: `./gravity-sync.sh logs`
### The Push Function
Gravity Sync includes the ability to `push` from the secondary PH back to the primary. This would be useful in a situation where your primary PH is down for an extended period of time, and you have made list changes on the secondary PH that you want to force back to the primary, when it comes online.
Gravity Sync includes the ability to `push` from the secondary Pi-hole back to the primary. This would be useful in a situation where your primary Pi-hole is down for an extended period of time, and you have made list changes on the secondary Pi-hole that you want to force back to the primary, when it comes online.
```bash
./gravity-sync.sh push
```
Before executing, this will make a copy of the remote database under `backup/gravity.db.push` then sync the local configuration to the primary PH.
Before executing, this will make a copy of the remote database under `backup/gravity.db.push` then sync the local configuration to the primary Pi-hole.
This function purposefuly asks for user interaction to avoid being accidentally automated.
### The Restore Function
Graviy Sync can also `restore` the database on the secondary Pi-hole in the event you've overwritten it accidentally. This might happen in the above scenario where you've had your primary Pi-hole down for an extended period, made changes to the secondary, but perhaps didn't get a chance to perform a `push` of the changes back to the primary, before your automated sync ran.
```bash
./gravity-sync.sh restore
```
This will copy your last `gravity.db.backup` to the running copy on the secondary Pi-hole.
This function purposefuly asks for user interaction to avoid being accidentally automated.
@ -225,3 +236,5 @@ chmod +x gravity-sync.sh
- If you'd like to know what version of the script you have running by running `./gravity-sync.sh version`
- If the update script fails, make sure you did your original deployment via `git clone` and not a manual install.
- If it doesn't kick off, you can manually execute a `git pull` while in the `gravity-sync` directory.
If all else fails, delete the entire `gravity-sync` folder from your system and re-deploy. This will have no impact on your replicated databases.

View File

@ -25,4 +25,5 @@ REMOTE_PASS=''
# SSH_PKIF=''
# LOG_PATH=''
# SYNCING_LOG=''
# CRONJOB_LOG=''
# CRONJOB_LOG=''
# VERIFY_PASS='' # 0=Verify 1=Bypass

View File

@ -2,7 +2,7 @@
# GRAVITY SYNC BY VMSTAN #####################
PROGRAM='Gravity Sync'
VERSION='1.5.0'
VERSION='1.6.0'
# 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
@ -24,10 +24,13 @@ 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
# PH Folder/File Locations
PIHOLE_DIR='/etc/pihole' # default PH data directory
GRAVITY_FI='gravity.db' # default PH database file
PIHOLE_BIN='/usr/local/bin/pihole' # default PH binary directory
# Interaction Customization
VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite
# Pi-hole Folder/File Locations
PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory
GRAVITY_FI='gravity.db' # default Pi-hole database file
PIHOLE_BIN='/usr/local/bin/pihole' # default Pi-hole binary directory
# OS Settings
BASH_PATH='/bin/bash' # default OS bash path
@ -73,12 +76,12 @@ function import_gs {
error_validate
MESSAGE="Using ${REMOTE_USER}@${REMOTE_HOST}"
echo -e "${INFO} ${MESSAGE}"
echo_info
else
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
MESSAGE="${CONFIG_FILE} Missing"
echo -e "${INFO} ${MESSAGE}"
echo_info
TASKTYPE='CONFIG'
config_generate
@ -93,7 +96,7 @@ function update_gs {
# logs_export # dumps log prior to execution because script stops after successful pull
MESSAGE="Requires GitHub Installation"
echo -e "${INFO} ${MESSAGE}"
echo_info
git reset --hard
git pull
exit
@ -105,7 +108,7 @@ function beta_gs {
# logs_export # dumps log prior to execution because script stops after successful pull
MESSAGE="Requires GitHub Installation"
echo -e "${INFO} ${MESSAGE}"
echo_info
git reset --hard
git fetch origin
git pull origin development
@ -115,77 +118,72 @@ function beta_gs {
# Gravity Core Functions
## Pull Function
function pull_gs {
TASKTYPE='PULL'
echo -e "${INFO} ${TASKTYPE} Requested"
md5_compare
echo -e "${INFO} ${TASKTYPE} Commencing"
MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME"
echo -en "${STAT} ${MESSAGE}"
echo_stat
cp ${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup >/dev/null 2>&1
error_validate
MESSAGE="Pulling ${GRAVITY_FI} from ${REMOTE_HOST}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
${SSHPASSWORD} rsync -e "ssh -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"
echo -en "${STAT} ${MESSAGE}"
echo_stat
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Validating Ownership on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}')
if [ $GRAVDB_OWN == "piholepihole" ]
then
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
else
echo -e "\r${FAIL} $MESSAGE"
echo_fail
MESSAGE2="Attempting to Compensate"
echo -e "${INFO} ${MESSAGE2}"
MESSAGE="Attempting to Compensate"
echo_info
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
fi
MESSAGE="Validating Permissions on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
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 -e "\r${GOOD} ${MESSAGE}"
echo_good
else
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
MESSAGE2="Attempting to Compensate"
echo -e "${INFO} ${MESSAGE2}"
MESSAGE="Attempting to Compensate"
echo_info
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
fi
MESSAGE="Inverting Tachyon Pulse"
echo -e "${INFO} ${MESSAGE}"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo -en "${STAT} ${MESSAGE}"
echo_stat
${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1
error_validate
MESSAGE="Reloading FTLDNS Services"
echo -en "${STAT} ${MESSAGE}"
echo_stat
${PIHOLE_BIN} restartdns >/dev/null 2>&1
error_validate
@ -195,66 +193,122 @@ function pull_gs {
## Push Function
function push_gs {
TASKTYPE='PUSH'
echo -e "${INFO} ${TASKTYPE} Requested"
md5_compare
echo -e "${WARN} Are you sure you want to overwrite the primary PH configuration on ${REMOTE_HOST}?"
select yn in "Yes" "No"; do
case $yn in
Yes )
intent_validate
MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}"
echo_stat
${SSHPASSWORD} rsync -e "ssh -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
error_validate
MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}"
echo_stat
${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
echo_stat
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
error_validate
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
error_validate
MESSAGE="Contacting Borg Collective"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo_stat
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1
error_validate
MESSAGE="Reloading FTLDNS Services"
echo_stat
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1
error_validate
logs_export
exit_withchange
}
function restore_gs {
MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!"
echo_warn
intent_validate
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="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}"
echo -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} rsync -e "ssh -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
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
MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}"
echo -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} rsync --rsync-path="sudo rsync" -e "ssh -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${PIHOLE_DIR}/${GRAVITY_FI} ${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
error_validate
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 -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}" >/dev/null 2>&1
error_validate
MESSAGE="Contacting Borg Collective"
echo -e "${INFO} ${MESSAGE}"
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns reloadlists" >/dev/null 2>&1
echo_stat
sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Reloading FTLDNS Services"
echo -en "${STAT} ${MESSAGE}"
${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "${PIHOLE_BIN} restartdns" >/dev/null 2>&1
error_validate
logs_export
exit_withchange
;;
fi
No )
exit_nochange
;;
esac
done
MESSAGE="Evacuating Saucer Section"
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
}
# Logging Functions
## Core Logging
### Write Logs Out
function logs_export {
echo -e "${INFO} Logging Timestamps to ${SYNCING_LOG}"
MESSAGE="Logging Timestamps to ${SYNCING_LOG}"
echo_info
echo -e $(date) "[${TASKTYPE}]" >> ${LOG_PATH}/${SYNCING_LOG}
}
@ -262,13 +316,16 @@ function logs_export {
function logs_gs {
import_gs
echo -e "${INFO} Tailing ${LOG_PATH}/${SYNCING_LOG}"
MESSAGE="Tailing ${LOG_PATH}/${SYNCING_LOG}"
echo_info
echo -e "========================================================"
echo -e "Recent Complete ${YELLOW}PULL${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PULL
echo -e "Recent Complete ${YELLOW}PUSH${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PUSH
echo -e "========================================================"
exit_nochange
}
@ -278,28 +335,37 @@ function show_crontab {
import_gs
MESSAGE="Replaying Last Cronjob"
echo -en "${STAT} ${MESSAGE}"
echo_stat
if [ -f ${LOG_PATH}/${CRONJOB_LOG} ]
then
if [ -s ${LOG_PATH}/${CRONJOB_LOG} ]
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
echo -e "${INFO} Tailing ${LOG_PATH}/${CRONJOB_LOG}"
MESSAGE="Tailing ${LOG_PATH}/${CRONJOB_LOG}"
echo_info
echo -e "========================================================"
date -r ${LOG_PATH}/${CRONJOB_LOG}
cat ${LOG_PATH}/${CRONJOB_LOG}
echo -e "========================================================"
exit_nochange
exit_nochange
then
echo -e "\r${FAIL} ${MESSAGE}"
echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} appears empty"
exit_nochange
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Empty"
echo_info
exit_nochange
fi
else
echo -e "\r${FAIL} ${MESSAGE}"
echo -e "${INFO} ${LOG_PATH}/${CRONJOB_LOG} not yet created"
exit_nochange
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Missing"
echo_info
exit_nochange
fi
}
@ -307,35 +373,35 @@ function show_crontab {
## Validate GS Folders
function validate_gs_folders {
MESSAGE="Locating $HOME/${LOCAL_FOLDR}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
if [ -d $HOME/${LOCAL_FOLDR} ]
then
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
else
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
exit_nochange
fi
MESSAGE="Locating $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
if [ -d $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD} ]
then
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
else
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
exit_nochange
fi
}
## Validate PH Folders
## Validate Pi-hole Folders
function validate_ph_folders {
MESSAGE="Locating ${PIHOLE_DIR}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
if [ -d ${PIHOLE_DIR} ]
then
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
else
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
exit_nochange
fi
}
@ -343,7 +409,7 @@ function validate_ph_folders {
## Validate SSHPASS
function validate_os_sshpass {
MESSAGE="Checking SSH Configuration"
echo -e "${INFO} ${MESSAGE}"
echo_info
if hash sshpass 2>/dev/null
then
@ -367,10 +433,10 @@ function validate_os_sshpass {
MESSAGE="Using SSH Key-Pair Authentication"
fi
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Testing SSH Connection"
echo -en "${STAT} ${MESSAGE}"
echo_stat
timeout 5 ${SSHPASSWORD} ssh -p ${SSH_PORT} -i '$HOME/${SSH_PKIF}' -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1
error_validate
}
@ -378,33 +444,72 @@ function validate_os_sshpass {
## Error Validation
function error_validate {
if [ "$?" != "0" ]; then
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
exit 1
else
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
fi
}
## Validate Sync Required
function md5_compare {
echo -e "${INFO} Comparing ${GRAVITY_FI} Changes"
MESSAGE="Comparing ${GRAVITY_FI} Changes"
echo_info
MESSAGE="Analyzing Remote ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
primaryMD5=$(${SSHPASSWORD} ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}")
error_validate
MESSAGE="Analyzing Local ${GRAVITY_FI}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
secondMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI})
error_validate
if [ "$primaryMD5" == "$secondMD5" ]
then
echo -e "${INFO} No Differences in ${GRAVITY_FI}"
MESSAGE="No Differences in ${GRAVITY_FI}"
echo_info
exit_nochange
else
echo -e "${INFO} Changes Detected in ${GRAVITY_FI}"
MESSAGE="Changes Detected in ${GRAVITY_FI}"
echo_info
fi
}
## Validate Intent
function intent_validate {
if [ "$VERIFY_PASS" == "0" ]
then
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
read INPUT_INTENT
if [ "${INPUT_INTENT}" != "${INTENT}" ]
then
MESSAGE="${TASKTYPE} Aborted"
echo_info
exit_nochange
fi
else
MESSAGE="Verification Bypassed"
echo_warn
fi
}
@ -412,53 +517,53 @@ function md5_compare {
## Generate New Configuration
function config_generate {
MESSAGE="Creating ${CONFIG_FILE} from Template"
echo -en "${STAT} ${MESSAGE}"
echo_stat
cp $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}.example $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
MESSAGE="Enter IP or DNS of primary Pi-hole server: "
echo -en "${NEED} ${MESSAGE}"
MESSAGE="Enter IP or DNS of primary Pi-hole server"
echo_need
read INPUT_REMOTE_HOST
MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server: "
echo -en "${NEED} ${MESSAGE}"
MESSAGE="Enter SSH user with SUDO rights on primary Pi-hole server"
echo_need
read INPUT_REMOTE_USER
MESSAGE="Saving Host to ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
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}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
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"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Do you want to configure password based SSH authentication?"
echo -e "${WARN} ${MESSAGE}"
echo_warn
MESSAGE="Your password will be stored clear-text in the ${CONFIG_FILE}!"
echo -e "${WARN} ${MESSAGE}"
echo_warn
MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication: "
echo -en "${NEED} ${MESSAGE}"
MESSAGE="Leave blank to use (preferred) SSH Key-Pair Authentication"
echo_need
read INPUT_REMOTE_PASS
MESSAGE="Saving Password to ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
sed -i "/REMOTE_PASS=''/c\REMOTE_PASS='${INPUT_REMOTE_PASS}'" $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
else
MESSAGE="SSHPASS Not Installed"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Defaulting to SSH Key-Pair Authentication"
echo -e "${INFO} ${MESSAGE}"
echo_info
fi
if [ -z $INPUT_REMOTE_PASS ]
@ -466,13 +571,13 @@ function config_generate {
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Using Existing ~/${SSH_PKIF}"
echo -e "${INFO} ${MESSAGE}"
echo_info
else
MESSAGE="Generating ~/${SSH_PKIF}"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Accept All Defaults"
echo -e "${WARN} ${MESSAGE}"
echo_warn
MESSAGE="Complete Key-Pair Creation"
echo -e "${NEED} ${MESSAGE}"
@ -486,7 +591,7 @@ function config_generate {
fi
MESSAGE="Importing New ${CONFIG_FILE}"
echo -en "${STAT} ${MESSAGE}"
echo_stat
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
@ -495,7 +600,7 @@ function config_generate {
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Registering Key-Pair on ${REMOTE_HOST}"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Enter ${REMOTE_USER}@${REMOTE_HOST} Password Below"
echo -e "${NEED} ${MESSAGE}"
@ -520,37 +625,30 @@ function config_generate {
function config_delete {
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
MESSAGE="Configuration File Exists"
echo -e "${WARN} ${MESSAGE}"
echo_warn
echo -e "========================================================"
cat $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
echo -e "========================================================"
MESSAGE="Are you sure you want to erase this configuration?"
echo -e "${WARN} ${MESSAGE}"
select yn in "Yes" "No"; do
case $yn in
Yes )
MESSAGE="Erasing Existing Configuration"
echo -en "${STAT} ${MESSAGE}"
rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
config_generate
;;
echo_warn
No )
exit_nochange
;;
esac
done
intent_validate
MESSAGE="Erasing Existing Configuration"
echo_stat
rm -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
config_generate
}
# Exit Codes
## No Changes Made
function exit_nochange {
echo -e "${INFO} ${PROGRAM} ${TASKTYPE} Aborting"
MESSAGE="${PROGRAM} ${TASKTYPE} Aborting"
echo_info
exit 0
}
@ -558,7 +656,7 @@ function exit_nochange {
function exit_withchange {
SCRIPT_END=$SECONDS
MESSAGE="${PROGRAM} ${TASKTYPE} Completed in $((SCRIPT_END-SCRIPT_START)) seconds"
echo -e "${INFO} ${MESSAGE}"
echo_info
exit 0
}
@ -572,8 +670,9 @@ function list_gs_arguments {
echo -e " ${YELLOW}automate${NC} Add scheduled task to run sync"
echo -e ""
echo -e "Replication Options:"
echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary PH to this server"
echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary PH"
echo -e " ${YELLOW}pull${NC} Sync the ${GRAVITY_FI} database on primary Pi-hole to this server"
echo -e " ${YELLOW}push${NC} Force any changes made on this server back to the primary Pi-hole"
echo -e " ${YELLOW}restore${NC} Restore ${GRAVITY_FI} on this server from previous copy"
echo -e " ${YELLOW}compare${NC} Just check for differences between primary and secondary"
echo -e ""
echo -e "Update Options:"
@ -590,14 +689,15 @@ function list_gs_arguments {
# Output Version
function show_version {
echo -e "${INFO} ${PROGRAM} ${VERSION}"
MESSAGE="${PROGRAM} ${VERSION}"
echo_info
}
# Task Stack
## Automate Task
function task_automate {
TASKTYPE='AUTOMATE'
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
import_gs
@ -605,14 +705,14 @@ function task_automate {
if [ ${CRON_CHECK} == 1 ]
then
MESSAGE="Automation Task Already Exists"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="Use 'crontab -e' to manually remove/edit"
echo -e "${INFO} ${MESSAGE}"
echo_info
exit_nochange
fi
MESSAGE="Set Automation Frequency Per Hour"
echo -e "${INFO} ${MESSAGE}"
echo_info
MESSAGE="1 = Every 60 Minutes"
echo -e "++++++ ${MESSAGE}"
@ -625,8 +725,8 @@ function task_automate {
MESSAGE="12 = Every 05 Minutes"
echo -e "++++++ ${MESSAGE}"
MESSAGE="Input Automation Frequency: "
echo -en "${NEED} ${MESSAGE}"
MESSAGE="Input Automation Frequency"
echo_need
read INPUT_AUTO_FREQ
if [ $INPUT_AUTO_FREQ == 1 ]
@ -651,35 +751,72 @@ function task_automate {
fi
MESSAGE="Saving to Crontab"
echo -en "${STAT} ${MESSAGE}"
echo_stat
(crontab -l 2>/dev/null; echo "*/${AUTO_FREQ} * * * * ${BASH_PATH} $HOME/${LOCAL_FOLDR}/${GS_FILENAME} pull > ${LOG_PATH}/${CRONJOB_LOG}") | crontab -
error_validate
exit_withchange
}
# Echo Stack
## Informative
function echo_info {
echo -e "${INFO} ${MESSAGE}"
}
## Warning
function echo_warn {
echo -e "${WARN} ${MESSAGE}"
}
## 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}: "
}
# SCRIPT EXECUTION ###########################
SCRIPT_START=$SECONDS
MESSAGE="Evaluating Script Arguments"
echo -en "${STAT} ${MESSAGE}"
echo_stat
case $# in
0)
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
list_gs_arguments
;;
1)
case $1 in
pull)
echo -e "\r${GOOD} ${MESSAGE}"
TASKTYPE='PULL'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
import_gs
MESSAGE="Validating Folder Configuration"
echo -e "${INFO} ${MESSAGE}"
echo_info
validate_gs_folders
validate_ph_folders
validate_os_sshpass
@ -689,11 +826,16 @@ case $# in
;;
push)
echo -e "\r${GOOD} ${MESSAGE}"
TASKTYPE='PUSH'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
import_gs
echo -e "${INFO} Validating Folder Configuration"
MESSAGE="Validating Folder Configuration"
echo_info
validate_gs_folders
validate_ph_folders
validate_os_sshpass
@ -701,50 +843,81 @@ case $# in
push_gs
exit
;;
restore)
TASKTYPE='RESTORE'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
import_gs
MESSAGE="Validating Folder Configuration"
echo_info
validate_gs_folders
validate_ph_folders
# validate_os_sshpass
restore_gs
exit
;;
version)
TASKTYPE='VERSION'
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
show_version
exit_nochange
;;
update)
# TASKTYPE='UPDATE'
echo -e "\r${GOOD} ${MESSAGE}"
TASKTYPE='UPDATE'
echo_good
echo -e "${INFO} Update Requested"
update_gs
MESSAGE="${TASKTYPE} Requested"
echo_info
update_gs
exit_nochange
;;
beta)
# TASKTYPE='BETA'
echo -e "\r${GOOD} ${MESSAGE}"
TASKTYPE='BETA'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
echo -e "${INFO} Beta Update Requested"
beta_gs
beta_gs
exit_nochange
;;
logs)
TASKTYPE='LOGS'
echo_good
echo -e "\r${GOOD} ${MESSAGE}"
MESSAGE="Logs Requested"
echo -e "${INFO} ${MESSAGE}"
logs_gs
MESSAGE="${TASKTYPE} Requested"
echo_info
logs_gs
;;
compare)
TASKTYPE='COMPARE'
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
echo -e "\r${GOOD} ${MESSAGE}"
import_gs
import_gs
echo -e "${INFO} Validating Folder Configuration"
MESSAGE="Validating OS Configuration"
echo_info
validate_gs_folders
validate_ph_folders
validate_os_sshpass
@ -754,15 +927,20 @@ case $# in
cron)
TASKTYPE='CRON'
echo -e "\r${GOOD} ${MESSAGE}"
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
show_crontab
;;
config)
TASKTYPE='CONFIGURE'
echo -e "\r${GOOD} ${MESSAGE}"
echo -e "${INFO} Entering ${TASKTYPE} Mode"
echo_good
MESSAGE="${TASKTYPE} Requested"
echo_info
if [ -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} ]
then
@ -770,7 +948,7 @@ case $# in
else
MESSAGE="${CONFIG_FILE} Missing"
echo -e "${INFO} ${MESSAGE}"
echo_info
config_generate
fi
@ -785,14 +963,14 @@ case $# in
;;
*)
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
list_gs_arguments
;;
esac
;;
*)
echo -e "\r${FAIL} ${MESSAGE}"
echo_fail
list_gs_arguments
;;
esac