Skip to content

Instantly share code, notes, and snippets.

@rhowardiv
Created September 7, 2012 20:04
Show Gist options
  • Save rhowardiv/3669192 to your computer and use it in GitHub Desktop.
Save rhowardiv/3669192 to your computer and use it in GitHub Desktop.
remote file sync for Mac
#!/bin/bash
LOG=/tmp/sync.log
SELF=$(basename "$0")
usage() {
echo
echo "Usage: $SELF LOCALDIR REMOTEDIR"
echo "Syncs files in LOCALDIR to REMOTEDIR"
echo "Daemonize this yourself with & or nohup"
exit 1
}
if [ $# -lt 2 ]; then
usage
fi
if [ ! -d $1 ]; then
echo "First argument must be an existing directory!"
usage
fi
FROM=$(echo "$1" | sed 's@\([^/]\)$@\1/@')
TO=$(echo "$2" | sed 's@\([^/]\)$@\1/@')
# Do initial sync
rsync -rt --exclude=.git --exclude='*.swp' --exclude='*.swo' --exclude='*~' "$FROM" "$TO"
if [ $? -ne 0 ]; then
echo "Initial sync failed!"
exit 2
fi
osn() {
if [ -n "$1" ]; then
terminal-notifier -message "$1" -title "$SELF" > /dev/null
fi
}
osn "Initial sync complete; filewatch will start now."
on_exit () {
osn "exited"
}
trap on_exit EXIT
files=""
onscreen() {
while read line; do
# ignore empty lines
test -z "$line" && continue
# ignore notify_loop messages and stats lines from rsync, except the last one
echo $line | grep '^\(Change\|Running\|Watching\|Number of\|Total\|Literal\|Matched\|File list\|Total bytes\|sent\) ' >/dev/null && continue
if [ -n "$(echo "$line" | grep '^total size ')" ]; then
# rsync is done; echo out all files
osn "Synced: $files"
files=""
else
files="$files $line"
fi
done
}
notify_loop "$FROM" rsync -rt --stats --out-format='%n' --exclude=.git --exclude='*.swp' --exclude='*.swo' --exclude='*~' "$FROM" "$TO" | tee "$LOG" | onscreen
@rhowardiv
Copy link
Author

Requirements:

  • https://github.com/ggreer/fsevents-tools
    Clone/download, cd and then run source autogen.sh (you may need to brew install automake and/or brew install autoconf, followed by sudo make install.
  • Mountain Lion (for notifications) with terminal-notifier (sudo gem install terminal-notifier). Could be easily modified to use Growl or whatever for earlier versions of Mac OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment