Rev Author Line No. Line
3407 kakl 1 #!/usr/bin/env bash
2  
3 [[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1
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
3417 kakl 32 info "Trying to sync $1; $2; $3"
33 "$RSYNC" $1 $2 $3
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() {
55 ./test.sh
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  
3449 kakl 65 function main() {
66 # start timer
3407 kakl 67 start_time=`date +%s`
68  
3417 kakl 69 OLD="$LAST"
70 OLD_HOUR="$LAST_HOUR"
3407 kakl 71 # sort
3417 kakl 72 tidyup $1 || error "Sort failed, please analyze logfile.txt ."
3407 kakl 73  
74  
75 if [ -f "$1"/LAST ]
76 then
77  
3417 kakl 78 # read last processed day
79 read LAST < "$1"/LAST
80 read LAST_HOUR < "$1"/capture/LAST
81  
82 # sync last updated folders
83 syncdir audio/"$LAST" -a
84 syncdir capture/"$LAST_HOUR" -a
85 syncdir data/"$LAST" -a
86 syncdir data --dirs --delete
87  
88 # days changed, sync whole yesterday too
89 if [ "$LAST" != "$OLD" ]
90 then
91 info "syncing yesterday"
92 syncdir audio/"$OLD" -a
93 syncdir capture/"$OLD" -a
94 syncdir data/"$OLD" -a
95 else
96 # hours changed, sync previous hour too
97 if [ "$LAST_HOUR" != "$OLD_HOUR" ]
98 then
99 info "syncing previous hour"
100 syncdir capture/"$OLD_HOUR" -a
101 fi
102 fi
3407 kakl 103 fi
104  
105 # end timer
106 end_time=`date +%s`
107 ELAPSED=`expr $end_time - $start_time`
3417 kakl 108 info "execution time was $ELAPSED s"
3410 kakl 109 date
3449 kakl 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 ."
3410 kakl 120  
3449 kakl 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  
3417 kakl 145 # save only tail from logfile
146 tail -n 1000 "$1"/logfile > "$1"/logfile.txt
147 rm "$1"/logfile
3418 kakl 148 syncdir . --dirs --delete > /dev/null 2>&1
3417 kakl 149  
150 # wait for a next syncing period
3410 kakl 151 sleep $FREQUENCY
3407 kakl 152 done