Compare commits
No commits in common. "master" and "3690a6405368242a1711ab7ba94057a8291e6568" have entirely different histories.
master
...
3690a64053
|
@ -15,24 +15,17 @@
|
||||||
#You should have received a copy of the GNU General Public License
|
#You should have received a copy of the GNU General Public License
|
||||||
#along with masync. If not, see <http://www.gnu.org/licenses/>.
|
#along with masync. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
###############################################
|
#
|
||||||
# the file contains all sync managed by masync
|
# the file contains all sync managed by masync
|
||||||
###############################################
|
#
|
||||||
SYNCFILE=~/.syncdir.sync
|
SYNCFILE=~/.syncdir.sync
|
||||||
###############################################
|
#
|
||||||
# the file contains the snapshot. A snapshot is the list of files
|
# the file contains the snapshot of a stopped sync
|
||||||
# locally synchronized when the sync was stopped
|
#
|
||||||
###############################################
|
|
||||||
SNAPSHOTFILE=~/.snapshot_{{hash}}
|
SNAPSHOTFILE=~/.snapshot_{{hash}}
|
||||||
TMPQUEUEFILE=~/.syncdir_{{hash}}.queue
|
TMPQUEUEFILE=~/.syncdir_{{hash}}.queue
|
||||||
TMPQUEUEDELETES=~/.syncdir_{{hash}}.deletes
|
TMPQUEUEDELETES=~/.syncdir_{{hash}}.deletes
|
||||||
###############################################
|
|
||||||
# the file when the loop logs the activity
|
|
||||||
###############################################
|
|
||||||
SYNCLOOPFILE=~/syncloop_{{hash}}.nohup
|
SYNCLOOPFILE=~/syncloop_{{hash}}.nohup
|
||||||
###############################################
|
|
||||||
# raw rsync log (used by .synccmd.sh)
|
|
||||||
###############################################
|
|
||||||
LOGFILERSYNC=~/syncdir_{{hash}}.log
|
LOGFILERSYNC=~/syncdir_{{hash}}.log
|
||||||
|
|
||||||
################################
|
################################
|
||||||
|
|
13
.synccmd.sh
13
.synccmd.sh
|
@ -29,16 +29,17 @@ sync() {
|
||||||
if [ ! -f $logfile ]; then
|
if [ ! -f $logfile ]; then
|
||||||
touch $logfile
|
touch $logfile
|
||||||
fi
|
fi
|
||||||
|
echo $src
|
||||||
|
echo $logfile
|
||||||
|
echo $syncloopfile
|
||||||
# PUSH TO REMOTE - and PULL FROM REMOTE swapping `src` with `dest`
|
# PUSH TO REMOTE - and PULL FROM REMOTE swapping `src` with `dest`
|
||||||
#echo "executing rsync -aPu --log-file=$LOGFILE -e ssh $otheropts $src $dest"
|
#echo "executing rsync -aPu --log-file=$LOGFILE -e ssh $otheropts $src $dest"
|
||||||
# NOTE REMOTEDIR ALREADY EXISTS IN DESTINATION
|
# NOTE REMOTEDIR ALREADY EXISTS IN DESTINATION
|
||||||
# in this form rsync receive only data from source 1>$syncloopfile 2>&1
|
# in this form rsync receive only data from source 1>$syncloopfile 2>&1
|
||||||
rsync -i -aPu --progress --out-format="%i ${GREEN}%n%L${ENDCOLOR} %''b" --log-file=$logfile -e ssh $otheropts $src $dest | \
|
rsync -ni -aPu --progress --out-format="%i ${GREEN}%n%L${ENDCOLOR} %''b" --log-file=$logfile -e ssh $otheropts $src $dest | \
|
||||||
grep -E "(<|>|deleting)" | \
|
grep -e "[<|>]" | \
|
||||||
sed -E "s~<(\w|\W){10}~\\${PURPLE}\[SEND\]\\${ENDCOLOR} to remote ~g" | \
|
sed -E "s~<(\w|\W){10}~Sending to ${dest}~g" | \
|
||||||
sed -E "s~>(\w|\W){10}~\\${PURPLE}\[RECEIVE\]\\${ENDCOLOR} from remote ~g" | \
|
sed -E "s~>(\w|\W){10}~Receiving from ${src}~g" 1>>$syncloopfile 2>&1
|
||||||
sed -E "s~\*deleting(\s)*~\\${PURPLE}\[DELETE\]\\${ENDCOLOR} in local ~g" | \
|
|
||||||
xargs -r -0 printf 1>>$syncloopfile 2>&1
|
|
||||||
# last command execution exit code $?"
|
# last command execution exit code $?"
|
||||||
rsync_result=$?
|
rsync_result=$?
|
||||||
if [[ ${rsync_result} -ne 0 ]]; then
|
if [[ ${rsync_result} -ne 0 ]]; then
|
||||||
|
|
40
masync
40
masync
|
@ -90,31 +90,13 @@ feeddeletes() {
|
||||||
if [[ -e "${path}" ]]; then
|
if [[ -e "${path}" ]]; then
|
||||||
tocheckremotepath=$(echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g")
|
tocheckremotepath=$(echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g")
|
||||||
if ! grep -Fxq "${tocheckremotepath}" ${remotefiles}; then
|
if ! grep -Fxq "${tocheckremotepath}" ${remotefiles}; then
|
||||||
# echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in remote"
|
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] "
|
rm -rf "${path}"
|
||||||
read -u 1 ui
|
|
||||||
if [[ -z "${ui}" ]]; then
|
|
||||||
if [ ${ui} = 'y' ]; then
|
|
||||||
rm -rf "${path}"
|
|
||||||
fi
|
|
||||||
if [ ${ui} = 'N' ]; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# echo -e "${PURPLE}[PURGE]${ENDCOLOR} ${path} in snapshot no longer exists in local, delete in remote"
|
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] "
|
## if controlled file doesn't exist in local delete it on remote
|
||||||
read -u 1 ui
|
echo "${path}" | sed -e "s~${localpath}~${remoterelativepath}~g" | tee -a ${queuedeletes}
|
||||||
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
|
fi
|
||||||
done < ${snapfile}
|
done < ${snapfile}
|
||||||
rm -rf ${remotefiles}
|
rm -rf ${remotefiles}
|
||||||
|
@ -161,16 +143,14 @@ startsyncpath() {
|
||||||
snapshotfile=$(format ${SNAPSHOTFILE} hash=${localpath_hash})
|
snapshotfile=$(format ${SNAPSHOTFILE} hash=${localpath_hash})
|
||||||
#tempqueuefile=~/.syncdir_${localpath_hash}.queue
|
#tempqueuefile=~/.syncdir_${localpath_hash}.queue
|
||||||
tempqueuefile=$(format ${TMPQUEUEFILE} hash=${localpath_hash})
|
tempqueuefile=$(format ${TMPQUEUEFILE} hash=${localpath_hash})
|
||||||
syncloopfile=$(format ${SYNCLOOPFILE} hash=${localpath_hash})
|
|
||||||
|
|
||||||
if [ -e ${snapshotfile} ]; then
|
if [ -e ${snapshotfile} ]; then
|
||||||
echo "Founded snapshot file: ${snapshotfile} checking files to remove..."
|
echo "Founded snapshot file: ${snapshotfile} collect files to delete in origin"
|
||||||
feeddeletes ${localpath_hash}
|
feeddeletes ${localpath_hash}
|
||||||
echo "Remove snapshot file ${snapshotfile}"
|
echo "Remove snapshot file ${snapshotfile}"
|
||||||
rm -f ${snapshotfile}
|
rm -f ${snapshotfile}
|
||||||
fi
|
fi
|
||||||
## ensure local computer sends eventually new files adding a fake line in the queue
|
## 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 "Startsync DUMMY ACTION" >> ${tempqueuefile}
|
||||||
echo -e "${GREEN}DONE${ENDCOLOR}"
|
echo -e "${GREEN}DONE${ENDCOLOR}"
|
||||||
loopsyncpath $1
|
loopsyncpath $1
|
||||||
|
@ -205,12 +185,10 @@ loopsyncpath() {
|
||||||
echo 'Sync already running do nothing...'
|
echo 'Sync already running do nothing...'
|
||||||
exit 0
|
exit 0
|
||||||
else
|
else
|
||||||
## delete old rsync log file
|
## delete old log file
|
||||||
rsynclogfile=$(format ${LOGFILERSYNC} hash=$hash)
|
rm -f ~/syncdir_$hash.log
|
||||||
rm -f ${rsynclogfile}
|
|
||||||
## starting loop on background and catch pid
|
## starting loop on background and catch pid
|
||||||
syncloopfile=$(format ${SYNCLOOPFILE} hash=$hash)
|
nohup syncloop.sh $hash $localpath $remotepath 1>~/syncloop_$hash.nohup 2>&1 &
|
||||||
nohup syncloop.sh $hash $localpath $remotepath 1>$syncloopfile 2>&1 &
|
|
||||||
mypid=$!
|
mypid=$!
|
||||||
when=$(date +%s)
|
when=$(date +%s)
|
||||||
## if line exists i must replace it with new pid
|
## if line exists i must replace it with new pid
|
||||||
|
|
21
syncloop.sh
21
syncloop.sh
|
@ -26,17 +26,11 @@ source ~/bin/.synccmd.sh
|
||||||
source ~/bin/.colordef.sh
|
source ~/bin/.colordef.sh
|
||||||
tmpqueuefile=$(format ${TMPQUEUEFILE} hash=$1)
|
tmpqueuefile=$(format ${TMPQUEUEFILE} hash=$1)
|
||||||
tmpqueuedeletes=$(format ${TMPQUEUEDELETES} 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_HASH=$1
|
||||||
LOCALPATH=$2
|
LOCALPATH=$2
|
||||||
REMOTEPATH=$3
|
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)
|
||||||
|
|
||||||
|
@ -70,9 +64,9 @@ synccycle() {
|
||||||
if [ -f "$tmpqueuedeletes" ]; then
|
if [ -f "$tmpqueuedeletes" ]; then
|
||||||
ndeletes=$(wc -l < ${tmpqueuedeletes})
|
ndeletes=$(wc -l < ${tmpqueuedeletes})
|
||||||
while [ $ndeletes -gt 0 ]; do
|
while [ $ndeletes -gt 0 ]; do
|
||||||
echo -e "${PURPLE}[DELETE]${ENDCOLOR} at remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}"
|
echo -n -e "[Masync log - PUSH FILES TO DELETE] to remote: ${RED}$(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')${ENDCOLOR}"
|
||||||
ssh $REMOTEHOST "rm -rf $(head -n $ndeletes ${tmpqueuedeletes} | tr '\n', ' ')"
|
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
|
# remove the first ndeletes lines
|
||||||
flock $lock_deletes
|
flock $lock_deletes
|
||||||
tmpfile=$(mktemp)
|
tmpfile=$(mktemp)
|
||||||
|
@ -89,8 +83,9 @@ synccycle() {
|
||||||
nqueue=$(wc -l < ${tmpqueuefile})
|
nqueue=$(wc -l < ${tmpqueuefile})
|
||||||
while [ $nqueue -gt 0 ]; do
|
while [ $nqueue -gt 0 ]; do
|
||||||
#echo "${tmpqueuefile} not empty consume ${nqueue} updates in queue"
|
#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
|
sync $LOCALPATH_HASH $LOCALPATH $REMOTEPATH
|
||||||
echo -e "${PURPLE}[PUSH DONE]${ENDCOLOR}"
|
echo -e " ${PURPLE}DONE${ENDCOLOR}"
|
||||||
# remove the first nqueue lines from queue
|
# remove the first nqueue lines from queue
|
||||||
flock $lock_queue
|
flock $lock_queue
|
||||||
tmpfile=$(mktemp)
|
tmpfile=$(mktemp)
|
||||||
|
@ -107,9 +102,9 @@ synccycle() {
|
||||||
elapsed="$(bc <<<"$now-$lastsynctime")"
|
elapsed="$(bc <<<"$now-$lastsynctime")"
|
||||||
## if elapsed is greater than maxpulllimit sync from remote with delete option
|
## 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
|
||||||
echo -e "${PURPLE}[PULL LIMIT]${ENDCOLOR} $maxpulllimit seconds reached: sync from remote with --delete option..."
|
echo -n "[Masync log - PULL LIMIT reached] $maxpulllimit seconds reached: sync from remote with --delete option..."
|
||||||
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
|
sync $LOCALPATH_HASH $REMOTEPATH $LOCALPATH '--delete'
|
||||||
echo -e "${PURPLE}[PULL LIMIT DONE]${ENDCOLOR}"
|
echo -e " ${PURPLE}DONE${ENDCOLOR}"
|
||||||
lastsynctime="$(date -u +%s.%N)"
|
lastsynctime="$(date -u +%s.%N)"
|
||||||
## increment the pull iteration or reset it
|
## increment the pull iteration or reset it
|
||||||
if [ ${pulliteration} -eq ${maxpulliteration} ]; then
|
if [ ${pulliteration} -eq ${maxpulliteration} ]; then
|
||||||
|
@ -129,7 +124,7 @@ synccycle &
|
||||||
# exclude swp,swpx and 4913 files created by vim
|
# 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
|
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
|
if [ $action = 'DELETE' ] || [ $action = 'DELETE,ISDIR' ] || [ $action = 'MOVED_FROM' ] || [ $action = 'MOVED_FROM,ISDIR' ]; then
|
||||||
echo -e "syncloop - ENQUEUE delete $dir$file"
|
echo -e "[Masync log - ENQUEUE delete] $dir$file"
|
||||||
flock $lock_deletes
|
flock $lock_deletes
|
||||||
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${tmpqueuedeletes} 1>/dev/null
|
echo $dir$file | sed -e "s~$LOCALPATH~$REMOTERELATIVEPATH~g" | tee -a ${tmpqueuedeletes} 1>/dev/null
|
||||||
flock -u $lock_deletes
|
flock -u $lock_deletes
|
||||||
|
@ -138,7 +133,7 @@ inotifywait -m -r -e create -e close_write -e move -e delete --exclude "\.swp|\.
|
||||||
## add to queue only if not already in it
|
## add to queue only if not already in it
|
||||||
if [ $(grep -E ${file}$ ${tmpqueuefile} | wc -l) -lt 1 ]; then
|
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 "Add notify to queue: file '$file' in directory '$dir' for '$action'" | tee -a ${tmpqueuefile}
|
||||||
echo -e "syncloop - ENQUEUE file $dir$file"
|
echo -e "[Masync log - ENQUEUE file] $dir$file"
|
||||||
# AVOID TO USE TEE: SIMPLY append
|
# AVOID TO USE TEE: SIMPLY append
|
||||||
#echo ${dir}${file} | tee -a ${tmpqueuefile}
|
#echo ${dir}${file} | tee -a ${tmpqueuefile}
|
||||||
echo ${dir}${file} >> ${tmpqueuefile}
|
echo ${dir}${file} >> ${tmpqueuefile}
|
||||||
|
|
Loading…
Reference in New Issue