Skip to content

Instantly share code, notes, and snippets.

@agustingianni
Created November 30, 2018 11:01
Show Gist options
  • Save agustingianni/3f9f2455f7a48f5d1959f9c9fe0becc7 to your computer and use it in GitHub Desktop.
Save agustingianni/3f9f2455f7a48f5d1959f9c9fe0becc7 to your computer and use it in GitHub Desktop.
#!/bin/bash
LATENCY=1
NAME=`basename ${BASH_SOURCE[0]}`
red='\033[0;31m'
green='\033[0;32m'
nocolor='\033[00m'
if [[ "$1" = "" || "$2" = "" || "$3" = "" ]]; then
echo -n "Usage: ${NAME} /local/path/ /target/path/ user@ssh_server"
exit
else
LOCAL_PATH="$1"
REMOTE_PATH="$2"
REMOTE_HOST="$3"
fi
# Perform initial complete sync
read -n1 -r -p "Press any key to continue (or abort with Ctrl-C)... " key
echo ""
echo -en "${green}"`date` "${nocolor}"". Synchronizing... "
rsync -avzr --progress --delete --force $LOCAL_PATH $REMOTE_HOST:$REMOTE_PATH
echo -e "${green}""Done.""${nocolor}"
# Watch for changes and sync (exclude hidden files)
echo "Watching for changes @ ${LOCAL_PATH} ..."
# Watch directories. Exclude those that are hidden.
fswatch -0 -r -l $LATENCY $LOCAL_PATH --exclude="/\.[^/]*$" | \
# Listen for events.
while read -d "" event
do
# Save the filenames to a file.
echo $event > .tmp_files
echo -en "${green}" `date` "${nocolor}\"$event\" changed. Synchronizing ... "
# Sync.
rsync \
-avzrq \
--include-from=.tmp_files \
$LOCAL_PATH $REMOTE_HOST:$REMOTE_PATH
echo "done!"
# Remove the filenames file.
rm -rf .tmp_files
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment