shell-network/modules/sshfs.sh

93 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# depends on sshfs fuse for ssh
module_load filesystem # mounted
module_load net-utils # host_reachable
module_load ssh
function smount() {
local HOST
local PORT
local CONFIG=$SSH_CONFIG
local PORT=22
declare SSHOPTS
declare OPTION
declare OPTARG
declare OPTIND
declare MNTUSER
while getopts 'u:np:o:F:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
p)
PORT=$OPTARG
# echo option d: $DIRS
;;
u)
MNTUSER=$OPTARG
;;
n)
MNTUSER=_NONE_
;;
o)
echo "adding sshfs option: $OPTARG"
SSHOPTS="$SSHOPTS -o $OPTARG"
;;
F)
echo "using SSH Config file at: $OPTARG"
CONFIG=$OPTARG
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $((OPTIND - 1))
# first item is nowsource, second is local mount point, third is possbile local user
HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1")
[[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1
if [[ $(mounted $2) ]]; then
echo "aborting mount: $1 already mounted at $2"
else
mkdir -p $2
# can add any options after mount point directory like -o default_permissions
config=$([[ -f $CONFIG ]] && echo "-F $CONFIG")
if [[ ! $MNTUSER == "_NONE_" ]]; then
MNTUSER=${MNTUSER:-$USER}
id=$(id -u ${MNTUSER})
if [[ $id ]]; then
SSHOPTS="$SSHOPTS -o uid=$id -o allow_other"
else
echo no user ${MNTUSER} on this machine, aborting mount
return 1
fi
else
MNTUSER=""
fi
args="-p $PORT $SSHOPTS $config $1 $2"
echo SSHFS $([[ $MNTUSER ]] && echo mounted as user ${MNTUSER}): $args
sshfs $args
fi
}
function usmount() {
if [[ $(mounted $1) ]]; then
echo "unmounting remote file system at $1"
fusermount -u $1
else
echo "nothing mounted at $1, aborting unmount"
fi
}
function mntBackup() {
smount root@$1:/backup /backup/remote -p 22 -o allow_other
}
function umntBackup() {
usmount /backup/remote
}