test
This commit is contained in:
+96
-82
@@ -20,113 +20,127 @@ set -e
|
|||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
source "$SCRIPT_DIR/../Master.conf"
|
# shellcheck source=common.sh
|
||||||
source "$SCRIPT_DIR/../common.sh"
|
source "$SCRIPT_DIR/common.sh"
|
||||||
|
|
||||||
# ----------------------------- INPUT -----------------------------
|
#############################################
|
||||||
|
# ARG PARSING (UNRAID SAFE)
|
||||||
|
#############################################
|
||||||
|
|
||||||
DIRECTORY=""
|
SOURCE=""
|
||||||
RAW_ARGS=()
|
DEST=""
|
||||||
|
ARGS=()
|
||||||
|
|
||||||
for ARG in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$ARG" in
|
if [[ "$arg" == -* ]]; then
|
||||||
--*|*=*) RAW_ARGS+=("$ARG") ;;
|
ARGS+=("$arg")
|
||||||
*)
|
elif [[ -z "$SOURCE" ]]; then
|
||||||
if [[ -z "$DIRECTORY" ]]; then
|
SOURCE="$arg"
|
||||||
DIRECTORY="$ARG"
|
elif [[ -z "$DEST" ]]; then
|
||||||
|
DEST="$arg"
|
||||||
else
|
else
|
||||||
RAW_ARGS+=("$ARG")
|
warn "Unknown argument: $arg"
|
||||||
fi
|
fi
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
done
|
||||||
|
|
||||||
parse_args "${RAW_ARGS[@]}"
|
#############################################
|
||||||
|
# LOAD CONFIG
|
||||||
|
#############################################
|
||||||
|
|
||||||
[[ -z "$DIRECTORY" ]] && error "Missing directory" && exit 1
|
PROFILE_NAME=${PROFILE_NAME:-"default"}
|
||||||
|
|
||||||
# ----------------------------- CORE -----------------------------
|
|
||||||
|
|
||||||
detect_hosts
|
|
||||||
resolve_remote_ip
|
|
||||||
|
|
||||||
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
|
|
||||||
|
|
||||||
|
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS:-2}
|
||||||
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
|
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
|
||||||
RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
|
|
||||||
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
|
|
||||||
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
|
|
||||||
|
|
||||||
# ----------------------------- STATUS -----------------------------
|
#############################################
|
||||||
|
# HEADER
|
||||||
if [[ "${SHOW_STATUS:-false}" == true ]]; then
|
#############################################
|
||||||
show_status
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ----------------------------- FLOW -----------------------------
|
|
||||||
|
|
||||||
|
info "Host: $(hostname) → ${REMOTE_HOST:-unknown}"
|
||||||
info "Sync Starting"
|
info "Sync Starting"
|
||||||
info "Source: $DIRECTORY"
|
info "Source: $SOURCE"
|
||||||
info "Remote: $REMOTE_SERVER:$DIRECTORY"
|
info "Remote: ${REMOTE_HOST:-$DEST}"
|
||||||
info "Profile: $PROFILE_NAME"
|
info "Profile: $PROFILE_NAME"
|
||||||
|
|
||||||
|
#############################################
|
||||||
|
# REMOTE RESOLVE
|
||||||
|
#############################################
|
||||||
|
|
||||||
|
info "Resolving remote IP..."
|
||||||
|
REMOTE_IP=$(resolve_remote_ip "$REMOTE_HOST")
|
||||||
|
info "Remote IP: $REMOTE_IP"
|
||||||
|
|
||||||
|
REMOTE_TARGET="${REMOTE_IP}:${DEST}"
|
||||||
|
|
||||||
|
#############################################
|
||||||
|
# CHECK REMOTE
|
||||||
|
#############################################
|
||||||
|
|
||||||
info "Checking remote..."
|
info "Checking remote..."
|
||||||
|
if ! ping -c 1 -W 1 "$REMOTE_IP" >/dev/null 2>&1; then
|
||||||
REMOTE_OK=false
|
error "Remote unreachable"
|
||||||
|
exit 1
|
||||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
|
||||||
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
|
|
||||||
success "Remote reachable"
|
|
||||||
REMOTE_OK=true
|
|
||||||
break
|
|
||||||
fi
|
fi
|
||||||
warn "Retry $i/$RETRY_COUNT"
|
ok "Remote reachable"
|
||||||
|
|
||||||
|
#############################################
|
||||||
|
# CONTAINER STOP (SAFE BARRIER)
|
||||||
|
#############################################
|
||||||
|
|
||||||
|
info "Stopping containers..."
|
||||||
|
|
||||||
|
stop_containers || true
|
||||||
|
|
||||||
|
# ensure docker settle (prevents race condition)
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
wait_for_containers_to_stop || true
|
||||||
|
|
||||||
|
#############################################
|
||||||
|
# RSYNC SLOT LIMITER (FIXED)
|
||||||
|
#############################################
|
||||||
|
|
||||||
|
wait_for_rsync() {
|
||||||
|
: "${RETRY_COUNT:=5}"
|
||||||
|
: "${MAX_RSYNC_PROCS:=2}"
|
||||||
|
: "${SLEEP:=2}"
|
||||||
|
|
||||||
|
for _ in $(seq 1 "$RETRY_COUNT"); do
|
||||||
|
|
||||||
|
# ONLY COUNT TOP-LEVEL RSYNC COMMANDS
|
||||||
|
COUNT=$(pgrep -x rsync | wc -l || true)
|
||||||
|
|
||||||
|
if [[ "$COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then
|
||||||
|
warn "Waiting rsync slot ($COUNT/$MAX_RSYNC_PROCS)"
|
||||||
sleep "$SLEEP"
|
sleep "$SLEEP"
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
[[ "$REMOTE_OK" != true ]] && error "Remote unreachable" && exit 1
|
error "Too many rsync processes"
|
||||||
|
exit 1
|
||||||
# ----------------------------- EXECUTION BARRIER -----------------------------
|
}
|
||||||
|
|
||||||
wait_for_rsync
|
wait_for_rsync
|
||||||
stop_containers
|
|
||||||
sync
|
|
||||||
sleep 2
|
|
||||||
|
|
||||||
get_rsync_opts
|
#############################################
|
||||||
|
# RSYNC OPTIONS
|
||||||
|
#############################################
|
||||||
|
|
||||||
for ex in "${EXCLUDE_DIRS[@]}"; do
|
info "Building rsync opts..."
|
||||||
RSYNC_OPTS+=(--exclude="$ex")
|
RSYNC_OPTS=(-aHAX --numeric-ids --delete)
|
||||||
done
|
|
||||||
|
|
||||||
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
|
if [[ "${DRY_RUN:-false}" == "true" ]]; then
|
||||||
|
RSYNC_OPTS+=("--dry-run")
|
||||||
RSYNC_CMD=(
|
|
||||||
rsync
|
|
||||||
"${RSYNC_OPTS[@]}"
|
|
||||||
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput"
|
|
||||||
"$DIRECTORY"
|
|
||||||
"root@${REMOTE_SERVER}:$DIRECTORY"
|
|
||||||
)
|
|
||||||
|
|
||||||
START=$(date +%s)
|
|
||||||
|
|
||||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
|
||||||
if "${RSYNC_CMD[@]}"; then
|
|
||||||
success "Rsync complete"
|
|
||||||
break
|
|
||||||
else
|
|
||||||
warn "Rsync failed ($i/$RETRY_COUNT)"
|
|
||||||
sleep "$SLEEP"
|
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
|
||||||
start_containers
|
#############################################
|
||||||
|
# EXECUTE RSYNC
|
||||||
|
#############################################
|
||||||
|
|
||||||
END=$(date +%s)
|
info "Running rsync..."
|
||||||
|
rsync "${RSYNC_OPTS[@]}" \
|
||||||
info "Directory: $DIRECTORY"
|
-e "ssh -o StrictHostKeyChecking=no" \
|
||||||
info "Profile: $PROFILE_NAME"
|
"$SOURCE" \
|
||||||
info "Duration: $((END-START))s"
|
"root@$REMOTE_TARGET"
|
||||||
info "Status: DONE"
|
|
||||||
Reference in New Issue
Block a user