1
0
mirror of https://github.com/namibia/demo-sync.git synced 2024-11-25 04:37:34 +00:00

Changed to md5 for unique exclusion.

This commit is contained in:
Llewellyn van der Merwe 2020-03-06 21:55:41 +02:00
parent 8f604d53ac
commit 802605a318
No known key found for this signature in database
GPG Key ID: 8A8F406BA7238C4E

31
sync.sh
View File

@ -15,6 +15,10 @@
############################################################## ##############################################################
command -v rsync >/dev/null 2>&1 || { echo >&2 "We require rsync for this script to run, but it's not installed. Aborting."; exit 1; } command -v rsync >/dev/null 2>&1 || { echo >&2 "We require rsync for this script to run, but it's not installed. Aborting."; exit 1; }
command -v crontab >/dev/null 2>&1 || { echo >&2 "We require crontab for this script to run, but it's not installed. Aborting."; exit 1; } command -v crontab >/dev/null 2>&1 || { echo >&2 "We require crontab for this script to run, but it's not installed. Aborting."; exit 1; }
command -v md5sum >/dev/null 2>&1 || { echo >&2 "We require md5sum for this script to run, but it's not installed. Aborting."; exit 1; }
command -v awk >/dev/null 2>&1 || { echo >&2 "We require awk for this script to run, but it's not installed. Aborting."; exit 1; }
command -v realpath >/dev/null 2>&1 || { echo >&2 "We require realpath for this script to run, but it's not installed. Aborting."; exit 1; }
command -v stat >/dev/null 2>&1 || { echo >&2 "We require stat for this script to run, but it's not installed. Aborting."; exit 1; }
############################ GLOBAL ########################## ############################ GLOBAL ##########################
ACTION="sync" ACTION="sync"
@ -107,6 +111,11 @@ function repeat () {
head -c $1 < /dev/zero | tr '\0' $2 head -c $1 < /dev/zero | tr '\0' $2
} }
# md5 strings
function setMD5() {
echo -n $1 | md5sum | awk '{print $1}'
}
# little echo tweak # little echo tweak
function echoTweak () { function echoTweak () {
echoMessage="$1" echoMessage="$1"
@ -330,11 +339,15 @@ function getSyncFolders () {
# start again # start again
exit 1 exit 1
fi fi
# must set realpath
source_folder=$(realpath -s "${INPUT_SOURCE_PATH}")
target_folder=$(realpath -s "${INPUT_TARGET_PATH}")
# add to the file # add to the file
echo "${INPUT_SOURCE_PATH} ${INPUT_TARGET_PATH}" >> "$1" echo "${source_folder} ${target_folder}" >> "$1"
# check if there are files or folders to exclude # get hash
SOURCE_OWNER=$(stat -c '%U' "${INPUT_SOURCE_PATH}") HASH=$(setMD5 "${source_folder}${target_folder}")
getExcluded "${EXCLUDEPATH}${SOURCE_OWNER}" # check if exclusion is needed
getExcluded "${EXCLUDEPATH}${HASH}"
# check if another should be added # check if another should be added
echo "" echo ""
echo -ne "\n Would you like to add another set of sync folders? [y/N]: " echo -ne "\n Would you like to add another set of sync folders? [y/N]: "
@ -415,17 +428,19 @@ function syncFolders (){
### sync folder ### ### sync folder ###
function syncFolder (){ function syncFolder (){
local source_folder=$(realpath -s $1) local source_folder="$1"
local target_folder=$(realpath -s $2) local target_folder="$2"
# get the owners # get the owners
local source_owner=$(stat -c '%U' "${source_folder}") local source_owner=$(stat -c '%U' "${source_folder}")
local target_owner=$(stat -c '%U' "${target_folder}") local target_owner=$(stat -c '%U' "${target_folder}")
# give the user log data # give the user log data
echoTweak "Syncing folders of [${source_owner}] with [${target_owner}]..." echoTweak "Syncing folders of [${source_owner}] with [${target_owner}]..."
# get hash
HASH=$(setMD5 "${source_folder}${target_folder}")
# check if we have exclude file # check if we have exclude file
local exclude='' local exclude=''
if [ -f "${EXCLUDEPATH}${source_owner}" ]; then if [ -f "${EXCLUDEPATH}${HASH}" ]; then
exclude="--exclude-from='${EXCLUDEPATH}${source_owner}'" exclude="--include-from='${EXCLUDEPATH}${HASH}'"
fi fi
# we use rsync to do all the sync work (very smart) # we use rsync to do all the sync work (very smart)
rsync -qrd --delete "${exclude}" "${source_folder}/" "${target_folder}" rsync -qrd --delete "${exclude}" "${source_folder}/" "${target_folder}"