Line 1... |
Line 1... |
1 |
#!/usr/bin/env bash |
1 |
#!/usr/bin/env bash |
2 |
|
2 |
|
3 |
[[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1 |
3 |
[[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1 |
4 |
|
4 |
|
5 |
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" |
5 |
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" |
6 |
exec > >(tee "$1"/logfile) |
6 |
#exec > >(tee "$1"/logfile) |
7 |
|
7 |
|
8 |
# Also redirect stderr |
8 |
# Also redirect stderr |
9 |
exec 2>&1 |
9 |
#exec 2>&1 |
10 |
|
10 |
|
11 |
# 9 min delay between resync |
11 |
# 9 min delay between resync |
12 |
FREQUENCY=540 |
12 |
FREQUENCY=540 |
13 |
|
13 |
|
14 |
TIDYUP="$(pwd)/tidyup.sh" |
14 |
TIDYUP="$(pwd)/tidyup.sh" |
Line 42... |
Line 42... |
42 |
# Mask |
42 |
# Mask |
43 |
function tidyup() { |
43 |
function tidyup() { |
44 |
info "sorting $1" |
44 |
info "sorting $1" |
45 |
"$TIDYUP" $1 |
45 |
"$TIDYUP" $1 |
46 |
if [ "$?" -ne 0 ]; then |
46 |
if [ "$?" -ne 0 ]; then |
47 |
error "sorting failed, please send logfile.txt to toxygen1@gmail.com" |
47 |
error "sorting failed, please send logfile.txt" |
48 |
return 1 |
48 |
return 1 |
49 |
fi |
49 |
fi |
50 |
return 0 |
50 |
return 0 |
51 |
} |
51 |
} |
52 |
|
52 |
|
Line 60... |
Line 60... |
60 |
error "Authentication does not work" |
60 |
error "Authentication does not work" |
61 |
return 1 |
61 |
return 1 |
62 |
fi |
62 |
fi |
63 |
} |
63 |
} |
64 |
|
64 |
|
65 |
# Check if we can connect, otherwise terminate |
- |
|
66 |
sshtest || exit 1 |
- |
|
67 |
|
- |
|
68 |
# Change working directory |
- |
|
69 |
cd $1 |
- |
|
70 |
|
- |
|
71 |
# first sort |
- |
|
72 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
- |
|
73 |
|
- |
|
74 |
# first sync |
- |
|
75 |
info "Doing complete sync" |
- |
|
76 |
syncdir . -a |
65 |
function main() { |
77 |
|
- |
|
78 |
if [ -f "$1"/LAST ] |
- |
|
79 |
then |
- |
|
80 |
read LAST < "$1"/LAST |
- |
|
81 |
else |
- |
|
82 |
LAST="." |
- |
|
83 |
fi |
- |
|
84 |
|
- |
|
85 |
if [ -f "$1"/capture/LAST ] |
- |
|
86 |
then |
- |
|
87 |
read LAST_HOUR < "$1"/capture/LAST |
- |
|
88 |
else |
- |
|
89 |
LAST_HOUR="." |
- |
|
90 |
fi |
- |
|
91 |
|
- |
|
92 |
|
- |
|
93 |
# Periodically tidy up and do incremental sync |
- |
|
94 |
while : |
- |
|
95 |
do |
- |
|
96 |
# start timer |
66 |
# start timer |
97 |
start_time=`date +%s` |
67 |
start_time=`date +%s` |
98 |
|
68 |
|
99 |
OLD="$LAST" |
69 |
OLD="$LAST" |
100 |
OLD_HOUR="$LAST_HOUR" |
70 |
OLD_HOUR="$LAST_HOUR" |
101 |
# sort |
71 |
# sort |
Line 135... |
Line 105... |
135 |
# end timer |
105 |
# end timer |
136 |
end_time=`date +%s` |
106 |
end_time=`date +%s` |
137 |
ELAPSED=`expr $end_time - $start_time` |
107 |
ELAPSED=`expr $end_time - $start_time` |
138 |
info "execution time was $ELAPSED s" |
108 |
info "execution time was $ELAPSED s" |
139 |
date |
109 |
date |
- |
|
110 |
} |
- |
|
111 |
|
- |
|
112 |
# Check if we can connect, otherwise terminate |
- |
|
113 |
sshtest || exit 1 |
- |
|
114 |
|
- |
|
115 |
# Change working directory |
- |
|
116 |
cd $1 |
- |
|
117 |
|
- |
|
118 |
# first sort |
- |
|
119 |
tidyup $1 || error "Sorted, please analyze logfile.txt ." |
140 |
|
120 |
|
- |
|
121 |
# first sync |
- |
|
122 |
info "Doing complete sync" |
- |
|
123 |
syncdir . -a |
- |
|
124 |
|
- |
|
125 |
if [ -f "$1"/LAST ] |
- |
|
126 |
then |
- |
|
127 |
read LAST < "$1"/LAST |
- |
|
128 |
else |
- |
|
129 |
LAST="." |
- |
|
130 |
fi |
- |
|
131 |
|
- |
|
132 |
if [ -f "$1"/capture/LAST ] |
- |
|
133 |
then |
- |
|
134 |
read LAST_HOUR < "$1"/capture/LAST |
- |
|
135 |
else |
- |
|
136 |
LAST_HOUR="." |
- |
|
137 |
fi |
- |
|
138 |
|
- |
|
139 |
|
- |
|
140 |
# Periodically tidy up and do incremental sync |
- |
|
141 |
while : |
- |
|
142 |
do |
- |
|
143 |
main $1 2>&1 | tee "$1"/logfile |
- |
|
144 |
|
141 |
# save only tail from logfile |
145 |
# save only tail from logfile |
142 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
146 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
143 |
rm "$1"/logfile |
147 |
rm "$1"/logfile |
144 |
syncdir . --dirs --delete > /dev/null 2>&1 |
148 |
syncdir . --dirs --delete > /dev/null 2>&1 |
145 |
exec > >(tee "$1"/logfile) |
- |
|
146 |
exec 2>&1 |
- |
|
147 |
|
149 |
|
148 |
# wait for a next syncing period |
150 |
# wait for a next syncing period |
149 |
sleep $FREQUENCY |
151 |
sleep $FREQUENCY |
150 |
done |
152 |
done |
151 |
|
153 |
|