If you have a Hugo static site and your hosting provider just offers FTP access and not SSH, synchronising the site manually can be difficult. However, you can use sitecopy to incrementally deploy your entire Hugo site, similar to rsync solution.
sitecopy is a tool for copying locally stored web sites to remote web servers. A single command will upload files to the server which have changed locally and delete files from the server which have been removed locally, to keep the remote site synchronised with the local site. The aim is to remove the hassle of uploading and deleting individual files using an FTP client. sitecopy will also optionally try to spot files you move locally, and move them remotely.
For synchronising a Hugo site using rsync please see the official guide.
Installation
sitecopy is available in all major distributions, so just use your favourite package manager to install it (sudo apt install sitecopy
). If you are using cygwin, sitecopy can also be installed on Windows via apt-cyg.
Optionally, you should take a quick look at sitecopy’s man page to familiarize yourself with its options: man sitecopy
.
Configuration
In your home directory create a new directory called .sitecopy
(with permissions 700
) – sitecopy uses this directory to store synced files details.
|
|
After that, you will need to create the sitecopy configuration file called .sitecopyrc
:
|
|
Open .sitecopyrc file in your editor and fill in the configuration for an imaginary hugosite.com site: vim .sitecopyrc
|
|
The state checksum
configuration from line 15 is important. Since Hugo does not support incremental builds, all the files in the public folder will be regenerated when you rebuild your Hugo site. By using the checksum method for the sitecopy state (and not the default timesize) we make sure that only the files that are truly modified will be synchronised.
Most of the configuration options are self-explanatory. The site
directive must be followed by a name for the web site – you can freely choose one, e.g. hugosite.com or ftp.mysite.com – this name will be used later on with the other sitecopy commands. The local
directive contains the local path of the web site – in this case, Hugo’s public folder; remote
contains the path of the web site on the remote server – it can be absolute or relative.
The exclude
lines are optional, they allow you to exclude files and folders from being synchronised with sitecopy.
Initialise a new site
For this example, we will use the scenario where the local copy exists – your Hugo site – and you have an empty remote site.
There are other scenarios that might fit your situation for which you will need to consult the sitecopy man page and modify the helper script below accordingly.
First, initialise the site (replace hugosite.com with the name of the site you used in the .sitecopyrc file created previously):
|
|
Then, upload the local copy to the remote site using sitecopy --update hugosite.com
:
|
|
sitecopy will MD5 checksum every file from your public Hugo folder and store the information in the ~/.sitecopy/[sitename] file. Here is an excerpt from the ~/.sitecopy/hugosite.com file generated by the command above:
|
|
Next time when you will build your Hugo site and run sitecopy --update
, sitecopy will check the MD5 checksum of the files and synchronise them accordingly.
Hugo and sitecopy
Hugo FTP synchronisation with sitecopy is really easy. First, modify and build your Hugo site as usual (hugo --minify --cleanDestinationDir --gc
). Next, an optional step is to run sitecopy hugosite.com
to find out which files have changed locally (replace hugosite.com with the name of the site you used in the .sitecopyrc file).
|
|
Then, to actually synchronise your local Hugo site, uploading only new and changed files to the remote server and deleting files that have been removed locally, you simply run sitecopy --update hugosite.com
.
|
|
Shell script
For your convenience, you can find most of above in the small shell script below. For this script to work, you need to initialise the site first, as described above. Create a new file in your home directory called ftp-sync.sh
with the following content:
|
|
The script can be invoked using the following syntax:
|
|
Name | Description |
---|---|
check (default) |
Will check the changes in the local site (via the ~/.sitecopy/[sitename] file), put them in an HTML file (see the –flatlist option in man page) and display them via lynx browser in your terminal. |
sync |
Performs the actual synchronisation if there are any changes available. |
Feel free to adapt it to suit your needs and happy Hugo!