Imagine that you are running a static site. In most cases, you would build the site locally on Windows rather than setting up a full toolchain on the server. In this setup, synchronizing files between your local machine and the server becomes the main concern.
The first tool that comes to mind is usually scp, since it is available out of the box with ssh. It is easy to use and the command is simple. For example:
|
|
However, this approach has several drawbacks, so I do not recommend using it.
First, scp essentially perform a full copy every time, rather than true synchronization. It cannot remove obsolete files and is relatively inefficient.
In addition, permission control is quite limited. If you just simply run the command above, you may get this:
|
|
Clearly, this is not the desired result. As far as I know, a better practice is to have a dedicated user (e.g. web here) manage the site with read and write permissions, while the web server user (e.g. www-data) has read-only access, and zero access from other users.
rsync is the solution. However, since rsync is a Linux tool, some additional setup is required to use it on Windows.
Remote Setup
Suppose that the users are web and www-data. First, add the user web to the www-data group to avoid permission issues related to group ownership.
|
|
Then set the owner and mode for the site directory:
|
|
The g+s (setgid) bit ensures that new files and directories inherit the parent directory’s group instead of the creator’s default group. This step is optional but recommended.
Once the remote server setup is complete, you can choose one of the following methods to use rsync.
Choice 1: Install cwRsync
Download cwRsync on Github and unzip it somewhere. Then the sync script can be written as:
|
|
For some reason, you must specify the ssh.exe it should use explicitly. Additionally, the LocalDir must be an absolute path with the Windows-specific //?/ path prefix otherwise it may be interpreted as a remote host.
Choice 2: Using WSL
Using WSL is simpler and more convenient, so I recommend this method.
Make sure WSL is installed on your system. Then the sync script can be written as:
|
|
As a non-native English speaker, I used AI tools to help refine the language.