From a3d272a126fc5fc1ba48166fd06299b375ee690d Mon Sep 17 00:00:00 2001 From: FailedProxy Date: Fri, 27 Mar 2026 18:46:50 +0000 Subject: [PATCH] test --- Rsync/rsync.sh | 199 ++++++++++++++++++++++--------------------------- 1 file changed, 91 insertions(+), 108 deletions(-) diff --git a/Rsync/rsync.sh b/Rsync/rsync.sh index c6f5d84..7e3ea5c 100644 --- a/Rsync/rsync.sh +++ b/Rsync/rsync.sh @@ -18,129 +18,112 @@ set -e # -------------- End Of User Variables, Please adjust in Master.conf as needed ----------------- # ---------------------------------------------------------------------------------------------- -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=common.sh -source "$SCRIPT_DIR/common.sh" - -############################################# -# ARG PARSING (UNRAID SAFE) -############################################# - -SOURCE="" -DEST="" -ARGS=() - -for arg in "$@"; do - if [[ "$arg" == -* ]]; then - ARGS+=("$arg") - elif [[ -z "$SOURCE" ]]; then - SOURCE="$arg" - elif [[ -z "$DEST" ]]; then - DEST="$arg" - else - warn "Unknown argument: $arg" - fi -done - -############################################# -# LOAD CONFIG -############################################# - -PROFILE_NAME=${PROFILE_NAME:-"default"} - -MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS:-2} -MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} - -############################################# -# HEADER -############################################# - -info "Host: $(hostname) → ${REMOTE_HOST:-unknown}" -info "Sync Starting" -info "Source: $SOURCE" -info "Remote: ${REMOTE_HOST:-$DEST}" -info "Profile: $PROFILE_NAME" - -############################################# -# REMOTE RESOLVE -############################################# - -info "Resolving remote IP..." -REMOTE_IP=$(resolve_remote_ip "$REMOTE_HOST") -info "Remote IP: $REMOTE_IP" - -REMOTE_TARGET="${REMOTE_IP}:${DEST}" - -############################################# -# CHECK REMOTE -############################################# - -info "Checking remote..." -if ! ping -c 1 -W 1 "$REMOTE_IP" >/dev/null 2>&1; then - error "Remote unreachable" +# ----------------------------------------------------------------------------- +# Self-healing config loader (OPTION C) +# ----------------------------------------------------------------------------- +if [[ -f "$BASE_DIR/../common.sh" ]]; then + # normal layout: Rsync/ + shared root files + source "$BASE_DIR/../Master.conf" + source "$BASE_DIR/../common.sh" +elif [[ -f "$BASE_DIR/common.sh" ]]; then + # flat layout: everything inside Rsync/ + source "$BASE_DIR/Master.conf" + source "$BASE_DIR/common.sh" +else + echo "ℹ️ [INFO] Host: $(hostname)" + echo "❌ [ERROR] common.sh and Master.conf not found in expected locations" + echo "ℹ️ [INFO] BASE_DIR = $BASE_DIR" exit 1 fi -ok "Remote reachable" -############################################# -# CONTAINER STOP (SAFE BARRIER) -############################################# +# ----------------------------------------------------------------------------- +# Load shared helpers (if your common.sh provides these) +# ----------------------------------------------------------------------------- +log_info "Sync script initialized" -info "Stopping containers..." +# ----------------------------------------------------------------------------- +# Host resolution (safe fallback) +# ----------------------------------------------------------------------------- +LOCAL_HOST="$(hostname)" +REMOTE_HOST="${REMOTE_HOST:-jayred365}" -stop_containers || true +log_info "Host: ${LOCAL_HOST} → ${REMOTE_HOST}" -# ensure docker settle (prevents race condition) -sleep 3 +# ----------------------------------------------------------------------------- +# Resolve remote IP +# ----------------------------------------------------------------------------- +log_info "Resolving remote IP..." -wait_for_containers_to_stop || true +REMOTE_IP="$(getent hosts "$REMOTE_HOST" | awk '{print $1}' || true)" -############################################# -# RSYNC SLOT LIMITER (FIXED) -############################################# - -wait_for_rsync() { - : "${RETRY_COUNT:=5}" - : "${MAX_RSYNC_PROCS:=2}" - : "${SLEEP:=2}" - - for _ in $(seq 1 "$RETRY_COUNT"); do - - # ONLY COUNT TOP-LEVEL RSYNC COMMANDS - COUNT=$(pgrep -x rsync | wc -l || true) - - if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then - warn "Waiting rsync slot ($COUNT/$MAX_RSYNC_PROCS)" - sleep "$SLEEP" - else - return 0 - fi - done - - error "Too many rsync processes" +if [[ -z "$REMOTE_IP" ]]; then + log_error "Failed to resolve remote host: $REMOTE_HOST" exit 1 -} +fi -wait_for_rsync +log_info "Remote IP: $REMOTE_IP" -############################################# -# RSYNC OPTIONS -############################################# +# ----------------------------------------------------------------------------- +# Config validation +# ----------------------------------------------------------------------------- +PROFILE="${1:-arrs_stack}" -info "Building rsync opts..." -RSYNC_OPTS=(-aHAX --numeric-ids --delete) +SOURCE_PATH="${SOURCE_PATH:-/mnt/user/appdata-Failover/Arrs_Stack}" +REMOTE_PATH="${REMOTE_PATH:-/mnt/user/appdata-Failover/Arrs_Stack}" +REMOTE_USER="${REMOTE_USER:-root}" + +log_info "Sync Starting" +log_info "Source: $SOURCE_PATH" +log_info "Remote: $REMOTE_IP:$REMOTE_PATH" +log_info "Profile: $PROFILE" +log_info "Dry Run: ${DRY_RUN:-false}" + +# ----------------------------------------------------------------------------- +# Remote check +# ----------------------------------------------------------------------------- +log_info "Checking remote..." + +if ! ssh -o BatchMode=yes -o ConnectTimeout=5 "${REMOTE_USER}@${REMOTE_IP}" "echo ok" >/dev/null 2>&1; then + log_error "Remote unreachable" + exit 1 +fi + +log_ok "Remote reachable" + +# ----------------------------------------------------------------------------- +# Stop containers (if defined in common.sh) +# ----------------------------------------------------------------------------- +if declare -f stop_containers >/dev/null 2>&1; then + log_info "Stopping containers..." + stop_containers "$PROFILE" || true +fi + +# ----------------------------------------------------------------------------- +# RSYNC EXECUTION +# ----------------------------------------------------------------------------- +log_info "Running rsync..." + +RSYNC_OPTS="-aHAX --delete --numeric-ids" if [[ "${DRY_RUN:-false}" == "true" ]]; then - RSYNC_OPTS+=("--dry-run") + RSYNC_OPTS="$RSYNC_OPTS -n" fi -############################################# -# EXECUTE RSYNC -############################################# +rsync $RSYNC_OPTS \ + -e "ssh" \ + "$SOURCE_PATH/" \ + "${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH}/" -info "Running rsync..." -rsync "${RSYNC_OPTS[@]}" \ - -e "ssh -o StrictHostKeyChecking=no" \ - "$SOURCE" \ - "root@$REMOTE_TARGET" \ No newline at end of file +log_ok "Rsync complete" + +# ----------------------------------------------------------------------------- +# Restart containers (if defined) +# ----------------------------------------------------------------------------- +if declare -f start_containers >/dev/null 2>&1; then + log_info "Starting containers..." + start_containers "$PROFILE" || true +fi + +log_ok "Sync finished successfully" \ No newline at end of file