#!/usr/bin/env bash[[ $# -ne 2 ]] && echo "Usage: ./sync.sh /path/to/folder observatory_name" && exit 1# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"#exec > >(tee "$1"/logfile)# Also redirect stderr#exec 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; $4""$RSYNC" $1 $2 $3 $4EXIT=$?[ $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"return 1fireturn 0}# Test public key authenticationfunction sshtest() {./test.sh $1if [ "$?" -eq 0 ]; theninfo "Authentication works"return 0elseerror "Authentication does not work"return 1fi}# parameters $1=LocalPathToData $2=ObservatoryNamefunction main() {# 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 day# read LAST < "$1"/LAST# read LAST_HOUR < "$1"/capture/LAST# sync last updated folderssyncdir $2 audio/"$LAST" -asyncdir $2 capture/"$LAST_HOUR" -asyncdir $2 data/"$LAST" -asyncdir $2 data --dirs --delete# days changed, sync whole yesterday tooif [ "$LAST" != "$OLD" ]theninfo "syncing yesterday"syncdir $2 audio/"$OLD" -asyncdir $2 capture/"$OLD" -asyncdir $2 data/"$OLD" -aelse# hours changed? sync previous hour too?if [ "$LAST_HOUR" != "$OLD_HOUR" ]theninfo "syncing previous hour"syncdir $2 capture/"$OLD_HOUR" -afififi# end timerend_time=`date +%s`ELAPSED=`expr $end_time - $start_time`info "execution time was $ELAPSED s"date}# Check if we can connect, otherwise terminateinfo "Checking connection to the server"sshtest $2 || exit 1# Change working directorycd $1# first sortinfo "Tidyup..."tidyup $1 || error "Sorted, please analyze logfile.txt ."# first syncinfo "Doing complete sync"syncdir $2 . -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 :doOLD=$LASTOLD_HOUR=$LAST_HOUR# read last processed day/hourread LAST < "$1"/LASTread LAST_HOUR < "$1"/capture/LASTmain $1 $2 2>&1 | tee "$1"/logfile# save only tail from logfiletail -n 1000 "$1"/logfile > "$1"/logfile.txtrm "$1"/logfilesyncdir $2 . --dirs --delete > /dev/null 2>&1# wait for a next syncing periodsleep $FREQUENCYdone