Rev 3417 Rev 3418
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 to toxygen1@gmail.com"
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 65 # Check if we can connect, otherwise terminate
66 sshtest || exit 1 66 sshtest || exit 1
67   67  
68 # Change working directory 68 # Change working directory
69 cd $1 69 cd $1
70   70  
71 # first sort 71 # first sort
72 tidyup $1 || error "Sort failed, please analyze logfile.txt ." 72 tidyup $1 || error "Sort failed, please analyze logfile.txt ."
73 73
74 # first sync 74 # first sync
75 info "Doing complete sync" 75 info "Doing complete sync"
76 syncdir . -a 76 syncdir . -a
77   77  
78 if [ -f "$1"/LAST ] 78 if [ -f "$1"/LAST ]
79 then 79 then
80 read LAST < "$1"/LAST 80 read LAST < "$1"/LAST
81 else 81 else
82 LAST="." 82 LAST="."
83 fi 83 fi
84   84  
85 if [ -f "$1"/capture/LAST ] 85 if [ -f "$1"/capture/LAST ]
86 then 86 then
87 read LAST_HOUR < "$1"/capture/LAST 87 read LAST_HOUR < "$1"/capture/LAST
88 else 88 else
89 LAST_HOUR="." 89 LAST_HOUR="."
90 fi 90 fi
91   91  
92   92  
93 # Periodically tidy up and do incremental sync 93 # Periodically tidy up and do incremental sync
94 while : 94 while :
95 do 95 do
96 # start timer 96 # start timer
97 start_time=`date +%s` 97 start_time=`date +%s`
98 98
99 OLD="$LAST" 99 OLD="$LAST"
100 OLD_HOUR="$LAST_HOUR" 100 OLD_HOUR="$LAST_HOUR"
101 # sort 101 # sort
102 tidyup $1 || error "Sort failed, please analyze logfile.txt ." 102 tidyup $1 || error "Sort failed, please analyze logfile.txt ."
103 103
104 104
105 if [ -f "$1"/LAST ] 105 if [ -f "$1"/LAST ]
106 then 106 then
107 107
108 # read last processed day 108 # read last processed day
109 read LAST < "$1"/LAST 109 read LAST < "$1"/LAST
110 read LAST_HOUR < "$1"/capture/LAST 110 read LAST_HOUR < "$1"/capture/LAST
111 111
112 # sync last updated folders 112 # sync last updated folders
113 syncdir audio/"$LAST" -a 113 syncdir audio/"$LAST" -a
114 syncdir capture/"$LAST_HOUR" -a 114 syncdir capture/"$LAST_HOUR" -a
115 syncdir data/"$LAST" -a 115 syncdir data/"$LAST" -a
116 syncdir data --dirs --delete 116 syncdir data --dirs --delete
117 syncdir . --dirs --delete -  
118 117
119 # days changed, sync whole yesterday too 118 # days changed, sync whole yesterday too
120 if [ "$LAST" != "$OLD" ] 119 if [ "$LAST" != "$OLD" ]
121 then 120 then
122 info "syncing yesterday" 121 info "syncing yesterday"
123 syncdir audio/"$OLD" -a 122 syncdir audio/"$OLD" -a
124 syncdir capture/"$OLD" -a 123 syncdir capture/"$OLD" -a
125 syncdir data/"$OLD" -a 124 syncdir data/"$OLD" -a
126 else 125 else
127 # hours changed, sync previous hour too 126 # hours changed, sync previous hour too
128 if [ "$LAST_HOUR" != "$OLD_HOUR" ] 127 if [ "$LAST_HOUR" != "$OLD_HOUR" ]
129 then 128 then
130 info "syncing previous hour" 129 info "syncing previous hour"
131 syncdir capture/"$OLD_HOUR" -a 130 syncdir capture/"$OLD_HOUR" -a
132 fi 131 fi
133 fi 132 fi
134 fi 133 fi
135 134
136 # end timer 135 # end timer
137 end_time=`date +%s` 136 end_time=`date +%s`
138 ELAPSED=`expr $end_time - $start_time` 137 ELAPSED=`expr $end_time - $start_time`
139 info "execution time was $ELAPSED s" 138 info "execution time was $ELAPSED s"
140 date 139 date
141 140
142 # save only tail from logfile 141 # save only tail from logfile
143 tail -n 1000 "$1"/logfile > "$1"/logfile.txt 142 tail -n 1000 "$1"/logfile > "$1"/logfile.txt
144 rm "$1"/logfile 143 rm "$1"/logfile
-   144 syncdir . --dirs --delete > /dev/null 2>&1
145 exec > >(tee "$1"/logfile) 145 exec > >(tee "$1"/logfile)
146 exec 2>&1 146 exec 2>&1
147   147  
148 # wait for a next syncing period 148 # wait for a next syncing period
149 sleep $FREQUENCY 149 sleep $FREQUENCY
150 done 150 done