Skip to content

Instantly share code, notes, and snippets.

@mindfullsilence
Last active June 13, 2020 22:16
Show Gist options
  • Save mindfullsilence/933b9e8d7035cc7b6c99b753ece73bb1 to your computer and use it in GitHub Desktop.
Save mindfullsilence/933b9e8d7035cc7b6c99b753ece73bb1 to your computer and use it in GitHub Desktop.
Mount synology nas on Mac through bash

Installation

  1. Copy the contents of the quickmount.sh file to your ~/.bash_profile
  2. Edit lines 4, 5, and 6 with the appropriate information for connecting to your synology NAS
  3. Edit/duplicate the projects function for any folders you want to mount on your mac.
  4. Save your ~/.bash_profile, then in terminal run source ~/.bash_profile

Usage

To mount a directory from terminal, just call your function:

mindfullsilences-MBP:~ mindfullsilence$ projects
mindfullsilences-MBP:Projects mindfullsilence$ 

To disconnect all mounted folders, call the disconnect command:

mindfullsilences-MBP:Projects mindfullsilence$ disconnect
Disconnected all mounted drives.
mindfullsilences-MBP:~ mindfullsilence$ 
MOUNTED=()
function synology() {
USER="admin" #synology login username
PASSWORD="myP%40ssW%7B%7Drd" # should be the uri encoded password. Eg: "myP@ssW{}rd" becomes "myP%40ssW%7B%7Drd"
SERVER="SynologyDS718" # the name you gave your server during setup. Can also be an IP address
MOUNTPOINT=$1
SERVERDIR=$2
SERVERURI="$SERVER/$2"
# ensures directory exists for mounting
if [ ! -d $MOUNTPOINT ]; then
mkdir $MOUNTPOINT
fi
# mounts directory if not already mounted
# TODO: this has an inheret flaw in that if the server folder is empty,
# this check will fail, and attempt to mount an already mounted drive.
# Unlikely to ever be the case for me so ¯\_(ツ)_/¯
if [ ! "$(ls -A $MOUNTPOINT)" ]; then
mount_smbfs //$USER:$PASSWORD@$SERVERURI $MOUNTPOINT
fi
MOUNTED+=($1)
}
# Recreate this function for each mountpoint
# and nas location you want to quickly mount
function projects() {
# Where you want the nas folder to be accessible on your mac
MOUNTPOINT=/Users/mindfullsilence/Documents/Projects
# Where the folder exists on the NAS server (dont include the beginning "/")
SERVERDIR=Projects/web
synology $MOUNTPOINT $SERVERDIR
cd $MOUNTPOINT
}
# Unmounts all mounted locations
function disconnect() {
# cd to home so umount doesnt ever fail
cd ~
#unmount all points
for i in "${MOUNTED[@]}"; do
umount $i
done
# clear the array of mountpoints
MOUNTED=()
echo "Disconnected all mounted drives."
}
# list all currently mounted folders
function mounted() {
printf '%s\n' "${MOUNTED[@]}" | sort -r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment