useful-scripts/mount_nas.sh
2024-09-13 14:46:10 +01:00

51 lines
1.1 KiB
Bash
Executable file

#!/bin/bash
# Runs script in local directory to pick up .env file
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
# Import variables
set -a
source .env
set +a
SERVER_IP=friday
declare -A MOUNT_POINTS=(
["photos"]="/media/nas/photos"
["cameraupload"]="/media/nas/cameraupload"
)
mount_share() {
local share=$1
local mount_point=$2
# Create directory if it doesn't exist
if [ ! -d "$mount_point" ]; then
mkdir -p "$mount_point"
fi
if ! mountpoint -q "$mount_point"; then
mount -t cifs -o username=${USERNAME},password=${PASSWORD},uid=1000 "//${SERVER_IP}/${share}" "$mount_point"
fi
}
unmount_share() {
local mount_point=$1
if mountpoint -q "$mount_point"; then
umount -l "$mount_point"
fi
}
ping -c1 -W2 "$SERVER_IP" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "Server is up. Mounting folders..."
for share in "${!MOUNT_POINTS[@]}"; do
mount_share "$share" "${MOUNT_POINTS[$share]}"
done
else
echo "Server is down. Unmounting folders..."
for mount_point in "${MOUNT_POINTS[@]}"; do
unmount_share "$mount_point"
done
fi