Skip to content

Commit 40e94c0

Browse files
authored
Add SSH sync script for remote file monitoring q
1 parent 8483e09 commit 40e94c0

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

pages/bash/index.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,62 @@ <h2>copy</h2>
399399
add v on pack and unpack
400400
tar vcf - -C ../override . | tar vxf -
401401
</script>
402+
<h2>sync remote via ssh in watch</h2>
403+
<script type="editor" data-lang="sh">
404+
405+
#!/usr/bin/env bash
406+
407+
# --- CONFIGURATION ---
408+
SSH_USER="bob"
409+
SSH_HOST="ssh-domain.com"
410+
SSH_PORT="22"
411+
REMOTE_DIR="/home/somwhereremote"
412+
SOCKET_PATH="${HOME}/.ssh/roles_sock"
413+
# ---------------------
414+
415+
S="\\"
416+
417+
PROJECT_DIR="$(pwd)"
418+
419+
if ! command -v fswatch >/dev/null 2>&1; then
420+
echo "❌ fswatch missing"
421+
echo "👉 brew install fswatch"
422+
exit 1
423+
fi
424+
425+
# start SSH multiplexing
426+
ssh -M -S "${SOCKET_PATH}" -fnNT \
427+
-p "${SSH_PORT}" "${SSH_USER}@${SSH_HOST}"
428+
429+
fswatch -0 ./ | while read -d "" file; do
430+
# echo fswatch: "${file}"
431+
432+
# Skip common ignored directories
433+
[[ "${file}" == */vendor/* ]] && continue
434+
[[ "${file}" == */node_modules/* ]] && continue
435+
[[ "${file}" == */.git/* ]] && continue
436+
437+
# Convert ABSOLUTE RELATIVE
438+
rel_path="${file#$PROJECT_DIR/}"
439+
REMOTE_PATH="${REMOTE_DIR}/${rel_path}"
440+
441+
if [[ -e "${file}" ]]; then
442+
echo "✅ File exists: ${file}"
443+
# --- FILE EXISTS: Sync it ---
444+
CMD="rsync -az -e \"ssh -p ${SSH_PORT} -S ${SOCKET_PATH}\" \"${file}\" \"${SSH_USER}@${SSH_HOST}:$REMOTE_PATH\""
445+
else
446+
echo "❌ File deleted: ${file}"
447+
# --- FILE DELETED LOCALLY: Remove remotely if it's a file ---
448+
# [ -f ... ] checks if it exists and is a regular file, then rm removes it.
449+
CMD="ssh -p ${SSH_PORT} -S ${SOCKET_PATH} ${SSH_USER}@${SSH_HOST} '[ -f \"${REMOTE_PATH}\" ] && rm \"${REMOTE_PATH}\"'"
450+
fi
451+
452+
printf '%s\n\n' " ${CMD}"
453+
eval "${CMD}"
454+
455+
done
456+
457+
</script>
402458
<h2>replace in file ONCE</h2>
403459
<script type="editor" data-lang="sh">
404460

0 commit comments

Comments
 (0)