Rclone 是个啥?

Rclone 是个命令行工具,功能是通过各个云存储平台提供的 API 来实现对各种云存储上的文件进行管理。目前它支持 40+ 家云存储的管理(大部分是国外网盘,国内网盘因为少有提供对应 API 的,所以暂时只有阿里云和腾讯云的对象存储服务支持),它支持的操作特别多,包括:移动、复制、删除、同步、挂载等等。

安装 Rclone

要使用 rclone,首先我们需要安装它。

  • 如果你是 Linux/macOS/BSD 系统,Rclone 官方提供了快速安装的脚本:
    curl https://rclone.org/install.sh | sudo bash
  • 如果习惯统一的包管理器来安装的话,也可以用下面的命令安装:
    Debian / Ubuntu ,用 apt install rclone 安装;
    macOS 可以直接通过 brew install rclone 安装。
  • Windows 机器,相对复杂一点:
    首先在 官方下载页 下载对应的软件包,为 zip 格式的压缩文件;
    下载到本地进行解压后,双击文件夹进入解压后的目录;
    在当前文件浏览器的地址栏输入 cmd 然后按下回车;
    解压后的文件夹可以存放到固定位置,比如平时安装软件的目录。

安装完成之后可以通过 rclone version 查看当前 rclone 的版本信息。

给 Rclone 添加配置信息(添加云存储)

接下来要做的就是配置自己要用到的网盘。我们先通过 rclone config 命令进入云存储管理的程式,这时候 rclone 中会展示一个当前已绑定的云存储列表,以及一些操作选项。

Rclone remote 管理菜单

新人第一次使用可能在进行配置的时候会有点迷糊,这边就模拟配置两个最常用的服务的过程,一个是 Dropbox,一个是 Google Drive

1. 配置 Dropbox 云盘

在命令行输入 rclone config 进入配置界面。

# 进入 rclone 的配置操作
❯ rclone config
n) New remote - 新建链接
d) Delete remote - 删除链接
r) Rename remote - 重命名链接
c) Copy remote - 复制链接
s) Set configuration password - 设置配置密码
q) Quit config - 退出配置操作
e/n/d/r/c/s/q> n //根据自己的需求进行选择
# 设置云存储名字
name> dropbox //输入一个网盘的名字,可以自由设置

# 选择云存储类型
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
……(不同 rclone 版本的序号不一定相同)
Storage> dropbox //可以输入数字编号,或者对应的双引号中的代号
# 输入自定义 App ID 这里一般不用填写
Dropbox App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> //默认留空,直接按回车下一步

# 输入自定义 App 的密码,这里一般不用填写
Dropbox App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> //默认留空,直接按回车下一步

# 是否编辑高级设置,一般选否
Edit advanced config? (y/n)
y) Yes
n) No
y/n> //选择 No,按 n 键

# 是否远程配置(这里根据情况选择,dropbox 的配置相对麻烦一点)
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No 
y/n> //机器有图形界面和浏览器选择 y,只有命令行界面的话选择 n

下面这一步是要获得 Dropbox 的授权,如果你在进行配置 Rclone 的机器拥有图形界面和浏览器的话,相对来说简单很多,当你按下了 y 键之后,Rclone 会自动弹出一个浏览器页面,进行 Dropbox 授权,在页面中选择 Allow(允许)即可。

如果你是要在一些没有图形界面的机器(比如部分 VPS)上配置 Rclone,就需要用一台拥有图形界面和浏览器的机器代为获取 Dropbox 的 token。这时候先按下 n 来进入下一步。

# 这里暂时留空,稍后获取到 token 再过来粘贴信息
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
	rclone authorize "dropbox"
Then paste the result below:
result> // 这里用来粘贴之后获取到的 token

现在要做的,是在一台有图形界面和浏览器的机器上安装好 rclone,在各种平台安装 rclone 的方法在最开始已经写过了。安装好后,在命令行中输入 rclone authorize "dropbox" 并回车,软件会自动打开浏览器来进行 dropbox 授权操作。在点击 Allow(允许)之后,命令行中会返回 dropbox 提供的 token,以供使用。

如果在浏览器点击了 Allow(允许)之后,命令行没有返回 token,请注意自己的网络状态,非透明代理情况下一般终端应用需要额外手动设置代理信息。

# 在拥有图形界面和浏览器的机器上代为获取 token
❯ rclone authorize "dropbox"
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=VBO0YbhagdksaRQzoUGA
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"sl.Aue8VvGHWzFeICVgJqzAQR1pJ3BnmHvICuIPVnIEAQgrwQn8TFeb44qSw7fhqchgshG7EtgagfaG4mhsVqBrIjw7Bs57BChJgGqnOYQABLvSSic_dbtfvQpqelczNWzl","token_type":"bearer","refresh_token":"MJVuIBgVklgaeAAAAAd9M5mMTPq_F8vKUkaMFX6LX2E-y3TnmXN9bnOMxcwvj","expiry":"2021-04-07T03:31:32.562859+08:00"}
<---End paste

