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
1 changed files with 199 additions and 188 deletions

75
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"
@ -126,12 +144,10 @@ 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}"
@ -149,7 +165,7 @@ function setCron () {
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
@ -200,14 +216,13 @@ function getSyncDBs () {
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,7 +305,7 @@ 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
@ -325,15 +340,14 @@ function getSyncFolders () {
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,7 +359,7 @@ 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
@ -385,8 +399,7 @@ function getExcluded () {
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/*): "
@ -406,8 +419,7 @@ 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]}"
@ -427,7 +439,7 @@ function syncDB (){
# 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 :)
@ -436,8 +448,7 @@ function syncDB (){
### 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]}"