CentOS 使用 rclone 挂载 OneDrive

封面:贝蒂(Pixiv ID:58416341)

掐指一算发现二月都快结束了,想想自己都已经月更了还断更就太丢脸了,不想断更无奈之下把最近折腾过的 rclone 挂载整理一下,其实大部分过程都是跟着网上的教程来的。


那么我们现在开始。

准备:

  • 安装好 rclone,包括本机和需要挂载的机器;
  • 可以正常使用的 OneDrive 网盘;
  • 网络通畅。

由于之前挂载的时候没有截图,考虑到删掉再挂载会带来不必要的麻烦,整理的内容就不配图了,尽量用代码来复现一下,后期可能会考虑补图。

安装过程请参考官网,不再赘述。

获取 Token

这一步需要在可以进行网页交互(打开浏览器)的机子上进行,我需要挂载的环境是 CentOS 的 VPS,并没有安装桌面环境,所以在本机上完成。

rclone config

执行命令进入 rclone 配置:

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> n

输入 n 并回车,表示进入 New remote 配置向导。
当然,如果是首次运行,会提示配置文件不存在,此时只有 n/s/q 三个选项,同样选择 n

name> remote

向导首先询问你对这个 remote 的命名。输入你希望的命名即可,这里使用 remote 举例。

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
[snip]
XX / Microsoft OneDrive
   \ "onedrive"
[snip]
Storage> onedrive

下一步需要设置储存的类型,可以找到 Microsoft OneDrive 对应的数字序号(因版本而异)或直接键入 onedrive
回车进行下一步配置。

Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
Microsoft 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> n
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

全部采用默认配置,无脑四次回车即可。
第一次是询问 Client Id,这个东西不是必要的,留空即可。
第二次是询问 Client Secret,同理。
第三次询问是否更改高级配置,一般不用更改,键入 n 或直接回车均可。
第四次询问是否自动获取 Token,这一步就是来干这个的,键入 y 或直接回车均可。

此时你的浏览器应该会打开,然后转到授权页面,登录你的账户完成授权即可。如果浏览器没有自动打开,注意看输出中的这些内容:

If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...

手动打开里面的网址,即可转到授权页面。

注意:之后的步骤中可能会出现意料之外的错误,多半是 rclone 远端抽风,如果一直搞不定建议换个线路或者洗洗脸再搞。

Got code
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Sharepoint site
   \ "sharepoint"
 3 / Type in driveID
   \ "driveid"
 4 / Type in SiteID
   \ "siteid"
 5 / Search a Sharepoint site
   \ "search"
Your choice> 1

成功获取 code,然后我们需要选择储存类型,这里选择 onedrive,输入 1 或直接键入 onedrive 均可。

Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!Eqwertyuiopasdfghjklzxcvbnm-7mnbvcxzlkjhgfdsapoiuytrewqk
Chose drive to use:> 0

rclone 将自动获取 drives,一般只有一个,选择一个作为 remote,键入希望选择的那一个 drive 前面的编号即可。

Found drive 'root' of type 'business', URL: https://org-my.sharepoint.com/personal/you/Documents
Is that okay?
y) Yes
n) No
y/n> y
--------------------
[remote]
type = onedrive
token = {"access_token":"youraccesstoken","token_type":"Bearer","refresh_token":"yourrefreshtoken","expiry":"2018-08-26T22:39:52.486512262+08:00"}
drive_id = b!Eqwertyuiopasdfghjklzxcvbnm-7mnbvcxzlkjhgfdsapoiuytrewqk
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

确认后获取到 Token。注意,需要复制的是整个大括号里面的内容(包括大括号),那就是我们拿到的 Token。

如果在控制台直接复制比较麻烦,也可以保存后从配置文件中复制。

在目标机器上配置

同样执行:

rclone config

步骤与在本机时类似,但是当询问是否自动获取 Token 时,回答 n 并将前面的 Token 粘贴入。

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> n
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine (same rclone version recommended) :
        rclone authorize "onedrive"
Then paste the result below:
result> {"access_token":"youraccesstoken","token_type":"Bearer","refresh_token":"yourrefreshtoken","expiry":"2018-08-26T22:39:52.486512262+08:00"}

之后过程相同,成功保存后即完成配置。

rclone mount

详细参考:rclone mount

rclone mount remote:path /path/to/mountpoint [flags]

其中参数的含义:

  • remote:你希望挂载的 remote 的名称;
  • path:远程挂载目录,填入 / 是根目录,也可以指定子目录;
  • /path/to/mountpoint:挂载点路径,预先建好这个目录;
  • [flags]:用于控制挂载行为的可选参数,可以不填。

测试:

$ mkdir /mnt/onedrive
$ rclone mount remote:/ /mnt/onedrive

跑起来之后终端上将没有什么反应,在 /mnt/onedrive 目录下新建文件,稍后可以看见自动同步到了 OneDrive。

作为服务启动

/usr/lib/systemd/system 目录下新建一个以 .service 结尾的文件,作为服务配置文件。例如 rc-onedrive.service

[Unit]
Description=rclone mount onedrive
After=network.target remote-fs.target nss-lookup.target
[Service]
User=root
Type=notify
PIDFile=/run/rc-onedrive.pid
ExecStart=/usr/bin/rclone mount remote:/ /mnt/onedrive --allow-other --allow-non-empty
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-abort
[Install]
WantedBy=multi-user.target

然后运行:

$ systemctl enable rc-onedrive
$ systemctl start rc-onedrive

即可开机自启,并通过服务管理 rclone 挂载。

详细的参数说明这里先不写(困[heimu]鸽[/heimu]了),以后补上。(会补的会补的一定不鸽)

那么我们的整个过程就告一段落了,之后要干什么就请随意咯~


参考:

最后修改:2020 年 03 月 05 日 08 : 45 AM
欢迎投食喵 ~

发表评论 取消回复

2 条评论

  1. 吾梦一梦

    巨佬又开始水了

  2. LOIKO

    苣佬也玩onedrive啦~