Compare commits

...

6 Commits

Author SHA1 Message Date
Amber 9108c6b0dc decomment 2022-12-02 16:36:21 +01:00
Amber e9082e375f masync log 2022-12-02 16:33:38 +01:00
Amber 799a7078b2 log and snapshot interactive 2022-11-29 13:55:34 +01:00
Amber 28277acb4f better log 2022-11-26 12:54:30 +01:00
Amber 2147ceebd6 color and logs 2022-11-18 21:18:55 +01:00
Amber b29cf27a05 solved color in rsynclog 2022-11-18 17:37:09 +01:00
4 changed files with 62 additions and 29 deletions

View File

@ -15,17 +15,24 @@
#You should have received a copy of the GNU General Public License
#along with masync. If not, see <http://www.gnu.org/licenses/>.
#
###############################################
# the file contains all sync managed by masync
#
###############################################
SYNCFILE=~/.syncdir.sync
#
# the file contains the snapshot of a stopped sync
#
###############################################
# the file contains the snapshot. A snapshot is the list of files
# locally synchronized when the sync was stopped
###############################################
SNAPSHOTFILE=~/.snapshot_{{hash}}
TMPQUEUEFILE=~/.syncdir_{{hash}}.queue
TMPQUEUEDELETES=~/.syncdir_{{hash}}.deletes
###############################################
# the file when the loop logs the activity
###############################################
SYNCLOOPFILE=~/syncloop_{{hash}}.nohup
###############################################
# raw rsync log (used by .synccmd.sh)
###############################################
LOGFILERSYNC=~/syncdir_{{hash}}.log
################################

View File

