masy/syncloop.sh

149 lines
5.2 KiB
Bash
Raw Permalink Normal View History

2021-06-09 17:04:43 +02:00
#!/bin/bash
2021-06-12 21:48:13 +02:00
# Copyright 2021 Luca Paris
#This file is part of masync.
#masync is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#masync is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with masync. If not, see <http://www.gnu.org/licenses/>.
2021-06-09 17:04:43 +02:00
## REQUIREMENT
# apt-get install inotify-tools
# @1 hash to retrieve sync
# @2 local path to sync
# @3 remote path to sync
#echo "$1, $2, $3"
2022-11-07 21:45:04 +01:00
source ~/bin/.filetemplates.sh
2021-06-09 17:04:43 +02:00
source ~/bin/.synccmd.sh
source ~/bin/.colordef.sh
2022-11-07 21:45:04 +01:00
tmpqueuefile=$(format ${TMPQUEUEFILE} hash=$1)
tmpqueuedeletes=$(format ${TMPQUEUEDELETES} hash=$1)
2022-11-18 21:18:55 +01:00
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
2021-06-09 17:04:43 +02:00
LOCALPATH_HASH=$1
LOCALPATH=$2
REMOTEPATH=$3
2022-11-18 21:18:55 +01:00
2021-06-09 17:04:43 +02:00
REMOTEHOST=$(echo "$REMOTEPATH" | cut -d : -f 1 | cut -d @ -f 2)
REMOTERELATIVEPATH=$(echo "$REMOTEPATH" | cut -d : -f 2)
2021-07-12 13:30:00 +02:00
#acquire lock_deletes file descritptor
2022-11-07 21:45:04 +01:00
exec {lock_deletes}>>${tmpqueuedeletes}
2021-07-12 13:30:00 +02:00
#acquire lock queue file descriptor
2022-11-07 21:45:04 +01:00
exec {lock_queue}>>${tmpqueuefile}
2021-07-12 13:30:00 +02:00
2021-07-28 17:17:44 +02:00
min() {
if [ $1 -gt $2 ]; then
echo $2
else
echo $1
fi
}
2021-06-09 17:04:43 +02:00
synccycle() {
lastsynctime="$(date -u +%s.%N)"
2021-07-28 17:17:44 +02:00
### initial max waiting time for pull (seconds) used as unit
2021-08-23 15:08:14 +02:00
### first pulliteration zero cause immediate pulling from remote
2021-07-28 17:17:44 +02:00
pulllimitunit=60
2021-08-23 15:08:14 +02:00
pulliteration=0
maxpulliteration=5
2021-07-28 17:17:44 +02:00
let "maxpulllimit = $pulllimitunit * $pulliteration"
### max waiting time for pull (seconds) 10 min
thresholdpulllimit=600
2021-06-09 17:04:43 +02:00
while :
do
2022-11-07 21:45:04 +01:00
if [ -f "$tmpqueuefile" ] || [ -f "$tmpqueuedeletes" ]; then
2021-07-12 13:30:00 +02:00
### DELETES WHILE LOOP
2022-11-07 21:45:04 +01:00
if [ -f "$tmpqueuedeletes" ]; then
ndeletes=$(wc -l < ${tmpqueuedeletes})
2021-07-12 13:30:00 +02:00
while [ $ndeletes -gt 0 ]; do
2022-11-26 12:54:30 +01:00
echo -e "${PURPLE}[DELETE]${ENDCOLOR} at remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}"
2022-11-07 21:45:04 +01:00
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')"
2022-12-02 16:33:38 +01:00
echo -e "${PURPLE}[DONE]${ENDCOLOR}"
2021-07-12 13:30:00 +02:00
# remove the first ndeletes lines
flock $lock_deletes
tmpfile=$(mktemp)
2022-11-07 21:45:04 +01:00
tail -n +$(expr ${ndeletes} + 1) < ${tmpqueuedeletes} > ${tmpfile}
cat ${tmpfile} > ${tmpqueuedeletes}
2021-07-12 13:30:00 +02:00
rm -f ${tmpfile}
flock -u $lock_deletes
2022-11-07 21:45:04 +01:00
ndeletes=$(wc -l < ${tmpqueuedeletes})
2021-07-12 13:30:00 +02:00
done
fi
### PUSH WHILE LOOP
2022-11-07 21:45:04 +01:00
if [ -f "$tmpqueuefile" ]; then
nqueue=$(wc -l < ${tmpqueuefile})
2021-07-12 13:30:00 +02:00
while [ $nqueue -gt 0 ]; do
2022-11-07 21:45:04 +01:00
#echo "${tmpqueuefile} not empty consume ${nqueue} updates in queue"
2021-07-12 13:30:00 +02:00
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH
2022-12-02 16:33:38 +01:00
echo -e "${PURPLE}[PUSH DONE]${ENDCOLOR}"
2021-07-12 13:30:00 +02:00
# remove the first nqueue lines from queue
flock $lock_queue
tmpfile=$(mktemp)
2022-11-07 21:45:04 +01:00
tail -n +$(expr ${nqueue} + 1) < ${tmpqueuefile} > ${tmpfile}
cat ${tmpfile} > ${tmpqueuefile}
2021-07-12 13:30:00 +02:00
rm -f ${tmpfile}
flock -u $lock_queue
2021-07-28 17:17:44 +02:00
#lastsynctime="$(date -u +%s.%N)"
2022-11-07 21:45:04 +01:00
nqueue=$(wc -l < ${tmpqueuefile})
2021-07-12 13:30:00 +02:00
done
2021-06-09 17:04:43 +02:00
fi
fi
now="$(date -u +%s.%N)"
elapsed="$(bc <<<"$now-$lastsynctime")"
2021-06-10 09:33:11 +02:00
## if elapsed is greater than maxpulllimit sync from remote with delete option
2021-06-09 17:04:43 +02:00
if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then
2022-11-26 12:54:30 +01:00
echo -e "${PURPLE}[PULL LIMIT]${ENDCOLOR} $maxpulllimit seconds reached: sync from remote with --delete option..."
2021-06-10 09:33:11 +02:00
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
2022-11-26 12:54:30 +01:00
echo -e "${PURPLE}[PULL LIMIT DONE]${ENDCOLOR}"
lastsynctime="$(date -u +%s.%N)"
2021-08-23 15:08:14 +02:00
## increment the pull iteration or reset it
if [ ${pulliteration} -eq ${maxpulliteration} ]; then
pulliteration=0
else
let pulliteration++
fi
2021-07-28 17:17:44 +02:00
let "limit = $pulliteration * $pulliteration * $pulllimitunit"
maxpulllimit=$(min $limit $thresholdpulllimit)
2021-06-09 17:04:43 +02:00
fi
2021-07-28 17:17:44 +02:00
sleep 10
2021-06-09 17:04:43 +02:00
done
}
synccycle &
# exclude swp,swpx and 4913 files created by vim
2021-07-01 17:07:12 +02:00
inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\.swx|4913|.txt~" $LOCALPATH | while read dir action file; do
2021-09-06 13:20:42 +02:00
if [ $action = 'DELETE' ] || [ $action = 'DELETE,ISDIR' ] || [ $action = 'MOVED_FROM' ] || [ $action = 'MOVED_FROM,ISDIR' ]; then
2022-11-18 21:18:55 +01:00
echo -e "syncloop - ENQUEUE delete $dir$file"
2021-07-12 13:30:00 +02:00
flock $lock_deletes
2022-11-07 21:45:04 +01:00
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${tmpqueuedeletes} 1>/dev/null
2021-07-12 13:30:00 +02:00
flock -u $lock_deletes
else
flock $lock_queue
2022-03-14 21:26:00 +01:00
## add to queue only if not already in it
2022-11-07 21:45:04 +01:00
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}
2022-11-18 21:18:55 +01:00
echo -e "syncloop - ENQUEUE file $dir$file"
# AVOID TO USE TEE: SIMPLY append
2022-11-07 21:45:04 +01:00
#echo ${dir}${file} | tee -a ${tmpqueuefile}
echo ${dir}${file} >> ${tmpqueuefile}
2022-03-14 21:26:00 +01:00
fi
2021-07-12 13:30:00 +02:00
flock -u $lock_queue
2021-06-09 17:04:43 +02:00
fi
done