diff --git a/Rsync/rsync.sh b/Rsync/rsync.sh index a3a7829..fa96f7e 100644 --- a/Rsync/rsync.sh +++ b/Rsync/rsync.sh @@ -25,29 +25,29 @@ set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../common.sh" -# Load configuration +# Load config load_master_conf -# Parse CLI args (supports --dry-run and VAR=value overrides) -parse_args "$@" - -# Detect hosts, resolve Tailscale IP, pick SSH key -detect_hosts - -# Input Arguments +# 🔥 Handle directory FIRST DIRECTORY="$1" if [[ -z "$DIRECTORY" ]]; then echo "Usage: $0 [--dry-run] [VAR=value ...]" exit 1 fi -shift 1 # Shift past DIRECTORY +shift 1 + +# Parse args AFTER shifting +parse_args "$@" + +# Detect hosts AFTER config + args +detect_hosts # Determine profile PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') echo "Detected profile: $PROFILE_NAME" -# Apply profile defaults, fallback to global defaults +# Apply profile defaults 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]}) @@ -63,11 +63,10 @@ echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS # Helper Functions #============================================================ -# Wait if too many rsync processes are running 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 + if [[ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then echo "[$LOCAL_SERVER_NAME] Waiting... ($RSYNC_COUNT active rsync processes)" sleep "$SLEEP" else @@ -78,18 +77,18 @@ wait_for_rsync() { exit 1 } -# Check if remote server is online check_server_online() { timeout 5 ping -c 1 "$REMOTE_SERVER" &>/dev/null } -# Stop critical containers stop_containers() { echo "[$LOCAL_SERVER_NAME] Checking containers on ${REMOTE_SERVER_NAME}..." RUNNING_CONTAINERS=() + for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \ "docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false") + if [[ "$STATUS" == "true" ]]; then echo "Stopping $container" RUNNING_CONTAINERS+=("$container") @@ -98,14 +97,15 @@ stop_containers() { done } -# Start critical containers start_containers() { echo "[$LOCAL_SERVER_NAME] Restarting containers on ${REMOTE_SERVER_NAME}..." + for container in "${RUNNING_CONTAINERS[@]}"; do if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then echo "Delaying start of $container by ${CONTAINER_DELAY}s..." sleep "$CONTAINER_DELAY" fi + [[ "$DRY_RUN" = true ]] || ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container" done } @@ -117,12 +117,12 @@ start_containers() { main() { echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY → $REMOTE_SERVER:$DIRECTORY" - # Ensure remote is online for i in $(seq 1 "$RETRY_COUNT"); do if check_server_online; then break; fi echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..." sleep "$SLEEP" done + check_server_online || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; } wait_for_rsync @@ -138,7 +138,6 @@ main() { --no-whole-file ) - # Apply exclude patterns for pattern in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$pattern") done @@ -147,6 +146,7 @@ main() { for attempt in $(seq 1 "$RETRY_COUNT"); do 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 @@ -154,6 +154,7 @@ main() { break else echo "Rsync attempt $attempt failed." + if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then echo "Retrying in $SLEEP seconds..." sleep "$SLEEP" @@ -169,7 +170,4 @@ main() { echo "[$LOCAL_SERVER_NAME] Sync complete." } -#============================================================ -# Execute -#============================================================ main \ No newline at end of file diff --git a/common.sh b/common.sh index 8264676..86a3361 100644 --- a/common.sh +++ b/common.sh @@ -66,7 +66,7 @@ require_var() { fi } -# 🔥 Host + SSH Detection (THE IMPORTANT FIX) +# Detect hosts + SSH key + Tailscale IP detect_hosts() { LOCAL_HOSTNAME="$(hostname)" @@ -86,13 +86,24 @@ detect_hosts() { # Resolve Tailscale IP REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null) + if [[ -z "$REMOTE_SERVER" ]]; then echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME" exit 1 fi - # Assign SSH key + # Validate SSH_KEYS + if [[ "$(declare -p SSH_KEYS 2>/dev/null)" != declare\ -A* ]]; then + echo "Error: SSH_KEYS is not a valid associative array." + exit 1 + fi + KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME" + + # Debug (safe to keep) + echo "DEBUG: KEY_ID=$KEY_ID" + echo "DEBUG: Available keys: ${!SSH_KEYS[@]}" + if [[ -n "${SSH_KEYS[$KEY_ID]}" ]]; then SSH_KEY="${SSH_KEYS[$KEY_ID]}" else