Restructured to add common functions and added a server reboot script

This commit is contained in:
2026-03-23 22:42:35 +00:00
parent 7db77e6445
commit 700614d4bf
3 changed files with 183 additions and 38 deletions
+28 -38
View File
@@ -8,64 +8,40 @@ set -e
# 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
# 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 /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
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../common.sh"
# 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
# Load central config
load_master_conf
# Input Arguments
DIRECTORY="$1"
if [[ -z "$DIRECTORY" ]]; then
echo "Usage: $0 <directory-to-sync> [--dry-run] [VAR=value ...]"
echo "Usage: $0 <directory-to-sync> [--dry-run|-n] [VAR=value ...]"
exit 1
fi
shift # remove DIRECTORY
# 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
# Parse CLI args (flags + VAR=value)
parse_args "$@"
# 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]})
@@ -73,9 +49,15 @@ 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
@@ -115,7 +97,11 @@ stop_containers() {
if [ "$STATUS" == "true" ]; then
echo "Stopping $container"
RUNNING_CONTAINERS+=("$container")
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $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
fi
done
}
@@ -125,9 +111,13 @@ start_containers() {
for container in "${RUNNING_CONTAINERS[@]}"; do
if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then
echo "Delaying start of $container by ${CONTAINER_DELAY}s..."
sleep "$CONTAINER_DELAY"
[ "$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"
fi
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
done
}