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

117 lines
4.3 KiB
Bash

#!/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)"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load configs & helpers
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
# -----------------------------------------------------------------------------
# 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"