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 2 ]] && echo "Usage: ./sync.sh /path/to/folder observatory_name" && 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 |
Line 27... |
Line 27... |
27 |
|
27 |
|
28 |
function syncdir() { |
28 |
function syncdir() { |
29 |
EXIT=1 |
29 |
EXIT=1 |
30 |
i=1 |
30 |
i=1 |
31 |
# while [ $EXIT -ne 0 ]; do |
31 |
# while [ $EXIT -ne 0 ]; do |
32 |
info "Trying to sync $1; $2; $3" |
32 |
info "Trying to sync $1; $2; $3; $4" |
33 |
"$RSYNC" $1 $2 $3 |
33 |
"$RSYNC" $1 $2 $3 $4 |
34 |
EXIT=$? |
34 |
EXIT=$? |
35 |
[ $EXIT -ne 0 ] && error "sync failed" |
35 |
[ $EXIT -ne 0 ] && error "sync failed" |
36 |
let i++ |
36 |
let i++ |
37 |
# done |
37 |
# done |
38 |
return $EXIT |
38 |
return $EXIT |
Line 50... |
Line 50... |
50 |
return 0 |
50 |
return 0 |
51 |
} |
51 |
} |
52 |
|
52 |
|
53 |
# Test public key authentication |
53 |
# Test public key authentication |
54 |
function sshtest() { |
54 |
function sshtest() { |
55 |
./test.sh |
55 |
./test.sh $1 |
56 |
if [ "$?" -eq 0 ]; then |
56 |
if [ "$?" -eq 0 ]; then |
57 |
info "Authentication works" |
57 |
info "Authentication works" |
58 |
return 0 |
58 |
return 0 |
59 |
else |
59 |
else |
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 |
# parameters $1=LocalPathToData $2=ObservatoryName |
65 |
function main() { |
66 |
function main() { |
66 |
# start timer |
67 |
# start timer |
67 |
start_time=`date +%s` |
68 |
start_time=`date +%s` |
68 |
|
69 |
|
69 |
OLD="$LAST" |
70 |
# OLD=$LAST |
70 |
OLD_HOUR="$LAST_HOUR" |
71 |
# OLD_HOUR=$LAST_HOUR |
71 |
# sort |
72 |
# sort |
72 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
73 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
73 |
|
74 |
|
74 |
|
75 |
|
75 |
if [ -f "$1"/LAST ] |
76 |
if [ -f "$1"/LAST ] |
76 |
then |
77 |
then |
77 |
|
78 |
|
78 |
# read last processed day |
79 |
# read last processed day |
79 |
read LAST < "$1"/LAST |
80 |
# read LAST < "$1"/LAST |
80 |
read LAST_HOUR < "$1"/capture/LAST |
81 |
# read LAST_HOUR < "$1"/capture/LAST |
81 |
|
82 |
|
82 |
# sync last updated folders |
83 |
# sync last updated folders |
83 |
syncdir audio/"$LAST" -a |
84 |
syncdir $2 audio/"$LAST" -a |
84 |
syncdir capture/"$LAST_HOUR" -a |
85 |
syncdir $2 capture/"$LAST_HOUR" -a |
85 |
syncdir data/"$LAST" -a |
86 |
syncdir $2 data/"$LAST" -a |
86 |
syncdir data --dirs --delete |
87 |
syncdir $2 data --dirs --delete |
87 |
|
88 |
|
88 |
# days changed, sync whole yesterday too |
89 |
# days changed, sync whole yesterday too |
89 |
if [ "$LAST" != "$OLD" ] |
90 |
if [ "$LAST" != "$OLD" ] |
90 |
then |
91 |
then |
91 |
info "syncing yesterday" |
92 |
info "syncing yesterday" |
92 |
syncdir audio/"$OLD" -a |
93 |
syncdir $2 audio/"$OLD" -a |
93 |
syncdir capture/"$OLD" -a |
94 |
syncdir $2 capture/"$OLD" -a |
94 |
syncdir data/"$OLD" -a |
95 |
syncdir $2 data/"$OLD" -a |
95 |
else |
96 |
else |
96 |
# hours changed, sync previous hour too |
97 |
# hours changed? sync previous hour too? |
97 |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
98 |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
98 |
then |
99 |
then |
99 |
info "syncing previous hour" |
100 |
info "syncing previous hour" |
100 |
syncdir capture/"$OLD_HOUR" -a |
101 |
syncdir $2 capture/"$OLD_HOUR" -a |
101 |
fi |
102 |
fi |
102 |
fi |
103 |
fi |
103 |
fi |
104 |
fi |
104 |
|
105 |
|
105 |
# end timer |
106 |
# end timer |
Line 108... |
Line 109... |
108 |
info "execution time was $ELAPSED s" |
109 |
info "execution time was $ELAPSED s" |
109 |
date |
110 |
date |
110 |
} |
111 |
} |
111 |
|
112 |
|
112 |
# Check if we can connect, otherwise terminate |
113 |
# Check if we can connect, otherwise terminate |
- |
|
114 |
info "Checking connection to the server" |
113 |
sshtest || exit 1 |
115 |
sshtest $2 || exit 1 |
114 |
|
116 |
|
115 |
# Change working directory |
117 |
# Change working directory |
116 |
cd $1 |
118 |
cd $1 |
117 |
|
119 |
|
118 |
# first sort |
120 |
# first sort |
- |
|
121 |
info "Tidyup..." |
119 |
tidyup $1 || error "Sorted, please analyze logfile.txt ." |
122 |
tidyup $1 || error "Sorted, please analyze logfile.txt ." |
120 |
|
123 |
|
121 |
# first sync |
124 |
# first sync |
122 |
info "Doing complete sync" |
125 |
info "Doing complete sync" |
123 |
syncdir . -a |
126 |
syncdir $2 . -a |
124 |
|
127 |
|
125 |
if [ -f "$1"/LAST ] |
128 |
if [ -f "$1"/LAST ] |
126 |
then |
129 |
then |
127 |
read LAST < "$1"/LAST |
130 |
read LAST < "$1"/LAST |
128 |
else |
131 |
else |
Line 138... |
Line 141... |
138 |
|
141 |
|
139 |
|
142 |
|
140 |
# Periodically tidy up and do incremental sync |
143 |
# Periodically tidy up and do incremental sync |
141 |
while : |
144 |
while : |
142 |
do |
145 |
do |
- |
|
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 |
- |
|
151 |
|
143 |
main $1 2>&1 | tee "$1"/logfile |
152 |
main $1 $2 2>&1 | tee "$1"/logfile |
144 |
|
153 |
|
145 |
# save only tail from logfile |
154 |
# save only tail from logfile |
146 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
155 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
147 |
rm "$1"/logfile |
156 |
rm "$1"/logfile |
148 |
syncdir . --dirs --delete > /dev/null 2>&1 |
157 |
syncdir $2 . --dirs --delete > /dev/null 2>&1 |
149 |
|
158 |
|
150 |
# wait for a next syncing period |
159 |
# wait for a next syncing period |
151 |
sleep $FREQUENCY |
160 |
sleep $FREQUENCY |
152 |
done |
161 |
done |
153 |
|
162 |
|