Skip to content

Instantly share code, notes, and snippets.

@kdauzickas kdauzickas/one-way-sync.sh
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.