62 lines
2.4 KiB
Bash
62 lines
2.4 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# Load configs & helpers
|
|
source "$SCRIPT_DIR/../Master.conf"
|
|
source "$SCRIPT_DIR/../common.sh"
|
|
|
|
# ----------------------------- Input -----------------------------
|
|
DIRECTORY="$1"
|
|
[[ -z "$DIRECTORY" ]] && { log "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"; exit 1; }
|
|
|
|
shift
|
|
parse_args "$@"
|
|
|
|
# ----------------------------- Profile -----------------------------
|
|
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
|
|
|
|
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}
|
|
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"
|
|
|
|
# ----------------------------- Main Sync -----------------------------
|
|
main() {
|
|
log "[$LOCAL_SERVER_NAME] Starting sync: $DIRECTORY → $REMOTE_SERVER:$DIRECTORY"
|
|
|
|
[[ $(check_server_online) ]] || { log "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")
|
|
|
|
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
|
log "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."
|
|
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; }
|
|
fi
|
|
done
|
|
|
|
start_containers
|
|
log "[$LOCAL_SERVER_NAME] Sync complete."
|
|
}
|
|
|
|
main |