Rev 3415 Rev 3417
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.txt) 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 FREQUENCY=180 12 FREQUENCY=540
12   13  
13 TIDYUP="$(pwd)/tidyup.sh" 14 TIDYUP="$(pwd)/tidyup.sh"
14 RSYNC="$(pwd)/upload.sh" 15 RSYNC="$(pwd)/upload.sh"
15   16  
16 DEFAULT="\033[00m" 17 DEFAULT="\033[00m"
17 RED="\033[01;31m" 18 RED="\033[01;31m"
18 BLUE="\033[01;36m" 19 BLUE="\033[01;36m"
19   20  
20 function info() { 21 function info() {
21 echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT" 22 echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT"
22 } 23 }
23 function error() { 24 function error() {
24 echo -en "$RED"; echo -n $1; echo -e "$DEFAULT" 25 echo -en "$RED"; echo -n $1; echo -e "$DEFAULT"
25 } 26 }
26   27  
27 function syncdir() { 28 function syncdir() {
28 EXIT=1 29 EXIT=1
29 i=1 30 i=1
30 # while [ $EXIT -ne 0 ]; do 31 # while [ $EXIT -ne 0 ]; do
31 info "Trying to sync $1, $2" 32 info "Trying to sync $1; $2; $3"
32 "$RSYNC" $1 $2 33 "$RSYNC" $1 $2 $3
33 EXIT=$? 34 EXIT=$?
34 [ $EXIT -ne 0 ] && error "sync failed" 35 [ $EXIT -ne 0 ] && error "sync failed"
35 let i++ 36 let i++
36 # done 37 # done
37 return $EXIT 38 return $EXIT
38 } 39 }
39   40  
40 # Sort files 41 # Sort files
41 # Mask 42 # Mask
42 function tidyup() { 43 function tidyup() {
43 info "sorting $1" 44 info "sorting $1"
44 "$TIDYUP" $1 45 "$TIDYUP" $1
45 if [ "$?" -ne 0 ]; then 46 if [ "$?" -ne 0 ]; then
46 error "sorting failed, please send logfile.txt to toxygen1@gmail.com" 47 error "sorting failed, please send logfile.txt to toxygen1@gmail.com"
47 return 1 48 return 1
48 fi 49 fi
49 return 0 50 return 0
50 } 51 }
51   52  
52 # Test public key authentication 53 # Test public key authentication
53 function sshtest() { 54 function sshtest() {
54 ./test.sh 55 ./test.sh
55 if [ "$?" -eq 0 ]; then 56 if [ "$?" -eq 0 ]; then
56 info "Authentication works" 57 info "Authentication works"
57 return 0 58 return 0
58 else 59 else
59 error "Authentication does not work" 60 error "Authentication does not work"
60 return 1 61 return 1
61 fi 62 fi
62 } 63 }
63   64  
64 # Check if we can connect, otherwise terminate 65 # Check if we can connect, otherwise terminate
65 sshtest || exit 1 66 sshtest || exit 1
66   67  
67 # Change working directory 68 # Change working directory
68 cd $1 69 cd $1
69   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
-   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
70   91  
71 # reset counter -  
72 HOURCOUNT=24 -  
73   92  
74 # Periodically tidy up and do incremental sync 93 # Periodically tidy up and do incremental sync
75 while : 94 while :
76 do 95 do
77 # start timer 96 # start timer
78 start_time=`date +%s` 97 start_time=`date +%s`
79 98
-   99 OLD="$LAST"
-   100 OLD_HOUR="$LAST_HOUR"
80 # sort 101 # sort
81 tidyup $1 || error "Sort failed, please send logfile.txt to toxygen1@gmail.com" 102 tidyup $1 || error "Sort failed, please analyze logfile.txt ."
82 103
83 # increase counter every hour -  
84 # if 24 hour mark is hit, do daily sync -  
85 if [[ "$HOURCOUNT" -eq 24 ]] -  
86 then -  
87 info "Doing complete sync" -  
88 syncdir . -  
89 HOURCOUNT=0 -  
90 fi -  
91 104
92 if [ -f "$1"/LAST ] 105 if [ -f "$1"/LAST ]
93 then 106 then
94 # next line is important for the first run of the loop -  
95 read LAST < "$1"/LAST -  
96 107
97 let HOURCOUNT++ -  
98   -  
99 # read last processed day 108 # read last processed day
100 OLD="$LAST" 109 read LAST < "$1"/LAST
101 read LAST < "$1"/LAST 110 read LAST_HOUR < "$1"/capture/LAST
102   111
103 # sync last updated folders 112 # sync last updated folders
104 syncdir ./audio/"$LAST" 113 syncdir audio/"$LAST" -a
105 syncdir ./capture/"$LAST" 114 syncdir capture/"$LAST_HOUR" -a
106 syncdir ./data/"$LAST" 115 syncdir data/"$LAST" -a
-   116 syncdir data --dirs --delete
107 syncdir ./data --delete 117 syncdir . --dirs --delete
108   118
109 # days changed, sync yesterday too 119 # days changed, sync whole yesterday too
-   120 if [ "$LAST" != "$OLD" ]
-   121 then
110 [[ "$LAST" != "$OLD" ]] && info "syncing yesterday" && syncdir "$OLD" 122 info "syncing yesterday"
-   123 syncdir audio/"$OLD" -a
-   124 syncdir capture/"$OLD" -a
-   125 syncdir data/"$OLD" -a
111   126 else
-   127 # hours changed, sync previous hour too
112 tail -n 1000 "$1"/logfile.txt > "$1"/tmp.txt 128 if [ "$LAST_HOUR" != "$OLD_HOUR" ]
-   129 then
-   130 info "syncing previous hour"
113 mv "$1"/tmp.txt "$1"/logfile.txt 131 syncdir capture/"$OLD_HOUR" -a
-   132 fi
-   133 fi
114 fi 134 fi
115 135
116 # end timer 136 # end timer
117 end_time=`date +%s` 137 end_time=`date +%s`
118 ELAPSED=`expr $end_time - $start_time` 138 ELAPSED=`expr $end_time - $start_time`
119 info "execution time was $ELAPSED s" 139 info "execution time was $ELAPSED s"
120 -  
121 date 140 date
122 141
123 # if last sync took less than TIME, sleep to make up 1 hour 142 # save only tail from logfile
124 # [[ $ELAPSED -lt $FREQUENCY ]] && sleep `expr $FREQUENCY - $ELAPSED` 143 tail -n 1000 "$1"/logfile > "$1"/logfile.txt
-   144 rm "$1"/logfile
-   145 exec > >(tee "$1"/logfile)
-   146 exec 2>&1
-   147  
-   148 # wait for a next syncing period
125 sleep $FREQUENCY 149 sleep $FREQUENCY
126 done 150 done