| 3 |
3 |
[[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1
|
| 4 |
4 |
|
| 5 |
5 |
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
|
| 6 |
|
exec > >(tee "$1"/logfile)
|
|
6 |
#exec > >(tee "$1"/logfile)
|
| 7 |
7 |
|
| 8 |
8 |
# Also redirect stderr
|
| 9 |
|
exec 2>&1
|
|
9 |
#exec 2>&1
|
| 10 |
10 |
|
| 11 |
11 |
# 9 min delay between resync
|
| 12 |
12 |
FREQUENCY=540
|
| ... | ... | |
| 44 |
44 |
info "sorting $1"
|
| 45 |
45 |
"$TIDYUP" $1
|
| 46 |
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"
|
| 48 |
48 |
return 1
|
| 49 |
49 |
fi
|
| 50 |
50 |
return 0
|
| ... | ... | |
| 62 |
62 |
fi
|
| 63 |
63 |
}
|
| 64 |
64 |
|
| 65 |
|
# Check if we can connect, otherwise terminate
|
| 66 |
|
sshtest || exit 1
|
| 67 |
|
|
| 68 |
|
# Change working directory
|
| 69 |
|
cd $1
|
| 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
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
# Periodically tidy up and do incremental sync
|
| 94 |
|
while :
|
| 95 |
|
do
|
| 96 |
|
# start timer
|
|
65 |
function main() {
|
|
66 |
# start timer
|
| 97 |
67 |
start_time=`date +%s`
|
| 98 |
68 |
|
| 99 |
69 |
OLD="$LAST"
|
| ... | ... | |
| 137 |
107 |
ELAPSED=`expr $end_time - $start_time`
|
| 138 |
108 |
info "execution time was $ELAPSED s"
|
| 139 |
109 |
date
|
|
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 ."
|
| 140 |
120 |
|
|
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 |
|
| 141 |
145 |
# save only tail from logfile
|
| 142 |
146 |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt
|
| 143 |
147 |
rm "$1"/logfile
|
| 144 |
148 |
syncdir . --dirs --delete > /dev/null 2>&1
|
| 145 |
|
exec > >(tee "$1"/logfile)
|
| 146 |
|
exec 2>&1
|
| 147 |
149 |
|
| 148 |
150 |
# wait for a next syncing period
|
| 149 |
151 |
sleep $FREQUENCY
|