hastrailingslash

This commit is contained in:
Amber 2021-06-10 09:33:11 +02:00
parent 4617a6b5bf
commit 3492a82763
2 changed files with 25 additions and 36 deletions

View File

@ -10,8 +10,8 @@ GREEN="\e[32m"
ENDCOLOR="\e[0m" ENDCOLOR="\e[0m"
HELP_CMD_NAME='syncdir.sh' HELP_CMD_NAME='syncdir.sh'
HELP_LOCAL_DIR="/home/$USER/localsync" HELP_LOCAL_DIR="/home/$USER/localsync/"
HELP_REMOTE_DIR="remoteuser@server:/home/remoteuser/localsync" HELP_REMOTE_DIR="remoteuser@server:/home/remoteuser/sync/"
myhelp() { myhelp() {
echo 'Synopsis: ' echo 'Synopsis: '
@ -30,6 +30,17 @@ timestamp() {
$(date +%s) $(date +%s)
} }
hastrailingslash() {
case "$1" in
*/)
echo true
;;
*)
echo false
;;
esac
}
syncexists() { syncexists() {
localpath_hash=$(echo "$1" | md5sum | cut -f1 --delimiter=" " -) localpath_hash=$(echo "$1" | md5sum | cut -f1 --delimiter=" " -)
if [[ $(cat "${SYNCFILE}" | grep "$localpath_hash" | wc -l) -ge 1 ]]; then if [[ $(cat "${SYNCFILE}" | grep "$localpath_hash" | wc -l) -ge 1 ]]; then
@ -178,6 +189,10 @@ case "$1" in
## create path ## create path
mkdir -p "${localpath}" mkdir -p "${localpath}"
fi fi
lhts=$(hastrailingslash $localpath)
if [ ${lhts} = false ]; then
localpath="${localpath}"/
fi
;; ;;
r) echo "r option: ${option} value ${OPTARG}" r) echo "r option: ${option} value ${OPTARG}"
#match an adress in this form username@from_host:/home/test #match an adress in this form username@from_host:/home/test
@ -198,6 +213,10 @@ case "$1" in
cat $SYNCFILE cat $SYNCFILE
exit 1 exit 1
else else
rhts=$(hastrailingslash $remotepath)
if [ ${rhts} = false ]; then
remotepath="${remotepath}"/
fi
initsyncpath $localpath $remotepath initsyncpath $localpath $remotepath
fi fi

View File

@ -16,38 +16,10 @@ REMOTEPATH=$3
REMOTEHOST=$(echo "$REMOTEPATH" | cut -d : -f 1 | cut -d @ -f 2) REMOTEHOST=$(echo "$REMOTEPATH" | cut -d : -f 1 | cut -d @ -f 2)
REMOTERELATIVEPATH=$(echo "$REMOTEPATH" | 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() { synccycle() {
echo 'Starting sync consumer cycle'
lastsynctime="$(date -u +%s.%N)" lastsynctime="$(date -u +%s.%N)"
### max waiting time for pull (seconds) ### max waiting time for pull (seconds)
maxpulllimit=60*2 maxpulllimit=30
while : while :
do do
if [ -f "$TMPQUEUEFILE" ]; then if [ -f "$TMPQUEUEFILE" ]; then
@ -70,19 +42,17 @@ synccycle() {
# remove the first nqueue lines from queue # remove the first nqueue lines from queue
tail -n +$(expr ${nqueue} + 1) < ${TMPQUEUEFILE} > ${TMPQUEUEFILE} tail -n +$(expr ${nqueue} + 1) < ${TMPQUEUEFILE} > ${TMPQUEUEFILE}
lastsynctime="$(date -u +%s.%N)" lastsynctime="$(date -u +%s.%N)"
sleep 2
## step to next iteration ## step to next iteration
continue continue
fi fi
fi fi
echo 'Queue empty verify if it is time for pull'
now="$(date -u +%s.%N)" now="$(date -u +%s.%N)"
elapsed="$(bc <<<"$now-$lastsynctime")" elapsed="$(bc <<<"$now-$lastsynctime")"
## if elapsed is greater than maxpulllimit sync from remote ## if elapsed is greater than maxpulllimit sync from remote with delete option
if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH echo 'maxpulllimit reached: sync from remote with --delete option'
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
fi fi
sleep 2
done done
} }