One way folder synchronisation. Changes in source are replicated at target. Changes at target stay there.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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