Created
September 7, 2012 20:04
-
-
Save rhowardiv/3669192 to your computer and use it in GitHub Desktop.
remote file sync for Mac
This file contains hidden or 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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements:
Clone/download, cd and then run
source autogen.sh
(you may need tobrew install automake
and/orbrew install autoconf
, followed bysudo make install
.sudo gem install terminal-notifier
). Could be easily modified to use Growl or whatever for earlier versions of Mac OS.