rclone

rclone

rsync for cloud storage

支持多种云存储的开源命令行文件传输/同步工具.

支持的云存储类型: Dropbox, Google Drive, Onedrive, Amazon S3, Google Cloud Storage, Nextcloud, FTP, WebDAV...

Quick Start

Installation & Config

下载对应平台的 rclone 程序. 官方提供的 rclone 二进制版本是单可执行文件程序,静态链接了所有库,无任何外部依赖。所以理论上只要是对应架构(x86/arm/misp, 32/64)的平台都可以直接运行可执行文件。(某些架构平台如 mipsel 的 rclone 可执行文件很大,高达 30MB 多,如果要放到 OpenWrt 路由器上跑几乎必须用 U盘)

建议将下载的 rclone 可执行文件放到 PATH 路径里,如 /usr/bin/rclone

首先配置 rclone 以获得对应存储平台的用户授权

rclone config

按照提示操作。选择 n 新建一个 remote 配置,输入其 id,选择平台类型等。一般情况下,如果提示输入 App ID / Token 之类的,可以直接留空。对于常见的几个平台,rclone 会使用浏览器打开 平台 OAuth 页面以进行授权。

如果在 VPS 等 headless 环境下部署 rclone,那么授权步骤稍有不同,在 rclone config 的 "Remote config: Use auto config? Say N if you are working on a remote or headless machine" 步骤要输入 n,然后根据提示在另一个具有浏览器的机器(如桌面PC)里操作,具体步骤不同网盘有所不同。比如 onedrive 需要在 PC 机的 terminal 里执行 rclone authorize <platform_id>,在打开的浏览器页面完成授权后,将 terminal 里输出的 JSON 内容({"accessToken": "...",...})复制并粘贴到 VPS 的 terminal 里即可完成授权。而 Google Drive 则是直接在 PC 浏览器打开地址授权并复制结果回 VPS terminal。

Usage

rclone 使用非常简单。直接输入 rclone 查看命令列表; rclone <command> -h 查看具体命令的详细帮助。

列出文件或文件夹(列出文件夹是单独的命令,因为很多 key-object 类型存储平台如S3本质上只有文件概念,文件夹是用文件的 key 名字里的 "/" 分隔符模拟的)

rclone ls <remote_id>:/remote_path
rclone lsd <remote_id>:/remote_path

可以看出在 rclone 里,远程路径的格式是 <remote_id>:/remote_path<remote_id> 是在 config 时配置的。

常用操作

rclone mkdir <remote_id>:/remote_path
rclone delete <remote_id>:/remote_path
rclone rmdir <remote_id>:/remote_path # Remove the path if empty.
rclone rmdirs <remote_id>:/remote_path # Remove empty directories under the path.

Options

  • -v, -vv : verbose, more verbose
  • -V : show version

Commands

size

rclone size source:path

(递归)计算某个目录里的文件(不含文件夹)数量和总大小。

计算结果与 Windows 资源管理器文件夹“属性”里显示的 Size 和 contains: XX files 一致。

check

比较 source 和 dst 目录

rclone check --one-way --size-only source:sourcepath dest:destpath
  • --one-way : 只检测 source 里的文件是否存在于 dst
  • --size-only : 仅比较文件大小是否一致。如果不指定此参数,某些情况下会计算 hash 判断文件是否一致(如果 source 和 dst 支持某种相同的 hash 算法时),非常耗时。

copy

rclone copy source:sourcepath dest:destpath [flags]

dst 是一个目录, 如果不存在会自动创建, src 参数指定的文件会被拷贝到这里。

src 是一个文件或目录, 如果是目录,会拷贝目录里的所有文件(而非此目录本身)。

默认会通过文件大小,修改时间,md5sum 等比对本地和远程文件,dst 上已有的未更改文件不会重新传输。

例如, 本地目录:

folder/
|--file1
|--file2

