Skip to content

Instantly share code, notes, and snippets.

@Dalesjo
Last active February 20, 2023 04:05
Show Gist options
  • Save Dalesjo/65140bfec571a22aaa12fc4dd2e5a348 to your computer and use it in GitHub Desktop.
Save Dalesjo/65140bfec571a22aaa12fc4dd2e5a348 to your computer and use it in GitHub Desktop.
Script to safetly mount a smb share.
#!/bin/bash
SMGUID=0
SMBGID=0
while getopts 'd:s:u:p:U:G:' flag; do
case "${flag}" in
d) DIRECTORY="${OPTARG}" ;;
s) SHARE="${OPTARG}" ;;
u) USERNAME="${OPTARG}" ;;
p) PASSWORD="${OPTARG}" ;;
U) SMGUID="${OPTARG}" ;;
G) SMBGID="${OPTARG}" ;;
esac
done
if [ -z $DIRECTORY ] || [ -z $SHARE ] || [ -z $USERNAME ] || [ -z $PASSWORD ] ; then
echo "smbmount [OPTIONS...]"
echo "";
echo "Reads text report and returns total size";
echo "";
echo "Flags:";
echo " -d Directory to mount samba share to."
echo " -s samba share ex //192.168.60.200/myshare"
echo " -u username to access samba share"
echo " -p password to access samba share"
echo " "
echo " -U UID of user who will own files in samba share. Default: 0 (root)"
echo " -G GID of group who will own files in samba share. Default: 0 (root)"
echo " "
echo "Example"
echo "./smbmount -d /mnt/share/192.168.60.200/myshare -s //192.168.60.200/myshare -u backup -p secret -U 0 -G 100"
exit 3;
fi;
mount | grep $DIRECTORY > /dev/null
if [ $? -eq "0" ] ; then
echo "Already mounted"
exit 0
fi
if [ ! -d $DIRECTORY ] ; then
mkdir -p $DIRECTORY
if [ $? -ne "0" ]; then
echo "Failed to create directory"
exit 3;
fi;
fi;
mount -t cifs -o username=$USERNAME,password=$PASSWORD,dir_mode=0750,file_mode=0750,uid=$SMGUID,gid=$SMBGID $SHARE $DIRECTORY
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment