#!/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 ----------------- # ---------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Load configs & helpers source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" # ----------------------------- Input ----------------------------- DIRECTORY="$1" if [[ -z "$DIRECTORY" ]]; then echo "Usage: $0 [--dry-run] [VAR=value ...]" exit 1 fi shift parse_args "$@" # ----------------------------- Host & IP ----------------------------- detect_hosts resolve_remote_ip # ----------------------------- Profile ----------------------------- PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') 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]}) DELAYED_CONTAINERS=(${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]}) CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY} RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT} SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP} EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}}) # ----------------------------- Status Mode ----------------------------- if [[ "$SHOW_STATUS" == true ]]; then show_status exit 0 fi # ----------------------------- User Info ----------------------------- echo "============================================================" echo " Sync Job Starting" echo "------------------------------------------------------------" echo " Source: $DIRECTORY" echo " Destination: $REMOTE_SERVER:$DIRECTORY" echo " Profile: $PROFILE_NAME" echo " Dry Run: $DRY_RUN" echo "============================================================" log "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT" log "Containers: ${CRITICAL_CONTAINER_NAMES[*]}" log "Excludes: ${EXCLUDE_DIRS[*]}" # ----------------------------- Rsync Wait ----------------------------- 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 echo "Waiting for available rsync slot... ($RSYNC_COUNT running)" log "RSYNC_COUNT=$RSYNC_COUNT >= MAX=$MAX_RSYNC_PROCS" sleep "$SLEEP" else return 0 fi done echo "Too many rsync processes, exiting." exit 1 } # ----------------------------- Main ----------------------------- main() { echo "" echo "🔍 Checking remote connectivity..." for i in $(seq 1 "$RETRY_COUNT"); do if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then echo "✅ Remote server reachable" break fi echo "⚠️ Remote down, retrying in $SLEEP seconds..." sleep "$SLEEP" done if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then echo "❌ Remote server unreachable. Exiting." exit 1 fi wait_for_rsync echo "" echo "🛑 Stopping containers on remote..." stop_containers echo "" echo "📦 Building rsync options..." get_rsync_opts for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex") done [ "$DRY_RUN" = true ] && RSYNC_OPTS+=("--dry-run") log "Final rsync opts: ${RSYNC_OPTS[*]}" echo "" echo "🚀 Starting rsync..." for attempt in $(seq 1 "$RETRY_COUNT"); do echo "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 echo "✅ Rsync completed successfully" break else echo "❌ Rsync failed" if [ "$attempt" -lt "$RETRY_COUNT" ]; then echo "Retrying in $SLEEP seconds..." sleep "$SLEEP" else echo "💥 All retries failed" start_containers exit 1 fi fi done echo "" echo "🔄 Restarting containers..." start_containers echo "" echo "🎉 Sync complete!" } main