diff --git a/basic/skripte/mount-buntes-monster-drives.sh b/basic/skripte/mount-buntes-monster-drives.sh new file mode 100755 index 0000000..ae884cc --- /dev/null +++ b/basic/skripte/mount-buntes-monster-drives.sh @@ -0,0 +1,47 @@ +#!/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