Hecatronic
397 words
2 minutes
Using rsync under Windows
2024-03-24

My main blog is hosted on DreamHost, and I was looking for the best way to upload files locally to the remote host.
The best way to do this is to use something that just uploads the changes rather than the whole thing each time.
With linux we have a tool available called rsync.
Under windows however we need to install a couple of things to get this to work.

Msys2#

If you’re using windows then there’s a couple of ways to gain access to Unix tools such as rsync.
One is the use of WSL2, this will however involve setting up a virtual machine which can use a lot of space on the disk.
Another way is the use of something called MSYS2.

After installing we can install rsync and jq

# Update MSYS2
pacman -Suy
# Install rsync
pacman -S rsync
# Install jq to read json files under bash
pacman -S mingw64/mingw-w64-x86_64-jq

Settings#

Upload Settings#

First lets create a json settings file called settings.json

{
  "username": "username-here@remote-server.com",
  "password": "password-here",
  "srcdir": "../dist/",
  "remotedir": "destination-directory"
}

In my case I’m placing all the publish scripts under a publish subdirectory.
The source files being up one level then down into a dist subdir.

RSync Ignore#

Lets say we want to tell rsync to ignore certain paths or directories during the upload / sync.
Create a rsync.rules file.

- /.git/

Upload Script#

Next we’re going to create an upload script called upload.sh

#!/bin/bash

# Script to upload content via ssh / rsync

username="$(jq -r '.username' settings.json)"
password="$(jq -r '.password' settings.json)"
srcdir="$(jq -r '.srcdir' settings.json)"
remotedir="$(jq -r '.remotedir' settings.json)"

echo "making sure the remote directory exists"
sshpass -p $password ssh $username mkdir -p $remotedir
echo "starting the upload"
sshpass -p $password rsync -e "/usr/bin/ssh" -av --delete --filter "merge rsync.rules" $srcdir $username:$remotedir

Lets make the script executable, so under the MSYS2 shell

chmod +x upload.sh

Windows PowerShell wrapper#

If we’re using windows then lets create a quick shortcut to launch the above script from powershell using msys2.
upload.ps1

# Dreamhost powershell publish script
iex "C:\msys64\msys2_shell.cmd -here -mingw64 -shell bash upload.sh"

Running the scripts#

In order for this to work we need to log onto the server at least once via ssh.
This will place the ssh server signature into the /home/ric/.ssh/known_hosts file

At a MinGW64 prompt run the following to connect at least once

ssh username-here@remote-server.com

We should now be ready to perform the upload

cd D:\examplesite\publsh
upload.ps1

You can then log onto your site via ssh to confirm the files have been uploaded. If you’re looking for a good ssh terminal for windows I’d suggest

Using rsync under Windows
https://www.hecatron.com/posts/2024/using-rsync-windows/
Author
Hecatronic
Published at
2024-03-24