Administrator
Published on 2024-06-07 / 57 Visits
0

Linux使用Rclone挂载OneDrive

因为VPS磁盘是需要收费的很多东西太大了移动也很麻烦(emby的刮削数据就有40g),刚好用到Office365 E5 订阅的OneDrive 5T的空间,速度也很快

使用脚本下载安装 Rclone:

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

Rclone初始化配置

rclone config
2024/06/07 13:50:30 NOTICE: Config file "/root/.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,新建配置,会提示输入name,自由命名,我这用的是网盘帐号

Enter name for new remote.
name> 1qaz12-my

然后会出现很多服务类型,自己查看Microsoft OneDrive对应的序号,之后提示输入client_idclient_secret,直接回车

Storage> 33

Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> 

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> 

选择区域,选择1全球

Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
 1 / Microsoft Cloud Global
   \ (global)
 2 / Microsoft Cloud for US Government
   \ (us)
 3 / Microsoft Cloud Germany
   \ (de)
 4 / Azure and Office 365 operated by Vnet Group in China
   \ (cn)
region> 1

接下来高级配置和自动配置都选择n,我们手动授权配置

Edit advanced config?
y) Yes
n) No (default)
y/n> n  

Use web browser to automatically authenticate rclone with remote?
 * Say Y if the machine running rclone has a web browser you can use
 * Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.

y) Yes (default)
n) No
y/n> n

接下来会让输入config_token,先暂停,token通过电脑获取

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
        rclone authorize "onedrive" "eyJkZXbiI6IiJ9"
Then paste the result.
Enter a value.
config_token> 

官网下载Rclone是,我是Windows 11直接下载Intel/AMD - 64 Bit压缩包,解压出来,使用Win+xA打开PowerShell,cd到解压目录

cd "F:\下载\rclone-v1.66.0-windows-amd64"

执行授权,会打开微软的授权验证,如果没登录会让登录,输入帐号密码,最后点接受,提示成功后返回PowerShell,会有access_token,直接把整个{}拷贝粘帖到上面要输入config_tokenLiunx终端中

./rclone authorize "onedrive"

选择OneDrive类型,输入1

Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
 1 / OneDrive Personal or Business
   \ (onedrive)
 2 / Root Sharepoint site
   \ (sharepoint)
   / Sharepoint site name or URL
 3 | 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)
   / Sharepoint server-relative path (advanced)
 7 | E.g. /teams/hr
   \ (path)
config_type> 1

查找OneDrive帐号,根据显示的序号输入

Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own string value.
Press Enter for the default (b!GdsDbxpdf06U2EZB3rMvrNbd-F8qY0gjbUJoM80RueRLOK).
 1 / OneDrive (business)
   \ (b!GdsDbxpdf06U2EZB3rMvrNbd-F8q__b-EZRueRLOK)
config_driveid> 1

输入y确认,之后会显示配置信息再输入y

Drive OK?

Found drive "root" of type "business"
URL: https://1qaz12-my.sharepoint.com/personal/xft_com/Documents

y) Yes (default)
n) No
y/n> y

配置完成,输入q退出

尝试挂载OneDrive

rclone mount configName:OneDriveLocation dir --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 --vfs-cache-mode writes
  • mount: 指定要执行的操作是挂载远程文件夹。

  • configName:OneDriveLocation: 指定要挂载的 OneDrive 文件夹路径。

    • configName 是你在 rclone config 中配置的 OneDrive 远程连接的名称。

    • OneDriveLocation 是你要挂载的 OneDrive 文件夹的路径,例如 /Documents/Pictures

  • dir: 指定要将 OneDrive 文件夹挂载到的本地目录。

  • --copy-links: 将符号链接复制为文件,而不是保留它们作为链接。

  • --no-gzip-encoding: 禁用 gzip 编码,这可能会提高某些操作的速度。

  • --no-check-certificate: 禁用 SSL 证书验证,这在使用自签名证书时很有用,但会降低安全性。

  • --allow-other: 允许其他用户访问挂载的文件夹。

  • --allow-non-empty: 允许将 OneDrive 文件夹挂载到非空目录。

  • --umask 000: 设置挂载文件夹的默认权限掩码,000 表示所有用户都拥有所有权限

  • --vfs-cache-mode:缓存模式,有两种writesfull,如果不需要缓存可以删除或者设置为off

    • 如果你主要使用 OneDrive 来存储和访问文件,很少进行修改,那么 --vfs-cache-mode writes 是一个不错的选择。

    • 如果你经常编辑和保存 OneDrive 上的文件,那么 --vfs-cache-mode full 会提供更好的性能。

例如我的1qaz12-my是之前设置的,/OneDrive的根目录挂载到/mnt/onedrive,成功之后就可以查看了

rclone mount 1qaz12-my:/ /mnt/onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

如果出现错误,像挂载的目录不存在(先mkdir)、缺少组件(先安装)

2024/06/07 14:21:57 Fatal error: failed to mount FUSE fs: mountpoint does not exist: /mnt/onedrive

自动挂载

上面是跑一个进程来挂载,我们通过配置服务来自动挂载

1. 创建一个 systemd 服务文件:

  • 创建一个名为 rclone-onedrive.service 的新文件:

    sudo nano /etc/systemd/system/rclone-onedrive.service
  • 将以下内容粘贴到文件中,并将ExecStart后面的替换成你刚刚上面执行的代码:

    [Unit]
    Description=Mount OneDrive
    After=network-online.target
    
    [Service]
    Type=notify
    ExecStart=/usr/bin/rclone mount 1qaz12-my:/ /mnt/onedrive \
        --copy-links \
        --no-gzip-encoding \
        --no-check-certificate \
        --allow-other \
        --allow-non-empty \
        --umask 000
    ExecStop=/bin/fusermount -u /mnt/onedrive
    Restart=on-failure
    RestartSec=5
    User=root
    
    [Install]
    WantedBy=multi-user.target
  • Ctrl+xy回车保存并关闭文件。

2. 重新加载 systemd 配置:

sudo systemctl daemon-reload

3. 启用并启动服务:

sudo systemctl enable rclone-onedrive.service
sudo systemctl start rclone-onedrive.service

4. 验证挂载:

df -h | grep onedrive

你应该能看到 /mnt/onedrive 挂载点。