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" |
15 |
RSYNC="$(pwd)/upload.sh" |
15 |
RSYNC="$(pwd)/upload.sh" |
16 |
|
16 |
|
17 |
DEFAULT="\033[00m" |
17 |
DEFAULT="\033[00m" |
18 |
RED="\033[01;31m" |
18 |
RED="\033[01;31m" |
19 |
BLUE="\033[01;36m" |
19 |
BLUE="\033[01;36m" |
20 |
|
20 |
|
21 |
function info() { |
21 |
function info() { |
22 |
echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT" |
22 |
echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT" |
23 |
} |
23 |
} |
24 |
function error() { |
24 |
function error() { |
25 |
echo -en "$RED"; echo -n $1; echo -e "$DEFAULT" |
25 |
echo -en "$RED"; echo -n $1; echo -e "$DEFAULT" |
26 |
} |
26 |
} |
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" |
33 |
"$RSYNC" $1 $2 $3 |
33 |
"$RSYNC" $1 $2 $3 |
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 |
39 |
} |
39 |
} |
40 |
|
40 |
|
41 |
# Sort files |
41 |
# Sort files |
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 |
|
53 |
# Test public key authentication |
53 |
# Test public key authentication |
54 |
function sshtest() { |
54 |
function sshtest() { |
55 |
./test.sh |
55 |
./test.sh |
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 |
# 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 |
102 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
72 |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
103 |
|
73 |
|
104 |
|
74 |
|
105 |
if [ -f "$1"/LAST ] |
75 |
if [ -f "$1"/LAST ] |
106 |
then |
76 |
then |
107 |
|
77 |
|
108 |
# read last processed day |
78 |
# read last processed day |
109 |
read LAST < "$1"/LAST |
79 |
read LAST < "$1"/LAST |
110 |
read LAST_HOUR < "$1"/capture/LAST |
80 |
read LAST_HOUR < "$1"/capture/LAST |
111 |
|
81 |
|
112 |
# sync last updated folders |
82 |
# sync last updated folders |
113 |
syncdir audio/"$LAST" -a |
83 |
syncdir audio/"$LAST" -a |
114 |
syncdir capture/"$LAST_HOUR" -a |
84 |
syncdir capture/"$LAST_HOUR" -a |
115 |
syncdir data/"$LAST" -a |
85 |
syncdir data/"$LAST" -a |
116 |
syncdir data --dirs --delete |
86 |
syncdir data --dirs --delete |
117 |
|
87 |
|
118 |
# days changed, sync whole yesterday too |
88 |
# days changed, sync whole yesterday too |
119 |
if [ "$LAST" != "$OLD" ] |
89 |
if [ "$LAST" != "$OLD" ] |
120 |
then |
90 |
then |
121 |
info "syncing yesterday" |
91 |
info "syncing yesterday" |
122 |
syncdir audio/"$OLD" -a |
92 |
syncdir audio/"$OLD" -a |
123 |
syncdir capture/"$OLD" -a |
93 |
syncdir capture/"$OLD" -a |
124 |
syncdir data/"$OLD" -a |
94 |
syncdir data/"$OLD" -a |
125 |
else |
95 |
else |
126 |
# hours changed, sync previous hour too |
96 |
# hours changed, sync previous hour too |
127 |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
97 |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
128 |
then |
98 |
then |
129 |
info "syncing previous hour" |
99 |
info "syncing previous hour" |
130 |
syncdir capture/"$OLD_HOUR" -a |
100 |
syncdir capture/"$OLD_HOUR" -a |
131 |
fi |
101 |
fi |
132 |
fi |
102 |
fi |
133 |
fi |
103 |
fi |
134 |
|
104 |
|
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 |