+#!/bin/bash
+
+# smbbind.sh share mountpoint
+# smbbind.sh "//SERVER/SHARE" "/mnt/somewhere"
+
+check_every=30 #Seconds
+timeout_after=5 #Seconds
+restart_after=30 #Seconds
+
+hide_path=~/.smbbind/
+cifs_opts="rw,cache=strict,username=nt_username,password=nt_password,domain=nt_domain,uid=0,noforceuid,gid=0,noforcegid,file_mode=0755,dir_mode=0755,nounix,serverino,rsize=61440,wsize=57344,actimeo=60,_netdev"
+
+share="$1"
+bind_mountpoint="$2"
+
+function clean {
+ #Umount bindfs, next umount cifs, then exit.
+ echo [..] Cleaning...
+ #The only open handle opened on the cifs share should be the bindfs one.
+ echo [..] forcing umounting bindfs on "$bind_mountpoint"
+ kill -9 $bindfs_pid
+ umount -f "$bind_mountpoint"
+ echo [OK] Done.
+ # Umounted bindfs, cifs should umount without problems
+ echo [..] Forcing umounting cifs on "$cifs_mountpoint"
+ umount -f "$cifs_mountpoint"
+ umount -l "$cifs_mountpoint"
+ echo [OK] Done cleaning.
+}
+
+function finish {
+ echo exiting...
+ clean
+ trap exit INT TERM EXIT
+ exit
+}
+
+trap finish INT TERM EXIT
+
+#Prepare environment
+ mkdir $hide_path &>/dev/null
+ cifs_mountpoint=$hide_path/$(echo $bind_mountpoint|tr '/' '_')
+ mkdir -p $cifs_mountpoint &>/dev/null
+ mkdir -p $bind_mountpoint &>/dev/null
+
+while true ; do
+
+ #Mount things:
+ echo [..] mounting cifs "$share" on "$cifs_mountpoint"
+ if timeout 10 mount.cifs "$share" "$cifs_mountpoint" -o $cifs_opts ; then
+ echo [OK] mounted cifs "$share" on "$cifs_mountpoint"
+
+ echo [..] mounting bind "$cifs_mountpoint" on "$bind_mountpoint"
+ bindfs "$cifs_mountpoint" "$bind_mountpoint"
+ #Getting the pid of bindfs is tricky.
+ bindfs_pid=$(ps -eo pid,args|grep bindfs |grep "$cifs_mountpoint" |grep "$bind_mountpoint" |cut -d " " -f 1)
+ echo [OK] mounted bind "$cifs_mountpoint" on "$bind_mountpoint"
+
+ #Check them
+ echo [OK] Start Main check cycle, whill check every $check_every seconds...
+ while true ; do
+ if ! timeout -k 1 $timeout_after ls "$bind_mountpoint" &>/dev/null ; then
+ echo no answer from bindfs for "$bind_mountpoint"
+ clean
+ break
+ #else
+ #echo Share is alive
+ fi
+ sleep $check_every
+ done
+
+ else
+ echo [!!] Cannot mount "$share" on "$cifs_mountpoint"
+ fi
+
+ echo [..] Waiting $restart_after seconds: $(date)
+ sleep $restart_after
+
+done