Here is a simple script to automatically synchronize a local directory with a remote one whenever a local change is detected. Combined with a compiler that watches for changes running on the remote server, it can be used to offload heavy compilation tasks to a remote server while using a less powerful computer to edit sources.

This script uses fswatch to monitor for changes in the local folder and rsync to perform the actual synchronization to the folder on the remote machine.

It is highly advisable to configure your SSH keys and hosts to have a completely seamless synchronization experience (i.e. no constant nagging for passwords).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

# Configure these variables to your reality
REMOTE_HOST=macpro
REMOTE_FOLDER=/tmp/silvae86.github.io
LOCAL_FOLDER=$(pwd)

# initial sync step
rsync -af --progress . "$REMOTE_HOST:$REMOTE_FOLDER"

# initial check may take some time, later changes may be instant
fswatch \
--one-per-batch \ 
--recursive \
--latency 1 \
--verbose \
"$LOCAL_FOLDER" | xargs -I{} rsync -a --progress "$LOCAL_FOLDER" "$REMOTE_HOST:$REMOTE_FOLDER"

Explained:

  • --one-per-batch → bubble/combine events so we do not ask for a sync every time a single file is changed
  • --recursive → scan directories and subdirectories
  • --latency 1 → wait 1 second before triggering sync
  • "$LOCAL_FOLDER" →source folder

Next, a piped command calls rsync to sync the local dir to the remote one:

  • rsync -a --progress "$LOCAL_FOLDER" "$REMOTE_HOST:$REMOTE_FOLDER" → call rsync to synchronize $LOCAL_FOLDER to $REMOTE_FOLDER on $REMOTE_HOST, via SSH.