From e7250a71507a79986da786a6775b8af5d251052a Mon Sep 17 00:00:00 2001 From: FailedProxy Date: Tue, 24 Mar 2026 21:07:16 +0000 Subject: [PATCH] Undo --- Master.conf | 49 ++++++++++++++++++---- Rsync/rsync.sh | 111 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 114 insertions(+), 46 deletions(-) diff --git a/Master.conf b/Master.conf index f850354..fcbcf3a 100644 --- a/Master.conf +++ b/Master.conf @@ -17,15 +17,48 @@ #----------------------------------------------------------------------------------------------- #-------------------- Host Configuration -------------------- -# Define the hostnames of both servers +# Hostnames of both servers HOST1="unRAID-Gmer4Lfe" HOST2="unRAID-Jayred365" -# Master.conf -declare -A SSH_KEYS -declare -A SSH_KEYS +# Detect local hostname +LOCAL_HOSTNAME="$(hostname)" +if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then + LOCAL_SERVER_NAME="$HOST1" + REMOTE_SERVER_NAME="$HOST2" +elif [[ "$LOCAL_HOSTNAME" == "$HOST2" ]]; then + LOCAL_SERVER_NAME="$HOST2" + REMOTE_SERVER_NAME="$HOST1" +else + echo "Error: Local hostname ($LOCAL_HOSTNAME) not recognized." + exit 1 +fi +echo "Local server: $LOCAL_SERVER_NAME" +echo "Remote server: $REMOTE_SERVER_NAME" + + +# Remote Server Ip Resolution +# Resolve Tailscale IP dynamically +REMOTE_SERVER=$(tailscale ip -4 "$REMOTE_SERVER_NAME" 2>/dev/null) +if [[ -z "$REMOTE_SERVER" ]]; then + echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME" + exit 1 +fi + + +# SSH Key Mapping SSH_KEYS["$HOST1|$HOST2"]="/root/.ssh/Gmer4Lfe-rsync-key" SSH_KEYS["$HOST2|$HOST1"]="/root/.ssh/Jayred365-rsync-key" + +KEY_ID="$LOCAL_SERVER_NAME|$REMOTE_SERVER_NAME" +if [[ -n "${SSH_KEYS[$KEY_ID]}" ]]; then + SSH_KEY="${SSH_KEYS[$KEY_ID]}" +else + echo "Error: No SSH key defined for $LOCAL_SERVER_NAME → $REMOTE_SERVER_NAME" + exit 1 +fi +echo "Using SSH key: $SSH_KEY" + #----------------------------------------------------------------------------------------------- #----------------- User Variables -------------------------------------------------------------- #----------------------------------------------------------------------------------------------- @@ -40,13 +73,13 @@ SLEEP=300 # Max number of concurrently running rsync processes MAX_RSYNC_PROCS=1 # Container start/stop/restart before and after rsync -CRITICAL_CONTAINER_NAMES=() +CRITICAL_CONTAINER_NAMES=("") # Containers that need delayed before starting -DELAYED_CONTAINERS=() +DELAYED_CONTAINERS=("") # Delay in seconds between starting containers, useful for things like Authelia CONTAINER_DELAY=5 # Not include logs during transfer -EXCLUDE_DIRS=() +EXCLUDE_DIRS=("") #-------------------- unRAID essentail scripts ------------------ # unRAID reboot script user warning time (seconds) @@ -118,7 +151,7 @@ PROFILE_CONTAINER_DELAY["important-data"]=10 PROFILE_CONTAINER_DELAY["emby"]=5 # Not include logs or other file types during transfer per profile declare -A PROFILE_EXCLUDE_DIRS -PROFILE_EXCLUDE_DIRS["arrs_stack"]="logs" +PROFILE_EXCLUDE_DIRS["arrs_stack"]="logs *.tmp" PROFILE_EXCLUDE_DIRS["critical-data"]="logs *.tmp" PROFILE_EXCLUDE_DIRS["gmer4lfe"]="logs *.tmp" PROFILE_EXCLUDE_DIRS["important-data"]="logs *.tmp" diff --git a/Rsync/rsync.sh b/Rsync/rsync.sh index 0833b6d..fa96f7e 100644 --- a/Rsync/rsync.sh +++ b/Rsync/rsync.sh @@ -1,30 +1,53 @@ #!/bin/bash set -e +#----------------------------------------------------------------------------------------------- +# --------------------------- Core Rsync Script for Unraid ------------------------------------- +#----------------------------------------------------------------------------------------------- +#----------------- 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 /mnt/user/appdata-test/Arrs/ +# +# /mnt/user/appdata-test/Arrs/ is the argument +# If the last name /Arrs/ matches a profile in Master.conf, then profile defaults are used +# Otherwise, global configuration in Master.conf is used +#----------------------------------------------------------------------------------------------- +#---------------End Of User Variables, Please adjust in Master.conf as needed ------------------ +#----------------------------------------------------------------------------------------------- + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../common.sh" -# Load config and detect hosts +# Load config load_master_conf -detect_hosts -# Input argument: directory +# 🔥 Handle directory FIRST DIRECTORY="$1" if [[ -z "$DIRECTORY" ]]; then - echo "Usage: $0 [--dry-run|-n] [VAR=value ...]" + echo "Usage: $0 [--dry-run] [VAR=value ...]" exit 1 fi -shift -# Parse CLI args +shift 1 + +# Parse args AFTER shifting parse_args "$@" +# Detect hosts AFTER config + args +detect_hosts + # Determine profile -DIR_CLEAN="${DIRECTORY%/}" # strip trailing slash -PROFILE_NAME=$(basename "$DIR_CLEAN" | tr '[:upper:]' '[:lower:]') +PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') echo "Detected profile: $PROFILE_NAME" -# Apply profile defaults or fallback +# Apply profile defaults MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT} CRITICAL_CONTAINER_NAMES=(${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}) @@ -34,26 +57,16 @@ RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT} SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP} EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}}) -validate_int MAX_RSYNC_PROCS "$MAX_RSYNC_PROCS" -validate_int BW_LIMIT "$BW_LIMIT" -validate_int CONTAINER_DELAY "$CONTAINER_DELAY" -validate_int RETRY_COUNT "$RETRY_COUNT" -validate_int SLEEP "$SLEEP" - echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}" -# Dependencies -for cmd in rsync ssh ping timeout docker tailscale; do - command -v "$cmd" >/dev/null 2>&1 || { echo "Error: $cmd not installed."; exit 1; } -done +#============================================================ +# Helper Functions +#============================================================ -trap 'echo "Script interrupted. Exiting."; exit 130' INT TERM - -# Helper functions wait_for_rsync() { for attempt in $(seq 1 "$RETRY_COUNT"); do RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true) - if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then + if [[ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]]; then echo "[$LOCAL_SERVER_NAME] Waiting... ($RSYNC_COUNT active rsync processes)" sleep "$SLEEP" else @@ -68,36 +81,39 @@ check_server_online() { timeout 5 ping -c 1 "$REMOTE_SERVER" &>/dev/null } -declare -a RUNNING_CONTAINERS=() - stop_containers() { echo "[$LOCAL_SERVER_NAME] Checking containers on ${REMOTE_SERVER_NAME}..." RUNNING_CONTAINERS=() + for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \ "docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false") - if [ "$STATUS" == "true" ]; then + + if [[ "$STATUS" == "true" ]]; then echo "Stopping $container" RUNNING_CONTAINERS+=("$container") - [ "$DRY_RUN" = false ] && ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container" - [ "$DRY_RUN" = true ] && echo "[DRY-RUN] Would stop $container" + [[ "$DRY_RUN" = true ]] || ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container" fi done } start_containers() { echo "[$LOCAL_SERVER_NAME] Restarting containers on ${REMOTE_SERVER_NAME}..." + for container in "${RUNNING_CONTAINERS[@]}"; do if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then echo "Delaying start of $container by ${CONTAINER_DELAY}s..." - [ "$DRY_RUN" = false ] && sleep "$CONTAINER_DELAY" + sleep "$CONTAINER_DELAY" fi - [ "$DRY_RUN" = true ] && echo "[DRY-RUN] Would start $container" - [ "$DRY_RUN" = false ] && ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container" + + [[ "$DRY_RUN" = true ]] || ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container" done } -# Main logic +#============================================================ +# Main Logic +#============================================================ + main() { echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY → $REMOTE_SERVER:$DIRECTORY" @@ -112,21 +128,41 @@ main() { wait_for_rsync stop_containers - RSYNC_OPTS=(-av --info=progress2 --human-readable --bwlimit="$BW_LIMIT" --delete --inplace --no-whole-file) + RSYNC_OPTS=( + -av + --info=progress2 + --human-readable + --bwlimit="$BW_LIMIT" + --delete + --inplace + --no-whole-file + ) + for pattern in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$pattern") done - [ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run") + + [[ "$DRY_RUN" = true ]] && RSYNC_OPTS+=(--dry-run) for attempt in $(seq 1 "$RETRY_COUNT"); do echo "Starting rsync attempt $attempt/$RETRY_COUNT..." - if rsync "${RSYNC_OPTS[@]}" -e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" "$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then + + if rsync "${RSYNC_OPTS[@]}" \ + -e "ssh -i \"$SSH_KEY\" -T -o Compression=no -o IPQoS=throughput" \ + "$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then echo "Rsync completed successfully." break else echo "Rsync attempt $attempt failed." - [ "$attempt" -lt "$RETRY_COUNT" ] && sleep "$SLEEP" - [ "$attempt" -eq "$RETRY_COUNT" ] && { start_containers; exit 1; } + + if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then + echo "Retrying in $SLEEP seconds..." + sleep "$SLEEP" + else + echo "All $RETRY_COUNT rsync attempts failed." + start_containers + exit 1 + fi fi done @@ -134,5 +170,4 @@ main() { echo "[$LOCAL_SERVER_NAME] Sync complete." } -# Execute main \ No newline at end of file