#!/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 logfile.txt)# Also redirect stderrexec 2>&1FREQUENCY=60TIDYUP="./tidyup.sh"RSYNC="./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=1while [ $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 . || 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=0# next line is important for the first run of the loopread LAST < LASTfilet HOURCOUNT++# read last processed dayOLD="$LAST"read LAST < LAST# sync last updated foldersyncdir "$LAST"# days changed, sync yesterday too[[ "$LAST" != "$OLD" ]] && info "syncing yesterday" && syncdir "$OLD"tail -n 1000 logfile.txt > tmp.txtmv tmp.txt logfile.txt# end timerend_time=`date +%s`ELAPSED=`expr $end_time - $start_time`info "execution time was $ELAPSED s"# if last sync took less than TIME, sleep to make up 1 hour[[ $ELAPSED -lt $FREQUENCY ]] && sleep `expr $FREQUENCY - $ELAPSED`done