@ -29,17 +29,16 @@ sync() {
if [ ! -f $logfile ]; then
touch $logfile
fi
echo $src
echo $logfile
echo $syncloopfile
# PUSH TO REMOTE - and PULL FROM REMOTE swapping `src` with `dest`
#echo "executing rsync -aPu --log-file=$LOGFILE -e ssh $otheropts $src $dest"
# NOTE REMOTEDIR ALREADY EXISTS IN DESTINATION
# 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 "[<|>]" | \
sed -E "s~<(\w|\W){10}~Sending to ${dest}~g" | \
sed -E "s~>(\w|\W){10}~Receiving from ${src}~g" 1>>$syncloopfile 2>&1
rsync -i -aPu --progress --out-format="%i ${GREEN}%n%L${ENDCOLOR} %''b" --log-file=$logfile -e ssh $otheropts $src $dest | \
grep -E "(<|>|deleting)" | \
sed -E "s~<(\w|\W){10}~\\${PURPLE}\[SEND\]\\${ENDCOLOR} to remote ~g" | \
sed -E "s~>(\w|\W){10}~\\${PURPLE}\[RECEIVE\]\\${ENDCOLOR} from remote ~g" | \
sed -E "s~\*deleting(\s)*~\\${PURPLE}\[DELETE\]\\${ENDCOLOR} in local ~g" | \
xargs -r -0 printf 1>>$syncloopfile 2>&1
# last command execution exit code $?"
rsync_result=$?
if [[ ${rsync_result} -ne 0 ]]; then

40
masync
View File

@ -90,13 +90,31 @@ feeddeletes() {
if [[ -e "${path}" ]]; then
tocheckremotepath=$(echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g")
if ! grep -Fxq "${tocheckremotepath}" ${remotefiles}; then
echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in remote";
rm -rf "${path}"
# echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in remote"
echo -e -n "Do you want to remove ${RED}${path}${ENDCOLOR} from local sync? [y to remove, return to skip it, N no to all] "
read -u 1 ui
if [[ -z "${ui}" ]]; then
if [ ${ui} = 'y' ]; then
rm -rf "${path}"
fi
if [ ${ui} = 'N' ]; then
break
fi
fi
fi
else
echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in local, delete in remote";
## if controlled file doesn't exist in local delete it on remote
echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g" | tee -a ${queuedeletes}
# echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in local, delete in remote"
echo -e -n "Do you want to remove ${RED}${path}${ENDCOLOR} from remote origin? [y to remove, return to skip it, N no to all] "
read -u 1 ui
if [[ -z "${ui}" ]]; then
if [ ${ui} = 'y' ]; then
## if controlled file doesn't exist in local delete it on remote
echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g" | tee -a ${queuedeletes}
fi
if [ ${ui} = 'N' ]; then
break
fi
fi
fi
done < ${snapfile}
rm -rf ${remotefiles}
@ -143,14 +161,16 @@ startsyncpath() {
snapshotfile=$(format ${SNAPSHOTFILE} hash=${localpath_hash})
#tempqueuefile=~/.syncdir_${localpath_hash}.queue
tempqueuefile=$(format ${TMPQUEUEFILE} hash=${localpath_hash})
syncloopfile=$(format ${SYNCLOOPFILE} hash=${localpath_hash})
if [ -e ${snapshotfile} ]; then
echo "Founded snapshot file: ${snapshotfile} collect files to delete in origin"
echo "Founded snapshot file: ${snapshotfile} checking files to remove..."
feeddeletes ${localpath_hash}
echo "Remove snapshot file ${snapshotfile}"
rm -f ${snapshotfile}
fi
## ensure local computer sends eventually new files adding a fake line in the queue
echo -e "${PURPLE}[DUMMY PUSH]${ENDCOLOR} to remote" >> ${syncloopfile}
echo "Startsync DUMMY ACTION" >> ${tempqueuefile}
echo -e "${GREEN}DONE${ENDCOLOR}"
loopsyncpath $1
@ -185,10 +205,12 @@ loopsyncpath() {
echo 'Sync already running do nothing...'
exit 0
else
## delete old log file
rm -f ~/syncdir_$hash.log
## delete old rsync log file
rsynclogfile=$(format ${LOGFILERSYNC} hash=$hash)
rm -f ${rsynclogfile}
## starting loop on background and catch pid
nohup syncloop.sh $hash $localpath $remotepath 1>~/syncloop_$hash.nohup 2>&1 &
syncloopfile=$(format ${SYNCLOOPFILE} hash=$hash)
nohup syncloop.sh $hash $localpath $remotepath 1>$syncloopfile 2>&1 &
mypid=$!
when=$(date +%s)
## if line exists i must replace it with new pid

View File

@ -26,11 +26,17 @@ source ~/bin/.synccmd.sh
source ~/bin/.colordef.sh
tmpqueuefile=$(format ${TMPQUEUEFILE} hash=$1)
tmpqueuedeletes=$(format ${TMPQUEUEDELETES} hash=$1)
syncloopfile=$(format ${SYNCLOOPFILE} hash=$1)
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>>$syncloopfile 2>&1
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)
@ -64,9 +70,9 @@ synccycle() {
if [ -f "$tmpqueuedeletes" ]; then
ndeletes=$(wc -l < ${tmpqueuedeletes})
while [ $ndeletes -gt 0 ]; do
echo -n -e "[Masync log - PUSH FILES TO DELETE] to remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}"
echo -e "${PURPLE}[DELETE]${ENDCOLOR} at remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}"
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')"
echo -e " ${PURPLE}DONE${ENDCOLOR}"
echo -e "${PURPLE}[DONE]${ENDCOLOR}"
# remove the first ndeletes lines
flock $lock_deletes
tmpfile=$(mktemp)
@ -83,9 +89,8 @@ synccycle() {
nqueue=$(wc -l < ${tmpqueuefile})
while [ $nqueue -gt 0 ]; do
#echo "${tmpqueuefile} not empty consume ${nqueue} updates in queue"
echo -n -e "[Masync log - PUSH FILES] to remote: ${GREEN}$(head -n ${nqueue} ${tmpqueuefile} | tr '\n', ' ')${ENDCOLOR}"
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH
echo -e " ${PURPLE}DONE${ENDCOLOR}"
echo -e "${PURPLE}[PUSH DONE]${ENDCOLOR}"
# remove the first nqueue lines from queue
flock $lock_queue
tmpfile=$(mktemp)
@ -102,9 +107,9 @@ synccycle() {
elapsed="$(bc <<<"$now-$lastsynctime")"
## if elapsed is greater than maxpulllimit sync from remote with delete option
if [ 1 -eq $(echo "$elapsed>$maxpulllimit" | bc) ]; then
echo -n "[Masync log - PULL LIMIT reached] $maxpulllimit seconds reached: sync from remote with --delete option..."
echo -e "${PURPLE}[PULL LIMIT]${ENDCOLOR} $maxpulllimit seconds reached: sync from remote with --delete option..."
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
echo -e " ${PURPLE}DONE${ENDCOLOR}"
echo -e "${PURPLE}[PULL LIMIT DONE]${ENDCOLOR}"
lastsynctime="$(date -u +%s.%N)"
## increment the pull iteration or reset it
if [ ${pulliteration} -eq ${maxpulliteration} ]; then
@ -124,7 +129,7 @@ synccycle &
# exclude swp,swpx and 4913 files created by vim
inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\.swx|4913|.txt~" $LOCALPATH | while read dir action file; do
if [ $action = 'DELETE' ] || [ $action = 'DELETE,ISDIR' ] || [ $action = 'MOVED_FROM' ] || [ $action = 'MOVED_FROM,ISDIR' ]; then
echo -e "[Masync log - ENQUEUE delete] $dir$file"
echo -e "syncloop - ENQUEUE delete $dir$file"
flock $lock_deletes
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${tmpqueuedeletes} 1>/dev/null
flock -u $lock_deletes
@ -133,7 +138,7 @@ inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\.
## add to queue only if not already in it
if [ $(grep -E ${file}$ ${tmpqueuefile} | wc -l) -lt 1 ]; then
#echo "Add notify to queue: file '$file' in directory '$dir' for '$action'" | tee -a ${tmpqueuefile}
echo -e "[Masync log - ENQUEUE file] $dir$file"
echo -e "syncloop - ENQUEUE file $dir$file"
# AVOID TO USE TEE: SIMPLY append
#echo ${dir}${file} | tee -a ${tmpqueuefile}
echo ${dir}${file} >> ${tmpqueuefile}