imput handling fix

This commit is contained in:
2026-03-27 18:12:39 +00:00
parent 5ece52b5e5
commit dddfe7b655
2 changed files with 108 additions and 150 deletions
+73 -91
View File
@@ -23,32 +23,24 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# ----------------------------
# INPUT HANDLING (FIXED)
# ----------------------------
# ----------------------------- ARG PARSING FIX -----------------------------
DIRECTORY=""
RAW_ARGS=()
for ARG in "$@"; do
case "$ARG" in
--*|*=*)
# skip flags and key=value
RAW_ARGS+=("$ARG")
;;
*)
if [[ -z "$DIRECTORY" ]]; then
DIRECTORY="$ARG"
fi
[[ -z "$DIRECTORY" ]] && DIRECTORY="$ARG" || RAW_ARGS+=("$ARG")
;;
esac
done
if [[ -z "$DIRECTORY" ]]; then
error "Missing directory"
echo "Usage: $0 <directory> [--dry-run] [--status] [VAR=value]"
exit 1
fi
parse_args "${RAW_ARGS[@]}"
[[ -z "$DIRECTORY" ]] && error "Missing directory" && exit 1
# ----------------------------- HOST -----------------------------
detect_hosts
@@ -66,8 +58,8 @@ 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" == true ]]; then
# ----------------------------- STATUS -----------------------------
if [[ "${SHOW_STATUS:-false}" == true ]]; then
show_status
exit 0
fi
@@ -75,20 +67,38 @@ fi
# ----------------------------- HEADER -----------------------------
echo
echo "$ICON_RUN Sync Starting"
echo "--------------------------------------------"
echo "Source: $DIRECTORY"
echo "Destination: $REMOTE_SERVER:$DIRECTORY"
echo "Profile: $PROFILE_NAME"
echo "Dry Run: $DRY_RUN"
echo "--------------------------------------------"
echo "Source: $DIRECTORY"
echo "Remote: $REMOTE_SERVER:$DIRECTORY"
echo "Profile: $PROFILE_NAME"
echo "Dry Run: $DRY_RUN"
# ----------------------------- REMOTE CHECK FIX -----------------------------
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 LIMIT -----------------------------
wait_for_rsync() {
for i in $(seq 1 "$RETRY_COUNT"); do
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
if [[ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then
warn "Waiting for rsync slot ($RSYNC_COUNT)"
if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then
warn "Waiting rsync slot ($COUNT)"
sleep "$SLEEP"
else
return 0
@@ -96,75 +106,47 @@ wait_for_rsync() {
done
error "Too many rsync processes"
exit 1
}
# ----------------------------- SUMMARY -----------------------------
print_summary() {
echo
echo "================ SUMMARY ================"
echo "Directory: $DIRECTORY"
echo "Profile: $PROFILE_NAME"
echo "Remote: $REMOTE_SERVER"
echo "Duration: ${DURATION:-unknown}s"
echo "Status: $1"
echo "Dry Run: $DRY_RUN"
echo "========================================"
}
wait_for_rsync
# ----------------------------- MAIN -----------------------------
main() {
# ----------------------------- RUN -----------------------------
log "Stopping containers"
stop_containers
echo "$ICON_GEAR Checking remote..."
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
success "Remote reachable"
break
fi
warn "Retrying ($i/$RETRY_COUNT)"
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
echo "$ICON_RUN Running rsync..."
START=$(date +%s)
for i in $(seq 1 "$RETRY_COUNT"); do
if rsync "${RSYNC_OPTS[@]}" \
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" \
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
success "Rsync complete"
break
else
warn "Rsync failed ($i/$RETRY_COUNT)"
sleep "$SLEEP"
done
fi
done
[[ ! $(ping -c 1 "$REMOTE_SERVER" &>/dev/null) ]] && error "Remote unreachable"
start_containers
wait_for_rsync
echo "$ICON_STOP Stopping containers..."
stop_containers
echo "$ICON_GEAR Building rsync opts..."
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
echo "$ICON_RUN Running rsync..."
START=$(date +%s)
for i in $(seq 1 "$RETRY_COUNT"); do
if rsync "${RSYNC_OPTS[@]}" \
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" \
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
success "Rsync complete"
break
else
warn "Rsync failed"
[[ $i -lt $RETRY_COUNT ]] && sleep "$SLEEP"
fi
done
echo "$ICON_SYNC Restarting containers..."
start_containers
END=$(date +%s)
DURATION=$((END - START))
print_summary "SUCCESS"
}
main
END=$(date +%s)
echo
echo "===== SUMMARY ====="
echo "Directory: $DIRECTORY"
echo "Profile: $PROFILE_NAME"
echo "Duration: $((END-START))s"
echo "Status: DONE"
echo "==================="