From e08258d65db677c92c2175b29b497f5ebb9c555d Mon Sep 17 00:00:00 2001 From: Bram Kelchtermans Date: Thu, 18 Jul 2024 21:12:27 +0200 Subject: [PATCH] refactor: Add script to mount remote directories --- gui/mounts/mount.sh | 102 +++++++++++++++++++++++++++++++++++++++++ gui/mounts/mounts.json | 70 ++++++++++++++++++++++++++++ gui/setup.sh | 5 +- 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100755 gui/mounts/mount.sh create mode 100644 gui/mounts/mounts.json diff --git a/gui/mounts/mount.sh b/gui/mounts/mount.sh new file mode 100755 index 00000000..8481d23c --- /dev/null +++ b/gui/mounts/mount.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +# Ensure jq is installed +if ! command -v jq &> /dev/null; then + echo "jq could not be found, please install it first." + exit 1 +fi + +# Ensure sshfs is installed +if ! command -v sshfs &> /dev/null; then + echo "sshfs could not be found, please install it first." + exit 1 +fi + +# Ensure cifs-utils is installed for SMB mounts +if ! command -v mount.cifs &> /dev/null; then + echo "cifs-utils could not be found, please install it first." + exit 1 +fi + +# Check if the correct number of arguments are provided +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +JSON_FILE=$1 + +# Check if the JSON file exists +if [ ! -f "$JSON_FILE" ]; then + echo "JSON file $JSON_FILE not found!" + exit 1 +fi + +# Read and process each mount entry from the JSON file +jq -c '.[]' "$JSON_FILE" | while read -r entry; do + PROTOCOL=$(echo "$entry" | jq -r '.protocol') + URL=$(echo "$entry" | jq -r '.url') + REMOTE_PATH=$(echo "$entry" | jq -r '.remote_path') + LOCAL_PATH=$(echo "$entry" | jq -r '.local_path') + + # Expand ~ to the home directory if it is used + LOCAL_PATH=$(eval echo "$LOCAL_PATH") + + # Create local path if it doesn't exist + if [ ! -d "$LOCAL_PATH" ]; then + mkdir -p "$LOCAL_PATH" + fi + + if [ "$PROTOCOL" == "ssh" ]; then + # Mount the remote path to the local path using sshfs + SSHFS_CMD="sshfs -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 $URL:$REMOTE_PATH $LOCAL_PATH" + + # Execute the sshfs command to mount + $SSHFS_CMD + + # Check if the mount was successful + if [ $? -eq 0 ]; then + echo "Successfully mounted $URL:$REMOTE_PATH to $LOCAL_PATH" + + # Add the mount entry to /etc/fstab for persistence + FSTAB_ENTRY="$URL:$REMOTE_PATH $LOCAL_PATH fuse.sshfs defaults,_netdev,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 0 0" + if ! grep -qF "$FSTAB_ENTRY" /etc/fstab; then + echo "$FSTAB_ENTRY" | sudo tee -a /etc/fstab > /dev/null + echo "Added $FSTAB_ENTRY to /etc/fstab" + else + echo "$FSTAB_ENTRY already exists in /etc/fstab" + fi + else + echo "Failed to mount $URL:$REMOTE_PATH to $LOCAL_PATH" + fi + + elif [ "$PROTOCOL" == "smb" ]; then + USERNAME=$(echo "$entry" | jq -r '.username') + PASSWORD=$(echo "$entry" | jq -r '.password') + + # Mount the remote path to the local path using CIFS + SMB_CMD="sudo mount.cifs $URL $LOCAL_PATH -o username=$USERNAME,password=$PASSWORD,uid=$(id -u),gid=$(id -g)" + + # Execute the SMB command to mount + $SMB_CMD + + # Check if the mount was successful + if [ $? -eq 0 ]; then + echo "Successfully mounted $URL to $LOCAL_PATH" + + # Add the mount entry to /etc/fstab for persistence + FSTAB_ENTRY="$URL $LOCAL_PATH cifs username=$USERNAME,password=$PASSWORD,uid=$(id -u),gid=$(id -g) 0 0" + if ! grep -qF "$FSTAB_ENTRY" /etc/fstab; then + echo "$FSTAB_ENTRY" | sudo tee -a /etc/fstab > /dev/null + echo "Added $FSTAB_ENTRY to /etc/fstab" + else + echo "$FSTAB_ENTRY already exists in /etc/fstab" + fi + else + echo "Failed to mount $URL to $LOCAL_PATH" + fi + + else + echo "Unsupported protocol: $PROTOCOL" + fi +done diff --git a/gui/mounts/mounts.json b/gui/mounts/mounts.json new file mode 100644 index 00000000..84824d19 --- /dev/null +++ b/gui/mounts/mounts.json @@ -0,0 +1,70 @@ +[ + { + "protocol": "ssh", + "url": "dockers.dev.bramkelchtermans.be", + "remote_path": "/var/dockers", + "local_path": "~/Remote/dockers" + }, + { + "protocol": "ssh", + "url": "home-assistant.dev.bramkelchtermans.be", + "remote_path": "/var/dockers", + "local_path": "~/Remote/home-assistant" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Library", + "remote_path": "", + "local_path": "~/Remote/library-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Data", + "remote_path": "", + "local_path": "~/Remote/data-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Downloads", + "remote_path": "", + "local_path": "~/Remote/downloads-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Games", + "remote_path": "", + "local_path": "~/Remote/games-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Movies", + "remote_path": "", + "local_path": "~/Remote/movies-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Series", + "remote_path": "", + "local_path": "~/Remote/series-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + }, + { + "protocol": "smb", + "url": "//qnap-bram.local/Zaalshows", + "remote_path": "", + "local_path": "~/Remote/zaalshows-qnap", + "username": "Bram", + "password": "HKmkVxuNa^Dj49" + } +] diff --git a/gui/setup.sh b/gui/setup.sh index 6e26d63c..74953f79 100755 --- a/gui/setup.sh +++ b/gui/setup.sh @@ -14,4 +14,7 @@ chmod +x ./gui/floorp/setup.sh ./gui/floorp/setup.sh chmod +x ./gui/plank/install.sh -./gui/plank/install.sh \ No newline at end of file +./gui/plank/install.sh + +chmod +x ./gui/mounts/mount.sh +./gui/mounts/mount.sh ./gui/mounts/mounts.json \ No newline at end of file