masy/.synccmd.sh

51 lines
1.9 KiB
Bash
Raw Normal View History

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/>.
2022-11-12 17:39:16 +01:00
source ~/bin/.filetemplates.sh
source ~/bin/.colordef.sh
2021-06-12 21:48:13 +02:00
2021-06-09 17:04:43 +02:00
# $1 -> hash sync
2021-06-12 21:48:13 +02:00
# $2 -> LOCAL PATH
# $3 -> REMOTE PATH -> user@myvps:/home/user/syncdir/ [trailing / avoids to copy remote folder in source]
2021-06-09 17:04:43 +02:00
sync() {
src=$2
dest=$3
otheropts=$4
2022-11-12 17:39:16 +01:00
logfile=$(format ${LOGFILERSYNC} hash=$1)
syncloopfile=$(format ${SYNCLOOPFILE} hash=$1)
if [ ! -f $logfile ]; then
touch $logfile
2021-06-09 17:04:43 +02:00
fi
2022-11-12 17:39:16 +01:00
echo $src
echo $logfile
echo $syncloopfile
2021-06-09 17:04:43 +02:00
# PUSH TO REMOTE - and PULL FROM REMOTE swapping `src` with `dest`
#echo "executing rsync -aPu --log-file=$LOGFILE -e ssh $otheropts $src $dest"
2021-06-09 17:04:43 +02:00
# NOTE REMOTEDIR ALREADY EXISTS IN DESTINATION
2022-11-12 17:39:16 +01:00
# in this form rsync receive only data from source 1>$syncloopfile 2>&1
rsync -ni -aPu --progress --out-format="%i ${GREEN}%n%L${ENDCOLOR} %''b" --log-file=$logfile -e ssh $otheropts $src $dest | \
grep -e "[<|>]" | \
2022-11-18 21:18:55 +01:00
sed -E "s~<(\w|\W){10}~\\${PURPLE}Sending\\${ENDCOLOR} TO ${dest}~g" | \
sed -E "s~>(\w|\W){10}~\\${CYAN}Receiving\\${ENDCOLOR} FROM ${src}~g" | \
xargs -0 printf 1>>$syncloopfile 2>&1
2022-11-04 17:59:09 +01:00
# last command execution exit code $?"
rsync_result=$?
if [[ ${rsync_result} -ne 0 ]]; then
echo "Generic sync error. rsync has failed with the code ${rsync_result}"
exit 1
fi
2021-06-09 17:04:43 +02:00
}