From f32a8e136961f53a83a9d2c7e21ff03b2cf1cb46 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 5 Oct 2022 10:51:15 +0200 Subject: [PATCH] Add script 'sync_from_nas_systems.sh'. --- sync_from_nas_systems.sh | 312 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100755 sync_from_nas_systems.sh diff --git a/sync_from_nas_systems.sh b/sync_from_nas_systems.sh new file mode 100755 index 0000000..34c7297 --- /dev/null +++ b/sync_from_nas_systems.sh @@ -0,0 +1,312 @@ +#!/usr/bin/env bash + +script_name="$(basename $(realpath $0))" +working_dir="$(dirname $(realpath $0))" + +conf_file="${working_dir}/conf/${script_name%%.*}.conf" + +LOCK_DIR="/tmp/$(basename $0).$$.LOCK" +log_file="${LOCK_DIR}/${script_name%%.*}.log" + +backup_date="$(date +%Y-%m-%d-%H%M)" +declare -a sync_dirs=() + +DEFAULT_sync_dirs="" + + +# ---------- +# Base Function(s) +# ---------- + +clean_up() { + + # Perform program exit housekeeping + rm -rf "$LOCK_DIR" + blank_line + exit $1 +} + + +echononl(){ + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n " $*\\c" 1>&2 + else + echo -e -n " $*" 1>&2 + fi + rm /tmp/shprompt$$ + fi +} + +fatal(){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ] $*" + else + echo -e " [ Fatal ] $*" + fi + echo "" + if $terminal ; then + echo -e " \033[1mScript terminated\033[m.." + else + echo -e " Script terminated.." + fi + echo "" + rm -rf $LOCK_DIR + exit 1 +} + +error (){ + echo "" + if $terminal ; then + echo -e " [ \033[31m\033[1mError\033[m ] $*" + else + echo " [ Error ] $*" + fi + echo "" +} + +warn (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[33m\033[1mWarn\033[m ] $*" + else + echo " [ Warn ] $*" + fi + echo "" + fi +} + +info (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mInfo\033[m ] $*" + else + echo " [ Info ] $*" + fi + echo "" + fi +} + +ok (){ + if $LOGGING || $terminal ; then + echo "" + if $terminal ; then + echo -e " [ \033[32m\033[1mOk\033[m ] $*" + else + echo " [ Ok ] $*" + fi + echo "" + fi +} +echo_done() { + if $terminal ; then + echo -e "\033[75G[ \033[32mdone\033[m ]" + fi +} +echo_ok() { + if $terminal ; then + echo -e "\033[75G[ \033[32mok\033[m ]" + fi +} +echo_warn() { + if $terminal ; then + echo -e "\033[75G[ \033[33mwarn\033[m ]" + fi +} +echo_failed(){ + if $terminal ; then + echo -e "\033[75G[ \033[1;31mfailed\033[m ]" + fi +} +echo_skipped() { + if $terminal ; then + echo -e "\033[75G[ \033[90m\033[1mskipped\033[m ]" + fi +} +echo_wait(){ + if $terminal ; then + echo -en "\033[75G[ \033[5m\033[1m...\033[m ]" + fi +} + +trim() { + local var="$*" + var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters + var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters + echo -n "$var" +} + +blank_line() { + if $terminal ; then + echo "" + fi +} + + +# ---------- +# - Jobhandling +# ---------- + +# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM +# - +trap clean_up SIGHUP SIGINT SIGTERM + +# - Create lock directory '$LOCK_DIR" +# +mkdir "$LOCK_DIR" + + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi + + +# ========== +# - Begin Main Script +# ========== + +# ---------- +# - Headline +# ---------- + +clear +if $terminal ; then + echo "" + echo -e "\033[1m----------\033[m" + echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" + echo -e "\033[1m----------\033[m" +fi + + + +# ---------- +# Read Configurations from $conf_file +# ---------- + +if [[ -f "$conf_file" ]]; then + source "$conf_file" +else + warn "No configuration file '$conf_file' present.\n + Loading default values.." +fi + +if ! (( ${#sync_dirs[@]} )) ; then + sync_dirs=("${DEFAULT_sync_dirs[@]}") +fi + + + +# ---------- +# - Main part of script +# ---------- + +if $terminal ; then + echo "" + echo "" + echo -e " \033[1mGoing to sync from NAS systems ..\033[m" + echo "" +fi + + +blank_line + +sync_dirs="${sync_dirs##*( )}" +sync_dirs="${sync_dirs%%*( )}" + +if [[ -z "${sync_dirs,,}" ]] ; then + fatal "No Directories for syncing given!" +fi + +for _val in $sync_dirs ; do + + blank_line + + IFS=':' read -a _val_arr <<< "${_val}" + + _src_dir="${_val_arr[0]}" + + if [[ -n "${_val_arr[1]}" ]] ; then + _dst_dir="${_val_arr[1]}" + else + warn "No destination folder for \033[1m${_src_dir}\033[m was given." + continue + fi + + echononl "Create destination folder '${_dst_dir}'.." + if [[ -d "${_dst_dir}" ]] ; then + echo_skipped + else + mkdir -p "${_dst_dir}" > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + error "$(cat $log_file)" + + echononl "Unmount folder '${_src_dir}' .." + umount "${_src_dir}" > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + else + echo_done + fi + + continue + else + echo_done + fi + fi + + echononl "Mount folder '${_src_dir}' from NAS system.." + if $(mount | grep -q -E "${_src_dir} " > /dev/null 2>&1) ; then + echo_skipped + else + mount "${_src_dir}" > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + error "$(cat $log_file)" + continue + else + echo_done + fi + fi + + echononl "Going to sync folder '${_src_dir}'.." + echo_wait + + rsync -av \ + --exclude '.TemporaryItems' \ + --exclude '#recycle' \ + --exclude '.apdisk' \ + --exclude '*.DS_Store' \ + --delete \ + "${_src_dir}" "${_dst_dir}" > $log_file 2>&1 + + if [[ $? -gt 0 ]]; then + echo_failed + error "$(cat $log_file)" + else + echo_done + fi + + echononl "Unmount folder '${_src_dir}' .." + umount "${_src_dir}" > $log_file 2>&1 + if [[ $? -gt 0 ]]; then + echo_failed + else + echo_done + fi + +done + +clean_up 0