这时候复制 token 信息(左右两边的大括号也要复制到),粘贴到上一步的 result 里面,然后按下回车,接下来界面会弹出确认信息:

[dropbox]
type = dropbox
token = {"access_token":"sl.Aue8VvGHWzFeICVgJqzAQR1pJ3BnmHvICuIPVnIEAQgrwQn8TFeb44qSw7fhqchgshG7EtgagfaG4mhsVqBrIjw7Bs57BChJgGqnOYQABLvSSic_dbtfvQpqelczNWzl","token_type":"bearer","refresh_token":"MJVuIBgVklgaeAAAAAd9M5mMTPq_F8vKUkaMFX6LX2E-y3TnmXN9bnOMxcwvj","expiry":"2021-04-07T03:31:32.562859+08:00"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y //信息无误的话,选择 y 就可以了

确认无误之后按下 y 键,Dropbox 就算是配置好了。rclone 会自动返回 config 的初始界面,这时候界面下面就会多出刚刚添加的「Dropbox」了。接下来如果没有其它操作,就可以按下 q 退出了。

Current remotes:

Name                 Type
====                 ====
dropbox              dropbox

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>  //没有其它操作的话就可以按下 q 退出配置界面了
2. 配置 Google Drive 云盘

其实网络上已经很多关于 Rclone 挂载 Google Drive 的教程了,刚接触 Rclone 的时候我也是看他们的教程来操作的,不过既然开始写了,就也说说吧。

前期的操作和配置 Dropbox 是一样的,先输入 rclone config 进入配置界面;

# 进入 rclone 的配置操作
❯ rclone config
n) New remote - 新建链接
d) Delete remote - 删除链接
r) Rename remote - 重命名链接
c) Copy remote - 复制链接
s) Set configuration password - 设置配置密码
q) Quit config - 退出配置操作
e/n/d/r/c/s/q> n //根据自己的需求进行选择
# 设置云存储名字
name> gdrive //输入一个网盘的名字,可以自由设置

# 选择云存储类型
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
……(不同 rclone 版本的序号不一定相同)
Storage> drive //可以输入数字编号,或者对应的双引号中的代号
# 输入自定义 App ID 这里可以也可以不用填写,想深入了解可以查看它提供的网址
Google Application Client Id
Setting your own is recommended.
# See https://rclone.org/drive/ Making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> //默认留空,直接按回车下一步

# 输入自定义 App 的密码,这里也可以不用填写
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> //默认留空,直接按回车下一步

# 这里要选择对应的权限
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 //根据需求选择权限,1 是可读写,2 是只读,3 是仅可访问 rclone 创建的文件
# 下面两项一般情况下直接回车跳过
ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.

Enter a string value. Press Enter for the default ("").
root_folder_id> //选择挂载云存储的目录,留空的话为根目录,直接回车

Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.

Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.

Enter a string value. Press Enter for the default ("").
service_account_file>  // 这一步直接回车就行

# 是否编辑高级设置,一般选否
Edit advanced config? (y/n)
y) Yes
n) No
y/n> //选择 No,按 n 键

# 是否远程配置(这里根据情况选择,dropbox 的配置相对麻烦一点)
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No 
y/n> //机器有图形界面和浏览器选择 y,只有命令行界面的话选择 n

在配置 Google Drive 的时候,这一步对于没有图形界面的用户来说,比 Dropbox 简单很多。当你选择 n 的时候,界面会提供一个链接,你可以直接复制链接在其它机器的浏览器打开来完成授权。授权完成后浏览器会提供一段 token,这时候复制这段 token 直接粘贴到 rclone 配置界面就可以了。

# rclone 提供的链接,访问后获取到的 token 就粘贴在下面就行
Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=Q1DOn4mlJwf_ESrIz_---w
Log in and authorize rclone for access
Enter verification code> //输入返回的 token 并回车

最后确认信息无误,按下 y 即可完成 Google Drive 的配置。在没有进一步需求的情况下就可以按下 q 退出配置界面啦。


其它的云存储绑定方法其实大同小异,根据提示操作就行了,虽然都是英文提示,但是都不是很难的英文,实在不懂就用用翻译软件看一下就行了。

Rclone 的功能很多,我现在除了用它来挂载 Google Drive 到树莓派上,通过 Plex 搭建了一个个人媒体库外,也用它来备份我的 VPS 数据。之后可以我也写一下自己怎么使用 Rclone 的吧。