diff --git a/dynamix.unraid.net.plg b/dynamix.unraid.net.plg index 6d1a5eabc..1bc73ee57 100644 --- a/dynamix.unraid.net.plg +++ b/dynamix.unraid.net.plg @@ -10,7 +10,7 @@ ]> - + ##&name; @@ -23,6 +23,7 @@ if [ -e /etc/rc.d/rc.unraid-api ]; then + /etc/rc.d/rc.flash_backup stop /etc/rc.d/rc.unraid-api uninstall rm -f /etc/rc.d/rc.unraid-api rm -f /etc/rc.d/rc.flash_backup @@ -601,16 +602,20 @@ response_complete($httpcode, $result, 'success'); # This file is /etc/rc.d/rc.flash_backup # use at queue "f" for flash backup QUEUE=" -q f " -INOTIFY="inotifywait -m -r /boot @/boot/.git -e modify -e move -e create -e delete" -TASKCMD="php /usr/local/emhttp/plugins/dynamix.unraid.net/include/UpdateFlashBackup.php update" +TASKNAME="/etc/rc.d/rc.flash_backup watch" +TASKACTION="php /usr/local/emhttp/plugins/dynamix.unraid.net/include/UpdateFlashBackup.php update" status() { - local flash_backup_pid=$(pgrep --full "${INOTIFY}") + local flash_backup_pid=$(pgrep --full "${TASKNAME}") if [[ $flash_backup_pid ]]; then echo "flash backup monitor is running." _hasqueue && echo "changes detected, backup queued" exit 0 else - echo "No processes are running." + if _enabled; then + echo "flash backup is enabled but the monitor is not running" + else + echo "flash backup is disabled so the monitor is disabled" + fi exit 1 fi } @@ -618,16 +623,16 @@ start() { # only run if flash_backup is enabled _enabled || exit 1 # terminate watcher loop/process - pkill --full "${INOTIFY}" &>/dev/null + pkill --full "${TASKNAME}" &>/dev/null # flush: this will ensure we start with a clean repo flush # start watcher loop as background process - _watch &>/dev/null & + exec ${TASKNAME} &>/dev/null & exit 0 } stop() { # terminate watcher loop/process - pkill --full "${INOTIFY}" &>/dev/null + pkill --full "${TASKNAME}" &>/dev/null # remove any queued jobs and flush changes flush exit 0 @@ -644,17 +649,16 @@ flush() { # remove any queued jobs _removequeue # push any changes ad-hoc - echo ${TASKCMD} | at ${QUEUE} now + echo ${TASKACTION} | at ${QUEUE} now &>/dev/null } _watch() { - # start inotify watcher loop - ${INOTIFY} | - while read path action file; do - #_removequeue - #logger '/boot changes detected, (re)starting 1 min countdown before backing up' --tag flash_backup - # create a new at job in queue f - _hasqueue || ( logger 'adding task: ${TASKCMD}' --tag flash_backup; echo ${TASKCMD} | at ${QUEUE} now +1 minute ) - done + # start watcher loop + while true; do + if [ "$(git -C /boot status -s)" ]; then + _hasqueue || ( logger "adding task: ${TASKACTION}" --tag flash_backup; echo ${TASKACTION} | at ${QUEUE} now +1 minute &>/dev/null ) + fi + sleep 60; + done } _hasqueue() { # returns false if the queue is empty, true otherwise @@ -685,7 +689,7 @@ case "$1" in start ;; 'stop') - stop true + stop ;; 'reload') reload @@ -693,6 +697,9 @@ case "$1" in 'flush') flush ;; +'watch') + _watch + ;; *) echo "usage $0 status|start|stop|reload|flush" esac @@ -743,10 +750,13 @@ function response_complete($httpcode, $result, $cli_success_msg='') { // deactivate if ($cli) { if ($argc > 1) $command = $argv[1]; + if ($argc > 2) $command = $argv[2]; } else { $command = $_POST['command']; + $commitmsg = $_POST['commitmsg']; } if (empty($command)) $command='init'; +if (empty($commitmsg)) $commitmsg='Config change'; // keyfile $var = parse_ini_file("/var/local/emhttp/var.ini"); @@ -826,7 +836,7 @@ IdentitiesOnly yes // add our server as a known host if (!file_exists('/root/.ssh/known_hosts') || strpos(file_get_contents('/root/.ssh/known_hosts'),'backup.unraid.net,54.70.72.154') === false) { - file_put_contents('/root/.ssh/known_hosts', 'backup.unraid.net,54.70.72.154 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKrKXKQwPZTY25MoveIw7fZ3IoZvvffnItrx6q7nkNriDMr2WAsoxu0DrU2QrSLH5zFF1ibv4tChS1hOpiYObiI=', FILE_APPEND); + file_put_contents('/root/.ssh/known_hosts', 'backup.unraid.net,54.70.72.154 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKrKXKQwPZTY25MoveIw7fZ3IoZvvffnItrx6q7nkNriDMr2WAsoxu0DrU2QrSLH5zFF1ibv4tChS1hOpiYObiI='."\n", FILE_APPEND); chmod('/root/.ssh/known_hosts', 0644); } @@ -912,7 +922,7 @@ if (($command == 'update') || ($command == 'reinit')) { exec('git -C /boot commit -m \'Initial commit\' &>/dev/null'); exec('git -C /boot push --force --set-upstream origin master &>/dev/null'); } else { - exec('git -C /boot commit -m \'Config change\' &>/dev/null'); + exec('git -C /boot commit -m ' . escapeshellarg($commitmsg) . ' &>/dev/null'); exec('git -C /boot push --set-upstream origin master &>/dev/null'); } }