3407 |
kakl |
1 |
#!/usr/bin/env bash |
|
|
2 |
|
3552 |
kakl |
3 |
[[ $# -ne 2 ]] && echo "Usage: ./sync.sh /path/to/folder observatory_name" && exit 1 |
3407 |
kakl |
4 |
|
|
|
5 |
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" |
3449 |
kakl |
6 |
#exec > >(tee "$1"/logfile) |
3407 |
kakl |
7 |
|
|
|
8 |
# Also redirect stderr |
3449 |
kakl |
9 |
#exec 2>&1 |
3407 |
kakl |
10 |
|
3417 |
kakl |
11 |
# 9 min delay between resync |
|
|
12 |
FREQUENCY=540 |
3407 |
kakl |
13 |
|
|
|
14 |
TIDYUP="$(pwd)/tidyup.sh" |
|
|
15 |
RSYNC="$(pwd)/upload.sh" |
|
|
16 |
|
|
|
17 |
DEFAULT="\033[00m" |
|
|
18 |
RED="\033[01;31m" |
|
|
19 |
BLUE="\033[01;36m" |
|
|
20 |
|
|
|
21 |
function info() { |
|
|
22 |
echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT" |
|
|
23 |
} |
|
|
24 |
function error() { |
|
|
25 |
echo -en "$RED"; echo -n $1; echo -e "$DEFAULT" |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
function syncdir() { |
|
|
29 |
EXIT=1 |
|
|
30 |
i=1 |
3410 |
kakl |
31 |
# while [ $EXIT -ne 0 ]; do |
3552 |
kakl |
32 |
info "Trying to sync $1; $2; $3; $4" |
|
|
33 |
"$RSYNC" $1 $2 $3 $4 |
3407 |
kakl |
34 |
EXIT=$? |
|
|
35 |
[ $EXIT -ne 0 ] && error "sync failed" |
|
|
36 |
let i++ |
3410 |
kakl |
37 |
# done |
3407 |
kakl |
38 |
return $EXIT |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
# Sort files |
|
|
42 |
# Mask |
|
|
43 |
function tidyup() { |
|
|
44 |
info "sorting $1" |
|
|
45 |
"$TIDYUP" $1 |
|
|
46 |
if [ "$?" -ne 0 ]; then |
3449 |
kakl |
47 |
error "sorting failed, please send logfile.txt" |
3407 |
kakl |
48 |
return 1 |
|
|
49 |
fi |
|
|
50 |
return 0 |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
# Test public key authentication |
|
|
54 |
function sshtest() { |
3552 |
kakl |
55 |
./test.sh $1 |
3407 |
kakl |
56 |
if [ "$?" -eq 0 ]; then |
|
|
57 |
info "Authentication works" |
|
|
58 |
return 0 |
|
|
59 |
else |
|
|
60 |
error "Authentication does not work" |
|
|
61 |
return 1 |
|
|
62 |
fi |
|
|
63 |
} |
|
|
64 |
|
3552 |
kakl |
65 |
# parameters $1=LocalPathToData $2=ObservatoryName |
3449 |
kakl |
66 |
function main() { |
|
|
67 |
# start timer |
3407 |
kakl |
68 |
start_time=`date +%s` |
|
|
69 |
|
3552 |
kakl |
70 |
# OLD=$LAST |
|
|
71 |
# OLD_HOUR=$LAST_HOUR |
3407 |
kakl |
72 |
# sort |
3417 |
kakl |
73 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
3407 |
kakl |
74 |
|
|
|
75 |
|
|
|
76 |
if [ -f "$1"/LAST ] |
|
|
77 |
then |
|
|
78 |
|
3417 |
kakl |
79 |
# read last processed day |
3552 |
kakl |
80 |
# read LAST < "$1"/LAST |
|
|
81 |
# read LAST_HOUR < "$1"/capture/LAST |
3417 |
kakl |
82 |
|
|
|
83 |
# sync last updated folders |
3552 |
kakl |
84 |
syncdir $2 audio/"$LAST" -a |
|
|
85 |
syncdir $2 capture/"$LAST_HOUR" -a |
|
|
86 |
syncdir $2 data/"$LAST" -a |
|
|
87 |
syncdir $2 data --dirs --delete |
3417 |
kakl |
88 |
|
|
|
89 |
# days changed, sync whole yesterday too |
|
|
90 |
if [ "$LAST" != "$OLD" ] |
|
|
91 |
then |
|
|
92 |
info "syncing yesterday" |
3552 |
kakl |
93 |
syncdir $2 audio/"$OLD" -a |
|
|
94 |
syncdir $2 capture/"$OLD" -a |
|
|
95 |
syncdir $2 data/"$OLD" -a |
3417 |
kakl |
96 |
else |
3552 |
kakl |
97 |
# hours changed? sync previous hour too? |
3417 |
kakl |
98 |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
|
|
99 |
then |
|
|
100 |
info "syncing previous hour" |
3552 |
kakl |
101 |
syncdir $2 capture/"$OLD_HOUR" -a |
3417 |
kakl |
102 |
fi |
|
|
103 |
fi |
3407 |
kakl |
104 |
fi |
|
|
105 |
|
|
|
106 |
# end timer |
|
|
107 |
end_time=`date +%s` |
|
|
108 |
ELAPSED=`expr $end_time - $start_time` |
3417 |
kakl |
109 |
info "execution time was $ELAPSED s" |
3410 |
kakl |
110 |
date |
3449 |
kakl |
111 |
} |
|
|
112 |
|
|
|
113 |
# Check if we can connect, otherwise terminate |
3552 |
kakl |
114 |
info "Checking connection to the server" |
|
|
115 |
sshtest $2 || exit 1 |
3449 |
kakl |
116 |
|
|
|
117 |
# Change working directory |
|
|
118 |
cd $1 |
|
|
119 |
|
|
|
120 |
# first sort |
3552 |
kakl |
121 |
info "Tidyup..." |
3449 |
kakl |
122 |
tidyup $1 || error "Sorted, please analyze logfile.txt ." |
3410 |
kakl |
123 |
|
3449 |
kakl |
124 |
# first sync |
|
|
125 |
info "Doing complete sync" |
3552 |
kakl |
126 |
syncdir $2 . -a |
3449 |
kakl |
127 |
|
|
|
128 |
if [ -f "$1"/LAST ] |
|
|
129 |
then |
|
|
130 |
read LAST < "$1"/LAST |
|
|
131 |
else |
|
|
132 |
LAST="." |
|
|
133 |
fi |
|
|
134 |
|
|
|
135 |
if [ -f "$1"/capture/LAST ] |
|
|
136 |
then |
|
|
137 |
read LAST_HOUR < "$1"/capture/LAST |
|
|
138 |
else |
|
|
139 |
LAST_HOUR="." |
|
|
140 |
fi |
|
|
141 |
|
|
|
142 |
|
|
|
143 |
# Periodically tidy up and do incremental sync |
|
|
144 |
while : |
|
|
145 |
do |
3552 |
kakl |
146 |
OLD=$LAST |
|
|
147 |
OLD_HOUR=$LAST_HOUR |
|
|
148 |
# read last processed day/hour |
|
|
149 |
read LAST < "$1"/LAST |
|
|
150 |
read LAST_HOUR < "$1"/capture/LAST |
3449 |
kakl |
151 |
|
3552 |
kakl |
152 |
main $1 $2 2>&1 | tee "$1"/logfile |
|
|
153 |
|
3417 |
kakl |
154 |
# save only tail from logfile |
|
|
155 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
|
|
156 |
rm "$1"/logfile |
3552 |
kakl |
157 |
syncdir $2 . --dirs --delete > /dev/null 2>&1 |
3417 |
kakl |
158 |
|
|
|
159 |
# wait for a next syncing period |
3410 |
kakl |
160 |
sleep $FREQUENCY |
3407 |
kakl |
161 |
done |