Restructured to add common functions and added a server reboot script
This commit is contained in:
+28
-38
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
# Common Shared Functions for Unraid Scripts
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
|
||||
# Load Master.conf relative to calling script
|
||||
load_master_conf() {
|
||||
local script_dir
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[1]}")" && pwd)"
|
||||
|
||||
local 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
|
||||
}
|
||||
|
||||
# Parse CLI arguments (flags + VAR=value overrides)
|
||||
parse_args() {
|
||||
DRY_RUN=${DRY_RUN:-false}
|
||||
|
||||
for ARG in "$@"; do
|
||||
if [[ "$ARG" == *=* ]]; then
|
||||
local VAR_NAME="${ARG%%=*}"
|
||||
local VAR_VALUE="${ARG#*=}"
|
||||
|
||||
if declare -p "$VAR_NAME" &>/dev/null; then
|
||||
if [[ "$VAR_NAME" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
||||
printf -v "$VAR_NAME" '%s' "$VAR_VALUE"
|
||||
echo "Overriding $VAR_NAME -> $VAR_VALUE"
|
||||
else
|
||||
echo "Warning: Invalid variable name $VAR_NAME"
|
||||
fi
|
||||
else
|
||||
echo "Warning: Unknown variable $VAR_NAME, ignoring."
|
||||
fi
|
||||
else
|
||||
case "$ARG" in
|
||||
--dry-run|-n)
|
||||
DRY_RUN=true
|
||||
;;
|
||||
--help|-h)
|
||||
echo "Usage: script [--dry-run|-n] [VAR=value ...]"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Warning: Unknown argument $ARG"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Validate integer (non-negative)
|
||||
validate_int() {
|
||||
local name="$1"
|
||||
local value="$2"
|
||||
|
||||
if ! [[ "${value:-}" =~ ^[0-9]+$ ]]; then
|
||||
echo "Error: $name must be a non-negative integer."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Optional: require variable to be set
|
||||
require_var() {
|
||||
local var="$1"
|
||||
if [[ -z "${!var:-}" ]]; then
|
||||
echo "Error: Required variable $var is not set."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#----------------- User Variables, Please adjust in Master.conf as needed ----------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
# This is the Unattended Reboot Script for Unraid
|
||||
#
|
||||
# User variables can be adjusted in Master.conf
|
||||
# This script is using the SLEEP Variable from Master.conf for the delay before reboot
|
||||
#
|
||||
# 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 ----------------------------------------------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/../common.sh"
|
||||
|
||||
# Load central config
|
||||
load_master_conf
|
||||
|
||||
# Parse CLI args (flags + VAR=value)
|
||||
parse_args "$@"
|
||||
|
||||
# Validate SLEEP
|
||||
validate_int SLEEP "$SLEEP"
|
||||
|
||||
# --- Functions
|
||||
|
||||
notify_users() {
|
||||
wall "⚠️ Unraid server will reboot in ${SLEEP} second(s). Save your work."
|
||||
}
|
||||
|
||||
# --- MAIN
|
||||
|
||||
echo "==== Scheduled Reboot Script Started: $(date) ===="
|
||||
|
||||
# Optional warning before reboot
|
||||
if [ "$SLEEP" -gt 0 ]; then
|
||||
notify_users
|
||||
sleep "$SLEEP"
|
||||
fi
|
||||
|
||||
# Stop Docker
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would stop Docker: /etc/rc.d/rc.docker stop"
|
||||
else
|
||||
echo "Stopping Docker..."
|
||||
/etc/rc.d/rc.docker stop
|
||||
fi
|
||||
|
||||
# Stop VM Manager
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would stop VM Manager: /etc/rc.d/rc.libvirt stop"
|
||||
else
|
||||
echo "Stopping VM Manager..."
|
||||
/etc/rc.d/rc.libvirt stop
|
||||
fi
|
||||
|
||||
# Sync disks
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would sync disks"
|
||||
else
|
||||
echo "Syncing disks..."
|
||||
sync
|
||||
fi
|
||||
|
||||
# Reboot system
|
||||
if [ "$DRY_RUN" = true ]; then
|
||||
echo "[DRY-RUN] Would reboot system: /sbin/reboot"
|
||||
else
|
||||
echo "Rebooting system..."
|
||||
/sbin/reboot
|
||||
fi
|
||||
Reference in New Issue
Block a user