Created
May 14, 2010 15:56
-
-
Save Sutto/401325 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
rsync_bin="/usr/bin/rsync" | |
rsync_args="--partial --delete -au" | |
sync_local_path="/Users/sutto/Code-Sync" | |
sync_remote_path="./Code-Sync" | |
sync_remote_hosts="[email protected]" | |
function path-with-trailing-slash { | |
echo "$1" | sed 's#/*$#/#' | |
} | |
function invalid-host-or-user { | |
[[ -z "$1" || -z "$2" || "$1" = "$2" ]] | |
} | |
function sync-folder-to-host { | |
local sync_user=$(echo "$1" | sed 's#@.*##') | |
local sync_host=$(echo "$1" | sed 's#.*@##') | |
if invalid-host-or-user $sync_user $sync_host; then | |
echo "[ERROR] No host or user given to sync-folder-to-host" >&2 | |
return 1 | |
fi | |
real_local_path=$(path-with-trailing-slash $sync_local_path) | |
real_remote_path=$(path-with-trailing-slash $sync_remote_path) | |
echo "Syncing $real_local_path to $sync_user on $sync_host to $real_remote_path" | |
$rsync_bin $rsync_args $real_local_path $sync_user@$sync_host:$real_remote_path | |
} | |
function sync-folder-from-host { | |
local sync_user=$(echo "$1" | sed 's#@.*##') | |
local sync_host=$(echo "$1" | sed 's#.*@##') | |
if invalid-host-or-user $sync_user $sync_host; then | |
echo "[ERROR] No host or user given to sync-folder-from-host" >&2 | |
return 1 | |
fi | |
real_local_path=$(path-with-trailing-slash $sync_local_path) | |
real_remote_path=$(path-with-trailing-slash $sync_remote_path) | |
echo "Syncing $real_remote_path from $sync_user on $sync_host to $real_local_path" | |
$rsync_bin $rsync_args $sync_user@$sync_host:$real_remote_path $real_local_path | |
} | |
function check-host-availability { | |
ping -t 1 -c 1 "$(echo "$1" | sed 's#.*@##')" &> /dev/null | |
} | |
function fail-sync { | |
echo "Sync failed: $1" 2>&1 | |
exit 1 | |
} | |
# We check host availability at each point. | |
echo "Step 1: Syncing from all hosts" | |
for sync_host in $sync_remote_hosts; do | |
if check-host-availability "$sync_host"; then | |
sync-folder-from-host "$sync_host" || fail-sync "Failure syncing from $sync_host" | |
fi | |
done | |
echo "Step 2: Syncing to all hosts" | |
for sync_host in $sync_remote_hosts; do | |
if check-host-availability "$sync_host"; then | |
sync-folder-to-host "$sync_host" || fail-sync "Failure syncing to $sync_host" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment