#!/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)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ---------------------------- # INPUT HANDLING (FIXED) # ---------------------------- DIRECTORY="" for ARG in "$@"; do case "$ARG" in --*|*=*) # skip flags and key=value ;; *) if [[ -z "$DIRECTORY" ]]; then DIRECTORY="$ARG" fi ;; esac done if [[ -z "$DIRECTORY" ]]; then error "Missing directory" echo "Usage: $0 [--dry-run] [--status] [VAR=value]" exit 1 fi # ----------------------------- HOST ----------------------------- 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 # ----------------------------- HEADER ----------------------------- echo echo "$ICON_RUN Sync Starting" echo "--------------------------------------------" echo "Source: $DIRECTORY" echo "Destination: $REMOTE_SERVER:$DIRECTORY" echo "Profile: $PROFILE_NAME" echo "Dry Run: $DRY_RUN" echo "--------------------------------------------" # ----------------------------- RSYNC LIMIT ----------------------------- wait_for_rsync() { for i 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)" sleep "$SLEEP" else return 0 fi done error "Too many rsync processes" } # ----------------------------- SUMMARY ----------------------------- print_summary() { echo echo "================ SUMMARY ================" echo "Directory: $DIRECTORY" echo "Profile: $PROFILE_NAME" echo "Remote: $REMOTE_SERVER" echo "Duration: ${DURATION:-unknown}s" echo "Status: $1" echo "Dry Run: $DRY_RUN" echo "========================================" } # ----------------------------- MAIN ----------------------------- main() { echo "$ICON_GEAR Checking remote..." for i in $(seq 1 "$RETRY_COUNT"); do if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then success "Remote reachable" break fi warn "Retrying ($i/$RETRY_COUNT)" sleep "$SLEEP" done [[ ! $(ping -c 1 "$REMOTE_SERVER" &>/dev/null) ]] && error "Remote unreachable" wait_for_rsync echo "$ICON_STOP Stopping containers..." stop_containers echo "$ICON_GEAR Building rsync opts..." get_rsync_opts for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex") done [[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run") echo "$ICON_RUN Running rsync..." START=$(date +%s) for i in $(seq 1 "$RETRY_COUNT"); do if rsync "${RSYNC_OPTS[@]}" \ -e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput" \ "$DIRECTORY" "root@${REMOTE_SERVER}:$DIRECTORY"; then success "Rsync complete" break else warn "Rsync failed" [[ $i -lt $RETRY_COUNT ]] && sleep "$SLEEP" fi done echo "$ICON_SYNC Restarting containers..." start_containers END=$(date +%s) DURATION=$((END - START)) print_summary "SUCCESS" } main