使用Rclone增量上传Hugo静态网站

Hugo生成的静态文件默认保存在public目录,只需要将这个目录中的所有文件打包上传到主机的网站根目录就可以了。但如果文章比较多,每次发表或更新了文章都要打包上传就很累了。

安装rclone:

sudo apt install rclone

我的计算机系统是Zorin OS,使用以上命令安装后发现软件不能正常使用,于是直接在官网下载安装:

Rclone下载地址

Rclone支持FTP、SFTP、SSH、S3、WebDAV等协议,无论是虚拟主机、云服务器、对象存储都可以使用。

这里以FTP举例说明,其他的配置方法基本相同。

执行配置命令:

rclone config

输入n表示创建新的链接:

beizigen@beizigen-pc:~$ rclone config
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

输入远程节点名称(自定义):

Enter name for new remote.
name> tcloud

选择存储,可以看到支持非常多的类型,这里选择ftp,输入对应编号16:

...
16 / FTP
   \ (ftp)
Storage> 16

输入FTP主机地址:

Option host.
FTP host to connect to.
E.g. "ftp.example.com".
Enter a value.
host> 42.156.76.60

输入FTP账号:

Option user.
FTP username.
Enter a string value. Press Enter for the default (beizigen).
user> beizigen

输入FTP端口号,一般默认21,回车即可:

Option port.
FTP port number.
Enter a signed integer. Press Enter for the default (21).
port> 

y表示输入自己的密码,g表示生成随机密码,n表示密码为空。一般FTP密码在主机那边设置好了,所以选择y:

Option pass.
FTP password.
Choose an alternative below. Press Enter for the default (n).
y) Yes, type in my own password
g) Generate random password
n) No, leave this optional password blank (default)
y/g/n> y 

输入两次密码:

Enter the password:
password:
Confirm the password:
password:

后面的配置项一般默认(一路回车)即可,除了高级选项中,可能需要关心的有两个参数:

并发数(服务端FTP软件配置里一般默认为10):

Edit advanced config?
y) Yes
n) No (default)
y/n> y

Option concurrency.
Maximum number of FTP simultaneous connections, 0 for unlimited.
Note that setting this is very likely to cause deadlocks so it should
be used with care.
If you are doing a sync or copy then make sure concurrency is one more
than the sum of `--transfers` and `--checkers`.
If you use `--check-first` then it just needs to be one more than the
maximum of `--checkers` and `--transfers`.
So for `concurrency 3` you'd use `--checkers 2 --transfers 2
--check-first` or `--checkers 1 --transfers 1`.
Enter a signed integer. Press Enter for the default (0).
concurrency> 10

服务端软件选择,根据主机上的FTP软件选择即可:

Option encoding.
The encoding for the backend.
See the [encoding section in the overview](/overview/#encoding) for more info.
Choose a number from below, or type in your own encoder.MultiEncoder value.
Press Enter for the default (Slash,Del,Ctl,RightSpace,Dot).
 1 / ProFTPd can't handle '*' in file names
   \ (Asterisk,Ctl,Dot,Slash)
 2 / PureFTPd can't handle '[]' or '*' in file names
   \ (BackSlash,Ctl,Del,Dot,RightSpace,Slash,SquareBracket)
 3 / VsFTPd can't handle file names starting with dot
   \ (Ctl,LeftPeriod,Slash)
encoding> 2

设置后执行以下命令查看主机文件列表,看下是否成功:

rclone lsf tcloud:

这里输入了之前定义的远程节点名称tcloud,冒号后面可以跟路径,没有路径就表示根目录。

如果输入以上命令后没有反应,主要有两个原因:

  • Rclone配置的并发数大过了服务器上FTP配置文件中的设置;
  • Rclone默认使用被动模式连接,服务器上需要开启被动模式并放行39000-40000端口。

Pure-Ftpd服务端配置文件修改并发数和端口的参数如下:

MaxClientsNumber	10
MaxClientsPerIP		10
PassivePortRange	39000	40000

注意被动端口要添加到防火墙中。如果没有权限修改FTP服务端的并发数,则只能在Rclone配置FTP时尽可能设置较小的并发数。

同步文件命令:

rclone sync -P -c /public tcloud: --exclude "{.htaccess,.user.ini}"

以上命令会将/public目录中的文件同步到远程主机根目录,并排除.htaccess和.user.ini文件。

常用参数说明:

  • sync:同步,会比较远程目录与本地目录的文件,远程目录中比本地目录多出的文件会被删除;
  • copy:将本地文件拷贝到远程目录,不会删除远程目录中的文件;
  • -P:显示上传进度;
  • -c:校验文件摘要来判断文件是否已更新,如果不存在摘要,则回退为只判断文件大小。如果没有该参数,默认还会校验文件修改时间,由于Hugo每次生成文件都是删除旧的重新生成,文件修改时间都会发生变化,会导致所有文件重复上传,所以建议加上这个参数;
  • –excloude:排除的文件,支持多个文件。可以避免本地不需要上传的文件被同步到远程主机,也可以避免远程主机上的文件被删除;

将命令保存为.sh脚本,以后只需要执行这个脚本就可以了。

阿里云