1
0
mirror of https://github.com/namibia/demo-sync.git synced 2024-11-21 19:15:10 +00:00

Formatted the code layout.

This commit is contained in:
Llewellyn van der Merwe 2020-03-07 18:59:48 +02:00
parent 7a5bd86dea
commit 228020f584
No known key found for this signature in database
GPG Key ID: 8A8F406BA7238C4E

387
sync.sh
View File

@ -13,12 +13,30 @@
############################################################## ##############################################################
############## CHECK ########## ############## CHECK ##########
############################################################## ##############################################################
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 || {
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; } echo >&2 "We require rsync for this script to run, but it's not installed. Aborting."
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; } 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 crontab >/dev/null 2>&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; } 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"
@ -39,7 +57,7 @@ REPOURL="https://raw.githubusercontent.com/${OWNER}/${REPONAME}/master/"
############## DEFAULTS ########## ############## DEFAULTS ##########
############## ########## ############## ##########
############################################################## ##############################################################
Datetimenow=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)" ) Datetimenow=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)")
SCRIPTURL="${REPOURL}$ACTION.sh" SCRIPTURL="${REPOURL}$ACTION.sh"
ACTIVEUSER=$(whoami) ACTIVEUSER=$(whoami)
HOMEPATH=~/ HOMEPATH=~/
@ -56,7 +74,7 @@ CRONPATH="${BASEPATH}.cron"
############## MAIN ########## ############## MAIN ##########
############## ########## ############## ##########
############################################################## ##############################################################
function main () { function main() {
## set time for this run ## set time for this run
echoTweak "$ACTION on $Datetimenow" echoTweak "$ACTION on $Datetimenow"
echo "started" echo "started"
@ -73,31 +91,31 @@ function main () {
} }
### MAIN SETUP ### ### MAIN SETUP ###
function runSetup () { function runSetup() {
# check if already set # check if already set
if [ ! -f "$2" ]; then if [ ! -f "$2" ]; then
# if setup database # if setup database
if [ "$1" -eq "1" ]; then if [ "$1" -eq "1" ]; then
getSyncDBs "$2" getSyncDBs "$2"
# if setup folders # if setup folders
elif [ "$1" -eq "2" ]; then elif [ "$1" -eq "2" ]; then
getSyncFolders "$2" getSyncFolders "$2"
fi fi
fi fi
} }
### MAIN SYNC ### ### MAIN SYNC ###
function runSync () { function runSync() {
# check if already set # check if already set
if [ -f "$2" ]; then if [ -f "$2" ]; then
# if setup database # if setup database
if [ "$1" -eq "1" ]; then if [ "$1" -eq "1" ]; then
syncDBs "$2" syncDBs "$2"
# if setup folders # if setup folders
elif [ "$1" -eq "2" ]; then elif [ "$1" -eq "2" ]; then
syncFolders "$2" syncFolders "$2"
fi fi
fi fi
} }
############################################################## ##############################################################
@ -107,107 +125,104 @@ function runSync () {
############################################################## ##############################################################
# little repeater # little repeater
function repeat () { function repeat() {
head -c $1 < /dev/zero | tr '\0' $2 head -c $1 </dev/zero | tr '\0' $2
} }
# simple basic random # simple basic random
function getRandom () { function getRandom() {
echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=5 count=1 status=none) echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' </dev/urandom | dd bs=5 count=1 status=none)
} }
# md5 strings # md5 strings
function setMD5() { function setMD5() {
echo -n $1 | md5sum | awk '{print $1}' echo -n $1 | md5sum | awk '{print $1}'
} }
# little echo tweak # little echo tweak
function echoTweak () { function echoTweak() {
echoMessage="$1" echoMessage="$1"
mainlen="$2" mainlen="$2"
characters="$3" characters="$3"
if [ $# -lt 2 ] if [ $# -lt 2 ]; then
then
mainlen=60 mainlen=60
fi fi
if [ $# -lt 3 ] if [ $# -lt 3 ]; then
then
characters='\056' characters='\056'
fi fi
chrlen="${#echoMessage}" chrlen="${#echoMessage}"
increaseBy=$((mainlen-chrlen)) increaseBy=$((mainlen - chrlen))
tweaked=$(repeat "$increaseBy" "$characters") tweaked=$(repeat "$increaseBy" "$characters")
echo -n "$echoMessage$tweaked" echo -n "$echoMessage$tweaked"
} }
# Set cronjob without removing existing # Set cronjob without removing existing
function setCron () { function setCron() {
if [ ! -f "${CRONPATH}" ]; then if [ ! -f "${CRONPATH}" ]; then
echo "" echo ""
echo -ne "\n Would you like set the cronjob now? [y/N]: " echo -ne "\n Would you like set the cronjob now? [y/N]: "
read -r answer read -r answer
if [[ $answer == "y" ]]; then if [[ $answer == "y" ]]; then
# check if user crontab is set # check if user crontab is set
currentCron=$(crontab -u $CLIENTUSER -l 2>/dev/null) currentCron=$(crontab -u $CLIENTUSER -l 2>/dev/null)
if [[ -z "${currentCron// }" ]]; then if [[ -z "${currentCron// /}" ]]; then
currentCron="# SYNC WEBSITES crontab settings" currentCron="# SYNC WEBSITES crontab settings"
echo "$currentCron" > "${CRONPATH}" echo "$currentCron" >"${CRONPATH}"
else else
echo "$currentCron" > "${CRONPATH}" echo "$currentCron" >"${CRONPATH}"
fi fi
# check if the MAILTO is already set # check if the MAILTO is already set
if [[ $currentCron != *"MAILTO"* ]]; then if [[ $currentCron != *"MAILTO"* ]]; then
echo "MAILTO=\"\"" >> "${CRONPATH}" echo "MAILTO=\"\"" >>"${CRONPATH}"
echo "" >> "${CRONPATH}" echo "" >>"${CRONPATH}"
fi fi
# get the Source Database IP/Domain # get the Source Database IP/Domain
echo -e "\n ################################################################################################" echo -e "\n ################################################################################################"
echo -ne " ## Add the CRON TIMER here so you sync will run every day at 4am.\n" echo -ne " ## Add the CRON TIMER here so you sync will run every day at 4am.\n"
echo -ne " ## That will look like our example below, see https://crontab.guru/#0_4_*_*_* for more details.\n" echo -ne " ## That will look like our example below, see https://crontab.guru/#0_4_*_*_* for more details.\n"
echo -e " ################################################################################################" echo -e " ################################################################################################"
read -e -p " ## Example (0 4 * * *): " -i "0 4 * * *" INPUT_CRON_CICLE read -e -p " ## Example (0 4 * * *): " -i "0 4 * * *" INPUT_CRON_CICLE
# check if the @reboot curl -s $SCRIPTURL | sudo bash is already set # check if the @reboot curl -s $SCRIPTURL | sudo bash is already set
if [[ $currentCron != *"${INPUT_CRON_CICLE} curl -s $SCRIPTURL | bash"* ]]; then if [[ $currentCron != *"${INPUT_CRON_CICLE} curl -s $SCRIPTURL | bash"* ]]; then
echo "${INPUT_CRON_CICLE} curl -s $SCRIPTURL | bash" >> "${CRONPATH}" echo "${INPUT_CRON_CICLE} curl -s $SCRIPTURL | bash" >>"${CRONPATH}"
fi fi
# set the user cron # set the user cron
crontab -u $ACTIVEUSER "${CRONPATH}" crontab -u $ACTIVEUSER "${CRONPATH}"
# close the block # close the block
echo -e "\n ################################################################################################" echo -e "\n ################################################################################################"
else else
# to avoid asking again # to avoid asking again
echo "See ${CRONPATH} for more details!" echo "See ${CRONPATH} for more details!"
echo '# Do not remove this file!' > "${CRONPATH}" echo '# Do not remove this file!' >"${CRONPATH}"
echo '# Please set your cronjob manually, with the following details' >> "${CRONPATH}" echo '# Please set your cronjob manually, with the following details' >>"${CRONPATH}"
echo "# 0 4 * * * curl -s $SCRIPTURL | bash" >> "${CRONPATH}" echo "# 0 4 * * * curl -s $SCRIPTURL | bash" >>"${CRONPATH}"
fi fi
fi fi
} }
### setup sync databases file ### ### setup sync databases file ###
function getSyncDBs () { function getSyncDBs() {
# start building the database details # start building the database details
echo "# SOURCE_DBSERVER SOURCE_DATABASE SOURCE_USER SOURCE_PASS TARGET_DBSERVER TARGET_DATABASE TARGET_USER TARGET_PASS" > "$1" echo "# SOURCE_DBSERVER SOURCE_DATABASE SOURCE_USER SOURCE_PASS TARGET_DBSERVER TARGET_DATABASE TARGET_USER TARGET_PASS" >"$1"
# default it no to run setup # default it no to run setup
GETTING=0 GETTING=0
echo "" echo ""
echo -ne "\n Would you like to add a set of sync databases? [y/N]: " echo -ne "\n Would you like to add a set of sync databases? [y/N]: "
read -r answer read -r answer
if [[ $answer == "y" ]]; then if [[ $answer == "y" ]]; then
# start Database tutorial # start Database tutorial
echo -ne "\n RUNNING DATABASE SETUP\n" echo -ne "\n RUNNING DATABASE SETUP\n"
# set checker to get more # set checker to get more
GETTING=1 GETTING=1
fi fi
# start setup # start setup
while [ "$GETTING" -eq "1" ] while [ "$GETTING" -eq "1" ]; do
do
# get the Source Database IP/Domain # get the Source Database IP/Domain
echo -ne "\n Set the Source Database IP/Domain\n" echo -ne "\n Set the Source Database IP/Domain\n"
read -e -p " # Example (127.0.0.1 | localhost): " -i "127.0.0.1" INPUT_SOURCE_DBSERVER read -e -p " # Example (127.0.0.1 | localhost): " -i "127.0.0.1" INPUT_SOURCE_DBSERVER
# check that we have a string # check that we have a string
if [ ! ${#INPUT_SOURCE_DBSERVER} -ge 2 ]; then if [ ! ${#INPUT_SOURCE_DBSERVER} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A SOURCE DATABASE IP/DOMAIN!\n\n" ; echo -ne "\n YOU MUST ADD A SOURCE DATABASE IP/DOMAIN!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -219,7 +234,7 @@ function getSyncDBs () {
read -r INPUT_SOURCE_DATABASE read -r INPUT_SOURCE_DATABASE
# check that we have a string # check that we have a string
if [ ! ${#INPUT_SOURCE_DATABASE} -ge 2 ]; then if [ ! ${#INPUT_SOURCE_DATABASE} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A SOURCE DATABASE NAME!\n\n" ; echo -ne "\n YOU MUST ADD A SOURCE DATABASE NAME!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -231,7 +246,7 @@ function getSyncDBs () {
read -r INPUT_SOURCE_USER read -r INPUT_SOURCE_USER
# check that we have a string # check that we have a string
if [ ! ${#INPUT_SOURCE_USER} -ge 2 ]; then if [ ! ${#INPUT_SOURCE_USER} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A SOURCE DATABASE USER NAME!\n\n" ; echo -ne "\n YOU MUST ADD A SOURCE DATABASE USER NAME!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -243,7 +258,7 @@ function getSyncDBs () {
read -s INPUT_SOURCE_PASSWORD read -s INPUT_SOURCE_PASSWORD
# check that we have a string # check that we have a string
if [ ! ${#INPUT_SOURCE_PASSWORD} -ge 2 ]; then if [ ! ${#INPUT_SOURCE_PASSWORD} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A SOURCE DATABASE USER PASSWORD!\n\n" ; echo -ne "\n YOU MUST ADD A SOURCE DATABASE USER PASSWORD!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -254,7 +269,7 @@ function getSyncDBs () {
read -e -p " # Example (127.0.0.1 | localhost): " -i "127.0.0.1" INPUT_TARGET_DBSERVER read -e -p " # Example (127.0.0.1 | localhost): " -i "127.0.0.1" INPUT_TARGET_DBSERVER
# check that we have a string # check that we have a string
if [ ! ${#INPUT_TARGET_DBSERVER} -ge 2 ]; then if [ ! ${#INPUT_TARGET_DBSERVER} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A TARGET DATABASE IP/DOMAIN!\n\n" ; echo -ne "\n YOU MUST ADD A TARGET DATABASE IP/DOMAIN!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -266,7 +281,7 @@ function getSyncDBs () {
read -r INPUT_TARGET_DATABASE read -r INPUT_TARGET_DATABASE
# check that we have a string # check that we have a string
if [ ! ${#INPUT_TARGET_DATABASE} -ge 2 ]; then if [ ! ${#INPUT_TARGET_DATABASE} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A TARGET DATABASE NAME!\n\n" ; echo -ne "\n YOU MUST ADD A TARGET DATABASE NAME!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -278,7 +293,7 @@ function getSyncDBs () {
read -r INPUT_TARGET_USER read -r INPUT_TARGET_USER
# check that we have a string # check that we have a string
if [ ! ${#INPUT_TARGET_USER} -ge 2 ]; then if [ ! ${#INPUT_TARGET_USER} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A TARGET DATABASE USER NAME!\n\n" ; echo -ne "\n YOU MUST ADD A TARGET DATABASE USER NAME!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -290,14 +305,14 @@ function getSyncDBs () {
read -s INPUT_TARGET_PASSWORD read -s INPUT_TARGET_PASSWORD
# check that we have a string # check that we have a string
if [ ! ${#INPUT_TARGET_PASSWORD} -ge 2 ]; then if [ ! ${#INPUT_TARGET_PASSWORD} -ge 2 ]; then
echo -ne "\n YOU MUST ADD A TARGET DATABASE USER PASSWORD!\n\n" ; echo -ne "\n YOU MUST ADD A TARGET DATABASE USER PASSWORD!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
exit 1 exit 1
fi fi
# add to the file # add to the file
echo "${INPUT_SOURCE_DBSERVER} ${INPUT_SOURCE_DATABASE} ${INPUT_SOURCE_USER} ${INPUT_SOURCE_PASSWORD} ${INPUT_TARGET_DBSERVER} ${INPUT_TARGET_DATABASE} ${INPUT_TARGET_USER} ${INPUT_TARGET_PASSWORD}" >> "$1" echo "${INPUT_SOURCE_DBSERVER} ${INPUT_SOURCE_DATABASE} ${INPUT_SOURCE_USER} ${INPUT_SOURCE_PASSWORD} ${INPUT_TARGET_DBSERVER} ${INPUT_TARGET_DATABASE} ${INPUT_TARGET_USER} ${INPUT_TARGET_PASSWORD}" >>"$1"
# 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 databases? [y/N]: " echo -ne "\n Would you like to add another set of sync databases? [y/N]: "
@ -310,30 +325,29 @@ function getSyncDBs () {
} }
### setup sync folders file ### ### setup sync folders file ###
function getSyncFolders () { function getSyncFolders() {
# start building the website folder details # start building the website folder details
echo "# SOURCE_PATH TARGET_PATH" > "$1" echo "# SOURCE_PATH TARGET_PATH" >"$1"
# default it no to run setup # default it no to run setup
GETTING=0 GETTING=0
echo "" echo ""
echo -ne "\n Would you like to add a set of sync folders? [y/N]: " echo -ne "\n Would you like to add a set of sync folders? [y/N]: "
read -r answer read -r answer
if [[ $answer == "y" ]]; then if [[ $answer == "y" ]]; then
# start Folder tutorial # start Folder tutorial
echo -ne "\n RUNNING FOLDER SETUP\n" echo -ne "\n RUNNING FOLDER SETUP\n"
# set checker to get more # set checker to get more
GETTING=1 GETTING=1
fi fi
# start setup # start setup
while [ "$GETTING" -eq "1" ] while [ "$GETTING" -eq "1" ]; do
do
# get source folder path path # get source folder path path
echo -ne "\n Set the Source Folder Path\n" echo -ne "\n Set the Source Folder Path\n"
echo -ne " # Example (/home/username_a/): " echo -ne " # Example (/home/username_a/): "
read -r INPUT_SOURCE_PATH read -r INPUT_SOURCE_PATH
# check that we have a source path # check that we have a source path
if [ ! -d "$INPUT_SOURCE_PATH" ]; then if [ ! -d "$INPUT_SOURCE_PATH" ]; then
echo -ne "\n YOU MUST ADD A SOURCE PATH!\n\n" ; echo -ne "\n YOU MUST ADD A SOURCE PATH!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
@ -345,20 +359,20 @@ function getSyncFolders () {
read -r INPUT_TARGET_PATH read -r INPUT_TARGET_PATH
# check that we have a target path # check that we have a target path
if [ ! -d "$INPUT_TARGET_PATH" ]; then if [ ! -d "$INPUT_TARGET_PATH" ]; then
echo -ne "\n YOU MUST ADD A TARGET PATH!\n\n" ; echo -ne "\n YOU MUST ADD A TARGET PATH!\n\n"
# remove the file # remove the file
rm "$1" rm "$1"
# start again # start again
exit 1 exit 1
fi fi
# must set realpath # must set realpath
source_folder=$(realpath -s "${INPUT_SOURCE_PATH}") source_folder=$(realpath -s "${INPUT_SOURCE_PATH}")
target_folder=$(realpath -s "${INPUT_TARGET_PATH}") target_folder=$(realpath -s "${INPUT_TARGET_PATH}")
# add to the file # add to the file
echo "${source_folder} ${target_folder}" >> "$1" echo "${source_folder} ${target_folder}" >>"$1"
# get hash # get hash
HASH=$(setMD5 "${source_folder}${target_folder}") HASH=$(setMD5 "${source_folder}${target_folder}")
# check if exclusion is needed # check if exclusion is needed
getExcluded "${EXCLUDEPATH}${HASH}" getExcluded "${EXCLUDEPATH}${HASH}"
# check if another should be added # check if another should be added
echo "" echo ""
@ -372,27 +386,26 @@ function getSyncFolders () {
} }
### setup sync folders file ### ### setup sync folders file ###
function getExcluded () { function getExcluded() {
# default it no to run setup # default it no to run setup
GETTING=0 GETTING=0
echo "" echo ""
echo -ne "\n Would you like to add excluded files/folders? [y/N]: " echo -ne "\n Would you like to add excluded files/folders? [y/N]: "
read -r answer read -r answer
if [[ $answer == "y" ]]; then if [[ $answer == "y" ]]; then
# start exclution # start exclution
echo -ne "\n See for more details https://linuxize.com/post/how-to-exclude-files-and-directories-with-rsync/\n" echo -ne "\n See for more details https://linuxize.com/post/how-to-exclude-files-and-directories-with-rsync/\n"
# set checker to get more # set checker to get more
GETTING=1 GETTING=1
fi fi
# start setup # start setup
while [ "$GETTING" -eq "1" ] while [ "$GETTING" -eq "1" ]; do
do
# get source folder path path # get source folder path path
echo -ne "\n Add file or folder to exclude\n" echo -ne "\n Add file or folder to exclude\n"
echo -ne " # Example (configuration.php or administrator/*): " echo -ne " # Example (configuration.php or administrator/*): "
read -r INPUT_EXCLUDE read -r INPUT_EXCLUDE
# add to file # add to file
echo "${INPUT_EXCLUDE}" >> "$1" echo "${INPUT_EXCLUDE}" >>"$1"
# check if another should be added # check if another should be added
echo "" echo ""
echo -ne "\n Would you like to add another exclusion? [y/N]: " echo -ne "\n Would you like to add another exclusion? [y/N]: "
@ -405,53 +418,51 @@ function getExcluded () {
} }
### sync databases ### ### sync databases ###
function syncDBs (){ function syncDBs() {
while IFS=$'\t' read -r -a databases while IFS=$'\t' read -r -a databases; do
do
[[ "$databases" =~ ^#.*$ ]] && continue [[ "$databases" =~ ^#.*$ ]] && continue
# SOURCE_DBSERVER SOURCE_DATABASE SOURCE_USER SOURCE_PASS TARGET_DBSERVER TARGET_DATABASE TARGET_USER TARGET_PASS # SOURCE_DBSERVER SOURCE_DATABASE SOURCE_USER SOURCE_PASS TARGET_DBSERVER TARGET_DATABASE TARGET_USER TARGET_PASS
syncDB "${databases[0]}" "${databases[1]}" "${databases[2]}" "${databases[3]}" "${databases[4]}" "${databases[5]}" "${databases[6]}" "${databases[7]}" syncDB "${databases[0]}" "${databases[1]}" "${databases[2]}" "${databases[3]}" "${databases[4]}" "${databases[5]}" "${databases[6]}" "${databases[7]}"
done < $1 done <$1
} }
### sync database ### ### sync database ###
function syncDB (){ function syncDB() {
# give the user log data # give the user log data
echoTweak "Syncing databases of [$2] with [$6]..." echoTweak "Syncing databases of [$2] with [$6]..."
# local source_server="$1" # local source_server="$1"
# local source_db="$2" # local source_db="$2"
# local source_user="$3" # local source_user="$3"
# local source_pass="$4" # local source_pass="$4"
# local target_server="$5" # local target_server="$5"
# local target_db="$6" # local target_db="$6"
# local target_user="$7" # local target_user="$7"
# local target_pass="$8" # local target_pass="$8"
# move tables from one database to the other # move tables from one database to the other
mysqldump --opt -q --host="$1" --user="$3" --password="$4" "$2" | \ mysqldump --opt -q --host="$1" --user="$3" --password="$4" "$2" |
mysql --host="$5" --user="$7" --password="$8" -C "$6" mysql --host="$5" --user="$7" --password="$8" -C "$6"
# we may want to look at passing the password more securly (TODO) # we may want to look at passing the password more securly (TODO)
# done :) # done :)
echo "done" echo "done"
} }
### sync folders ### ### sync folders ###
function syncFolders (){ function syncFolders() {
while IFS=$'\t' read -r -a folders while IFS=$'\t' read -r -a folders; do
do
[[ "$folders" =~ ^#.*$ ]] && continue [[ "$folders" =~ ^#.*$ ]] && continue
# SOURCE_PATH TARGET_PATH # SOURCE_PATH TARGET_PATH
syncFolder "${folders[0]}" "${folders[1]}" syncFolder "${folders[0]}" "${folders[1]}"
done < $1 done <$1
} }
### sync folder ### ### sync folder ###
function syncFolder (){ function syncFolder() {
local source_folder="$1" local source_folder="$1"
local target_folder="$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 # get hash
HASH=$(setMD5 "${source_folder}${target_folder}") HASH=$(setMD5 "${source_folder}${target_folder}")
@ -461,39 +472,39 @@ function syncFolder (){
# local exclude='' # local exclude=''
if [ -f "${EXCLUDEPATH}${HASH}" ]; then if [ -f "${EXCLUDEPATH}${HASH}" ]; then
########################################################################## ##########################################################################
# I tried doing this exclude with rsync but it just does not work (TODO) # I tried doing this exclude with rsync but it just does not work (TODO)
# while IFS= read -r line; do # while IFS= read -r line; do
# exclude+=" --exclude '${line}'" # exclude+=" --exclude '${line}'"
# done < "${EXCLUDEPATH}${HASH}" # done < "${EXCLUDEPATH}${HASH}"
# IF YOU CAN HELP LET ME KNOW # IF YOU CAN HELP LET ME KNOW
######################################################################### #########################################################################
# make tmp dir # make tmp dir
mkdir "$tmpPath" mkdir "$tmpPath"
# move file/folders out to tmp folder # move file/folders out to tmp folder
for line in $(cat ${EXCLUDEPATH}${HASH}); do mv -f "${target_folder}/${line}" "$tmpPath"; done for line in $(cat ${EXCLUDEPATH}${HASH}); do mv -f "${target_folder}/${line}" "$tmpPath"; done
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 "${source_folder}/" "${target_folder}" rsync -qrd --delete "${source_folder}/" "${target_folder}"
########################################################################## ##########################################################################
# I tried doing this exclude with rsync but it just does not work (TODO) # I tried doing this exclude with rsync but it just does not work (TODO)
# if [ ! ${#exclude} -ge 2 ]; then # if [ ! ${#exclude} -ge 2 ]; then
# rsync -qrd --delete "${source_folder}/" "${target_folder}" # rsync -qrd --delete "${source_folder}/" "${target_folder}"
# else # else
# rsync -qrd $exclude --delete "${source_folder}/" "${target_folder}" # rsync -qrd $exclude --delete "${source_folder}/" "${target_folder}"
# fi # fi
# IF YOU CAN HELP LET ME KNOW # IF YOU CAN HELP LET ME KNOW
######################################################################### #########################################################################
# move the files back # move the files back
if [ -f "${EXCLUDEPATH}${HASH}" ]; then if [ -f "${EXCLUDEPATH}${HASH}" ]; then
# move file/folders out # move file/folders out
for line in $(cat ${EXCLUDEPATH}${HASH}); do mv -f "$tmpPath/$line" "${target_folder}"; done for line in $(cat ${EXCLUDEPATH}${HASH}); do mv -f "$tmpPath/$line" "${target_folder}"; done
# remove tmp dir # remove tmp dir
rm -r "$tmpPath" rm -r "$tmpPath"
fi fi
# run chown again (will only work if script run as root) # run chown again (will only work if script run as root)
chown -R "${target_owner}":"${target_owner}" "${target_folder}/"* chown -R "${target_owner}":"${target_owner}" "${target_folder}/"*
@ -506,4 +517,4 @@ function syncFolder (){
############## MAIN ########## ############## MAIN ##########
############## ########## ############## ##########
############################################################## ##############################################################
main main