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

104 lines
3.8 KiB
Bash

#!/bin/bash
set -e
# ----------------------------------------------------------------------------------------------
# --------------------------------- Rsync Core Script ------------------------------------------
# ----------------------------------------------------------------------------------------------
# User Variables handled in Master.conf
# ----------------------------------------------------------------------------------------------
# Resolve script directory (Unraid-safe /tmp compatible)
PROJECT_ROOT="/mnt/user/appdata/unraid_scripts"
SCRIPT_DIR="$PROJECT_ROOT/Rsync"
# ----------------------------------------------------------------------------------------------
# Load configs & helpers (FIXED PATHS)
# ----------------------------------------------------------------------------------------------
source "$PROJECT_ROOT/Master.conf"
source "$PROJECT_ROOT/common.sh"
# ----------------------------------------------------------------------------------------------
# Init
# ----------------------------------------------------------------------------------------------
log_info "Sync script initialized"
LOCAL_HOST="$(hostname)"
REMOTE_HOST="${REMOTE_HOST:-jayred365}"
log_info "Host: ${LOCAL_HOST}${REMOTE_HOST}"
# ----------------------------------------------------------------------------------------------
# Resolve remote IP (RESTORED ORIGINAL LOGIC)
# ----------------------------------------------------------------------------------------------
log_info "Resolving remote IP..."
REMOTE_IP="$(getent hosts "$REMOTE_HOST" | awk '{print $1}')"
if [[ -z "$REMOTE_IP" ]]; then
log_error "Failed to resolve remote host: $REMOTE_HOST"
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"
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..."
ssh -o BatchMode=yes -o ConnectTimeout=5 \
"${REMOTE_USER}@${REMOTE_IP}" "echo ok" >/dev/null 2>&1
log_ok "Remote reachable"
# ----------------------------------------------------------------------------------------------
# Stop containers (unchanged behavior)
# ----------------------------------------------------------------------------------------------
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_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 (unchanged)
# ----------------------------------------------------------------------------------------------
if declare -f start_containers >/dev/null 2>&1; then
log_info "Starting containers..."
start_containers "$PROFILE" || true
fi
log_ok "Sync finished successfully"