hastrailingslash
This commit is contained in:
parent
4617a6b5bf
commit
3492a82763
23
syncdir.sh
23
syncdir.sh
@ -10,8 +10,8 @@ GREEN="\e[32m"
|
||||
ENDCOLOR="\e[0m"
|
||||
|
||||
HELP_CMD_NAME='syncdir.sh'
|
||||
HELP_LOCAL_DIR="/home/$USER/localsync"
|
||||
HELP_REMOTE_DIR="remoteuser@server:/home/remoteuser/localsync"
|
||||
HELP_LOCAL_DIR="/home/$USER/localsync/"
|
||||
HELP_REMOTE_DIR="remoteuser@server:/home/remoteuser/sync/"
|
||||
|
||||
myhelp() {
|
||||
echo 'Synopsis: '
|
||||
@ -30,6 +30,17 @@ timestamp() {
|
||||
$(date +%s)
|
||||
}
|
||||
|
||||
hastrailingslash() {
|
||||
case "$1" in
|
||||
*/)
|
||||
echo true
|
||||
;;
|
||||
*)
|
||||
echo false
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
syncexists() {
|
||||
localpath_hash=$(echo "$1" | md5sum | cut -f1 --delimiter=" " -)
|
||||
if [[ $(cat "${SYNCFILE}" | grep "$localpath_hash" | wc -l) -ge 1 ]]; then
|
||||
@ -178,6 +189,10 @@ case "$1" in
|
||||
## create path
|
||||
mkdir -p "${localpath}"
|
||||
fi
|
||||
lhts=$(hastrailingslash $localpath)
|
||||
if [ ${lhts} = false ]; then
|
||||
localpath="${localpath}"/
|
||||
fi
|
||||
;;
|
||||
r) echo "r option: ${option} value ${OPTARG}"
|
||||
#match an adress in this form username@from_host:/home/test
|
||||
@ -198,6 +213,10 @@ case "$1" in
|
||||
cat $SYNCFILE
|
||||
exit 1
|
||||
else
|
||||
rhts=$(hastrailingslash $remotepath)
|
||||
if [ ${rhts} = false ]; then
|
||||
remotepath="${remotepath}"/
|
||||
fi
|
||||
initsyncpath $localpath $remotepath
|
||||
fi
|
||||
|
||||
|
38
syncloop.sh
38
syncloop.sh
@ -16,38 +16,10 @@ 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
|
||||
maxpulllimit=30
|
||||
while :
|
||||
do
|
||||
if [ -f "$TMPQUEUEFILE" ]; then
|
||||
@ -70,19 +42,17 @@ synccycle() {
|
||||
# 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 elapsed is greater than maxpulllimit sync from remote with delete option
|
||||
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
|
||||
sleep 2
|
||||
done
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user