#!/usr/bin/env bash[[ $# -ne 1 ]] && echo "Usage: ./sync.sh /path/to/folder" && exit 1# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"exec > >(tee "$1"/logfile.txt)# Also redirect stderrexec 2>&1FREQUENCY=180TIDYUP="$(pwd)/tidyup.sh"RSYNC="$(pwd)/upload.sh"DEFAULT="\033[00m"RED="\033[01;31m"BLUE="\033[01;36m"function info() {echo -en "$BLUE"; echo -n $1; echo -e "$DEFAULT"}function error() {echo -en "$RED"; echo -n $1; echo -e "$DEFAULT"}function syncdir() {EXIT=1i=1# while [ $EXIT -ne 0 ]; doinfo "Trying to sync $1, try number $i""$RSYNC" "$1"EXIT=$?[ $EXIT -ne 0 ] && error "sync failed"let i++# donereturn $EXIT}# Sort files# Maskfunction tidyup() {info "sorting $1""$TIDYUP" $1if [ "$?" -ne 0 ]; thenerror "sorting failed, please send logfile.txt to toxygen1@gmail.com"return 1fireturn 0}# Test public key authenticationfunction sshtest() {./test.shif [ "$?" -eq 0 ]; theninfo "Authentication works"return 0elseerror "Authentication does not work"return 1fi}# Check if we can connect, otherwise terminatesshtest || exit 1# Change working directorycd $1# reset counterHOURCOUNT=24# Periodically tidy up and do incremental syncwhile :do# start timerstart_time=`date +%s`# sorttidyup $1 || error "Sort failed, please send logfile.txt to toxygen1@gmail.com"# increase counter every hour# if 24 hour mark is hit, do daily syncif [[ "$HOURCOUNT" -eq 24 ]]theninfo "Doing complete sync"syncdir .HOURCOUNT=0fiif [ -f "$1"/LAST ]then# next line is important for the first run of the loopread LAST < "$1"/LASTlet HOURCOUNT++# read last processed dayOLD="$LAST"read LAST < "$1"/LAST# sync last updated folderssyncdir ./audio/"$LAST"syncdir ./capture/"$LAST"syncdir ./data/"$LAST"# days changed, sync yesterday too[[ "$LAST" != "$OLD" ]] && info "syncing yesterday" && syncdir "$OLD"tail -n 1000 "$1"/logfile.txt > "$1"/tmp.txtmv "$1"/tmp.txt "$1"/logfile.txtfi# end timerend_time=`date +%s`ELAPSED=`expr $end_time - $start_time`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`sleep $FREQUENCYdone