-
Notifications
You must be signed in to change notification settings - Fork 23
/
fswatch-rsync.sh
executable file
·101 lines (87 loc) · 2.86 KB
/
fswatch-rsync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# @author Clemens Westrup
# @date 07.07.2014
# This is a script to automatically synchronize a local project folder to a
# folder on a cluster server via a middle server.
# It watches the local folder for changes and recreates the local state on the
# target machine as soon as a change is detected.
# For setup and usage see README.md
################################################################################
PROJECT="fswatch-rsync"
VERSION="0.2.0"
# Set up your path to fswatch here if you don't want to / can't add it
# globally to your PATH variable (default is "fswatch" when specified in PATH).
# e.g. FSWATCH_PATH="/Users/you/builds/fswatch/fswatch"
FSWATCH_PATH="fswatch"
# Sync latency / speed in seconds
LATENCY="3"
# default server setup
MIDDLE="brute.aalto.fi" # middle ssh server
TARGET="login2.triton.aalto.fi" # target ssh server
# check color support
colors=$(tput colors)
if (($colors >= 8)); then
red='\033[0;31m'
green='\033[0;32m'
nocolor='\033[00m'
else
red=
green=
nocolor=
fi
# Check compulsory arguments
if [[ "$1" = "" || "$2" = "" || "$3" = "" ]]; then
echo -e "${red}Error: $PROJECT takes 3 compulsory arguments.${nocolor}"
echo -n "Usage: fswatch-rsync.sh /local/path /targetserver/path ssh_user "
echo "[middleserver] [targetserver] [target_ssh_user]"
exit
else
LOCAL_PATH="$1"
TARGET_PATH="$2"
SSH_USER="$3"
fi
# Check optional arguments
if [[ "$4" != "" ]]; then
MIDDLE="$4"
fi
if [[ "$5" != "" ]]; then
TARGET="$5"
fi
if [[ "$6" != "" ]]; then
TARGET_SSH_USER="$6"
else
TARGET_SSH_USER="$SSH_USER"
fi
# Welcome
echo ""
echo -e "${green}Hei! This is $PROJECT v$VERSION.${nocolor}"
echo "Local source path: \"$LOCAL_PATH\""
echo "Remote target path: \"$TARGET_PATH\""
echo "Via middle server: \"$SSH_USER@$MIDDLE\""
echo "To target server: \"$TARGET_SSH_USER@$TARGET\""
echo ""
echo -n "Performing initial complete synchronization "
echo -n "(Warning: Target directory will be overwritten "
echo "with local version if differences occur)."
# Perform initial complete sync
read -n1 -r -p "Press any key to continue (or abort with Ctrl-C)... " key
echo ""
echo -n "Synchronizing... "
rsync -avzr -q --delete --force --exclude=".*" \
-e "ssh $SSH_USER@$MIDDLE ssh" $LOCAL_PATH $TARGET_SSH_USER@$TARGET:$TARGET_PATH
echo "done."
echo ""
# Watch for changes and sync (exclude hidden files)
echo "Watching for changes. Quit anytime with Ctrl-C."
${FSWATCH_PATH} -0 -r -l $LATENCY $LOCAL_PATH --exclude="/\.[^/]*$" \
| while read -d "" event
do
echo $event > .tmp_files
echo -en "${green}" `date` "${nocolor}\"$event\" changed. Synchronizing... "
rsync -avzr -q --delete --force \
--include-from=.tmp_files \
-e "ssh $SSH_USER@$MIDDLE ssh" \
$LOCAL_PATH $TARGET_SSH_USER@$TARGET:$TARGET_PATH
echo "done."
rm -rf .tmp_files
done