当前目录 ($PWD) 是 folder/ 的父目录,执行 rclone copy folder onedrive:/test 之后,远程 onrdrive 文件夹里内容如下:

test/
|--file1
|--file2

copyto

rclone copyto source:path dest:path [flags]

If source:path is a file or directory then it copies it to a file or
directory named dest:path.

This can be used to upload single files to other than their current
name.  If the source is a directory then it acts exactly like the copy
command.

sync

rclone sync source:path dest:path [flags]

将 source:path 单向同步到 dest:path。

  • src:path 需要是一个目录。会同步里面内容而非该目录本身。
  • dst:path 也是一个目录,如果不存在会自动创建。
  • 同步时,会根据文件 size / md5 自动跳过已存在的相同文件。

Type:

  • no flags - (size, modtime)
  • --size-only (size)
  • --checksum (size, checksum)

Modifiers

  • --ignore-size makes all of the above skip the size check
  • --ignore-times - uploads unconditionally (no checks)

常用参数

  • --delete-excluded: Delete files on dest excluded from sync

Example:

# 把本地 "Docs" 文件夹里内容同步到远程 "Documents" 文件夹里
rclone sync /root/Docs gdrive:/Backups/Documents

"rclone check" will always caclucate the checksum

serve

rclone serve http --addr 0.0.0.0:8080 remote:/
rclone serve webdav --addr 0.0.0.0:8080 --read-only remote:/

mount

rclone mount "OneDrive:/Scanned Documents/" /tmp/OneDrive/ --vfs-cache-mode full --umask 000 --allow-other

rclone mount 的性能显著低于 rclone serve。

Tips

rclone 配置文件

rclone 的配置文件是位于用户主目录下的 .config/rclone/rclone.conf 文件,其中内容包含已授权的平台 OAuth access / refresh tokens 等 private credentials。一般可以直接复制这个配置文件到其他机器上使用。

各平台下的 rclone 配置文件路径:

  • Linux: ~/.config/rclone/rclone.conf
  • Windows: %USERPROFILE%\AppData\Roaming\rclone\rclone.conf (注:旧版 rclone windows 版的配置路径曾是 %USERPROFILE%\.config\rclone\rclone.conf)

使用代理

rclone 支持 HTTP 代理。方法是设置一个 HTTP_PROXY 环境变量值,格式为 http://ip:port

例如,对于 shadowsocks-win 默认配置,设置以下环境变量即可:(shadowsocks-win在同一端口同时提供 HTTP 和 SOCKS5 代理)

HTTP_PROXY=socks5://127.0.0.1:1080
HTTPS_PROXY=socks5://127.0.0.1:1080

HTTP_PROXY=http://127.0.0.1:1080
HTTPS_PROXY=http://127.0.0.1:1080

Tips

  • 经测试,某些情况下,Windows 平台下 rclone mount 用 HTTPS?_PROXY 环境变量设置的代理性能很差。换成 Proxifier 自动给 rclone.exe 设置代理有所改善。
  • 很多程序都会读取并使用 HTTP(S)_PROXY 环境变量。并且其设置里的 socks5:// 协议很多程序不支持,如果设为全局环境变量会导致那些程序出错。建议使用 bat 脚本设置局部的环境变量即 "set HTTP_PROXY=http://127.0.0.1:1080"

Windows 下可以使用 Proxifier 自动使 rclone 使用代理。

Suspend & Resume

  • Linux: suspend the rclone process using ctrl+z and resume it with fg later
  • Windows: open Resource Monitor (win + r ->resmon.exe), find rclone.exe in one of the tabs, rightclick and suspend process. Rightclick again to resume. There is also pssuspend.exe fom sysinternals.

Suspend & Resume rclone 对部分平台(如 Google Drive) 支持断点续传。但如果 rclone 进程已经退出,再次运行 rclone copy / sync 则不支持已部分传输文件的断点续传,会从头开始重新传。


Last update: 2023-04-05 10:17:03 UTC