Using rsync to update a remote Hugo website
by Tullio Facchinetti
After having moved to Hugo for my personal website, I found that the usual way of using rsync to update the copy of the website on the remote server does not work efficiently. In particular, everytime I rebuild the local copy of the site with Hugo, and I run the command to synchronize the remote copy, all the local files are uploaded to the server.
This happens because Hugo regenerates all the files at every local build. For this reason, when rsync checks the changes in the modification times between local and remote files, the local ones are all always newer, and thus all of them are uploaded remotely.
To limit this problem, I use this workaround.
I have a local directory called upload/
, which is located in the same Hugo base directory.
The upload/
directory is kept synchronized with the Hugo’s public/
directory.
The synchronization is not done by modification date, but by file checksum, which is one of the methods provided by rsync.
In this way, the files in the upload/
directory are updated only when there is an actual change in the content within the public/
directory.
Afterwards, I can use the usual remote synchronization method between the local upload/
directory and the remote server using the modification times.
The following sync.sh
bash script does all the work:
#!/bin/bash
BASEPATH=/your-path-to-local-hugo-base-directory/
rsync -r --delete --checksum $BASEPATH/public/ $BASEPATH/upload/
rsync -avz --delete $BASEPATH/upload/ username@your-server:remote-directory/
It would be possible to directly perform the synchronization between the local public/
directory and the remote server using the checksum method.
However, this incurs in a large overhead due to the checksum operations done in the remote machine.