Files
dotfiles/basic/skripte/mount-buntes-monster-drives.sh
2026-10-15 10:24:18 +01:00

48 lines
983 B
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
SERVER="buntes-monster"
SERVER_IP="192.168.0.4"
MOUNTS=(
"/mnt/anbau:/mnt/anbau"
"/mnt/bilder:/mnt/bilder"
)
# Re-run as root when needed.
if [[ "${EUID}" -ne 0 ]]; then
exec sudo "$0" "$@"
fi
# Preserve ownership for the user who invoked sudo.
OWNER="${SUDO_USER:-${USER}}"
OWNER_UID="$(id -u "$OWNER")"
OWNER_GID="$(id -g "$OWNER")"
for entry in "${MOUNTS[@]}"; do
IFS=":" read -r remote_path local_path <<< "$entry"
# Create the mount point if necessary and give it to the invoking user.
install -d \
-o "$OWNER_UID" \
-g "$OWNER_GID" \
-m 0755 \
"$local_path"
# Do not remount an already-mounted location.
if mountpoint -q "$local_path"; then
echo "Already mounted: $local_path"
continue
fi
echo "Mounting $SERVER:$remote_path at $local_path..."
mount \
-t nfs \
-o rw,sync,addr="$SERVER_IP" \
"$SERVER:$remote_path" \
"$local_path"
echo "Mounted: $local_path"
done