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