Files
Varaverk/Rsync/rsync.sh
T
2026-03-27 18:56:10 +00:00

141 lines
5.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
# ----------------------------------------------------------------------------------------------
# --------------------------------- Rsync Core Script ------------------------------------------
# ----------------------------------------------------------------------------------------------
# User Variables handled in Master.conf
# ----------------------------------------------------------------------------------------------
# Resolve script directory (Unraid-safe /tmp compatible)
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="$BASE_DIR"
# ----------------------------------------------------------------------------------------------
# Load configs (Master.conf)
# ----------------------------------------------------------------------------------------------
if [[ -f "$SCRIPT_DIR/../Master.conf" ]]; then
source "$SCRIPT_DIR/../Master.conf"
else
echo "❌ [ERROR] Master.conf not found"
exit 1
fi
# ----------------------------------------------------------------------------------------------
# Load common.sh safely (MULTI-PATH FIX)
# ----------------------------------------------------------------------------------------------
COMMON_LOADED=false
COMMON_CANDIDATES=(
"$SCRIPT_DIR/../common.sh"
"$SCRIPT_DIR/common.sh"
"/mnt/user/appdata/unraid_scripts/common.sh"
"/mnt/user/appdata/unraid_scripts/Rsync/common.sh"
)
for c in "${COMMON_CANDIDATES[@]}"; do
if [[ -f "$c" ]]; then
source "$c"
COMMON_LOADED=true
break
fi
done
# ----------------------------------------------------------------------------------------------
# Fallback logging (ONLY if common.sh not loaded)
# ----------------------------------------------------------------------------------------------
if [[ "$COMMON_LOADED" != true ]]; then
log_info() { echo "️ [INFO] $*"; }
log_ok() { echo "✅ [OK] $*"; }
log_warn() { echo "⚠️ [WARN] $*"; }
log_error() { echo "❌ [ERROR] $*"; }
log_warn "common.sh not found — using fallback logger"
fi
# ----------------------------------------------------------------------------------------------
# Init
# ----------------------------------------------------------------------------------------------
log_info "Sync script initialized"
LOCAL_HOST="$(hostname)"
REMOTE_HOST="${REMOTE_HOST:-jayred365}"
log_info "Host: ${LOCAL_HOST}${REMOTE_HOST}"
# ----------------------------------------------------------------------------------------------
# Resolve remote IP
# ----------------------------------------------------------------------------------------------
log_info "Resolving remote IP..."
REMOTE_IP="$(getent hosts "$REMOTE_HOST" | awk '{print $1}' || true)"
if [[ -z "$REMOTE_IP" ]]; then
log_error "Failed to resolve remote host: $REMOTE_HOST"
exit 1
fi
log_info "Remote IP: $REMOTE_IP"
# ----------------------------------------------------------------------------------------------
# Profile + paths
# ----------------------------------------------------------------------------------------------
PROFILE="${1:-arrs_stack}"
SOURCE_PATH="${SOURCE_PATH:-/mnt/user/appdata-Failover/Arrs_Stack}"
REMOTE_PATH="${REMOTE_PATH:-/mnt/user/appdata-Failover/Arrs_Stack}"
REMOTE_USER="${REMOTE_USER:-root}"
log_info "Sync Starting"
log_info "Source: $SOURCE_PATH"
log_info "Remote: $REMOTE_IP:$REMOTE_PATH"
log_info "Profile: $PROFILE"
log_info "Dry Run: ${DRY_RUN:-false}"
# ----------------------------------------------------------------------------------------------
# Remote check
# ----------------------------------------------------------------------------------------------
log_info "Checking remote..."
if ! ssh -o BatchMode=yes -o ConnectTimeout=5 \
"${REMOTE_USER}@${REMOTE_IP}" "echo ok" >/dev/null 2>&1; then
log_error "Remote unreachable"
exit 1
fi
log_ok "Remote reachable"
# ----------------------------------------------------------------------------------------------
# Stop containers (optional)
# ----------------------------------------------------------------------------------------------
if declare -f stop_containers >/dev/null 2>&1; then
log_info "Stopping containers..."
stop_containers "$PROFILE" || true
fi
# ----------------------------------------------------------------------------------------------
# RSYNC EXECUTION
# ----------------------------------------------------------------------------------------------
log_info "Running rsync..."
RSYNC_OPTS="-aHAX --delete --numeric-ids"
if [[ "${DRY_RUN:-false}" == "true" ]]; then
RSYNC_OPTS="$RSYNC_OPTS -n"
fi
rsync $RSYNC_OPTS \
-e "ssh" \
"$SOURCE_PATH/" \
"${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH}/"
log_ok "Rsync complete"
# ----------------------------------------------------------------------------------------------
# Start containers (optional)
# ----------------------------------------------------------------------------------------------
if declare -f start_containers >/dev/null 2>&1; then
log_info "Starting containers..."
start_containers "$PROFILE" || true
fi
log_ok "Sync finished successfully"