103 lines
3.3 KiB
Bash
103 lines
3.3 KiB
Bash
|
#!/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
|