seperated logg and echo a lot

This commit is contained in:
2026-03-25 01:20:27 +00:00
parent 7bfacea33f
commit 9a637b7ca8
2 changed files with 146 additions and 44 deletions
+63 -15
View File
@@ -16,7 +16,7 @@ fi
shift
parse_args "$@"
# ----------------------------- Hosts & IP -----------------------------
# ----------------------------- Host & IP -----------------------------
detect_hosts
resolve_remote_ip
@@ -32,16 +32,27 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
echo "Detected profile: $PROFILE_NAME"
log "Profile settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}, DRY_RUN=$DRY_RUN"
# ----------------------------- User Info -----------------------------
echo "============================================================"
echo " Sync Job Starting"
echo "------------------------------------------------------------"
echo " Source: $DIRECTORY"
echo " Destination: $REMOTE_SERVER:$DIRECTORY"
echo " Profile: $PROFILE_NAME"
echo " Dry Run: $DRY_RUN"
echo "============================================================"
log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT"
log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
log "Excludes: ${EXCLUDE_DIRS[*]}"
# ----------------------------- Rsync Wait -----------------------------
wait_for_rsync() {
for attempt in $(seq 1 "$RETRY_COUNT"); do
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
log "Current rsync processes: $RSYNC_COUNT"
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
echo "Waiting for available rsync slot ($RSYNC_COUNT active)..."
echo "Waiting for available rsync slot... ($RSYNC_COUNT running)"
log "RSYNC_COUNT=$RSYNC_COUNT >= MAX=$MAX_RSYNC_PROCS"
sleep "$SLEEP"
else
return 0
@@ -53,37 +64,74 @@ wait_for_rsync() {
# ----------------------------- Main -----------------------------
main() {
echo "Starting sync: $DIRECTORY$REMOTE_SERVER"
echo ""
echo "🔍 Checking remote connectivity..."
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
echo "✅ Remote server reachable"
break
fi
echo "⚠️ Remote down, retrying in $SLEEP seconds..."
sleep "$SLEEP"
done
if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
echo "❌ Remote server unreachable. Exiting."
exit 1
fi
wait_for_rsync
echo ""
echo "🛑 Stopping containers on remote..."
stop_containers
echo ""
echo "📦 Building rsync options..."
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
log "Rsync options: ${RSYNC_OPTS[*]}"
log "Final rsync opts: ${RSYNC_OPTS[*]}"
echo ""
echo "🚀 Starting rsync..."
for attempt in $(seq 1 "$RETRY_COUNT"); do
echo "Rsync attempt $attempt/$RETRY_COUNT..."
if rsync "${RSYNC_OPTS[@]}" -e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \
echo "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
echo "Rsync completed successfully."
echo "✅ Rsync completed successfully"
break
else
echo "Rsync attempt $attempt failed."
echo "Rsync failed"
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
echo "Retrying in $SLEEP seconds..."
sleep "$SLEEP"
else
echo "All $RETRY_COUNT rsync attempts failed."
echo "💥 All retries failed"
start_containers
exit 1
fi
fi
done
echo ""
echo "🔄 Restarting containers..."
start_containers
echo "Sync complete."
log "[$LOCAL_SERVER_NAME] Sync finished for $DIRECTORY$REMOTE_SERVER"
echo ""
echo "🎉 Sync complete!"
}
main