Files
Varaverk/Rsync/rsync.sh
T

96 lines
3.7 KiB
Bash

#!/bin/bash
set -e
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust here in Master.conf as needed -----------------
#-----------------------------------------------------------------------------------------------
## This is the main Core Rsync script
#
# User variables can be adjusted in Master.conf
# Scripts now just contain runtime logic
#
# This allows for profiles and multiple things 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
#-----------------------------------------------------------------------------------------------
#----------------- User Variables, Please adjust here in Master.conf as needed -----------------
#-----------------------------------------------------------------------------------------------
#
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load config & helpers
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
# Input directory
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1
fi
shift 1
parse_args "$@"
# Profile detection
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
echo "Detected profile: $PROFILE_NAME"
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[@]}})
echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}"
# Main sync logic
main() {
echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
for i in $(seq 1 "$RETRY_COUNT"); do
if check_server_online; then break; fi
echo "Remote $REMOTE_SERVER_NAME down, retrying in $SLEEP seconds..."
sleep "$SLEEP"
done
check_server_online || { echo "Remote $REMOTE_SERVER_NAME unreachable. Exiting."; exit 1; }
wait_for_rsync
stop_containers
RSYNC_OPTS=(-av --info=progress2 --human-readable --bwlimit="$BW_LIMIT" --delete --inplace --no-whole-file)
for ex in "${EXCLUDE_DIRS[@]}"; do RSYNC_OPTS+=(--exclude="$ex"); done
[ "$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
echo "Rsync completed successfully."
break
else
echo "Rsync attempt $attempt failed."
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
start_containers
echo "[$LOCAL_SERVER_NAME] Sync complete."
}
main