fix in rsync

This commit is contained in:
2026-03-24 00:34:29 +00:00
parent 9300ae11c2
commit 564da10718
+31 -39
View File
@@ -25,25 +25,29 @@ set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../common.sh" source "$SCRIPT_DIR/../common.sh"
# Load central config # Load configuration
load_master_conf 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 # Input Arguments
DIRECTORY="$1" DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run|-n] [VAR=value ...]" echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1 exit 1
fi fi
shift # remove DIRECTORY
# Parse CLI args (flags + VAR=value) shift 1 # Shift past DIRECTORY
parse_args "$@"
# Determine profile # Determine profile
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
echo "Detected profile: $PROFILE_NAME" echo "Detected profile: $PROFILE_NAME"
# Apply profile defaults or fallback to Master.conf # Apply profile defaults, fallback to global defaults
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT} BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT}
CRITICAL_CONTAINER_NAMES=(${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}) CRITICAL_CONTAINER_NAMES=(${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]})
@@ -53,23 +57,13 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT}
SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP} SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP}
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}}) EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
# Validate numeric variables
validate_int MAX_RSYNC_PROCS "$MAX_RSYNC_PROCS"
validate_int BW_LIMIT "$BW_LIMIT"
validate_int CONTAINER_DELAY "$CONTAINER_DELAY"
validate_int RETRY_COUNT "$RETRY_COUNT"
validate_int SLEEP "$SLEEP"
echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}" echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}"
# Dependency check #============================================================
for cmd in rsync ssh ping timeout docker tailscale; do # Helper Functions
command -v "$cmd" >/dev/null 2>&1 || { echo "Error: Required command '$cmd' is not installed."; exit 1; } #============================================================
done
trap 'echo "Script interrupted. Exiting."; exit 130' INT TERM # Wait if too many rsync processes are running
# Helper functions
wait_for_rsync() { wait_for_rsync() {
for attempt in $(seq 1 "$RETRY_COUNT"); do for attempt in $(seq 1 "$RETRY_COUNT"); do
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true) RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
@@ -84,55 +78,51 @@ wait_for_rsync() {
exit 1 exit 1
} }
# Check if remote server is online
check_server_online() { check_server_online() {
timeout 5 ping -c 1 "$REMOTE_SERVER" &>/dev/null timeout 5 ping -c 1 "$REMOTE_SERVER" &>/dev/null
} }
declare -a RUNNING_CONTAINERS=() # Stop critical containers
stop_containers() { stop_containers() {
echo "[$LOCAL_SERVER_NAME] Checking containers on ${REMOTE_SERVER_NAME}..." echo "[$LOCAL_SERVER_NAME] Checking containers on ${REMOTE_SERVER_NAME}..."
RUNNING_CONTAINERS=() RUNNING_CONTAINERS=()
for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do
STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \ STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \
"docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false") "docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false")
if [ "$STATUS" == "true" ]; then if [[ "$STATUS" == "true" ]]; then
echo "Stopping $container" echo "Stopping $container"
RUNNING_CONTAINERS+=("$container") RUNNING_CONTAINERS+=("$container")
if [ "$DRY_RUN" = true ]; then [[ "$DRY_RUN" = true ]] || ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
echo "[DRY-RUN] Would stop container $container"
else
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
fi
fi fi
done done
} }
# Start critical containers
start_containers() { start_containers() {
echo "[$LOCAL_SERVER_NAME] Restarting containers on ${REMOTE_SERVER_NAME}..." echo "[$LOCAL_SERVER_NAME] Restarting containers on ${REMOTE_SERVER_NAME}..."
for container in "${RUNNING_CONTAINERS[@]}"; do for container in "${RUNNING_CONTAINERS[@]}"; do
if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then
echo "Delaying start of $container by ${CONTAINER_DELAY}s..." echo "Delaying start of $container by ${CONTAINER_DELAY}s..."
[ "$DRY_RUN" = false ] && sleep "$CONTAINER_DELAY" sleep "$CONTAINER_DELAY"
fi
if [ "$DRY_RUN" = true ]; then
echo "[DRY-RUN] Would start container $container"
else
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
fi fi
[[ "$DRY_RUN" = true ]] || ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
done done
} }
# Main sync logic #============================================================
# Main Logic
#============================================================
main() { main() {
echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY$REMOTE_SERVER:$DIRECTORY" echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
# Ensure remote is online
for i in $(seq 1 "$RETRY_COUNT"); do for i in $(seq 1 "$RETRY_COUNT"); do
if check_server_online; then break; fi if check_server_online; then break; fi
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..." echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
sleep "$SLEEP" sleep "$SLEEP"
done done
check_server_online || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; } check_server_online || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
wait_for_rsync wait_for_rsync
@@ -148,12 +138,12 @@ main() {
--no-whole-file --no-whole-file
) )
# Apply multiple exclude patterns # Apply exclude patterns
for pattern in "${EXCLUDE_DIRS[@]}"; do for pattern in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$pattern") RSYNC_OPTS+=(--exclude="$pattern")
done done
[ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run") [[ "$DRY_RUN" = true ]] && RSYNC_OPTS+=(--dry-run)
for attempt in $(seq 1 "$RETRY_COUNT"); do for attempt in $(seq 1 "$RETRY_COUNT"); do
echo "Starting rsync attempt $attempt/$RETRY_COUNT..." echo "Starting rsync attempt $attempt/$RETRY_COUNT..."
@@ -164,7 +154,7 @@ main() {
break break
else else
echo "Rsync attempt $attempt failed." echo "Rsync attempt $attempt failed."
if [ "$attempt" -lt "$RETRY_COUNT" ]; then if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then
echo "Retrying in $SLEEP seconds..." echo "Retrying in $SLEEP seconds..."
sleep "$SLEEP" sleep "$SLEEP"
else else
@@ -179,5 +169,7 @@ main() {
echo "[$LOCAL_SERVER_NAME] Sync complete." echo "[$LOCAL_SERVER_NAME] Sync complete."
} }
#============================================================
# Execute # Execute
#============================================================
main main