Files
Varaverk/Rsync/rsync.sh
T
2026-03-27 19:00:00 +00:00

141 lines
4.6 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"
# -----------------------------------------------------------------------------
# MASTER CONF (must exist)
# -----------------------------------------------------------------------------
if [[ -f "$SCRIPT_DIR/../Master.conf" ]]; then
source "$SCRIPT_DIR/../Master.conf"
else
echo "❌ Master.conf missing"
exit 1
fi
# -----------------------------------------------------------------------------
# SAFE SOURCE FUNCTION (CRITICAL FIX)
# -----------------------------------------------------------------------------
safe_source() {
local file="$1"
if [[ -f "$file" ]]; then
# shellcheck disable=SC1090
source "$file" || {
echo "❌ FAILED sourcing: $file"
return 1
}
return 0
fi
return 1
}
# -----------------------------------------------------------------------------
# LOAD common.sh (STRICT VALIDATION)
# -----------------------------------------------------------------------------
COMMON_LOADED=false
COMMON_PATHS=(
"$SCRIPT_DIR/../common.sh"
"$SCRIPT_DIR/common.sh"
"/mnt/user/appdata/unraid_scripts/common.sh"
)
for f in "${COMMON_PATHS[@]}"; do
if safe_source "$f"; then
COMMON_LOADED=true
break
fi
done
# -----------------------------------------------------------------------------
# HARD CHECK (THIS PREVENTS YOUR ERROR)
# -----------------------------------------------------------------------------
if ! declare -F log_info >/dev/null 2>&1; then
echo "⚠️ [WARN] log_info not found — forcing fallback logger"
log_info() { echo "️ [INFO] $*"; }
log_ok() { echo "✅ [OK] $*"; }
log_warn() { echo "⚠️ [WARN] $*"; }
log_error() { echo "❌ [ERROR] $*"; }
fi
# -----------------------------------------------------------------------------
# INIT
# -----------------------------------------------------------------------------
log_info "Sync script initialized"
LOCAL_HOST="$(hostname)"
REMOTE_HOST="${REMOTE_HOST:-jayred365}"
log_info "Host: ${LOCAL_HOST}${REMOTE_HOST}"
# -----------------------------------------------------------------------------
# RESOLVE REMOTE
# -----------------------------------------------------------------------------
REMOTE_IP="$(getent hosts "$REMOTE_HOST" | awk '{print $1}' || true)"
if [[ -z "$REMOTE_IP" ]]; then
log_error "Remote resolve failed"
exit 1
fi
log_info "Remote IP: $REMOTE_IP"
# -----------------------------------------------------------------------------
# CONFIG
# -----------------------------------------------------------------------------
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"
# -----------------------------------------------------------------------------
# SSH CHECK
# -----------------------------------------------------------------------------
ssh -o BatchMode=yes -o ConnectTimeout=5 \
"${REMOTE_USER}@${REMOTE_IP}" "echo ok" >/dev/null 2>&1
log_ok "Remote reachable"
# -----------------------------------------------------------------------------
# CONTAINERS
# -----------------------------------------------------------------------------
if declare -F stop_containers >/dev/null 2>&1; then
log_info "Stopping containers..."
stop_containers "$PROFILE" || true
fi
# -----------------------------------------------------------------------------
# RSYNC
# -----------------------------------------------------------------------------
log_info "Running rsync..."
rsync -aHAX --delete --numeric-ids \
-e "ssh" \
"$SOURCE_PATH/" \
"${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH}/"
log_ok "Rsync complete"
# -----------------------------------------------------------------------------
# START CONTAINERS
# -----------------------------------------------------------------------------
if declare -F start_containers >/dev/null 2>&1; then
log_info "Starting containers..."
start_containers "$PROFILE" || true
fi
log_ok "Sync finished"