added common.sh and made the split from master.conf
This commit is contained in:
@@ -1,41 +1,11 @@
|
||||
#!/bin/bash
|
||||
# ----------------------------- Master Variables for unRAID scripts -----------------------------
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
#----------------------------- Common helpers for unRAID scripts -------------------------------
|
||||
# Requires Master.conf to be sourced first
|
||||
#-----------------------------------------------------------------------------------------------
|
||||
|
||||
# Hosts
|
||||
HOST1="unRAID-Gmer4Lfe"
|
||||
HOST2="unRAID-Jayred365"
|
||||
HOST1_SSH_KEY="/root/.ssh/Gmer4Lfe-rsync-key"
|
||||
HOST2_SSH_KEY="/root/.ssh/Jayred365-rsync-key"
|
||||
|
||||
# Rsync Defaults
|
||||
BW_LIMIT=12500
|
||||
RETRY_COUNT=3
|
||||
SLEEP=300
|
||||
MAX_RSYNC_PROCS=1
|
||||
CRITICAL_CONTAINER_NAMES=("")
|
||||
DELAYED_CONTAINERS=("")
|
||||
CONTAINER_DELAY=5
|
||||
EXCLUDE_DIRS=("")
|
||||
|
||||
# Profiles
|
||||
declare -A PROFILE_MAX_PROCS=([arrs_stack]=2 [critical-data]=2 [gmer4lfe]=1 [important-data]=2 [emby]=1)
|
||||
declare -A PROFILE_BW_LIMIT=([arrs_stack]=5000 [critical-data]=9500 [gmer4lfe]=8000 [important-data]=9500 [emby]=8000)
|
||||
declare -A PROFILE_RETRY_COUNT=([arrs_stack]=3 [critical-data]=3 [gmer4lfe]=3 [important-data]=3 [emby]=3)
|
||||
declare -A PROFILE_SLEEP=([arrs_stack]=300 [critical-data]=300 [gmer4lfe]=300 [important-data]=300 [emby]=300)
|
||||
declare -A PROFILE_CRITICAL_CONTAINER_NAMES=(
|
||||
[arrs_stack]="Sonarr Lidarr Readarr Radarr Prowlarr Bazarr Pinchflat"
|
||||
[critical-data]="Mariadb-Authelia Redis-Authelia Lldap-Gmer4Lfe NginxProxyManager Authelia"
|
||||
[gmer4lfe]="Organizrv2-Gmer4Lfe UptimeKuma-Gmer4Lfe VaultWarden-Gmer4Lfe"
|
||||
[important-data]="Postgres-NextCloud NextCloud"
|
||||
[emby]=""
|
||||
)
|
||||
declare -A PROFILE_DELAYED_CONTAINERS=([arrs_stack]="" [critical-data]="Authelia" [gmer4lfe]="" [important-data]="NextCloud" [emby]="")
|
||||
declare -A PROFILE_CONTAINER_DELAY=([arrs_stack]=5 [critical-data]=10 [gmer4lfe]=5 [important-data]=10 [emby]=5)
|
||||
declare -A PROFILE_EXCLUDE_DIRS=([arrs_stack]="logs *.tmp" [critical-data]="logs *.tmp" [gmer4lfe]="logs *.tmp" [important-data]="logs *.tmp" [emby]="logs *.tmp")
|
||||
|
||||
# ----------------------------- Common Helpers -----------------------------
|
||||
# ----------------------------- Helpers -----------------------------
|
||||
parse_args() {
|
||||
DRY_RUN=${DRY_RUN:-false}
|
||||
for ARG in "$@"; do
|
||||
if [[ "$ARG" == *=* ]]; then
|
||||
local VAR_NAME="${ARG%%=*}"
|
||||
@@ -74,7 +44,6 @@ validate_int() {
|
||||
}
|
||||
|
||||
# ----------------------------- Host Detection & SSH -----------------------------
|
||||
# Detect local/remote host
|
||||
LOCAL_HOSTNAME="$(hostname)"
|
||||
if [[ "$LOCAL_HOSTNAME" == "$HOST1" ]]; then
|
||||
LOCAL_SERVER_NAME="$HOST1"
|
||||
@@ -86,10 +55,8 @@ else
|
||||
echo "Error: Local hostname ($LOCAL_HOSTNAME) not recognized."
|
||||
exit 1
|
||||
fi
|
||||
echo "Local server: $LOCAL_SERVER_NAME"
|
||||
echo "Remote server: $REMOTE_SERVER_NAME"
|
||||
|
||||
# SSH Key Mapping
|
||||
# SSH key mapping
|
||||
declare -A SSH_KEYS
|
||||
SSH_KEYS["$HOST1|$HOST2"]="$HOST1_SSH_KEY"
|
||||
SSH_KEYS["$HOST2|$HOST1"]="$HOST2_SSH_KEY"
|
||||
@@ -107,4 +74,50 @@ if [[ -z "$REMOTE_SERVER" ]]; then
|
||||
echo "Error: Could not resolve Tailscale IP for $REMOTE_SERVER_NAME"
|
||||
exit 1
|
||||
fi
|
||||
echo "Remote IP: $REMOTE_SERVER"
|
||||
echo "Remote IP: $REMOTE_SERVER"
|
||||
|
||||
# ----------------------------- Container Helpers -----------------------------
|
||||
declare -a RUNNING_CONTAINERS=()
|
||||
|
||||
stop_containers() {
|
||||
echo "[$LOCAL_SERVER_NAME] Checking containers on ${REMOTE_SERVER_NAME}..."
|
||||
RUNNING_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
|
||||
echo "Stopping $container"
|
||||
RUNNING_CONTAINERS+=("$container")
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker stop $container"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
start_containers() {
|
||||
echo "[$LOCAL_SERVER_NAME] Restarting containers on ${REMOTE_SERVER_NAME}..."
|
||||
for container in "${RUNNING_CONTAINERS[@]}"; do
|
||||
if [[ " ${DELAYED_CONTAINERS[*]} " == *" $container "* ]]; then
|
||||
echo "Delaying start of $container by ${CONTAINER_DELAY}s..."
|
||||
sleep "$CONTAINER_DELAY"
|
||||
fi
|
||||
ssh -i "$SSH_KEY" root@"$REMOTE_SERVER" "docker start $container"
|
||||
done
|
||||
}
|
||||
|
||||
wait_for_rsync() {
|
||||
for attempt in $(seq 1 "$RETRY_COUNT"); do
|
||||
RSYNC_COUNT=$(pgrep -fc "rsync.*root@${REMOTE_SERVER}" || true)
|
||||
if [ "$RSYNC_COUNT" -ge "$MAX_RSYNC_PROCS" ]; then
|
||||
echo "[$LOCAL_SERVER_NAME] Waiting... ($RSYNC_COUNT active rsync processes)"
|
||||
sleep "$SLEEP"
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
echo "Too many rsync processes, exiting."
|
||||
exit 1
|
||||
}
|
||||
|
||||
check_server_online() {
|
||||
timeout 5 ping -c 1 "$REMOTE_SERVER" &>/dev/null
|
||||
}
|
||||
Reference in New Issue
Block a user