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