Arai2 Rclone 系列教程

警告
本文最后更新于 2022-04-02,文中内容可能已过时。

Rclone 是个发展迅速的开源软件。它可以很方便的对多种网盘进行同步/传输。也支持挂载功能。

配合下载工具 Aria2 可以轻松把我们的服务器配置成一个简易的一个离线下载服务器。

本文汇总了 2018 年以来撰写的相关教程并进行精简。

2018 年 Rclone 这个项目并不是十分知名,可能会介绍使用诸如davfs2之类的工具,但是 2022 年的今天,Rclone 已经是非常成熟且知名的工具了。

由于 Rclone 使用 Go 编写。可以很方便通过二进制方式安装。

1
2
3
4
5
wget https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
chmod +x ./rclone-*/rclone
cp ./rclone-*/rclone /usr/bin/
rm -rf ./rclone-*

由于逗比的脚本已经不再更新,这里建议通过 P3TERX 的修改版脚本进行安装。

1
2
wget -N git.io/aria2.sh && chmod +x aria2.sh
./aria2.sh
1
rclone config

Rclone 支持多种网盘格式,除专有的网盘如 OneDrive、Google Drive 外,还支持如 WebDAV、FTP、S3 等通用的协议。

如果你的网盘或存储块支持 AWS S3 协议,可以直接通过 S3 协议进行配置(例如腾讯云 COS 阿里云 OSS 七牛云等均支持 Amazon S3 协议)

如果你是通过 P3TERX 的修改版脚本安装的 Aria2,则不需要手动去配置这一项。因为他的配置文件提供了开箱即用的 Rclone 自动上传配置。

可以参考它的文档来进行配置:https://p3terx.github.io/aria2.conf/

下面的部分是如果你通过其他方法安装的 Aria2 (比如之前的逗比写的脚本)需要进行设定。

以下为基本配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash
path=$3
downloadpath='/data/aria2'
if [ $2 -eq 0 ]; then
  exit 0
fi
while true; do
  filepath=$path
  path=${path%/*}
  if [ "$path" = "$downloadpath" ] && [ $2 -eq 1 ]; then
    rclone move "$filepath" Your_Drive_Name:
    exit 0
  elif [ "$path" = "$downloadpath" ]; then
    rclone move "$filepath"/ Your_Drive_Name:"${filepath##*/}"/
    exit 0
  fi
done

修改脚本后保存为 autoupload.sh 放在某一目录下给可执行权限 chmod +x autoupload.sh

在 Aria2 配置文件后加 on-download-complete=/root/autoupload.sh

上面的命令意思是 Aria2 任务完成后会自动调用这个脚本,同时会传入相关参数。

但是如果下载任务已经完成,但是却还在进行做种时不会调用这个脚本。因此不建议下载完成后继续保种 seed-time=0(确实积极保种是一件美德,但是作为离线下载服务器时,Aria2 缺少一个下载结束后就执行而不是等待任务完成后就执行的 Feature)

