master with all helpers

This commit is contained in:
2026-03-24 22:43:43 +00:00
parent 56548f409e
commit 1d806711c9
3 changed files with 171 additions and 188 deletions
+22 -49
View File
@@ -1,29 +1,12 @@
#!/bin/bash
set -e
#-----------------------------------------------------------------------------------------------
#----------------- 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 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
#-----------------------------------------------------------------------------------------------
#---------------End Of User Variables ----------------------------------------------------------
#-----------------------------------------------------------------------------------------------
# Detect script directory
#---------------------------------------------
# Detect script directory and load Master.conf
#---------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Load Master.conf
MASTER_CONF="$SCRIPT_DIR/../Master.conf"
if [ -f "$MASTER_CONF" ]; then
source "$MASTER_CONF"
echo "Loaded Master.conf from $MASTER_CONF"
@@ -32,40 +15,23 @@ else
exit 1
fi
# Input Arguments
DIRECTORY="$1" # Directory to sync
#---------------------------------------------
# Parse arguments (using Master.conf helper)
#---------------------------------------------
parse_args "$@"
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
exit 1
fi
# 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
#---------------------------------------------
# Determine profile & apply defaults
#---------------------------------------------
PROFILE_NAME=$(basename "$DIRECTORY" | tr '[:upper:]' '[:lower:]')
echo "Detected profile: $PROFILE_NAME"
# 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]})
@@ -77,7 +43,9 @@ 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[*]}"
#---------------------------------------------
# Dependency check
#---------------------------------------------
for cmd in rsync ssh ping timeout docker tailscale; do
command -v "$cmd" >/dev/null 2>&1 || {
echo "Error: Required command '$cmd' is not installed."
@@ -87,7 +55,9 @@ done
trap 'echo "Script interrupted. Exiting."; exit 130' INT TERM
#---------------------------------------------
# Helper functions
#---------------------------------------------
wait_for_rsync() {
for attempt in $(seq 1 "$RETRY_COUNT"); do
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
@@ -114,7 +84,7 @@ stop_containers() {
for container in "${CRITICAL_CONTAINER_NAMES[@]}"; do
STATUS=$(ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" \
"docker inspect -f '{{.State.Running}}' $container 2>/dev/null" || echo "false")
if [ "$STATUS" == "true" ]; then
if [[ "$STATUS" == "true" ]]; then
echo "Stopping $container"
RUNNING_CONTAINERS+=("$container")
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
@@ -133,7 +103,9 @@ start_containers() {
done
}
#---------------------------------------------
# Main sync logic
#---------------------------------------------
main() {
echo "[$LOCAL_SERVER_NAME] Syncing $DIRECTORY$REMOTE_SERVER:$DIRECTORY"
@@ -160,7 +132,6 @@ main() {
--no-whole-file
)
# Apply profile-specific excludes
for ex in "${EXCLUDE_DIRS[@]}"; do
RSYNC_OPTS+=(--exclude="$ex")
done
@@ -191,5 +162,7 @@ main() {
echo "[$LOCAL_SERVER_NAME] Sync complete."
}
#---------------------------------------------
# Execute
#---------------------------------------------
main