#!/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)" # Core System source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # Input if [[ -z "$DIRECTORY" ]]; then echo "Usage: $0 [--dry-run] [--status] [VAR=value ...]" exit 1 fi shift # Host detect_hosts resolve_remote # Profile PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]') log "โ„น๏ธ Detected profile: $PROFILE_NAME" # Safe array normalization normalize_list() { local input="$1" [[ -z "$input" ]] && return read -r -a __out <<< "$input" echo "${__out[@]}" } CRITICAL_CONTAINER_NAMES=($(normalize_list "${PROFILE_CRITICAL_CONTAINER_NAMES[$PROFILE_NAME]}")) DELAYED_CONTAINERS=($(normalize_list "${PROFILE_DELAYED_CONTAINERS[$PROFILE_NAME]}")) EXCLUDE_DIRS=($(normalize_list "${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]}")) # Profile overrides MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS} BW_LIMIT=${PROFILE_BW_LIMIT[$PROFILE_NAME]:-$BW_LIMIT} RETRY_COUNT=${PROFILE_RETRY_COUNT[$PROFILE_NAME]:-$RETRY_COUNT} SLEEP=${PROFILE_SLEEP[$PROFILE_NAME]:-$SLEEP} CONTAINER_DELAY=${PROFILE_CONTAINER_DELAY[$PROFILE_NAME]:-$CONTAINER_DELAY} # Status Mode if [[ "$SHOW_STATUS" == true ]]; then echo "==================================================" echo " โ„น๏ธ RSYNC STATUS" echo "--------------------------------------------------" echo " ๐Ÿ“ Source: $DIRECTORY" echo " ๐ŸŒ Remote: $REMOTE_SERVER" echo " ๐Ÿงฉ Profile: $PROFILE_NAME" echo " ๐Ÿšฆ BW_LIMIT: $BW_LIMIT" echo " ๐Ÿ” Max Procs: $MAX_RSYNC_PROCS" echo " ๐Ÿ” Retries: $RETRY_COUNT" echo " ๐Ÿงช Dry Run: $DRY_RUN" echo " ๐Ÿ“ฆ Containers: ${CRITICAL_CONTAINER_NAMES[*]}" echo " ๐Ÿšซ Excludes: ${EXCLUDE_DIRS[*]}" echo "==================================================" exit 0 fi # Header echo echo "============================================================" echo " ๐Ÿš€ SYNC STARTING" echo "------------------------------------------------------------" echo " ๐Ÿ“ Source: $DIRECTORY" echo " ๐ŸŒ Destination: $REMOTE_SERVER:$DIRECTORY" echo " ๐Ÿงฉ Profile: $PROFILE_NAME" echo " ๐Ÿงช Dry Run: $DRY_RUN" echo "============================================================" log "โ„น๏ธ MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS BW_LIMIT=$BW_LIMIT" log "โ„น๏ธ Containers: ${CRITICAL_CONTAINER_NAMES[*]}" log "โ„น๏ธ Excludes: ${EXCLUDE_DIRS[*]}" # RSYNC SLOT CONTROL 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 warn "๐ŸŸก Waiting for rsync slot ($RSYNC_COUNT active)" sleep "$SLEEP" else return 0 fi done error "๐Ÿ”ด Too many rsync processes running" } # MAIN main() { info "โ„น๏ธ Checking remote connectivity..." for i in $(seq 1 "$RETRY_COUNT"); do if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then info "๐ŸŸข Remote reachable" break fi warn "๐ŸŸก Remote unreachable (attempt $i/$RETRY_COUNT)" sleep "$SLEEP" done if ! ping -c 1 "$REMOTE_SERVER" &>/dev/null; then error "๐Ÿ”ด Remote server unreachable" fi wait_for_rsync info "๐ŸŸก Stopping containers on remote..." stop_containers # Build RSYNC OPTIONS get_rsync_opts for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex") done if [[ "$DRY_RUN" == true ]]; then RSYNC_OPTS+=(--dry-run) info "๐Ÿงช Dry-run enabled" fi log "โ„น๏ธ Final rsync opts: ${RSYNC_OPTS[*]}" # EXECUTION info "๐Ÿš€ Starting rsync..." START_TIME=$(date +%s) for attempt in $(seq 1 "$RETRY_COUNT"); do info "โ„น๏ธ 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 info "๐ŸŸข Rsync completed successfully" break else warn "โŒ Rsync failed" if [[ "$attempt" -lt "$RETRY_COUNT" ]]; then warn "๐ŸŸก Retrying in $SLEEP seconds..." sleep "$SLEEP" else error "๐Ÿ”ด All rsync attempts failed" start_containers fi fi done # FINALIZE info "๐ŸŸก Restarting containers..." start_containers END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) # SUMMARY echo "============================================================" if [[ "${PIPESTATUS[0]}" -eq 0 ]]; then echo " ๐ŸŸข SYNC SUMMARY" echo "------------------------------------------------------------" echo " ๐Ÿ“ Directory: $DIRECTORY" echo " ๐Ÿงฉ Profile: $PROFILE_NAME" echo " ๐ŸŒ Remote: $REMOTE_SERVER" echo " โฑ Duration: ${DURATION}s" echo " ๐Ÿ“ก Status: ๐ŸŸข SUCCESS" echo " ๐Ÿงช Dry Run: $DRY_RUN" else echo " ๐Ÿ”ด SYNC SUMMARY" echo "------------------------------------------------------------" echo " ๐Ÿ“ Directory: $DIRECTORY" echo " ๐Ÿงฉ Profile: $PROFILE_NAME" echo " ๐ŸŒ Remote: $REMOTE_SERVER" echo " โฑ Duration: ${DURATION}s" echo " ๐Ÿ“ก Status: ๐Ÿ”ด FAILED" echo " ๐Ÿงช Dry Run: $DRY_RUN" fi echo "============================================================" info "โ„น๏ธ Sync complete" } main