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"
|
|
|
|
source ~/bin/.synccmd.sh
|
|
|
|
TMPQUEUEFILE=~/.syncdir_$1.queue
|
|
|
|
TMPQUEUEDELETES=~/.syncdir_$1.deletes
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-07-12 13:30:00 +02:00
|
|
|
#acquire lock_deletes file descritptor
|
|
|
|
exec {lock_deletes}>>${TMPQUEUEDELETES}
|
|
|
|
#acquire lock queue file descriptor
|
|
|
|
exec {lock_queue}>>${TMPQUEUEFILE}
|
|
|
|
|
2021-06-09 17:04:43 +02:00
|
|
|
synccycle() {
|
|
|
|
lastsynctime="$(date -u +%s.%N)"
|
|
|
|
### max waiting time for pull (seconds)
|
2021-07-12 13:30:00 +02:00
|
|
|
maxpulllimit=60
|
2021-06-09 17:04:43 +02:00
|
|
|
while :
|
|
|
|
do
|
2021-07-02 17:10:47 +02:00
|
|
|
if [ -f "$TMPQUEUEFILE" ] || [ -f "$TMPQUEUEDELETES" ]; then
|
2021-07-12 13:30:00 +02:00
|
|
|
### DELETES WHILE LOOP
|
|
|
|
if [ -f "$TMPQUEUEDELETES" ]; then
|
|
|
|
ndeletes=$(wc -l < ${TMPQUEUEDELETES})
|
|
|
|
while [ $ndeletes -gt 0 ]; do
|
|
|
|
echo "consume ${ndeletes} deletes"
|
|
|
|
echo "remote deletes: $(head -n $ndeletes ${TMPQUEUEDELETES} | tr '\n', ' ')"
|
|
|
|
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${TMPQUEUEDELETES} | tr '\n', ' ')"
|
|
|
|
# remove the first ndeletes lines
|
|
|
|
flock $lock_deletes
|
|
|
|
tmpfile=$(mktemp)
|
|
|
|
tail -n +$(expr ${ndeletes} + 1) < ${TMPQUEUEDELETES} > ${tmpfile}
|
|
|
|
cat ${tmpfile} > ${TMPQUEUEDELETES}
|
|
|
|
rm -f ${tmpfile}
|
|
|
|
flock -u $lock_deletes
|
2021-06-09 17:04:43 +02:00
|
|
|
ndeletes=$(wc -l < ${TMPQUEUEDELETES})
|
2021-07-12 13:30:00 +02:00
|
|
|
echo "ndeletes: $ndeletes"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
### PUSH WHILE LOOP
|
|
|
|
if [ -f "$TMPQUEUEFILE" ]; then
|
|
|
|
nqueue=$(wc -l < ${TMPQUEUEFILE})
|
|
|
|
while [ $nqueue -gt 0 ]; do
|
2021-07-19 15:29:27 +02:00
|
|
|
echo "${TMPQUEUEFILE} not empty consume ${nqueue} updates in queue"
|
2021-07-12 13:30:00 +02:00
|
|
|
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH
|
|
|
|
# remove the first nqueue lines from queue
|
|
|
|
flock $lock_queue
|
|
|
|
tmpfile=$(mktemp)
|
|
|
|
tail -n +$(expr ${nqueue} + 1) < ${TMPQUEUEFILE} > ${tmpfile}
|
|
|
|
cat ${tmpfile} > ${TMPQUEUEFILE}
|
|
|
|
rm -f ${tmpfile}
|
|
|
|
flock -u $lock_queue
|
|
|
|
lastsynctime="$(date -u +%s.%N)"
|
|
|
|
nqueue=$(wc -l < ${TMPQUEUEFILE})
|
|
|
|
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
|
2021-06-10 09:33:11 +02:00
|
|
|
echo 'maxpulllimit reached: sync from remote with --delete option'
|
|
|
|
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
|
2021-06-16 22:00:36 +02:00
|
|
|
lastsynctime="$(date -u +%s.%N)"
|
2021-06-09 17:04:43 +02:00
|
|
|
fi
|
|
|
|
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-06-16 22:00:36 +02:00
|
|
|
if [ $action = 'DELETE' ] || [ $action = 'DELETE,ISDIR' ]; then
|
2021-06-09 17:04:43 +02:00
|
|
|
echo "Enqueue delete: $dir$file"
|
2021-07-12 13:30:00 +02:00
|
|
|
flock $lock_deletes
|
2021-06-09 17:04:43 +02:00
|
|
|
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${TMPQUEUEDELETES}
|
2021-07-12 13:30:00 +02:00
|
|
|
flock -u $lock_deletes
|
|
|
|
else
|
|
|
|
flock $lock_queue
|
|
|
|
echo "Add notify to queue: file '$file' in directory '$dir' for '$action'" | tee -a ${TMPQUEUEFILE}
|
|
|
|
flock -u $lock_queue
|
2021-06-09 17:04:43 +02:00
|
|
|
fi
|
|
|
|
done
|