bethaus-app/check_ssh_tunnel.sh
2025-03-16 21:04:43 +00:00

63 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# SSH and NFS Tunnel Configuration
SSH_USER="root"
SSH_SERVER="bethaus-speyer.de"
SSH_SERVER_PORT=1122 # Remote SSH server SSH port
LOCAL_PORT=2022 # Local port for NFS tunnel
REMOTE_NFS_PORT=2049 # Remote NFS server port
MOUNT_POINT="/mnt/app.bethaus/Gottesdienste Speyer"
NFS_SHARE="/volume1/Aufnahme-stereo/010 Gottesdienste ARCHIV"
# Function to check if the SSH tunnel is active
is_tunnel_active() {
timeout 1 bash -c "</dev/tcp/localhost/${LOCAL_PORT}" &>/dev/null
}
# Function to check if the NFS share is mounted
is_nfs_mounted() {
mount | grep -q "${MOUNT_POINT}"
}
# Restart the SSH tunnel if it's not running
if ! is_tunnel_active; then
echo "[INFO] SSH Tunnel is down. Attempting to reconnect..."
ssh -f -N -L "${LOCAL_PORT}:localhost:${REMOTE_NFS_PORT}" \
-o ExitOnForwardFailure=yes \
-p "${SSH_SERVER_PORT}" \
"${SSH_USER}@${SSH_SERVER}"
if is_tunnel_active; then
echo "[SUCCESS] SSH Tunnel established on local port ${LOCAL_PORT}, forwarding to remote NFS port ${REMOTE_NFS_PORT}."
else
echo "[ERROR] Failed to establish SSH tunnel!"
exit 1
fi
else
echo "[INFO] SSH Tunnel is already active on port ${LOCAL_PORT}."
fi
# Ensure mount point exists
if [ ! -d "${MOUNT_POINT}" ]; then
echo "[INFO] Creating mount point: ${MOUNT_POINT}"
sudo mkdir -p "${MOUNT_POINT}"
fi
# Mount the NFS share if it's not already mounted
if ! is_nfs_mounted; then
echo "[INFO] NFS is not mounted. Attempting to mount..."
sudo mount -t nfs -o port=${LOCAL_PORT},nolock,soft 127.0.0.1:"${NFS_SHARE}" "${MOUNT_POINT}"
if is_nfs_mounted; then
echo "[SUCCESS] NFS mounted successfully at ${MOUNT_POINT}."
else
echo "[ERROR] Failed to mount NFS share!"
exit 1
fi
else
echo "[INFO] NFS is already mounted at ${MOUNT_POINT}."
fi