Created
March 7, 2020 18:27
-
-
Save podanypepa/e55fdcd60097b6b918a56063f15a03b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
NODE="web" | |
USER="fac" | |
REMOTE_DIRS=("/home/fac" "/") | |
REMOTE_DIRS_COUNT=${#REMOTE_DIRS[@]} | |
DST_DIRS=(./home ./root ) | |
DST_DIRS_COUNT=${#DST_DIRS[@]} | |
if [ $DST_DIRS_COUNT -ne $REMOTE_DIRS_COUNT ]; then | |
echo "err: num of REMOTE_DIRS is not equal to DST_DIRS" | |
echo "REMOTE DIRS: ${REMOTE_DIRS[@]}" | |
echo "MOUNTED TO: ${DST_DIRS[@]}" | |
exit 1 | |
fi | |
function is_empty { | |
local dir="$1" | |
shopt -s nullglob | |
local files=( "$dir"/* "$dir"/.* ) | |
[[ ${#files[@]} -eq 2 ]] | |
} | |
case $1 in | |
"up"|"1"|"mount") | |
i=0 | |
while [ $i -lt $REMOTE_DIRS_COUNT ]; do | |
RD=${REMOTE_DIRS[$i]} | |
DD=${DST_DIRS[$i]} | |
if [ ! -d $DD ]; then | |
mkdir $DD | |
fi | |
if is_empty $DD | |
then | |
sshfs $USER@$NODE:$RD $DD | |
if [ $? -ne 0 ]; then | |
echo "sorry: smth wrong in sshfs mount $RD" | |
else | |
echo "OK: $RD at $NODE mounted to $DD" | |
fi | |
else | |
echo "$DD is already mounted" | |
fi | |
i=$(( i+1 )) | |
done | |
;; | |
"down"|"0"|"unmount") | |
i=0 | |
while [ $i -lt $DST_DIRS_COUNT ]; do | |
DD=${DST_DIRS[$i]} | |
if is_empty $DD | |
then | |
echo "$DD is not mounted" | |
else | |
umount $DD | |
if [ $? -eq 0 ]; then | |
echo "OK: $DD unmounted" | |
fi | |
fi | |
i=$(( i+1 )) | |
done | |
;; | |
"status"|"s") | |
i=0 | |
while [ $i -lt $DST_DIRS_COUNT ]; do | |
DD=${DST_DIRS[$i]} | |
if is_empty $DD | |
then | |
echo "$DD is not mounted" | |
else | |
echo "$DD is mounted to ${REMOTE_DIRS[$i]} on $NODE" | |
fi | |
i=$(( i+1 )) | |
done | |
;; | |
*) | |
echo "up | down" | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment