Skip to content

Instantly share code, notes, and snippets.

@alifarazz
Last active December 22, 2024 05:25
Show Gist options
  • Save alifarazz/2e356b2588ca5ab6b889d27c4153c34b to your computer and use it in GitHub Desktop.
Save alifarazz/2e356b2588ca5ab6b889d27c4153c34b to your computer and use it in GitHub Desktop.
Basic script to sync a local project with a remote server
#!/usr/bin/env bash
set -euEo pipefail
set -x # show every command being executed
##### Constants
remote_path_prefix="nesaean:/project/ali" # Remote ssh server is set in my ~/.ssh/config
watcher_match_regex=".*\.(cu|cc|hh|bash|md|c|h|inc)" # Only watch for file paths matching regex
##### Process argv
src_path="$(realpath $2)"
gitignore_path="$(realpath $1)"
##### Watch src_path directory for events
inotifywait -qmre modify,create,move "${src_path}" --format '%w%f%0' \
--no-newline --includei "${watcher_match_regex}" |\
while IFS= read -r -d '' file
do
# Copy the file that was just changed
# echo "${file}"
remote_path_postfix="$(echo ${file} | cut -d/ -f-1,5-)"
dst_path="${remote_path_prefix}${remote_path_postfix}"
rsync -urvPz --exclude-from="${gitignore_path}" "${file}" "${dst_path}" # This doesn't do mkdir on remote
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment