diff --git a/mount_nas.sh b/mount_nas.sh new file mode 100755 index 0000000..cec4684 --- /dev/null +++ b/mount_nas.sh @@ -0,0 +1,51 @@ +#!/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