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

129 lines
4.8 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, Please adjust in Master.conf as needed ----------------------
# ----------------------------------------------------------------------------------------------
#
# User variables can be adjusted in Master.conf
#
# Scripts now just contain runtime logic
#
# This new setup allows for profiles and arguments to use the core script
#
# Use arguments in unRAID User Plugin for directory
# Example = /mnt/user/appdata/unraid_scripts/Rsync/rsync.sh --dry-run
# ----------------------------------------------------------------------------------------------
# -------------- End Of User Variables, Please adjust in Master.conf as needed -----------------
# ----------------------------------------------------------------------------------------------
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# -----------------------------------------------------------------------------
# Self-healing config loader (OPTION C)
# -----------------------------------------------------------------------------
if [[ -f "$BASE_DIR/../common.sh" ]]; then
# normal layout: Rsync/ + shared root files
source "$BASE_DIR/../Master.conf"
source "$BASE_DIR/../common.sh"
elif [[ -f "$BASE_DIR/common.sh" ]]; then
# flat layout: everything inside Rsync/
source "$BASE_DIR/Master.conf"
source "$BASE_DIR/common.sh"
else
echo "️ [INFO] Host: $(hostname)"
echo "❌ [ERROR] common.sh and Master.conf not found in expected locations"
echo "️ [INFO] BASE_DIR = $BASE_DIR"
exit 1
fi
# -----------------------------------------------------------------------------
# Load shared helpers (if your common.sh provides these)
# -----------------------------------------------------------------------------
log_info "Sync script initialized"
# -----------------------------------------------------------------------------
# Host resolution (safe fallback)
# -----------------------------------------------------------------------------
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"
# -----------------------------------------------------------------------------
# Config validation
# -----------------------------------------------------------------------------
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 (if defined in common.sh)
# -----------------------------------------------------------------------------
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"
# -----------------------------------------------------------------------------
# Restart containers (if defined)
# -----------------------------------------------------------------------------
if declare -f start_containers >/dev/null 2>&1; then
log_info "Starting containers..."
start_containers "$PROFILE" || true
fi
log_ok "Sync finished successfully"