diff --git a/Rsync/rsync.sh b/Rsync/rsync.sh index fd857f1..c6f5d84 100644 --- a/Rsync/rsync.sh +++ b/Rsync/rsync.sh @@ -20,113 +20,127 @@ set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -source "$SCRIPT_DIR/../Master.conf" -source "$SCRIPT_DIR/../common.sh" +# shellcheck source=common.sh +source "$SCRIPT_DIR/common.sh" -# ----------------------------- INPUT ----------------------------- +############################################# +# ARG PARSING (UNRAID SAFE) +############################################# -DIRECTORY="" -RAW_ARGS=() +SOURCE="" +DEST="" +ARGS=() -for ARG in "$@"; do - case "$ARG" in - --*|*=*) RAW_ARGS+=("$ARG") ;; - *) - if [[ -z "$DIRECTORY" ]]; then - DIRECTORY="$ARG" - else - RAW_ARGS+=("$ARG") - fi - ;; - esac +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 -parse_args "${RAW_ARGS[@]}" +############################################# +# LOAD CONFIG +############################################# -[[ -z "$DIRECTORY" ]] && error "Missing directory" && exit 1 - -# ----------------------------- CORE ----------------------------- - -detect_hosts -resolve_remote_ip - -PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') +PROFILE_NAME=${PROFILE_NAME:-"default"} +MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS:-2} MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} -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 ----------------------------- - -if [[ "${SHOW_STATUS:-false}" == true ]]; then - show_status - exit 0 -fi - -# ----------------------------- FLOW ----------------------------- +############################################# +# HEADER +############################################# +info "Host: $(hostname) → ${REMOTE_HOST:-unknown}" info "Sync Starting" -info "Source: $DIRECTORY" -info "Remote: $REMOTE_SERVER:$DIRECTORY" +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" + exit 1 +fi +ok "Remote reachable" -REMOTE_OK=false +############################################# +# CONTAINER STOP (SAFE BARRIER) +############################################# -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 +info "Stopping containers..." -[[ "$REMOTE_OK" != true ]] && error "Remote unreachable" && exit 1 +stop_containers || true -# ----------------------------- EXECUTION BARRIER ----------------------------- +# ensure docker settle (prevents race condition) +sleep 3 + +wait_for_containers_to_stop || 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" + exit 1 +} wait_for_rsync -stop_containers -sync -sleep 2 -get_rsync_opts +############################################# +# RSYNC OPTIONS +############################################# -for ex in "${EXCLUDE_DIRS[@]}"; do - RSYNC_OPTS+=(--exclude="$ex") -done +info "Building rsync opts..." +RSYNC_OPTS=(-aHAX --numeric-ids --delete) -[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run") +if [[ "${DRY_RUN:-false}" == "true" ]]; then + RSYNC_OPTS+=("--dry-run") +fi -RSYNC_CMD=( - rsync - "${RSYNC_OPTS[@]}" - -e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" - "$DIRECTORY" - "root@${REMOTE_SERVER}:$DIRECTORY" -) +############################################# +# EXECUTE RSYNC +############################################# -START=$(date +%s) - -for i in $(seq 1 "$RETRY_COUNT"); do - if "${RSYNC_CMD[@]}"; then - success "Rsync complete" - break - else - warn "Rsync failed ($i/$RETRY_COUNT)" - sleep "$SLEEP" - fi -done - -start_containers - -END=$(date +%s) - -info "Directory: $DIRECTORY" -info "Profile: $PROFILE_NAME" -info "Duration: $((END-START))s" -info "Status: DONE" \ No newline at end of file +info "Running rsync..." +rsync "${RSYNC_OPTS[@]}" \ + -e "ssh -o StrictHostKeyChecking=no" \ + "$SOURCE" \ + "root@$REMOTE_TARGET" \ No newline at end of file