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

127 lines
3.4 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 -----------------
# ----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
# ----------------------------- INPUT -----------------------------
DIRECTORY=""
RAW_ARGS=()
for ARG in "$@"; do
case "$ARG" in
--*|*=*) RAW_ARGS+=("$ARG") ;;
*)
if [[ -z "$DIRECTORY" ]]; then
DIRECTORY="$ARG"
else
RAW_ARGS+=("$ARG")
fi
;;
esac
done
parse_args "${RAW_ARGS[@]}"
[[ -z "$DIRECTORY" ]] && error "Missing directory" && exit 1
# ----------------------------- CORE -----------------------------
detect_hosts
resolve_remote_ip
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
MAX_RSYNC_PROCS=${PROFILE_MAX_PROCS[$PROFILE_NAME]:-$MAX_RSYNC_PROCS}
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 -----------------------------
if [[ "${SHOW_STATUS:-false}" == true ]]; then
show_status
exit 0
fi
# ----------------------------- FLOW -----------------------------
info "Sync Starting"
info "Source: $DIRECTORY"
info "Remote: $REMOTE_SERVER:$DIRECTORY"
info "Profile: $PROFILE_NAME"
info "Checking remote..."
REMOTE_OK=false
for i in $(seq 1 "$RETRY_COUNT"); do
if ping -c 1 "$REMOTE_SERVER" &>/dev/null; then
success "Remote reachable"
REMOTE_OK=true
break
fi
warn "Retry $i/$RETRY_COUNT"
sleep "$SLEEP"
done
[[ "$REMOTE_OK" != true ]] && error "Remote unreachable" && exit 1
wait_for_rsync
stop_containers
get_rsync_opts
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
[[ "$DRY_RUN" == true ]] && RSYNC_OPTS+=("--dry-run")
RSYNC_CMD=(
rsync
"${RSYNC_OPTS[@]}"
-e "ssh -i $SSH_KEY -T -o Compression=no -o IPQoS=throughput"
"$DIRECTORY"
"root@${REMOTE_SERVER}:$DIRECTORY"
)
START=$(date +%s)
for i in $(seq 1 "$RETRY_COUNT"); do
if "${RSYNC_CMD[@]}"; then
success "Rsync complete"
break
else
warn "Rsync failed ($i/$RETRY_COUNT)"
sleep "$SLEEP"
fi
done
start_containers
END=$(date +%s)
info "Directory: $DIRECTORY"
info "Profile: $PROFILE_NAME"
info "Duration: $((END-START))s"
info "Status: DONE"