diff --git a/VERSION b/VERSION index 1ac53bb..240bba9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.6.3 \ No newline at end of file +3.7.0 \ No newline at end of file diff --git a/gravity-sync.sh b/gravity-sync.sh index 86644f7..de9f9f4 100755 --- a/gravity-sync.sh +++ b/gravity-sync.sh @@ -3,7 +3,7 @@ SCRIPT_START=$SECONDS # GRAVITY SYNC BY VMSTAN ##################### PROGRAM='Gravity Sync' -VERSION='3.6.3' +VERSION='3.7.0' # For documentation or downloading updates visit https://github.com/vmstan/gravity-sync # Requires Pi-Hole 5.x or higher already be installed, for help visit https://pi-hole.net @@ -71,6 +71,7 @@ HISTORY_MD5='gravity-sync.md5' # replace in gravity-sync.conf to overwrite # OS Settings BASH_PATH='/bin/bash' # default OS bash path +DAEMON_PATH='/etc/systemd/system' # systemd timer/service folder ############################################## ### NEVER CHANGE ANYTHING BELOW THIS LINE! ### @@ -148,7 +149,7 @@ case $# in task_compare ;; cron) start_gs - task_cron ;; + task_autocron ;; config|configure) start_gs_noconfig task_configure ;; diff --git a/includes/gs-automate.sh b/includes/gs-automate.sh index 0259c93..0aa632c 100644 --- a/includes/gs-automate.sh +++ b/includes/gs-automate.sh @@ -10,6 +10,61 @@ function task_automate { MESSAGE="${MESSAGE}: ${TASKTYPE}" echo_good + CRON_EXIST='0' + CRON_CHECK=$(crontab -l | grep -q "${GS_FILENAME}" && echo '1' || echo '0') + if [ ${CRON_CHECK} == 1 ] + then + MESSAGE="${UI_AUTO_CRON_EXISTS}" + echo_warn + clear_cron + fi + + MESSAGE="Customizing service file username" + sed -i "/User=unknown/c\User=$USER" ${LOCAL_FOLDR}/templates/gravity-sync.service + error_validate + + MESSAGE="Customizing service file executable" + sed -i "/ExecStart=/c\ExecStart=${LOCAL_FOLDR}/${GS_FILENAME}" ${LOCAL_FOLDR}/templates/gravity-sync.service + error_validate + + if systemctl is-active --quiet gravity-sync + then + MESSAGE="Stopping existing systemd service" + sudo systemctl stop gravity-sync + error_validate + fi + + MESSAGE="Moving systemd timer into place" + sudo cp ${LOCAL_FOLDR}/templates/gravity-sync.timer ${DAEMON_PATH} + error_validate + + MESSAGE="Moving systemd service into place" + sudo cp ${LOCAL_FOLDR}/templates/gravity-sync.service ${DAEMON_PATH} + error_validate + + MESSAGE="Reloading systemd daemon" + sudo systemctl daemon-reload --quiet + error_validate + + MESSAGE="Enabling Gravity Sync timer" + sudo systemctl enable gravity-sync.timer --quiet + error_validate + + MESSAGE="Starting Gravity Sync service" + sudo systemctl start gravity-sync --quiet + error_validate + + exit_withchange +} + +function task_autocron { + TASKTYPE='AUTOCRON' + MESSAGE="${MESSAGE}: ${TASKTYPE}" + echo_good + + MESSAGE="Crontab automation is deprecated and will be removed in a future release" + echo_warn + CRON_EXIST='0' CRON_CHECK=$(crontab -l | grep -q "${GS_FILENAME}" && echo '1' || echo '0') if [ ${CRON_CHECK} == 1 ] diff --git a/includes/gs-exit.sh b/includes/gs-exit.sh index 453aee2..5d11416 100644 --- a/includes/gs-exit.sh +++ b/includes/gs-exit.sh @@ -43,7 +43,8 @@ function list_gs_arguments { echo_lines echo -e "Setup Options:" echo -e " ${YELLOW}config${NC} Creates a new ${PROGRAM} configuration file" - echo -e " ${YELLOW}automate${NC} Schedules the ${PROGRAM} replication task using crontab" + echo -e " ${YELLOW}automate${NC} Schedules the ${PROGRAM} replication task using systemd" + echo -e " ${YELLOW}cron${NC} Schedules the ${PROGRAM} replication task using crontab (legacy)" echo -e " ${YELLOW}version${NC} Shows the installed version of ${PROGRAM} and check for updates" echo -e " ${YELLOW}update${NC} Upgrades ${PROGRAM} to the latest available version using Git" echo -e " ${YELLOW}dev${NC} Sets update command to use a development version of ${PROGRAM}" @@ -57,7 +58,6 @@ function list_gs_arguments { echo_blank echo -e "Debug Options:" echo -e " ${YELLOW}logs${NC} Shows the recent successful replication jobs/times" - echo -e " ${YELLOW}cron${NC} Displays the output of last crontab execution" echo -e " ${YELLOW}info${NC} Shows information about the current configuration" echo_lines exit_nochange diff --git a/includes/gs-ui.sh b/includes/gs-ui.sh index 095886c..8946f7f 100644 --- a/includes/gs-ui.sh +++ b/includes/gs-ui.sh @@ -112,11 +112,11 @@ UI_PURGE_DELETE_SSH_KEYS='Deleting SSH key-files' UI_PURGE_CLEANING_DIR="Purging ${PROGRAM} directory" # Automation -UI_AUTO_CRON_EXISTS='Automation task already exists in crontab' +UI_AUTO_CRON_EXISTS='Automation task exists in crontab' UI_AUTO_CRON_DISPLAY_FREQ='Select synchronization frequency (in minutes)' UI_AUTO_CRON_SELECT_FREQ='Valid options are 5, 10, 15, 30 or 0 to disable (default: 15)' UI_AUTO_CRON_SAVING='Saving new synchronization task to crontab' -UI_AUTO_CRON_DISABLED='Synchronization automation has been disabled' +UI_AUTO_CRON_DISABLED='Automation with crontab has been disabled' # Configuration UI_CONFIG_YESNOY="'Yes' or 'No', blank is default 'Yes'" diff --git a/templates/gravity-sync.conf.example b/templates/gravity-sync.conf.example index f8b60bc..c83d073 100644 --- a/templates/gravity-sync.conf.example +++ b/templates/gravity-sync.conf.example @@ -60,4 +60,5 @@ REMOTE_USER='pi' # HISTORY_MD5='' # replace in gravity-sync.conf to overwrite ### OS Settings -# BASH_PATH='' # default OS bash path \ No newline at end of file +# BASH_PATH='' # default OS bash path +# DAEMON_PATH='' # systemd timer/service folder \ No newline at end of file diff --git a/templates/gravity-sync.service b/templates/gravity-sync.service new file mode 100644 index 0000000..cf6c322 --- /dev/null +++ b/templates/gravity-sync.service @@ -0,0 +1,12 @@ +[Unit] +Description=Synchronize Pi-hole instances +After=network-online.target +Wants=gravity-sync.timer + +[Service] +Type=simple +User=unknown +ExecStart= + +[Install] +WantedBy=multi-user.target diff --git a/templates/gravity-sync.timer b/templates/gravity-sync.timer new file mode 100644 index 0000000..38bbf24 --- /dev/null +++ b/templates/gravity-sync.timer @@ -0,0 +1,15 @@ +[Unit] +Description=Synchronize Pi-hole instances +RefuseManualStart=no +RefuseManualStop=no +Requires=gravity-sync.service + +[Timer] +Unit=gravity-sync.service +Persistent=true +OnBootSec=120 +OnUnitInactiveSec=5m +RandomizedDelaySec=5m + +[Install] +WantedBy=timers.target