Last active
July 21, 2022 14:56
-
-
Save tmillr/fc418230c45cfd9304a35985b8f2e88c to your computer and use it in GitHub Desktop.
Deploy a local filesystem to a remote host
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
#!/usr/bin/env sh | |
# Author: Tyler Miller (@tmillr) ([email protected]) | |
# Mount a local directory to a remote destination (give a remote host access | |
# to a local directory via sshfs via sftp). Requires mkfifo on local host. | |
# Requires sshfs to be installed in PATH on remote host. Might not work on | |
# Windows. | |
# Environment Variables | |
# SSHFS_PATH path to sshfs on remote host (optional) | |
# SFTPSRV_PATH path to sftp-server on local host (optional) | |
# Synopsis | |
# deployfs desthost destmnt [localdir] | |
# localdir is optional and defaults to sharing the current directory. | |
if [ $# -eq 0 ]; then | |
cat <<\EOF | |
Mount a local directory to a remote destination. | |
Give a remote host access to a local directory via sshfs via sftp. | |
EOF | |
echo "usage: deployfs desthost destmnt [localdir]" | |
exit 2 | |
fi | |
sshfs="${SSHFS_PATH:-sshfs}" | |
sftp="${SFTPSRV_PATH:-"$(which sftp-server 2>/dev/null)"}" | |
case "$sftp" in | |
*[![:space:]]*) : ;; | |
*) | |
sftp="$(find /usr -name 'sftp-server' -perm +u+x \( -type f -or -type l \) 2>/dev/null)" | |
sftp="${sftp%%"$(printf '[\n\r]')"*}" | |
[ -n "$sftp" ] || { | |
echo 'could not find sftp-server bin, set SFTPSRV_PATH' >&2 | |
exit 1 | |
} | |
;; | |
esac | |
dest="$1" | |
remote_mnt_point="$2" | |
local_dir_to_share=":$3" | |
tmpd="$TMPDIR" | |
[ -d "$tmpd" ] || tmpd="$TMP" | |
[ -d "$tmpd" ] || tmpd="/tmp" | |
fifo=0 | |
while [ -e "${tmpd}/${fifo}" ]; do | |
: $((fifo++)) | |
done | |
fifo="${tmpd}/${fifo}" | |
script=" | |
mkdir -p \"$remote_mnt_point\" | |
trap 'rm -d \"$remote_mnt_point\"' EXIT | |
\"$sshfs\" \"$local_dir_to_share\" \"$remote_mnt_point\" -o passive | |
" | |
mkfifo "$fifo" | |
trap "rm $fifo" EXIT | |
"$sftp" <"$fifo" | ssh "$dest" "$script" >"$fifo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment