#!/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)# Also redirect stderrexec 2>&1# 9 min delay between resyncFREQUENCY=540TIDYUP="$(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; $2; $3""$RSYNC" $1 $2 $3EXIT=$?[ $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# first sorttidyup $1 || error "Sort failed, please analyze logfile.txt ."# first syncinfo "Doing complete sync"syncdir . -aif [ -f "$1"/LAST ]thenread LAST < "$1"/LASTelseLAST="."fiif [ -f "$1"/capture/LAST ]thenread LAST_HOUR < "$1"/capture/LASTelseLAST_HOUR="."fi# Periodically tidy up and do incremental syncwhile :do# start timerstart_time=`date +%s`OLD="$LAST"OLD_HOUR="$LAST_HOUR"# sorttidyup $1 || error "Sort failed, please analyze logfile.txt ."if [ -f "$1"/LAST ]then# read last processed dayread LAST < "$1"/LASTread LAST_HOUR < "$1"/capture/LAST# sync last updated folderssyncdir audio/"$LAST" -asyncdir capture/"$LAST_HOUR" -asyncdir data/"$LAST" -asyncdir data --dirs --delete# days changed, sync whole yesterday tooif [ "$LAST" != "$OLD" ]theninfo "syncing yesterday"syncdir audio/"$OLD" -asyncdir capture/"$OLD" -asyncdir data/"$OLD" -aelse# hours changed, sync previous hour tooif [ "$LAST_HOUR" != "$OLD_HOUR" ]theninfo "syncing previous hour"syncdir capture/"$OLD_HOUR" -afififi# end timerend_time=`date +%s`ELAPSED=`expr $end_time - $start_time`info "execution time was $ELAPSED s"date# save only tail from logfiletail -n 1000 "$1"/logfile > "$1"/logfile.txtrm "$1"/logfilesyncdir . --dirs --delete > /dev/null 2>&1exec > >(tee "$1"/logfile)exec 2>&1# wait for a next syncing periodsleep $FREQUENCYdone