seperated logg and echo a lot
This commit is contained in:
+63
-15
@@ -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
|
||||
@@ -3,51 +3,71 @@
|
||||
# ----------------------- Common Helpers for Unraid Scripts -------------------------------------
|
||||
# -----------------------------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------- Logging -----------------------------
|
||||
# ----------------------------- Output Helpers -----------------------------
|
||||
|
||||
info() {
|
||||
echo "[INFO] $*"
|
||||
}
|
||||
|
||||
warn() {
|
||||
echo "[WARN] $*"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo "[ERROR] $*"
|
||||
}
|
||||
|
||||
log() {
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] $(date '+%Y-%m-%d %H:%M:%S') $*"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] $*"
|
||||
}
|
||||
|
||||
# ----------------------------- Argument Parsing -----------------------------
|
||||
|
||||
parse_args() {
|
||||
DRY_RUN=${DRY_RUN:-false}
|
||||
|
||||
for ARG in "$@"; do
|
||||
if [[ "$ARG" == *=* ]]; then
|
||||
VAR_NAME="${ARG%%=*}"
|
||||
VAR_VALUE="${ARG#*=}"
|
||||
|
||||
if declare -p "$VAR_NAME" &>/dev/null; then
|
||||
printf -v "$VAR_NAME" '%s' "$VAR_VALUE"
|
||||
log "Overriding $VAR_NAME -> $VAR_VALUE"
|
||||
log "Override: $VAR_NAME=$VAR_VALUE"
|
||||
else
|
||||
echo "Warning: Unknown variable $VAR_NAME, ignoring."
|
||||
warn "Unknown variable $VAR_NAME, ignoring."
|
||||
fi
|
||||
else
|
||||
case "$ARG" in
|
||||
--dry-run|-n)
|
||||
DRY_RUN=true
|
||||
info "Dry-run mode enabled"
|
||||
;;
|
||||
--log)
|
||||
LOG=true|--log)
|
||||
ENABLE_LOGGING=true
|
||||
info "Verbose logging enabled"
|
||||
;;
|
||||
--no-log)
|
||||
LOG=false)
|
||||
ENABLE_LOGGING=false
|
||||
;;
|
||||
--help|-h)
|
||||
echo "Usage: script [--dry-run|-n] [--log|--no-log] [VAR=value ...]"
|
||||
echo "Usage: script [--dry-run|-n] [LOG=true] [VAR=value ...]"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Warning: Unknown argument $ARG"
|
||||
warn "Unknown argument $ARG"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# ----------------------------- Validation -----------------------------
|
||||
|
||||
require_var() {
|
||||
local var="$1"
|
||||
if [[ -z "${!var:-}" ]]; then
|
||||
echo "Error: Required variable $var is not set."
|
||||
error "Required variable $var is not set."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
@@ -55,15 +75,18 @@ require_var() {
|
||||
validate_int() {
|
||||
local name="$1"
|
||||
local value="$2"
|
||||
|
||||
if ! [[ "${value:-}" =~ ^[0-9]+$ ]]; then
|
||||
echo "Error: $name must be a non-negative integer."
|
||||
error "$name must be a non-negative integer."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# ----------------------------- Host Detection & SSH -----------------------------
|
||||
# ----------------------------- Host Detection -----------------------------
|
||||
|
||||
detect_hosts() {
|
||||
LOCAL_HOSTNAME="$(hostname)"
|
||||
|
||||
if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then
|
||||
LOCAL_SERVER_NAME="$HOST1"
|
||||
REMOTE_SERVER_NAME="$HOST2"
|
||||
@@ -71,7 +94,7 @@ detect_hosts() {
|
||||
LOCAL_SERVER_NAME="$HOST2"
|
||||
REMOTE_SERVER_NAME="$HOST1"
|
||||
else
|
||||
echo "Error: Local hostname ($LOCAL_HOSTNAME) not recognized."
|
||||
error "Local hostname ($LOCAL_HOSTNAME) not recognized."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -81,63 +104,94 @@ detect_hosts() {
|
||||
|
||||
KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME"
|
||||
SSH_KEY="${SSH_KEYS[$KEY_ID]}"
|
||||
|
||||
if [[ -z "$SSH_KEY" ]]; then
|
||||
echo "Error: No SSH key defined for $LOCAL_SERVER_NAME → $REMOTE_SERVER_NAME"
|
||||
error "No SSH key defined for $LOCAL_SERVER_NAME → $REMOTE_SERVER_NAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Local server: $LOCAL_SERVER_NAME, Remote server: $REMOTE_SERVER_NAME, SSH key: $SSH_KEY"
|
||||
log "Detected hosts successfully"
|
||||
info "Local: $LOCAL_SERVER_NAME → Remote: $REMOTE_SERVER_NAME"
|
||||
log "SSH key: $SSH_KEY"
|
||||
}
|
||||
|
||||
# ----------------------------- Remote Server IP -----------------------------
|
||||
# ----------------------------- Remote IP -----------------------------
|
||||
|
||||
resolve_remote_ip() {
|
||||
log "Resolving Tailscale IP for $REMOTE_SERVER_NAME..."
|
||||
|
||||
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"
|
||||
error "Could not resolve Tailscale IP for $REMOTE_SERVER_NAME"
|
||||
exit 1
|
||||
fi
|
||||
echo "Remote IP: $REMOTE_SERVER"
|
||||
log "Resolved Tailscale IP successfully"
|
||||
|
||||
info "Remote IP: $REMOTE_SERVER"
|
||||
}
|
||||
|
||||
# ----------------------------- Container Helpers -----------------------------
|
||||
# ----------------------------- Connectivity -----------------------------
|
||||
|
||||
check_remote_online() {
|
||||
log "Pinging $REMOTE_SERVER..."
|
||||
ping -c 1 "$REMOTE_SERVER" &>/dev/null
|
||||
}
|
||||
|
||||
# ----------------------------- Containers -----------------------------
|
||||
|
||||
declare -a RUNNING_CONTAINERS=()
|
||||
|
||||
stop_containers() {
|
||||
echo "Checking critical containers on $REMOTE_SERVER_NAME..."
|
||||
if [[ ${#CRITICAL_CONTAINER_NAMES[@]} -eq 0 ]]; then
|
||||
log "No containers defined to stop"
|
||||
return
|
||||
fi
|
||||
|
||||
info "Stopping containers on $REMOTE_SERVER_NAME..."
|
||||
RUNNING_CONTAINERS=()
|
||||
|
||||
for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do
|
||||
log "Checking container: $container"
|
||||
|
||||
STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \
|
||||
"docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false")
|
||||
log "Container $container running: $STATUS"
|
||||
|
||||
if [[ "$STATUS" == "true" ]]; then
|
||||
echo "Stopping $container"
|
||||
info "Stopping $container"
|
||||
RUNNING_CONTAINERS+=("$container")
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
|
||||
else
|
||||
log "$container already stopped"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
start_containers() {
|
||||
echo "Restarting containers on $REMOTE_SERVER_NAME..."
|
||||
if [[ ${#RUNNING_CONTAINERS[@]} -eq 0 ]]; then
|
||||
log "No containers to restart"
|
||||
return
|
||||
fi
|
||||
|
||||
info "Starting 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..."
|
||||
info "Delaying $container (${CONTAINER_DELAY}s)"
|
||||
sleep "$CONTAINER_DELAY"
|
||||
fi
|
||||
|
||||
info "Starting $container"
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
|
||||
log "Container $container started"
|
||||
done
|
||||
}
|
||||
|
||||
# ----------------------------- Rsync Options Builder -----------------------------
|
||||
# ----------------------------- Rsync Options -----------------------------
|
||||
|
||||
get_rsync_opts() {
|
||||
if [[ -n "${PROFILE_RSYNC_OPTS[$PROFILE_NAME]:-}" ]]; then
|
||||
read -r -a RSYNC_OPTS <<< "${PROFILE_RSYNC_OPTS[$PROFILE_NAME]}"
|
||||
log "Using profile-specific rsync options: ${RSYNC_OPTS[*]}"
|
||||
log "Using profile rsync options: ${PROFILE_RSYNC_OPTS[$PROFILE_NAME]}"
|
||||
else
|
||||
RSYNC_OPTS=("${DEFAULT_RSYNC_OPTS[@]}")
|
||||
log "Using default rsync options: ${RSYNC_OPTS[*]}"
|
||||
log "Using default rsync options: ${DEFAULT_RSYNC_OPTS[*]}"
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user