#!/bin/bash
srcmnt="vault"
dstmnt="/media/shares"
mntopts="-o credentials=/etc/smbpass,file_mode=0660,dir_mode=0770,uid=1000,gid=1001"
mounts=( Anime Backups Movies Music Photos Software TVShows Videos)
HOST=${srcmnt}
srcdown=1;
srcsrvup=0;
mount=$(which mount)
umount=$(which umount)
nmap=$(which nmap)
ping=$(which ping)
user=$(whoami)
check_host() {
echo -n " * Waiting for $HOST "
while [ $srcdown -ne "0" ]; do
$ping -q -c 1 -w 1 $HOST &> /dev/null; srcdown=$?;
if [ $srcdown -eq "0" ]; then
break;
fi
echo -n '.'; sleep 3;
done
while [ $srcsrvup -ne "1" ]; do
srcsrvup=$($nmap -sT -p 445 -P0 -T5 -n $HOST | grep open | wc -l)
if [ $srcsrvup -eq "1" ]; then
break;
fi
echo -n '.'; sleep 3;
done
echo " Up."
}
mount_cifs() {
if [ $(echo -n $nmap | wc -m) -le 1 ]; then
echo "Error: nmap required";
exit;
fi
if [ $(echo -n $ping | wc -m) -le 1 ]; then
echo "Error: ping required";
exit;
fi
for i in "${mounts[@]}"; do
echo " * Preparing to mount //${srcmnt}/${i} ..."
if [ $(echo $srcsrvup) -ne "1" ]; then
check_host
fi
if [ ! -d "${dstmnt}/${i}" ]; then
mkdir -p "${dstmnt}/${i}"
fi
if [ "$(grep "//${srcmnt}/${i}" /etc/mtab | wc -l)" -ne "1" ]; then
skipmount=0
echo -n " * Mounting //${srcmnt}/${i} on ${dstmnt}/${i} "
if [ "$user" = "root" ]; then
echo -n "as root..."
else
echo -n "as non-root user ${user}..."
fi
while [ "$skipmount" -eq "0" ]; do
if [ ! "$(grep "//${srcmnt}/${i}" /etc/mtab)" ]; then
if [ "$user" = "root" ]; then
${mount} -t cifs ${mntopts} "//${srcmnt}/${i}" "${dstmnt}/${i}" &> /dev/null
else
if [ "$(grep "//${srcmnt}/${i}" /etc/fstab | grep user | wc -l)" -eq "1" ]; then
${mount} "//${srcmnt}/${i}"
else
echo ""
echo " * Could not mount //${srcmnt}/${i} .. You are not root, and mount is not user mountable in /etc/fstab"
skipmount=1
fi
fi
if [ "$skipmount" -ne "1" ]; then
if [ "$(grep //${srcmnt}/${i} /etc/mtab | wc -l)" -ne "1" ]; then
echo -n "."
sleep 10
else
echo " Done."
skipmount=1
fi
fi
fi
done
else
echo " * Already mounted: //${srcmnt}/${i} "
fi
done
}
umount_cifs () {
for i in "${mounts[@]}"; do
echo " * Preparing to unmount "//${srcmnt}/${i}" ..."
skipmount=0
if [ "$(grep "//${srcmnt}/${i}" /etc/mtab | wc -l)" -eq "1" ]; then
dstmnt=$(grep "//${srcmnt}/${i}" /etc/mtab | cut -d' ' -f 2);
echo -n " * Unmounting /${srcmnt}/${i} from $dstmnt "
if [ "$user" = "root" ]; then
echo -n "as root..."
else
echo -n "as non-root user ${user}..."
fi
while [ "$skipmount" -eq "0" ]; do
if [ "$(grep "//${srcmnt}/${i}" /etc/mtab | wc -l)" -eq "1" ]; then
if [ "$user" = "root" ]; then
${umount} "//${srcmnt}/${i}" &> /dev/null
else
if [ "$(grep "//${srcmnt}/${i}" /etc/fstab | grep user | wc -l)" -eq "1" ]; then
${umount} "//${srcmnt}/${i}"
else
echo ""
echo " * Could not unmount //${srcmnt}/${i} .. You are not root, and mount is not user mountable";
skipmount=1
fi
fi
if [ "$(grep "//${srcmnt}/${i}" /etc/mtab | wc -l)" -eq "1" ]; then
echo -n "."
sleep 10
else
echo " Done."
skipmount=1
fi
fi
done
else
echo " * Not mounted: //${srcmnt}/${i} "
fi
done
}
case "$1" in
"mount")
mount_cifs
;;
"umount")
umount_cifs
;;
"unmount")
umount_cifs
;;
*)
echo "Usage: $0 [mount|umount]"
;;
esac
Comments