This commit is contained in:
2026-03-27 17:50:01 +00:00
parent be849ed684
commit f3abddddc2
2 changed files with 170 additions and 182 deletions
+59 -112
View File
@@ -20,195 +20,142 @@ set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Core System
# Load configs & helpers
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# Input
# ----------------------------- Input -----------------------------
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [--status] [VAR=value ...]"
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1
fi
shift
parse_args "$@"
# Host
# ----------------------------- Host & IP -----------------------------
detect_hosts
resolve_remote
resolve_remote_ip
# Profile
# ----------------------------- Profile -----------------------------
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
log "️ Detected profile: $PROFILE_NAME"
# Safe array normalization
normalize_list() {
local input="$1"
[[ -z "$input" ]] && return
read -r -a __out <<< "$input"
echo "${__out[@]}"
}
CRITICAL_CONTAINER_NAMES=($(normalize_list "${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}"))
DELAYED_CONTAINERS=($(normalize_list "${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]}"))
EXCLUDE_DIRS=($(normalize_list "${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]}"))
# Profile overrides
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}
CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
# Status Mode
# ----------------------------- Status Mode -----------------------------
if [[ "$SHOW_STATUS" == true ]]; then
echo "=================================================="
echo " ️ RSYNC STATUS"
echo "--------------------------------------------------"
echo " 📁 Source: $DIRECTORY"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🚦 BW_LIMIT: $BW_LIMIT"
echo " 🔁 Max Procs: $MAX_RSYNC_PROCS"
echo " 🔁 Retries: $RETRY_COUNT"
echo " 🧪 Dry Run: $DRY_RUN"
echo " 📦 Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
echo " 🚫 Excludes: ${EXCLUDE_DIRS[*]}"
echo "=================================================="
show_status
exit 0
fi
# Header
echo
# ----------------------------- User Info -----------------------------
echo "============================================================"
echo " 🚀 SYNC STARTING"
echo " Sync Job Starting"
echo "------------------------------------------------------------"
echo " 📁 Source: $DIRECTORY"
echo " 🌐 Destination: $REMOTE_SERVER:$DIRECTORY"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🧪 Dry Run: $DRY_RUN"
echo " Source: $DIRECTORY"
echo " Destination: $REMOTE_SERVER:$DIRECTORY"
echo " Profile: $PROFILE_NAME"
echo " Dry Run: $DRY_RUN"
echo "============================================================"
log " MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS BW_LIMIT=$BW_LIMIT"
log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
log "Excludes: ${EXCLUDE_DIRS[*]}"
log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT"
log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}"
log "Excludes: ${EXCLUDE_DIRS[*]}"
# RSYNC SLOT CONTROL
# ----------------------------- 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
warn "🟡 Waiting for rsync slot ($RSYNC_COUNT active)"
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
echo "Waiting for available rsync slot... ($RSYNC_COUNT running)"
log "RSYNC_COUNT=$RSYNC_COUNT >= MAX=$MAX_RSYNC_PROCS"
sleep "$SLEEP"
else
return 0
fi
done
error "🔴 Too many rsync processes running"
echo "Too many rsync processes, exiting."
exit 1
}
# MAIN
# ----------------------------- Main -----------------------------
main() {
info " Checking remote connectivity..."
echo ""
echo "🔍 Checking remote connectivity..."
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
info "🟢 Remote reachable"
echo " Remote server reachable"
break
fi
warn "🟡 Remote unreachable (attempt $i/$RETRY_COUNT)"
echo "⚠️ Remote down, retrying in $SLEEP seconds..."
sleep "$SLEEP"
done
if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
error "🔴 Remote server unreachable"
echo " Remote server unreachable. Exiting."
exit 1
fi
wait_for_rsync
info "🟡 Stopping containers on remote..."
echo ""
echo "🛑 Stopping containers on remote..."
stop_containers
# Build RSYNC OPTIONS
echo ""
echo "📦 Building 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)
info "🧪 Dry-run enabled"
fi
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
log "Final rsync opts: ${RSYNC_OPTS[*]}"
log "Final rsync opts: ${RSYNC_OPTS[*]}"
# EXECUTION
info "🚀 Starting rsync..."
START_TIME=$(date +%s)
echo ""
echo "🚀 Starting rsync..."
for attempt in $(seq 1 "$RETRY_COUNT"); do
info "Attempt $attempt/$RETRY_COUNT"
echo "Attempt $attempt/$RETRY_COUNT"
if rsync "${RSYNC_OPTS[@]}" \
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" \
-e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
info "🟢 Rsync completed successfully"
echo " Rsync completed successfully"
break
else
warn "❌ Rsync failed"
if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then
warn "🟡 Retrying in $SLEEP seconds..."
else
echo "❌ Rsync failed"
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
echo "Retrying in $SLEEP seconds..."
sleep "$SLEEP"
else
error "🔴 All rsync attempts failed"
echo "💥 All retries failed"
start_containers
exit 1
fi
fi
done
# FINALIZE
info "🟡 Restarting containers..."
echo ""
echo "🔄 Restarting containers..."
start_containers
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# SUMMARY
echo "============================================================"
if [[ "${PIPESTATUS[0]}" -eq 0 ]]; then
echo " 🟢 SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📁 Directory: $DIRECTORY"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Status: 🟢 SUCCESS"
echo " 🧪 Dry Run: $DRY_RUN"
else
echo " 🔴 SYNC SUMMARY"
echo "------------------------------------------------------------"
echo " 📁 Directory: $DIRECTORY"
echo " 🧩 Profile: $PROFILE_NAME"
echo " 🌐 Remote: $REMOTE_SERVER"
echo " ⏱ Duration: ${DURATION}s"
echo " 📡 Status: 🔴 FAILED"
echo " 🧪 Dry Run: $DRY_RUN"
fi
echo "============================================================"
info "️ Sync complete"
echo ""
echo "🎉 Sync complete!"
}
main