Bisync.sh

From Essential
Revision as of 23:24, 14 March 2022 by Tcepo (talk | contribs)
Jump to navigation Jump to search
#!/bin/bash

#bidirectional sync

sourcePath="$1"
targetPath="$2"
targetHost="$3"
sshCommand="$4"

[ -z "${sshCommand}" ] &&sshCommand='ssh -i ~/.ssh/me@infocepo.com.key -p 1901 -oStrictHostKeyChecking=no'

backupDir=rsync-rollback/$(date '+%Y-%m-%d')
rsyncArgs='--numeric-ids -H --modify-window=1 --ignore-errors --block-size=128.00K --inplace --no-whole-file -z --compress-level=9 -aAXxv --backup-dir='${backupDir}' --exclude=rsync-rollback --exclude=.rsync_*'

#if never sync initiate
(cd "${sourcePath}"/ ;ls -1d .rsync_* |sed -rn 's#^\.rsync_([0-9a-f]{6})$#\1#p' >/tmp/rsync$$_ID_L
${sshCommand} "${targetHost}" 'cd '${targetPath}'/ ;ls -1d .rsync_*' |sed -rn 's#^\.rsync_([0-9a-f]{6})$#\1#p' >/tmp/rsync$$_ID_R )
rsyncID="$(cat /tmp/rsync$$_ID_R |grep -w -f /tmp/rsync$$_ID_L |head -1 )"

[ -z "${rsyncID}" ] &&rsyncID="$(openssl rand -hex 3)"

#List diff
rsync --numeric-ids --exclude=/rsync-rollback -n -aAXxv --delete --force --exclude=/.rsync_"${rsyncID}" "${sourcePath}"/ "${sourcePath}"/.rsync_"${rsyncID}" \
 |tail -n +2 |head -n -2 |grep . >~/rsync_"${rsyncID}"_L2R
${sshCommand} "${targetHost}" '\
 rsync --exclude=/rsync-rollback -n -aAXxv --delete --exclude=/.rsync_'${rsyncID}' '${targetPath}'/ '${targetPath}'/.rsync_'${rsyncID}'' \
 |tail -n +2 |head -n -2 |grep . >~/rsync_"${rsyncID}"_R2L

#New files
cat ~/rsync_"${rsyncID}"_R2L |grep -v '^deleting ' >~/rsync_"${rsyncID}"_R2L_new
cat ~/rsync_"${rsyncID}"_L2R |grep -v '^deleting ' >~/rsync_"${rsyncID}"_L2R_new

#conflict : remove new files from delete list
(cat ~/rsync_"${rsyncID}"_R2L_new ;echo "==" ;cat ~/rsync_"${rsyncID}"_L2R |sed -rn 's#^deleting (.*)$#\1#p' )|awk '!seen[$0]++' |sed '0,/^==$/d' >~/rsync_"${rsyncID}"_R_del
(cat ~/rsync_"${rsyncID}"_L2R_new ;echo "==" ;cat ~/rsync_"${rsyncID}"_R2L |sed -rn 's#^deleting (.*)$#\1#p' )|awk '!seen[$0]++' |sed '0,/^==$/d' >~/rsync_"${rsyncID}"_L_del

#Send list to remote
#   ~/rsync_"${rsyncID}"_R_new "${targetHost}":~/rsync_"${rsyncID}"_R_new
rsync -u ${rsyncArgs} --rsh="${sshCommand}" \
 ~/rsync_"${rsyncID}"_R_del "${targetHost}":~/rsync_"${rsyncID}"_R_del

#Update only
rsync --existing -u ${rsyncArgs} --rsh="${sshCommand}" \
 "${targetHost}":"${targetPath}"/ "${sourcePath}"/

rsync --existing -u ${rsyncArgs} --rsh="${sshCommand}" \
 "${sourcePath}"/ "${targetHost}":"${targetPath}"/

#Delete

${sshCommand} "${targetHost}" '\
  mkdir -p '${targetPath}/${backupDir}'/
  rsync --remove-source-files --files-from=~/rsync_'${rsyncID}'_R_del \
   -lptgoDAXxv \
   --link-dest='${targetPath}'/ \
   '${targetPath}'/ '${targetPath}/${backupDir}'/
  cat ~/rsync_'${rsyncID}'_R_del |while read lineMy ;do
    [ -e '${targetPath}'/"${lineMy}" ] &&find '${targetPath}'/"${lineMy}" -maxdepth 1 -type d -empty -delete 2>/dev/null
  done
  [ -e '${targetPath}'/"${lineMy}" ] &&find '${sourcePath}/${backupDir}' -maxdepth 1 -type d -empty -delete 2>/dev/null '

mkdir -p "${sourcePath}"/"${backupDir}"
rsync --remove-source-files --files-from=~/rsync_"${rsyncID}"_L_del \
 -lptgoDAXxv \
 --link-dest="${sourcePath}"/ \
 "${sourcePath}"/ "${sourcePath}"/"${backupDir}"/
cat ~/rsync_"${rsyncID}"_L_del |while read lineMy ;do
  [ -e '${targetPath}'/"${lineMy}" ] &&find "${sourcePath}"/"${lineMy}" -maxdepth 1 -type d -empty -delete 2>/dev/null
done
[ -e '${targetPath}'/"${lineMy}" ] &&find "${sourcePath}"/"${backupDir}" -maxdepth 1 -type d -empty -delete 2>/dev/null

#Rename
#--exclude=/.rsync_* --ignore-existing
rsync ${rsyncArgs} --rsh="${sshCommand}" \
 --no-inc-recursive "${targetHost}":"${targetPath}"/.rsync_"${rsyncID}" \
 "${targetHost}":"${targetPath}"/ "${sourcePath}"/

rsync ${rsyncArgs} --rsh="${sshCommand}" \
 --no-inc-recursive "${sourcePath}"/.rsync_"${rsyncID}" \
 "${sourcePath}"/ "${targetHost}":"${targetPath}"/

#create snap
rsync --exclude=rsync-rollback -aAXx --delete --link-dest="${sourcePath}"/ --exclude=.rsync_* "${sourcePath}"/ "${sourcePath}"/.rsync_"${rsyncID}"
${sshCommand} "${targetHost}" '\
 rsync --exclude=rsync-rollback -aAXx --delete --link-dest='${targetPath}'/ --exclude=.rsync_* '${targetPath}'/ '${targetPath}'/.rsync_'${rsyncID}''