This commit is contained in:
2026-03-25 00:44:41 +00:00
parent 795c6f5b3e
commit 8506ef4a6a
2 changed files with 97 additions and 70 deletions
+48 -15
View File
@@ -9,11 +9,17 @@ source "$SCRIPT_DIR/../common.sh"
# ----------------------------- Input -----------------------------
DIRECTORY="$1"
[[ -z "$DIRECTORY" ]] && { log "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"; exit 1; }
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1
fi
shift
parse_args "$@"
# ----------------------------- Host & IP -----------------------------
detect_hosts
resolve_remote_ip
# ----------------------------- Profile -----------------------------
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
@@ -26,37 +32,64 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
log "Detected profile: $PROFILE_NAME"
log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}, DRY_RUN=$DRY_RUN"
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Detected profile: $PROFILE_NAME"
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}, DRY_RUN=$DRY_RUN"
# ----------------------------- Main Sync -----------------------------
# ----------------------------- Rsync Wait -----------------------------
wait_for_rsync() {
for attempt in $(seq 1 "$RETRY_COUNT"); do
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Waiting... ($RSYNC_COUNT active rsync processes)"
sleep "$SLEEP"
else
return 0
fi
done
echo "Too many rsync processes, exiting."
exit 1
}
# ----------------------------- Main -----------------------------
main() {
log "[$LOCAL_SERVER_NAME] Starting sync: $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Starting sync: $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
[[ $(check_server_online) ]] || { log "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
# Ping check
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then break; fi
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
sleep "$SLEEP"
done
ping -c 1 "$REMOTE_SERVER" &>/dev/null || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
wait_for_rsync
stop_containers
# Build rsync command
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
for attempt in $(seq 1 "$RETRY_COUNT"); do
log "Starting rsync attempt $attempt/$RETRY_COUNT..."
echo "Starting rsync attempt $attempt/$RETRY_COUNT..."
if rsync "${RSYNC_OPTS[@]}" -e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
log "Rsync completed successfully."
echo "Rsync completed successfully."
break
else
log "Rsync attempt $attempt failed."
[[ "$attempt" -lt "$RETRY_COUNT" ]] && { log "Retrying in $SLEEP seconds..."; sleep "$SLEEP"; } || { log "All rsync attempts failed."; start_containers; exit 1; }
echo "Rsync attempt $attempt failed."
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
echo "Retrying in $SLEEP seconds..."
sleep "$SLEEP"
else
echo "All $RETRY_COUNT rsync attempts failed."
start_containers
exit 1
fi
fi
done
start_containers
log "[$LOCAL_SERVER_NAME] Sync complete."
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Sync complete."
}
main