c
This commit is contained in:
+40
-32
@@ -1,17 +1,14 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
# --------------------------- Core Rsync Script for Unraid -------------------------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#----------------- User Variables, Please adjust 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 new setup allows for profiles and arguments to use the core script
|
||||
# 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/
|
||||
#
|
||||
@@ -19,31 +16,56 @@ set -e
|
||||
# If the last name /Arrs/ matches a profile in Master.conf, then profile defaults are used
|
||||
# Otherwise, global configuration in Master.conf is used
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#---------------End Of User Variables, Please adjust in Master.conf as needed ------------------
|
||||
#---------------End Of User Variables ----------------------------------------------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
|
||||
# Detect script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/../common.sh"
|
||||
|
||||
# Load central config
|
||||
load_master_conf
|
||||
# Load Master.conf
|
||||
MASTER_CONF="$SCRIPT_DIR/../Master.conf"
|
||||
if [ -f "$MASTER_CONF" ]; then
|
||||
source "$MASTER_CONF"
|
||||
echo "Loaded Master.conf from $MASTER_CONF"
|
||||
else
|
||||
echo "Error: Master.conf not found at $MASTER_CONF"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Input Arguments
|
||||
DIRECTORY="$1"
|
||||
if [[ -z "$DIRECTORY" ]]; then
|
||||
echo "Usage: $0 <directory-to-sync> [--dry-run|-n] [VAR=value ...]"
|
||||
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
|
||||
exit 1
|
||||
fi
|
||||
shift # remove DIRECTORY
|
||||
|
||||
# Parse CLI args (flags + VAR=value)
|
||||
parse_args "$@"
|
||||
# Optional dry-run
|
||||
DRY_RUN=false
|
||||
shift 1
|
||||
if [[ "$1" == "--dry-run" ]]; then
|
||||
DRY_RUN=true
|
||||
shift 1
|
||||
fi
|
||||
|
||||
# Optional variable overrides: VAR=value
|
||||
for ARG in "$@"; do
|
||||
if [[ "$ARG" == *=* ]]; then
|
||||
VAR_NAME="${ARG%%=*}"
|
||||
VAR_VALUE="${ARG#*=}"
|
||||
if declare -p "$VAR_NAME" &>/dev/null; then
|
||||
eval "$VAR_NAME=\"$VAR_VALUE\""
|
||||
echo "Overriding $VAR_NAME -> $VAR_VALUE"
|
||||
else
|
||||
echo "Warning: Unknown variable $VAR_NAME, ignoring."
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Determine profile
|
||||
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
|
||||
echo "Detected profile: $PROFILE_NAME"
|
||||
|
||||
# Apply profile defaults or fallback to Master.conf
|
||||
# Apply profile defaults or fallback to master.conf
|
||||
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]})
|
||||
@@ -51,15 +73,9 @@ 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}
|
||||
# Allow multiple patterns per profile
|
||||
EXCLUDE_DIRS=(${PROFILE_EXCLUDE_DIRS[$PROFILE_NAME]:-${EXCLUDE_DIRS[@]}})
|
||||
|
||||
# Validate numeric variables
|
||||
validate_int MAX_RSYNC_PROCS "$MAX_RSYNC_PROCS"
|
||||
validate_int BW_LIMIT "$BW_LIMIT"
|
||||
validate_int CONTAINER_DELAY "$CONTAINER_DELAY"
|
||||
validate_int RETRY_COUNT "$RETRY_COUNT"
|
||||
validate_int SLEEP "$SLEEP"
|
||||
|
||||
echo "Settings: MAX_RSYNC_PROCS=$MAX_RSYNC_PROCS, BW_LIMIT=$BW_LIMIT, CONTAINERS=${CRITICAL_CONTAINER_NAMES[*]}, EXCLUDES=${EXCLUDE_DIRS[*]}"
|
||||
|
||||
# Dependency check
|
||||
@@ -99,11 +115,7 @@ stop_containers() {
|
||||
if [ "$STATUS" == "true" ]; then
|
||||
echo "Stopping $container"
|
||||
RUNNING_CONTAINERS+=("$container")
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would stop container $container"
|
||||
else
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
|
||||
fi
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
|
||||
fi
|
||||
done
|
||||
}
|
||||
@@ -113,13 +125,9 @@ start_containers() {
|
||||
for container in "${RUNNING_CONTAINERS[@]}"; do
|
||||
if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then
|
||||
echo "Delaying start of $container by ${CONTAINER_DELAY}s..."
|
||||
[ "$DRY_RUN" = false ] && sleep "$CONTAINER_DELAY"
|
||||
fi
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would start container $container"
|
||||
else
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
|
||||
sleep "$CONTAINER_DELAY"
|
||||
fi
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user