#!/bin/bash set -e # ---------------------------------------------------------------------------------------------- # --------------------------------- Rsync Core Script ------------------------------------------ # ---------------------------------------------------------------------------------------------- # ---------------- User Variables, Please adjust in Master.conf as needed ---------------------- # ---------------------------------------------------------------------------------------------- # # User variables can be adjusted in Master.conf # # Scripts now just contain runtime logic # # This new setup allows for profiles and arguments to use the core script # # Use arguments in unRAID User Plugin for directory # Example = /mnt/user/appdata/unraid_scripts/Rsync/rsync.sh --dry-run # ---------------------------------------------------------------------------------------------- # -------------- End Of User Variables, Please adjust in Master.conf as needed ----------------- # ---------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" # ---------------------------------------------------------------------------------- # INPUT PARSING (FIXED - positional args + flags separation) # ---------------------------------------------------------------------------------- DIRECTORY="" RAW_ARGS=() for ARG in "$@"; do case "$ARG" in --*|*=*) RAW_ARGS+=("$ARG") ;; *) if [[ -z "$DIRECTORY" ]]; then DIRECTORY="$ARG" else RAW_ARGS+=("$ARG") fi ;; esac done parse_args "${RAW_ARGS[@]}" if [[ -z "$DIRECTORY" ]]; then error "Missing directory" echo "Usage: $0 [--dry-run] [--status] [VAR=value]" exit 1 fi # ---------------------------------------------------------------------------------- # HOST SETUP # ---------------------------------------------------------------------------------- detect_hosts resolve_remote_ip # ---------------------------------------------------------------------------------- # PROFILE SETUP # ---------------------------------------------------------------------------------- PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT} CRITICAL_CONTAINER_NAMES=(${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}) DELAYED_CONTAINERS=(${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]}) CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY} RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT} SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP} EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}}) # ---------------------------------------------------------------------------------- # STATUS MODE # ---------------------------------------------------------------------------------- if [[ "${SHOW_STATUS:-false}" == true ]]; then show_status exit 0 fi # ---------------------------------------------------------------------------------- # HEADER # ---------------------------------------------------------------------------------- echo echo "$ICON_RUN Sync Starting" echo "Source: $DIRECTORY" echo "Remote: $REMOTE_SERVER:$DIRECTORY" echo "Profile: $PROFILE_NAME" echo "Dry Run: $DRY_RUN" echo "--------------------------------------------" # ---------------------------------------------------------------------------------- # REMOTE CHECK (FIXED LOGIC) # ---------------------------------------------------------------------------------- echo "$ICON_GEAR Checking remote..." REMOTE_OK=false for i in $(seq 1 "$RETRY_COUNT"); do if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then success "Remote reachable" REMOTE_OK=true break fi warn "Retry $i/$RETRY_COUNT" sleep "$SLEEP" done if [[ "$REMOTE_OK" != true ]]; then error "Remote unreachable" exit 1 fi # ---------------------------------------------------------------------------------- # RSYNC SLOT LIMITER # ---------------------------------------------------------------------------------- wait_for_rsync() { for i in $(seq 1 "$RETRY_COUNT"); do COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true) if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then warn "Waiting rsync slot ($COUNT)" sleep "$SLEEP" else return 0 fi done error "Too many rsync processes" exit 1 } wait_for_rsync # ---------------------------------------------------------------------------------- # STOP CONTAINERS (FIXED - NO DUPLICATE LOGGING) # ---------------------------------------------------------------------------------- stop_containers # ---------------------------------------------------------------------------------- # BUILD RSYNC OPTIONS # ---------------------------------------------------------------------------------- get_rsync_opts for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex") done if [[ "$DRY_RUN" == true ]]; then RSYNC_OPTS+=("--dry-run") fi # ---------------------------------------------------------------------------------- # RSYNC COMMAND (SAFE IMPROVEMENT - NO BEHAVIOR CHANGE) # ---------------------------------------------------------------------------------- echo "$ICON_RUN Running rsync..." RSYNC_CMD=( rsync "${RSYNC_OPTS[@]}" -e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" "$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY" ) log "RSYNC CMD: ${RSYNC_CMD[*]}" START=$(date +%s) # ---------------------------------------------------------------------------------- # EXECUTION (RETRY SAFE) # ---------------------------------------------------------------------------------- for i in $(seq 1 "$RETRY_COUNT"); do echo "Attempt $i/$RETRY_COUNT" if "${RSYNC_CMD[@]}"; then success "Rsync complete" break else warn "Rsync failed ($i/$RETRY_COUNT)" sleep "$SLEEP" fi done # ---------------------------------------------------------------------------------- # START CONTAINERS # ---------------------------------------------------------------------------------- start_containers # ---------------------------------------------------------------------------------- # SUMMARY # ---------------------------------------------------------------------------------- END=$(date +%s) echo echo "===== SUMMARY =====" echo "Directory: $DIRECTORY" echo "Profile: $PROFILE_NAME" echo "Duration: $((END-START))s" echo "Status: DONE" echo "===================" ✅ WHAT THIS FIX ACTUALLY SOLVES ✔ Removes duplicate: