100 lines
3.4 KiB
Bash
Executable File
100 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
## 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)
|
|
|
|
syncmng() {
|
|
#touch ${TMPLOCKFILE}
|
|
## push deletion
|
|
if [ -f "$TMPQUEUEDELETES" ]; then
|
|
ndeletes=$(wc -l < ${TMPQUEUEDELETES})
|
|
echo "delete queue $ndeletes"
|
|
cat "${TMPQUEUEDELETES}"
|
|
while [ $ndeletes -gt 0 ]; do
|
|
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${TMPQUEUEDELETES} | tr '\n', ' ')"
|
|
# remove the first ndeletes lines
|
|
tail -n +$(expr ${ndeletes} + 1) < ${TMPQUEUEDELETES} > ${TMPQUEUEDELETES}
|
|
done
|
|
fi
|
|
if [ -f "$TMPQUEUEFILE" ]; then
|
|
nqueue=$(wc -l < ${TMPQUEUEFILE})
|
|
while [ $nqueue -gt 0 ]; do
|
|
echo "${TMPQUEUEFILE} not empty before removing lock... syncing ${nqueue} actions from queue"
|
|
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH '--delete'
|
|
# remove the first nqueue lines from queue
|
|
tail -n +$(expr ${nqueue} + 1) < ${TMPQUEUEFILE} > ${TMPQUEUEFILE}
|
|
nqueue=$(wc -l < ${TMPQUEUEFILE})
|
|
done
|
|
fi
|
|
echo 'End sync... removing lock'
|
|
#rm "$TMPLOCKFILE"
|
|
}
|
|
|
|
synccycle() {
|
|
echo 'Starting sync consumer cycle'
|
|
lastsynctime="$(date -u +%s.%N)"
|
|
### max waiting time for pull (seconds)
|
|
maxpulllimit=60*2
|
|
while :
|
|
do
|
|
if [ -f "$TMPQUEUEFILE" ]; then
|
|
nqueue=$(wc -l < ${TMPQUEUEFILE})
|
|
if [ $nqueue -gt 0 ]; then
|
|
echo "${TMPQUEUEFILE} not empty syncing ${nqueue} actions from queue"
|
|
## FIRST STEP push deletion if there are
|
|
if [ -f "$TMPQUEUEDELETES" ]; then
|
|
ndeletes=$(wc -l < ${TMPQUEUEDELETES})
|
|
echo "delete queue $ndeletes"
|
|
cat "${TMPQUEUEDELETES}"
|
|
if [ $ndeletes -gt 0 ]; then
|
|
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${TMPQUEUEDELETES} | tr '\n', ' ')"
|
|
# remove the first ndeletes lines
|
|
tail -n +$(expr ${ndeletes} + 1) < ${TMPQUEUEDELETES} > ${TMPQUEUEDELETES}
|
|
fi
|
|
fi
|
|
## end push deletion if there are
|
|
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH
|
|
# remove the first nqueue lines from queue
|
|
tail -n +$(expr ${nqueue} + 1) < ${TMPQUEUEFILE} > ${TMPQUEUEFILE}
|
|
lastsynctime="$(date -u +%s.%N)"
|
|
sleep 2
|
|
## step to next iteration
|
|
continue
|
|
fi
|
|
fi
|
|
echo 'Queue empty verify if it is time for pull'
|
|
now="$(date -u +%s.%N)"
|
|
elapsed="$(bc <<<"$now-$lastsynctime")"
|
|
## if elapsed is greater than maxpulllimit sync from remote
|
|
if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then
|
|
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH
|
|
fi
|
|
sleep 2
|
|
done
|
|
}
|
|
|
|
synccycle &
|
|
|
|
# exclude swp,swpx and 4913 files created by vim
|
|
# inotifywait -m -r -e close_write -e move -e delete --exclude "\.swp|\.swx|4913|.txt~" ~/googleDrive/Copy/Appunti/
|
|
inotifywait -m -r -e close_write -e move -e delete --exclude "\.swp|\.swx|4913|.txt~" $LOCALPATH | while read dir action file; do
|
|
if [ $action = 'DELETE' ]; then
|
|
echo "Enqueue delete: $dir$file"
|
|
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${TMPQUEUEDELETES}
|
|
fi
|
|
echo "Add notify to queue: file '$file' in directory '$dir' for '$action'" | tee -a ${TMPQUEUEFILE}
|
|
done
|