From d5ededce123c916ff07f94e3483fd4f7a3e957e9 Mon Sep 17 00:00:00 2001 From: Michael Stanclift Date: Tue, 7 Jul 2020 15:15:42 -0500 Subject: [PATCH] checks for history_md5 existing --- ADVANCED.md | 2 +- CHANGELOG.md | 2 +- gravity-sync.sh | 56 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/ADVANCED.md b/ADVANCED.md index e3f3ea8..c9c408a 100644 --- a/ADVANCED.md +++ b/ADVANCED.md @@ -173,5 +173,5 @@ If you prefer to still use cron but modify your settings by hand, using the entr ```bash crontab -e -*/30 * * * * /bin/bash /home/USER/gravity-sync/gravity-sync.sh pull > /home/USER/gravity-sync/gravity-sync.cron +*/30 * * * * /bin/bash /home/USER/gravity-sync/gravity-sync.sh > /home/USER/gravity-sync/gravity-sync.cron ``` diff --git a/CHANGELOG.md b/CHANGELOG.md index c17a8e9..22197e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ For 2.0, the `push` and `pull` functions have been broken up for each component Example: If the `gravity.db` has been modified on the primary Pi-hole, but the `custom.list` file has been changed on the secondary, Gravity Sync will now do a pull and then push for each part, and then restart the correct components on each server. It will also only perform a sync of each component if there are changes within each type to replicate. -This allows you to be more flexible in where you make your configuration changes to block/allow lists and local DNS settings being made on either the primary or secondary, but it's best practice to continue making changes on the primary server where possible. In the event there are configuration changes to the same element (`custom.list` changes at both sides) then the primary will still be the authoritative source and overwrite the secondary copy. +This allows you to be more flexible in where you make your configuration changes to block/allow lists and local DNS settings being made on either the primary or secondary, but it's best practice to continue making changes on the primary server where possible. In the event there are configuration changes to the same element (`custom.list` changes at both sides) then Gravity Sync will attempt to determine based on timestamps on what side the last changed happened, in which case the latest changes will be considered authoritative and overwrite the other side. Existing users who want to use this new method as their standard should run `./gravity-sync.sh automate` function to replace the existing automated `pull` with the new Smart Sync. You can also continue to use `pull` in your automation if you choose. New installs will use the smart Sync function by default. Running `./gravity-sync.sh` without any arguments will now run the smart Sync function. diff --git a/gravity-sync.sh b/gravity-sync.sh index a64bd6c..9f6096c 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -397,10 +397,18 @@ function push_gs { function smart_gs { md5_compare - last_primaryDBMD5=$(sed "1q;d" ${HISTORY_MD5}) - last_secondDBMD5=$(sed "2q;d" ${HISTORY_MD5}) - last_primaryCLMD5=$(sed "3q;d" ${HISTORY_MD5}) - last_secondCLMD5=$(sed "4q;d" ${HISTORY_MD5}) + if [ -f "${LOG_PATH}/${HISTORY_MD5}" ] + then + last_primaryDBMD5=$(sed "1q;d" ${LOG_PATH}/${HISTORY_MD5}) + last_secondDBMD5=$(sed "2q;d" ${LOG_PATH}/${HISTORY_MD5}) + last_primaryCLMD5=$(sed "3q;d" ${LOG_PATH}/${HISTORY_MD5}) + last_secondCLMD5=$(sed "4q;d" ${LOG_PATH}/{HISTORY_MD5}) + else + last_primaryDBMD5="0" + last_secondDBMD5="0" + last_primaryCLMD5="0" + last_secondCLMD5="0" + fi PRIDBCHANGE="0" SECDBCHANGE="0" @@ -421,7 +429,7 @@ function smart_gs { then if [ "${PRIDBCHANGE}" != "0" ] then - MESSAGE="Both Sides Have Changed" + MESSAGE="Both Sides Gravity Database Changed" echo_warn PRIDBDATE=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "stat -c %Y ${PIHOLE_DIR}/${GRAVITY_FI}") @@ -429,13 +437,13 @@ function smart_gs { if [ "${PRIDBDATE}" -gt "$SECDBDATE" ] then - MESSAGE="Primary was Last Changed" + MESSAGE="Primary Gravity Database Last Changed" echo_info pull_gs_grav PULLRESTART="1" else - MESSAGE="Secondary was Last Changed" + MESSAGE="Secondary Gravity Database Last Changed" echo_info push_gs_grav @@ -468,7 +476,7 @@ function smart_gs { then if [ "${PRICLCHANGE}" != "0" ] then - MESSAGE="Both Sides Have Changed" + MESSAGE="Both Sides Custom DNS Changed" echo_warn PRICLDATE=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "stat -c %Y ${PIHOLE_DIR}/${CUSTOM_DNS}") @@ -476,13 +484,13 @@ function smart_gs { if [ "${PRICLDATE}" -gt "$SECCLDATE" ] then - MESSAGE="Primary was Last Changed" + MESSAGE="Primary Custom DNS Last Changed" echo_info pull_gs_cust PULLRESTART="1" else - MESSAGE="Secondary was Last Changed" + MESSAGE="Secondary Custom DNS Last Changed" echo_info push_gs_cust @@ -639,11 +647,11 @@ function restore_gs { function logs_export { MESSAGE="Saving File Hashes" echo_stat - rm -f gravity-sync.md5 - echo -e ${primaryDBMD5} >> gravity-sync.md5 - echo -e ${secondDBMD5} >> gravity-sync.md5 - echo -e ${primaryCLMD5} >> gravity-sync.md5 - echo -e ${secondCLMD5} >> gravity-sync.md5 + rm -f ${LOG_PATH}/${HISTORY_MD5} + echo -e ${primaryDBMD5} >> ${LOG_PATH}/${HISTORY_MD5} + echo -e ${secondDBMD5} >> ${LOG_PATH}/${HISTORY_MD5} + echo -e ${primaryCLMD5} >> ${LOG_PATH}/${HISTORY_MD5} + echo -e ${secondCLMD5} >> ${LOG_PATH}/${HISTORY_MD5} error_validate MESSAGE="Logging Successful ${TASKTYPE}" @@ -1623,7 +1631,7 @@ function root_check { case $# in 0) - TASKTYPE='SYNC' + TASKTYPE='SMART' MESSAGE="${MESSAGE}: ${TASKTYPE} Requested" echo_good @@ -1639,7 +1647,21 @@ case $# in 1) case $1 in sync) - TASKTYPE='SYNC' + TASKTYPE='SMART' + MESSAGE="${MESSAGE}: ${TASKTYPE} Requested" + echo_good + + import_gs + validate_gs_folders + validate_ph_folders + validate_os_sshpass + + smart_gs + exit + ;; + + smart) + TASKTYPE='SMART' MESSAGE="${MESSAGE}: ${TASKTYPE} Requested" echo_good