1
rclone mount Your_Drive_Name: /mnt/Your_Drive_Name --allow-other --vfs-cache-mode writes --allow-non-empty &
  • rclone config - 配置

  • rclone copy - 复制

  • rclone move - 移动,如果要在移动后删除空源目录,请加上 --delete-empty-src-dirs 参数

  • rclone sync - 同步:将源目录同步到目标目录,只更改目标目录。

  • rclone check - 检查源和目的地址数据是否匹配。

  • -P = --progress - 显示实时传输进度,500mS 刷新一次,否则默认 1 分钟刷新一次。

  • --transfers=N - 并行文件数,默认为 4。

  • -v - Rclone 将生成 ERRORNOTICEINFO 消息,推荐此项

  • -vv - Rclone 将生成 ERRORNOTICEINFODEBUG 消息。

  • -max-size 传输最大文件,例如可以写 100G(Onedrive 目前单文件最大大小)

  • --min-size 传输最小文件

  • --buffer-size= Use this sized buffer to speed up file transfers. Each --transfer will use this much memory for buffering. default 16M

  • --ignore-errors 跳过错误

  • --onedrive-chunk-size Chunk size to upload files with - must be multiple of 320k (327,680 bytes). Above this size files will be chunked - must be multiple of 320k (327,680 bytes) and should not exceed 250M (262,144,000 bytes) default 10M (5 倍)

  • --drive-chunk-size Must a power of 2 >= 256k. Making this larger will improve performance, but note that each chunk is buffered in memory one per transfer. Reducing this will reduce memory usage but decrease performance. Default: 8Mi

  • -u--update This forces rclone to skip any files which exist on the destination and have a modified time that is newer than the source file. 如果远端文件修改时间比传输文件修改时间要考前,直接覆盖更新,如果修改时间相同,则比较大小或哈希值。如果远端文件比较新,直接跳过

  • -c--checksum Normally rclone will look at modification time and size of files to see if they are equal. If you set this flag then rclone will check the file hash and size to determine if files are equal. This is useful when the remote doesn’t support setting modified time and a more accurate sync is desired than just checking the file size. Rclone 默认检查是否相同的方式是比较文件的修改时间和文件大小,开启此项将比较哈希值

  • --check-first 先检查需要传输哪些文件,之后再进行传输,默认越快开始传输越好,Using this flag can use more memory as it effectively sets --max-backlog to infinite. This means that all the info on the objects to transfer is held in memory before the transfers start.

  • --no-check-dest 强制覆盖文件 --retries 1 This flag is useful to minimise the transactions if you know that none of the files are on the destination. This is a specialized flag which should be ignored by most users!

  • --retries int Retry the entire sync if it fails this many times it fails (default 3). Some remotes can be unreliable and a few retries help pick up the files which didn’t get transferred because of errors.

  • --size-only 只检查文件大小不检查修改时间

  • --checkers=N

    The number of checkers to run in parallel. Checkers do the equality checking of files during a sync. For some storage systems (e.g. S3, Swift, Dropbox) this can take a significant amount of time so they are run in parallel. The default is to run 8 checkers in parallel. 并行检查文件数量,默认为 8

    --use-mmap

    If this flag is set then rclone will use anonymous memory allocated by mmap on Unix based platforms and VirtualAlloc on Windows for its transfer buffers (size controlled by --buffer-size ). Memory allocated like this does not go on the Go heap and can be returned to the OS immediately when it is finished with.

    If this flag is not set then rclone will allocate and free the buffers using the Go memory allocator which may use more memory as memory pages are returned less aggressively to the OS.

    It is possible this does not work well on all platforms so it is disabled by default; in the future it may be enabled by default.

块大小其实很大程度上影响传输速度,在上传时 Rclone 会将大于块大小的文件进行分块并缓冲到内存中。

1
2
--onedrive-chunk-size  # For Onedrive 默认值320K,不能超过250M,一般自行设置建议100M左右
---drive-chunk-size # For Gdrive 同上,默认为8M

增大块大小可有效提高传输效率,但会加大内存占用,请配置适合自己服务器的块大小。请注意,你的块大小*并行数量不要超过你的服务器最大内存,否则进程会被 kill,更要注意如果你同时运行多个 Rclone 也要算好内存占用防止被 kill。建议添加适量 swap 以避免出现类似情况。

以上指令填写大小时均可以以 K,M,G 等为单位。

1
--onedrive-chunk-size=100M
  1. 为什么不建议将 Rclone 挂载为本地硬盘,然后将 Aria2 下载的目录指向 Rclone 挂载的目录?

    因为 Rclone挂载会在本地磁盘产生缓存,这样对性能并不好,也不可能实现模拟成无限空间的大盘鸡。(在一定情况下,可以分片上传达到类似效果)

  2. 当复制到远端,发现目标文件已经存在怎么办?

    默认 Rclone 将使用文件大小和修改时间来确定文件是否不同,仅此而已。

    • 首先比较大小。
    • 如果大小相等,则比较时间戳。

    如果一个文件大小和时间戳都相等,则被视为相等,该文件将被跳过。如果一个文件被认为不同(不同的大小 / 不同的时间戳),Rclone 将使用 checksum 来决定如何处理它

    • 如果校验和相等,它只会更新目标的时间戳以匹配源,而不是实际复制文件。
    • 如果校验和不同,它将复制整个文件,替换远端存在的。

    可以改变一些参数来改变这个默认行为。