refactor: Add script to mount remote directories
This commit is contained in:
parent
8a31a4011c
commit
e08258d65d
|
@ -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 <json_file>"
|
||||
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
|
|
@ -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"
|
||||
}
|
||||
]
|
|
@ -15,3 +15,6 @@ chmod +x ./gui/floorp/setup.sh
|
|||
|
||||
chmod +x ./gui/plank/install.sh
|
||||
./gui/plank/install.sh
|
||||
|
||||
chmod +x ./gui/mounts/mount.sh
|
||||
./gui/mounts/mount.sh ./gui/mounts/mounts.json
|
Loading…
Reference in New Issue