added user facing messages echo and power user admin info logging with a true false for logging
This commit is contained in:
+1
-1
@@ -25,7 +25,7 @@ HOST1_SSH_KEY="/root/.ssh/Gmer4Lfe-rsync-key"
|
||||
HOST2_SSH_KEY="/root/.ssh/Jayred365-rsync-key"
|
||||
|
||||
# -------------------------- Logging ---------------------------
|
||||
ENABLE_LOGGING=true # true/false to enable script logging
|
||||
ENABLE_LOGGING=true # false = only echo user-facing messages
|
||||
|
||||
|
||||
#--------------------Rsync Script Defaults-----------------------
|
||||
|
||||
+10
-16
@@ -16,7 +16,7 @@ fi
|
||||
shift
|
||||
parse_args "$@"
|
||||
|
||||
# ----------------------------- Host & IP -----------------------------
|
||||
# ----------------------------- Hosts & IP -----------------------------
|
||||
detect_hosts
|
||||
resolve_remote_ip
|
||||
|
||||
@@ -32,15 +32,16 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
|
||||
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
|
||||
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
|
||||
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Detected profile: $PROFILE_NAME"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}, DRY_RUN=$DRY_RUN"
|
||||
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"
|
||||
|
||||
# ----------------------------- 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
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Waiting... ($RSYNC_COUNT active rsync processes)"
|
||||
echo "Waiting for available rsync slot ($RSYNC_COUNT active)..."
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
return 0
|
||||
@@ -52,25 +53,17 @@ wait_for_rsync() {
|
||||
|
||||
# ----------------------------- Main -----------------------------
|
||||
main() {
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Starting sync: $DIRECTORY → $REMOTE_SERVER:$DIRECTORY"
|
||||
|
||||
# Ping check
|
||||
for i in $(seq 1 "$RETRY_COUNT"); do
|
||||
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then break; fi
|
||||
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
|
||||
sleep "$SLEEP"
|
||||
done
|
||||
ping -c 1 "$REMOTE_SERVER" &>/dev/null || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
|
||||
|
||||
echo "Starting sync: $DIRECTORY → $REMOTE_SERVER"
|
||||
wait_for_rsync
|
||||
stop_containers
|
||||
get_rsync_opts
|
||||
|
||||
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
|
||||
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
|
||||
log "Rsync options: ${RSYNC_OPTS[*]}"
|
||||
|
||||
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
||||
echo "Starting rsync attempt $attempt/$RETRY_COUNT..."
|
||||
echo "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
|
||||
echo "Rsync completed successfully."
|
||||
@@ -89,7 +82,8 @@ main() {
|
||||
done
|
||||
|
||||
start_containers
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Sync complete."
|
||||
echo "Sync complete."
|
||||
log "[$LOCAL_SERVER_NAME] Sync finished for $DIRECTORY → $REMOTE_SERVER"
|
||||
}
|
||||
|
||||
main
|
||||
@@ -3,9 +3,9 @@
|
||||
# ----------------------- Common Helpers for Unraid Scripts -------------------------------------
|
||||
# -----------------------------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------- Logging Helper -----------------------------
|
||||
# ----------------------------- Logging -----------------------------
|
||||
log() {
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] $(date '+%Y-%m-%d %H:%M:%S') $*"
|
||||
}
|
||||
|
||||
# ----------------------------- Argument Parsing -----------------------------
|
||||
@@ -17,7 +17,7 @@ parse_args() {
|
||||
VAR_VALUE="${ARG#*=}"
|
||||
if declare -p "$VAR_NAME" &>/dev/null; then
|
||||
printf -v "$VAR_NAME" '%s' "$VAR_VALUE"
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Overriding $VAR_NAME -> $VAR_VALUE"
|
||||
log "Overriding $VAR_NAME -> $VAR_VALUE"
|
||||
else
|
||||
echo "Warning: Unknown variable $VAR_NAME, ignoring."
|
||||
fi
|
||||
@@ -73,7 +73,8 @@ detect_hosts() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Local server: $LOCAL_SERVER_NAME, Remote server: $REMOTE_SERVER_NAME, SSH key: $SSH_KEY"
|
||||
echo "Local server: $LOCAL_SERVER_NAME, Remote server: $REMOTE_SERVER_NAME, SSH key: $SSH_KEY"
|
||||
log "Detected hosts successfully"
|
||||
}
|
||||
|
||||
# ----------------------------- Remote Server IP -----------------------------
|
||||
@@ -83,20 +84,22 @@ resolve_remote_ip() {
|
||||
echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME"
|
||||
exit 1
|
||||
fi
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Remote IP: $REMOTE_SERVER"
|
||||
echo "Remote IP: $REMOTE_SERVER"
|
||||
log "Resolved Tailscale IP successfully"
|
||||
}
|
||||
|
||||
# ----------------------------- Container Helpers -----------------------------
|
||||
declare -a RUNNING_CONTAINERS=()
|
||||
|
||||
stop_containers() {
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Checking containers on $REMOTE_SERVER_NAME..."
|
||||
echo "Checking critical 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")
|
||||
log "Container $container running: $STATUS"
|
||||
if [[ "$STATUS" == "true" ]]; then
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Stopping $container"
|
||||
echo "Stopping $container"
|
||||
RUNNING_CONTAINERS+=("$container")
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
|
||||
fi
|
||||
@@ -104,13 +107,14 @@ stop_containers() {
|
||||
}
|
||||
|
||||
start_containers() {
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Restarting containers on $REMOTE_SERVER_NAME..."
|
||||
echo "Restarting containers on $REMOTE_SERVER_NAME..."
|
||||
for container in "${RUNNING_CONTAINERS[@]}"; do
|
||||
if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then
|
||||
[[ "$ENABLE_LOGGING" == true ]] && echo "[LOG] Delaying start of $container by ${CONTAINER_DELAY}s..."
|
||||
echo "Delaying start of $container by ${CONTAINER_DELAY}s..."
|
||||
sleep "$CONTAINER_DELAY"
|
||||
fi
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
|
||||
log "Container $container started"
|
||||
done
|
||||
}
|
||||
|
||||
@@ -118,7 +122,102 @@ start_containers() {
|
||||
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[*]}"
|
||||
else
|
||||
RSYNC_OPTS=("${DEFAULT_RSYNC_OPTS[@]}")
|
||||
log "Using default rsync options: ${RSYNC_OPTS[*]}"
|
||||
fi
|
||||
}
|
||||
}
|
||||
3️⃣ rsync.sh
|
||||
|
||||
Minimal user-facing echo + detailed logging via log:
|
||||
|
||||
#!/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"
|
||||
if [[ -z "$DIRECTORY" ]]; then
|
||||
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
|
||||
exit 1
|
||||
fi
|
||||
shift
|
||||
parse_args "$@"
|
||||
|
||||
# ----------------------------- Hosts & IP -----------------------------
|
||||
detect_hosts
|
||||
resolve_remote_ip
|
||||
|
||||
# ----------------------------- 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[@]}})
|
||||
|
||||
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"
|
||||
|
||||
# ----------------------------- 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)..."
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
echo "Too many rsync processes, exiting."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ----------------------------- Main -----------------------------
|
||||
main() {
|
||||
echo "Starting sync: $DIRECTORY → $REMOTE_SERVER"
|
||||
wait_for_rsync
|
||||
stop_containers
|
||||
get_rsync_opts
|
||||
|
||||
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
|
||||
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run")
|
||||
log "Rsync options: ${RSYNC_OPTS[*]}"
|
||||
|
||||
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" \
|
||||
"$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then
|
||||
echo "Rsync completed successfully."
|
||||
break
|
||||
else
|
||||
echo "Rsync attempt $attempt failed."
|
||||
if [ "$attempt" -lt "$RETRY_COUNT" ]; then
|
||||
echo "Retrying in $SLEEP seconds..."
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
echo "All $RETRY_COUNT rsync attempts failed."
|
||||
start_containers
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
start_containers
|
||||
echo "Sync complete."
|
||||
log "[$LOCAL_SERVER_NAME] Sync finished for $DIRECTORY → $REMOTE_SERVER"
|
||||
}
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user