logging and more checks

This commit is contained in:
Amber 2021-06-12 22:33:52 +02:00
parent 44909815cd
commit 3bf8209811
1 changed files with 24 additions and 16 deletions

40
masync
View File

@ -72,17 +72,16 @@ initsyncpath() {
}
startsyncpath() {
echo 'Starting sync...'
echo -e "Starting mirror local folder ${GREEN}$1${ENDCOLOR} with remote ${GREEN}$2${ENDCOLOR}..."
## write the file with remotepath and localpath for future use
localpath_hash=$(echo "$1" | md5sum | cut -f1 --delimiter=" " -)
sync $localpath_hash $2 $1
## create sync with status stopped -> after this start the loop
echo -e "Mirrorring ${GREEN}done${ENDCOLOR}"
loopsyncpath $1
}
loopsyncpath() {
echo "loopsyncpath $1"
#echo $(sed "1q;d" ~/.syncdir.sync) | sed -e "s/STOPPED/RUNNING/"
echo -e "Starting loop for synching ${GREEN}$1${ENDCOLOR}"
re_num='^[0-9]+$'
if [[ $1 =~ $re_num && $1 -gt 0 ]]; then
## $1 is the index of line to substitute
@ -91,7 +90,6 @@ loopsyncpath() {
## $1 is the path of sync of line to substitute
localpath_hash=$(echo "$1" | md5sum | cut -f1 --delimiter=" " -)
nline=$(grep -n $localpath_hash "$SYNCFILE" | cut -f1 -d ":")
echo $nline
fi
sed -n -s "$nline"p "$SYNCFILE" | while read hash pid when status localpath remotepath; do
if [ $status = $STATUS_RUNNING ]; then
@ -101,9 +99,8 @@ loopsyncpath() {
## starting loop on background and catch pid
nohup syncloop.sh $hash $localpath $remotepath 1>~/syncloop_$hash.nohup 2>&1 &
mypid=$!
echo "mypid: $mypid"
when=$(date +%s)
## if line exists i must replace with new pid
## if line exists i must replace it with new pid
sed -i -e "$nline {s/$STATUS_STOPPED/$STATUS_RUNNING/; s/p[0-9]\+/p$mypid/; s/w[0-9]\+/w$when/}" "$SYNCFILE"
fi
done
@ -124,19 +121,19 @@ nsync() {
}
stoploopsyncpath() {
echo "stoploopsyncpath $1"
#echo $(sed "1q;d" ~/.syncdir.sync) | sed -e "s/STOPPED/RUNNING/"
nline=$(nsync "$1")
sed -n -s "$nline"p "$SYNCFILE" | while read hash pid when status localpath remotepath; do
if [ $status = $STATUS_STOPPED ]; then
echo 'Sync already stopped do nothing...'
echo -e "${GREEN}Sync already stopped do nothing...${ENDCOLOR}"
exit 0
else
## stop process and all descendants
echo -e "Stop sync on local folder ${GREEN}${localpath}${ENDCOLOR} remote folder ${GREEN}${remotepath}${ENDCOLOR}..."
pid=$(echo $pid | cut -c 2-)
kill $(ps -o pid= --ppid $pid)
when=$(date +%s)
sed -i -e "$nline {s/$STATUS_RUNNING/$STATUS_STOPPED/; s/w[0-9]\+/w$when/}" "$SYNCFILE"
echo "Done"
fi
done
}
@ -260,6 +257,11 @@ case "$1" in
if [ ${empty} = true ]; then
echo "Sync ${indexsync} not exists, sync file empty, please use init first"
else
re_num='^[0-9]+$'
if [[ ! $indexsync =~ $re_num ]]; then
echo -e "-s option must be ${RED}integer${ENDCOLOR}"
exit 3
fi
oi=$(syncoutofindex $indexsync)
if [ ${oi} = true ]; then
echo "Sync ${indexsync} out of index"
@ -285,30 +287,36 @@ case "$1" in
if [[ -d "$localsync" ]]; then
#echo "Sync this local folder: $2"
synce=$(syncexists ${localsync})
echo "sync exits? ${synce}"
if [ ${synce} = true ]; then
echo "Stopping Sync local folder ${localsync}"
stoploopsyncpath ${localsync}
else
echo "Can't stop local sync for folder ${localsync}. It is not in sync task, please before use init"
echo -e "Can't stop local sync for folder ${RED}${localsync}${ENDCOLOR}. It is not in sync task, please before use init"
exit 2
fi
else
echo "ATTENTION Folder: ${localsync} not exists Aborting!"
echo -e "Local folder ${RED}${localsync}${ENDCOLOR} not exists Aborting!"
exit 2
fi
;;
s) indexsync=${OPTARG}
empty=$(emptysyncfile)
if [ ${empty} = true ]; then
echo "Sync ${indexsync} not exists, sync file empty, please use init first"
echo "Sync file empty, please use init first for creating a sync"
exit 2
else
re_num='^[0-9]+$'
if [[ ! $indexsync =~ $re_num ]]; then
echo -e "-s option must be ${RED}integer${ENDCOLOR}"
exit 3
fi
oi=$(syncoutofindex $indexsync)
if [ ${oi} = true ]; then
echo "Sync ${indexsync} out of index"
exit 1
echo -e "${RED}Sync out of index${ENDCOLOR}"
exit 2
else
stoploopsyncpath $indexsync
#echo "Starting Sync ${indexsync}"
fi
fi
;;