3,12 → 3,13 |
[[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1 |
|
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" |
exec > >(tee "$1"/logfile.txt) |
exec > >(tee "$1"/logfile) |
|
# Also redirect stderr |
exec 2>&1 |
|
FREQUENCY=180 |
# 9 min delay between resync |
FREQUENCY=540 |
|
TIDYUP="$(pwd)/tidyup.sh" |
RSYNC="$(pwd)/upload.sh" |
28,8 → 29,8 |
EXIT=1 |
i=1 |
# while [ $EXIT -ne 0 ]; do |
info "Trying to sync $1, $2" |
"$RSYNC" $1 $2 |
info "Trying to sync $1; $2; $3" |
"$RSYNC" $1 $2 $3 |
EXIT=$? |
[ $EXIT -ne 0 ] && error "sync failed" |
let i++ |
67,10 → 68,28 |
# Change working directory |
cd $1 |
|
# first sort |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
|
# first sync |
info "Doing complete sync" |
syncdir . -a |
|
# reset counter |
HOURCOUNT=24 |
if [ -f "$1"/LAST ] |
then |
read LAST < "$1"/LAST |
else |
LAST="." |
fi |
|
if [ -f "$1"/capture/LAST ] |
then |
read LAST_HOUR < "$1"/capture/LAST |
else |
LAST_HOUR="." |
fi |
|
|
# Periodically tidy up and do incremental sync |
while : |
do |
77,50 → 96,55 |
# start timer |
start_time=`date +%s` |
|
OLD="$LAST" |
OLD_HOUR="$LAST_HOUR" |
# sort |
tidyup $1 || error "Sort failed, please send logfile.txt to toxygen1@gmail.com" |
tidyup $1 || error "Sort failed, please analyze logfile.txt ." |
|
# increase counter every hour |
# if 24 hour mark is hit, do daily sync |
if [[ "$HOURCOUNT" -eq 24 ]] |
then |
info "Doing complete sync" |
syncdir . |
HOURCOUNT=0 |
fi |
|
if [ -f "$1"/LAST ] |
then |
# next line is important for the first run of the loop |
read LAST < "$1"/LAST |
|
let HOURCOUNT++ |
|
# read last processed day |
OLD="$LAST" |
read LAST < "$1"/LAST |
|
# sync last updated folders |
syncdir ./audio/"$LAST" |
syncdir ./capture/"$LAST" |
syncdir ./data/"$LAST" |
syncdir ./data --delete |
|
# days changed, sync yesterday too |
[[ "$LAST" != "$OLD" ]] && info "syncing yesterday" && syncdir "$OLD" |
|
tail -n 1000 "$1"/logfile.txt > "$1"/tmp.txt |
mv "$1"/tmp.txt "$1"/logfile.txt |
# read last processed day |
read LAST < "$1"/LAST |
read LAST_HOUR < "$1"/capture/LAST |
|
# sync last updated folders |
syncdir audio/"$LAST" -a |
syncdir capture/"$LAST_HOUR" -a |
syncdir data/"$LAST" -a |
syncdir data --dirs --delete |
syncdir . --dirs --delete |
|
# days changed, sync whole yesterday too |
if [ "$LAST" != "$OLD" ] |
then |
info "syncing yesterday" |
syncdir audio/"$OLD" -a |
syncdir capture/"$OLD" -a |
syncdir data/"$OLD" -a |
else |
# hours changed, sync previous hour too |
if [ "$LAST_HOUR" != "$OLD_HOUR" ] |
then |
info "syncing previous hour" |
syncdir capture/"$OLD_HOUR" -a |
fi |
fi |
fi |
|
# end timer |
end_time=`date +%s` |
ELAPSED=`expr $end_time - $start_time` |
info "execution time was $ELAPSED s" |
|
info "execution time was $ELAPSED s" |
date |
|
# if last sync took less than TIME, sleep to make up 1 hour |
# [[ $ELAPSED -lt $FREQUENCY ]] && sleep `expr $FREQUENCY - $ELAPSED` |
# save only tail from logfile |
tail -n 1000 "$1"/logfile > "$1"/logfile.txt |
rm "$1"/logfile |
exec > >(tee "$1"/logfile) |
exec 2>&1 |
|
# wait for a next syncing period |
sleep $FREQUENCY |
done |