0%

使用rclone挂载onedriver

rclone是一个从云端同步文件的命令行工具,常见的网盘和协议基本上都支持,更加详细的介绍可以查看官网。这还是一个开源项目,github

环境准备

  • 一台可以正常运行的树莓派
  • 宝塔面板
    • 新增网站和配置反向代理。如果可以手动配置,也可以不安装。
  • OneDriver账号,OneDriver个人账户可能无法正常登录,推荐OneDriver for business。

安装rclone

参考文档:https://rclone.org/downloads/

使用以下命令一键安装。如果因为网速原因导致下载失败,可以参考安装脚本的流程,手动下载并安装。

1
curl https://rclone.org/install.sh | sudo bash

Ubuntu系统可以直接使用apt安装。

1
apt-get install rclone

安装完成之后,可以执行以下测试命令,测试是否安装成功。如果提示rclone: command not found,则表示安装失败。否则安装成功。

1
rclone --help

rclone配置OneDriver账户

参考文档:https://rclone.org/onedrive/

获取客户ID和密码

以下是官方给出的获取步骤,注意保存好客户端ID和密码,关闭页面后将无法再次查看。第5步给API设置的权限,不能少,否则会影响后续对文件的操作。

  1. Open https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade, then click New registration.
  2. Enter a name for your app, choose account type Any Azure AD directory - Multitenant, select Web in Redirect URI Enter http://localhost:53682/ and click Register. Copy and keep the Application (client) ID under the app name for later use.
  3. Under manage select Certificates & secrets, click New client secret. Copy and keep that secret for later use.
  4. Under manage select API permissions, click Add a permission and select Microsoft Graph then select delegated permissions.
  5. Search and select the follwing permssions: Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, offline_access, User.Read. Once selected click Add permissions at the bottom.

配置反向代理

在配置rclone的过程中,需要弹出浏览器进行认证。由于树莓派没有图形界面,所以需要曲线救国,有三种思路:

  1. 先在Windows下安装rclone,并配置好OneDrive,获取token。最后在树莓派上使用token设置OneDrive,具体步骤可以参考这篇博客
  2. 利用反向代理,实现间接访问。需要使用宝塔配置反向代理。
  3. 树莓派安装浏览器后通过xserver访问。对机器的性能有一定的要求。

配置反向代理的步骤:

  1. 进入宝塔面板的安全界面,打开53682端口。
  2. 进入宝塔面板的网站界面,添加一个站点。假设域名是rclone.pi3b.inc
  3. 点击新增网站的设置按钮,进入反向代理,将网站代理到http://127.0.0.1:53682
  4. 添加域名的DNS解析。

rclone链接OneDrive账户

在终端输入rclone config,进入配置界面。这里选择n,创建一个新的remote。

1
2
3
4
5
6
7
$ rclone config
2021/10/02 02:33:08 NOTICE: Config file "/home/pk/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

输入n之后点击回车,这时要为remote设置一个名字。后续的操作都是通过此name要完成。

1
2
n/s/q> n
name> remote_name

设置好name之后,点击回车,接下来选项存储类型。rclone支持非常多的存储类型,这里选项Microsoft OneDrive。不同版本的rclone,前面的数字可能不一样,注意不要选错了。

1
2
3
4
5
6
7
8
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
......
26 / Microsoft OneDrive
\ "onedrive"
......
Storage> 26

点击回车后,接着需要输入应用程序ID和密码。如果不知道这两个值,可以往前看看获取客户ID和密码

1
2
3
4
5
6
7
8
9
Storage> 26
OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> xxxxxxxxx

选择账户的类型,根据自己的实际情况选项。

1
2
3
4
5
6
7
8
9
10
11
12
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value
1 / Microsoft Cloud Global
\ "global"
2 / Microsoft Cloud for US Government
\ "us"
3 / Microsoft Cloud Germany
\ "de"
4 / Azure and Office 365 operated by 21Vianet in China
\ "cn"
region> 1

是否需要高级配置,这里选项n。

1
2
3
4
Edit advanced config?
y) Yes
n) No (default)
y/n> n

选择是否自动配置,

1
2
3
4
5
6
7
8
9
10
11
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n> y
2021/10/02 02:46:56 NOTICE: Make sure your Redirect URL is set to "http://localhost:53682/" in your custom config.
2021/10/02 02:46:56 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=vtjMEdIokDmhiFPM0xa18A
2021/10/02 02:46:56 NOTICE: Log in and authorize rclone for access
2021/10/02 02:46:56 NOTICE: Waiting for code...

需要访问提示信息中出现的网址。将127.0.0.1:53682替换成反向代理的自定义域名,这里是rclone.pi3b.inc,即访问http://rclone.pi3b.inc/auth?state=vtjMEdIokDmhiFPM0xa18A。会出现下图,点击接受。

点击接受之后,会跳转到localhost:53682,一样替换成反向代理的域名,这里就授权成功了。

选择连接的类型,根据自己的实际情况选择即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Type of connection
Enter a string value. Press Enter for the default ("onedrive").
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
3 / Sharepoint site name or URL (e.g. mysite or https://contoso.sharepoint.com/sites/mysite)
\ "url"
4 / Search for a Sharepoint site
\ "search"
5 / Type in driveID (advanced)
\ "driveid"
6 / Type in SiteID (advanced)
\ "siteid"
7 / Sharepoint server-relative path (advanced, e.g. /teams/hr)
\ "path"
config_type> 3

输入sharepoint网站的url

1
2
3
4
5
6
Site URL

Example: "https://contoso.sharepoint.com/sites/mysite" or "mysite"

Enter a string value. Press Enter for the default ("").
config_site_url>

后续:略。

rclone操作云端文件

rclone支持非常多操作文件的命令,可以rclone -hman rclone查看帮助。

挂载

1
2
3
4
5
6
7
8
9
10
#安装fuse
yum -y install fuse
#创建挂载目录
mkdir -p /home/onedrive
#挂载
rclone mount remote_name:path/to/files /home/onedrive
#如果需要后台保持运行,使用下面的命令
nohup rclone mount remote:path/to/files /home/onedrive &
# 检查挂载是否成功
df -h

remote_name是设置OneDrive时配置的名字。

复制文件

1
rclone copy source:path dest:path [flags]
  • Use the -P/--progress flag to view real-time transfer statistics.
  • Use the --dry-run or the --interactive/-i flag to test without copying anything.