Skip to content

Instantly share code, notes, and snippets.

@kdauzickas
Last active August 29, 2015 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kdauzickas/9311350 to your computer and use it in GitHub Desktop.
One way folder synchronisation. Changes in source are replicated at target. Changes at target stay there.
#!/bin/sh
# Dir to watch
SOURCE_DIR="/tmp/source/"
# Dir to sync
TARGET_DIR="/tmp/target/"
inotifywait -mr --timefmt '%y-%m-%d %H:%M' --format '%T %w %f %e' \
-e modify \
-e attrib \
-e create \
-e delete \
-e move \
$SOURCE_DIR | while read date time dir file event; do
SOURCE=${dir}${file}
TARGET=`echo "$SOURCE" | sed 's_'$SOURCE_DIR'__'`
FULL_TARGET=$TARGET_DIR$TARGET
echo "${date} ${time} ${event} $SOURCE"
case ${event} in
CREATE|MOVED_TO|"MOVED_TO,ISDIR")
echo "Copying $SOURCE to $FULL_TARGET"
cp -r $SOURCE $FULL_TARGET
;;
"CREATE,ISDIR")
echo "Creating dir $FULL_TARGET"
mkdir $FULL_TARGET
;;
DELETE|"DELETE,ISDIR"|MOVED_FROM|"MOVED_FROM,ISDIR")
echo "Remove $FULL_TARGET"
rm -rf $FULL_TARGET
;;
UNMOUNT)
echo "Unmount detected. Exiting"
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment