From 8a16a2233edc1af87c2be054a9931d8529e027e8 Mon Sep 17 00:00:00 2001 From: deajan Date: Mon, 18 Nov 2013 22:44:20 +0100 Subject: [PATCH] Added monitor mode --- CHANGELOG.md | 2 ++ README.md | 5 +++++ osync.sh | 39 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0479da9..6b191b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ SHORT FUTURE IMPROVEMENTS (post v1.0) - Sync and delete propagation function merge (master and slave functions are the same, reduces code size and maintain effort) - Tree function merge (current and after tree functions are the same except for output filename and logging, reduces code size and maintain effort) - Tree functions execute piped commands (grep, awk) on master when launched on remote slave which can cause more bandwith usage +- Daemonize osync --on-changes mode FAR FUTURE IMPROVEMENTS ----------------------- @@ -20,6 +21,7 @@ KNOWN ISSUES RECENT CHANGES -------------- +- Added monitor mode, which will launch a sync task upon file operations on master replica - Changed conf file default format for ssh uri (old format is still compatible) - Added ssh uri support for slave replicas - Improved execution hooks logs diff --git a/README.md b/README.md index 417994b..2015548 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Bitpocked inspired me to write my own implementation of a two way sync script, i - Soft deletition and multiple backups handling - Before / after command execution - Time control +- Sync on changes Osync uses a master / slave sync schema. It can sync local and local or local and remote directories. By definition, master replica should always be a local directory on the system osync runs on. Also, osync uses pidlocks to prevent multiple concurrent sync processes on/to the same master / slave replica. Be sure a sync process is finished before launching next one. @@ -72,6 +73,10 @@ Once you're confident about your fist runs, you may add osync as cron task with: $ ./osync.sh /path/to/your.conf --silent +Additionnaly, you may run osync in monitor mode, which means it will perform a sync upon file operations on master replica. + + $ ./osync.sh /path/to/your.conf --on-changes + You may then find osync output in /var/log/osync-*.log (or current directory if /var/log is not writable). ## Author diff --git a/osync.sh b/osync.sh index 68c5d7d..96b395b 100755 --- a/osync.sh +++ b/osync.sh @@ -1466,8 +1466,13 @@ function Init set -o pipefail set -o errtrace - trap TrapStop SIGINT SIGKILL SIGHUP SIGTERM SIGQUIT - trap TrapQuit EXIT + # Do not use exit and quit traps if osync runs in monitor mode + if [ $sync_on_changes -eq 0 ] + then + trap TrapStop SIGINT SIGKILL SIGHUP SIGTERM SIGQUIT + trap TrapQuit EXIT + fi + if [ "$DEBUG" == "yes" ] then trap 'TrapError ${LINENO} $?' ERR @@ -1667,6 +1672,7 @@ function Usage echo "--verbose: adds command outputs" echo "--no-maxtime: disables any soft and hard execution time checks" echo "--force-unlock: will override any existing active or dead locks on master and slave replica" + echo "--on-changes: will launch a sync as soon as there is some file activity on master replica." echo "" echo "Quick usage only:" echo "--master= : Specify master replica path. Will contain state and backup directory." @@ -1675,6 +1681,22 @@ function Usage exit 128 } +function SyncOnChanges +{ + if ! type -p inotifywait > /dev/null 2>&1 + then + LogError "No inotifywait command found." + exit 1 + fi + + while true + do + inotifywait --exclude $OSYNC_DIR $RSYNC_EXCLUDE -qq -r -e create -e modify -e delete -e move -e attrib "$MASTER_SYNC_DIR/" + $osync_cmd "$ConfigFile" + done + +} + # Comand line argument flags dryrun=0 silent=0 @@ -1691,6 +1713,8 @@ soft_alert_total=0 error_alert=0 soft_stop=0 quick_sync=0 +sync_on_changes=0 +osync_cmd=$0 if [ $# -eq 0 ] then @@ -1731,6 +1755,9 @@ do --rsakey=*) SSH_RSA_PRIVATE_KEY=${i##*=} ;; + --on-changes) + sync_on_changes=1 + ;; esac done @@ -1746,9 +1773,15 @@ then SOFT_DELETE_DAYS=30 RESUME_TRY=1 else - LoadConfigFile "$1" + ConfigFile="$1" + LoadConfigFile "$ConfigFile" fi Init + if [ $sync_on_changes -eq 1 ] + then + SyncOnChanges + exit + fi DATE=$(date) Log "-------------------------------------------------------------" Log "$DRY_WARNING $DATE - Osync v$OSYNC_VERSION script begin."