From 2147ceebd6a3df645125cdb9000b5eff54edd83e Mon Sep 17 00:00:00 2001 From: Amber Date: Fri, 18 Nov 2022 21:18:55 +0100 Subject: [PATCH] color and logs --- .synccmd.sh | 5 +++-- masync | 2 +- syncloop.sh | 16 +++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.synccmd.sh b/.synccmd.sh index 13d30cb..ce6d85a 100755 --- a/.synccmd.sh +++ b/.synccmd.sh @@ -38,8 +38,9 @@ sync() { # in this form rsync receive only data from source 1>$syncloopfile 2>&1 rsync -ni -aPu --progress --out-format="%i ${GREEN}%n%L${ENDCOLOR} %''b" --log-file=$logfile -e ssh $otheropts $src $dest | \ grep -e "[<|>]" | \ - sed -E "s~<(\w|\W){10}~Sending to ${dest}~g" | \ - sed -E "s~>(\w|\W){10}~Receiving from ${src}~g" | xargs -0 printf 1>>$syncloopfile 2>&1 + sed -E "s~<(\w|\W){10}~\\${PURPLE}Sending\\${ENDCOLOR} TO ${dest}~g" | \ + sed -E "s~>(\w|\W){10}~\\${CYAN}Receiving\\${ENDCOLOR} FROM ${src}~g" | \ + xargs -0 printf 1>>$syncloopfile 2>&1 # last command execution exit code $?" rsync_result=$? if [[ ${rsync_result} -ne 0 ]]; then diff --git a/masync b/masync index 921044d..8b48584 100755 --- a/masync +++ b/masync @@ -190,7 +190,7 @@ loopsyncpath() { rm -f ${rsynclogfile} ## starting loop on background and catch pid syncloopfile=$(format ${SYNCLOOPFILE} hash=$hash) - nohup syncloop.sh $hash $localpath $remotepath 1>~/syncloop_$hash.nohup 2>&1 & + nohup syncloop.sh $hash $localpath $remotepath 1>$syncloopfile 2>&1 & mypid=$! when=$(date +%s) ## if line exists i must replace it with new pid diff --git a/syncloop.sh b/syncloop.sh index 0c220aa..fb12714 100755 --- a/syncloop.sh +++ b/syncloop.sh @@ -26,11 +26,17 @@ source ~/bin/.synccmd.sh source ~/bin/.colordef.sh tmpqueuefile=$(format ${TMPQUEUEFILE} hash=$1) tmpqueuedeletes=$(format ${TMPQUEUEDELETES} hash=$1) +syncloopfile=$(format ${SYNCLOOPFILE} hash=$1) + +exec 3>&1 4>&2 +trap 'exec 2>&4 1>&3' 0 1 2 3 +exec 1>>$syncloopfile 2>&1 LOCALPATH_HASH=$1 LOCALPATH=$2 REMOTEPATH=$3 + REMOTEHOST=$(echo "$REMOTEPATH" | cut -d : -f 1 | cut -d @ -f 2) REMOTERELATIVEPATH=$(echo "$REMOTEPATH" | cut -d : -f 2) @@ -64,7 +70,7 @@ synccycle() { if [ -f "$tmpqueuedeletes" ]; then ndeletes=$(wc -l < ${tmpqueuedeletes}) while [ $ndeletes -gt 0 ]; do - echo -n -e "[Masync log - PUSH FILES TO DELETE] to remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}" + echo -n -e "syncloop delete at remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}" ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')" echo -e " ${PURPLE}DONE${ENDCOLOR}" # remove the first ndeletes lines @@ -83,7 +89,7 @@ synccycle() { nqueue=$(wc -l < ${tmpqueuefile}) while [ $nqueue -gt 0 ]; do #echo "${tmpqueuefile} not empty consume ${nqueue} updates in queue" - echo -n -e "[Masync log - PUSH FILES] to remote: ${GREEN}$(head -n ${nqueue} ${tmpqueuefile} | tr '\n', ' ')${ENDCOLOR}" + echo -n "syncloop START SYNC" sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH echo -e " ${PURPLE}DONE${ENDCOLOR}" # remove the first nqueue lines from queue @@ -102,7 +108,7 @@ synccycle() { elapsed="$(bc <<<"$now-$lastsynctime")" ## if elapsed is greater than maxpulllimit sync from remote with delete option if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then - echo -n "[Masync log - PULL LIMIT reached] $maxpulllimit seconds reached: sync from remote with --delete option..." + echo -n "syncloop $maxpulllimit seconds reached: sync from remote with --delete option..." sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete' echo -e " ${PURPLE}DONE${ENDCOLOR}" lastsynctime="$(date -u +%s.%N)" @@ -124,7 +130,7 @@ synccycle & # exclude swp,swpx and 4913 files created by vim inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\.swx|4913|.txt~" $LOCALPATH | while read dir action file; do if [ $action = 'DELETE' ] || [ $action = 'DELETE,ISDIR' ] || [ $action = 'MOVED_FROM' ] || [ $action = 'MOVED_FROM,ISDIR' ]; then - echo -e "[Masync log - ENQUEUE delete] $dir$file" + echo -e "syncloop - ENQUEUE delete $dir$file" flock $lock_deletes echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${tmpqueuedeletes} 1>/dev/null flock -u $lock_deletes @@ -133,7 +139,7 @@ inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\. ## add to queue only if not already in it if [ $(grep -E ${file}$ ${tmpqueuefile} | wc -l) -lt 1 ]; then #echo "Add notify to queue: file '$file' in directory '$dir' for '$action'" | tee -a ${tmpqueuefile} - echo -e "[Masync log - ENQUEUE file] $dir$file" + echo -e "syncloop - ENQUEUE file $dir$file" # AVOID TO USE TEE: SIMPLY append #echo ${dir}${file} | tee -a ${tmpqueuefile} echo ${dir}${file} >> ${tmpqueuefile}