使用Rclone远程挂载Onedrive拓展服务器存储

 · 15 分钟阅读
 · 教授
文章目录

先决条件


  • 已有云计算实例,服务器操作系统为Ubuntu

  • 已有微软开发者E5账号,本次使用非管理员账号

    1. 前往Azure Portal,点击新注册
    2. 输入名称,账户类型选择任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户和个人 Microsoft 帐户(例如,Skype、Xbox)重定向 URI (可选)选择Web,地址输入http://localhost:53682/(建议手动输入,以免出错),点击注册。复制并保存显示名称下方的Application (client) ID 应用程序(客户端) ID,以供后续使用:b2...ef
    3. 在左侧管理菜单下,选择证书和密码,点击新客户端密码,输入说明,截止日期选730天(24个月),点击添加,获取客户端密码,保存备用(仅可见一次):.m28...ocqx
    4. 在左侧管理菜单下,选择API权限,点击添加权限,选择Microsoft Graph,选择委托的权限,搜索并选择这些权限: Files.ReadFiles.ReadWriteFiles.Read.AllFiles.ReadWrite.Alloffline_accessUser.Read 和 Sites.Read.All  (如果有定制过访问范围,也需选择对应权限)。 选择完成后,点击添加权限 
  • 如果使用商业版账号

    1. 重复1-4步,并需要增加额外步骤。注意在第2步选择账户类型时,要选择Accounts in this organizational directory only (*** - Single tenant)
    2. 前往Azure Portal获取租户IDe9...56
    3. 在配置时 rclone config, 设置 auth_url 为: https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/authorize
    4. 在配置时 rclone config, 设置 token_url 为 : https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/v2.0/token
  • 提前在官网https://rclone.org/downloads/下载好Rclone软件,并解压至本地备用

服务器端配置


1. 安装Rclone软件

输入官网安装脚本(此处使用的是非root账户执行,其实感觉使用root运行后续会更顺利一些):

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

输出示例:

$ sudo -v ; curl https://rclone.org/install.sh | sudo bash

# 输出:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4707  100  4707    0     0   4085      0  0:00:01  0:00:01 --:--:--  4089
Archive:  rclone-current-linux-arm64.zip
   creating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/
  inflating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/rclone  [binary]
  inflating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/README.txt  [text]  
  inflating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/git-log.txt  [text]  
  inflating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/rclone.1  [text]  
  inflating: tmp_unzip_dir_for_rclone/rclone-v1.63.0-linux-arm64/README.html  [text]  
Purging old database entries in /usr/share/man...
Processing manual pages under /usr/share/man...
Purging old database entries in /usr/share/man/zh_CN...
Processing manual pages under /usr/share/man/zh_CN...
Purging old database entries in /usr/share/man/fr...
Processing manual pages under /usr/share/man/fr...
Processing manual pages under /usr/local/man...
mandb: can't update index cache /var/cache/man/oldlocal/267107: No such file or directory
Updating index cache for path `/usr/local/man/man1'. Wait...done.
Checking for stray cats under /usr/local/man...
Checking for stray cats under /var/cache/man/oldlocal...
1 man subdirectory contained newer manual pages.
1 manual page was added.
0 stray cats were added.
0 old database entries were purged.

rclone v1.63.0 has successfully installed.
Now run "rclone config" for setup. Check https://rclone.org/docs/ for more details.

2. 配置Rclone:

rclone config

交互式操作,按实际情况进行选择:

$ rclone config

# 输出:
2023/07/04 15:59:21 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/s/q> n

Enter name for new remote.
name> onedrive

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
 2 / Akamai NetStorage
   \ (netstorage)
 3 / Alias for an existing remote
   \ (alias)
 4 / Amazon Drive
   \ (amazon cloud drive)
 5 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, ArvanCloud, Ceph, China Mobile, Cloudflare, GCS, DigitalOcean, Dreamhost, Huawei OBS, IBM COS, IDrive e2, IONOS Cloud, Liara, Lyve Cloud, Minio, Netease, Petabox, RackCorp, Scaleway, SeaweedFS, StackPath, Storj, Tencent COS, Qiniu and Wasabi
   \ (s3)
 6 / Backblaze B2
   \ (b2)
 7 / Better checksums for other remotes
   \ (hasher)
 8 / Box
   \ (box)
 9 / Cache a remote
   \ (cache)
10 / Citrix Sharefile
   \ (sharefile)
11 / Combine several remotes into one
   \ (combine)
12 / Compress a remote
   \ (compress)
13 / Dropbox
   \ (dropbox)
14 / Encrypt/Decrypt a remote
   \ (crypt)
15 / Enterprise File Fabric
   \ (filefabric)
16 / FTP
   \ (ftp)
17 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
18 / Google Drive
   \ (drive)
19 / Google Photos
   \ (google photos)
20 / HTTP
   \ (http)
21 / Hadoop distributed file system
   \ (hdfs)
22 / HiDrive
   \ (hidrive)
23 / In memory object storage system.
   \ (memory)
24 / Internet Archive
   \ (internetarchive)
25 / Jottacloud
   \ (jottacloud)
26 / Koofr, Digi Storage and other Koofr-compatible storage providers
   \ (koofr)
27 / Local Disk
   \ (local)
28 / Mail.ru Cloud
   \ (mailru)
29 / Mega
   \ (mega)
30 / Microsoft Azure Blob Storage
   \ (azureblob)
31 / Microsoft OneDrive
   \ (onedrive)
32 / OpenDrive
   \ (opendrive)
33 / OpenStack Swift (Rackspace Cloud Files, Blomp Cloud Storage, Memset Memstore, OVH)
   \ (swift)
34 / Oracle Cloud Infrastructure Object Storage
   \ (oracleobjectstorage)
35 / Pcloud
   \ (pcloud)
36 / PikPak
   \ (pikpak)
37 / Put.io
   \ (putio)
38 / QingCloud Object Storage
   \ (qingstor)
39 / SMB / CIFS
   \ (smb)
40 / SSH/SFTP
   \ (sftp)
41 / Sia Decentralized Cloud
   \ (sia)
42 / Storj Decentralized Cloud Storage
   \ (storj)
43 / Sugarsync
   \ (sugarsync)
44 / Transparently chunk/split large files
   \ (chunker)
45 / Union merges the contents of several upstream fs
   \ (union)
46 / Uptobox
   \ (uptobox)
47 / WebDAV
   \ (webdav)
48 / Yandex Disk
   \ (yandex)
49 / Zoho
   \ (zoho)
50 / premiumize.me
   \ (premiumizeme)
51 / seafile
   \ (seafile)
Storage> 31

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

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

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

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

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" "eyJjbGl...b2NxeCJ9"
Then paste the result.
Enter a value.
config_token>

接下来,需要前往本机进行执行授权操作,进入本机Rclone目录后执行:

./rclone authorize "onedrive" "eyJjbGl...b2NxeCJ9"

注意:执行后,会唤出浏览器进行授权,成功后即可在终端中获取到config_token。但是需要注意的是,由于Rclone自身的bug,在本机展示的token内容可能并不全,比如:

2023/07/04 23:19:39 NOTICE: Got code
Paste the following into your remote machine --->
eyJ0b2tlbi...In0ifQ
<---End paste

如果直接从终端中复制该段内容,粘贴至服务器端时会报错,需要使用rclone config reconnect onedrive:命令重新输入,但即便多试几次也依然无法成功。官方给出的workaround其实也很简单,就是将rclone config过程在本地完整执行一遍,用本地的配置文件(文件位置查询命令rclone config file)替换远程的配置文件。我们此处只需获取到完整的token信息即可:

- token: {"access_token":"eyJ0eX...chVKv-FC","expiry":"2023-07-05T00:29:16.923549+08:00"}

将token信息粘贴进远程终端即可顺利完成后续的交互操作:

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

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!a_9W...50kT).
 1 / OneDrive (business)
   \ (b!a_9W...50kT)
config_driveid> 1

Drive OK?

Found drive "root" of type "business"
URL: https://xxxx-my.sharepoint.com/personal/xxx_xxx_com/Documents

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

Configuration complete.
Options:
- type: onedrive
- client_id: b2...ef
- client_secret: .m28...ocqx
- token: {"access_token":"eyJ0eX...chVKv-FC","expiry":"2023-07-05T00:29:16.923549+08:00"}
- drive_id: b!a_9W...50kT
- drive_type: business
Keep this "onedrive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

Name                 Type
====                 ====
onedrive             onedrive

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

至此,Rclone的配置基本完成,接下来我们完成挂载。

3. 远程网盘挂载:

在服务器上创建好/onedrive文件夹,作为远程网盘映射到本地的文件夹。通过下方命令进行临时挂载:

rclone mount onedrive:/ /onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 --vfs-cache-mode writes

如果出现类似下方的错误:

2023/07/04 23:41:40 Fatal error: failed to mount FUSE fs: mount stopped before calling Init: mount failed: cgofuse: cannot find FUSE

或者类似这类错误:

2023/07/04 16:31:23 Fatal error: failed to mount FUSE fs: fusermount: exec: "fusermount3": executable file not found in $PATH

可以通过安装对应版本的软件进行解决:

apt install fuse3

依赖的软件安装完成后,重新进行挂载即可。挂载执行后,可以新开终端访问服务器,使用df -h验证是否挂载成功。如果挂载成功,即可配置后续的开机自启服务。

4. 配置开机自启服务

首先,创建Rclone的service:

command="mount onedrive:/ /onedrive --default-permissions --transfers 4 --buffer-size 32M --low-level-retries 200 --dir-cache-time 6h --vfs-read-chunk-size 64M --vfs-read-chunk-size-limit 1G --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 --vfs-cache-mode writes"

并将command加入service配置中:

cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

[Service]
Type=simple
ExecStart=$(command -v rclone) ${command}
Restart=on-abort
User=root

[Install]
WantedBy=default.target
EOF

或者直接

cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/rclone mount onedrive:/ /data/onedrive \
 --umask 0000 \
 --default-permissions \
 --allow-non-empty \
 --allow-other \
 --transfers 4 \
 --buffer-size 32M \
 --low-level-retries 200 \
 --dir-cache-time 6h \
 --vfs-read-chunk-size 64M \
 --vfs-read-chunk-size-limit 1G \
 --vfs-cache-mode writes
Restart=on-abort
User=root

[Install]
WantedBy=default.target
EOF

然后,重新加载daemon,让服务文件生效:

systemctl daemon-reload
systemctl start rclone
systemctl enable rclone

输出:

Created symlink /etc/systemd/system/default.target.wants/rclone.service  /etc/systemd/system/rclone.service.

其他Rclone服务相关命令供参考:

# 重启:
systemctl restart rclone 

# 停止:
systemctl stop rclone 

# 状态:
systemctl status rclone

最后,通过df -h查看磁盘是否成功挂载,结果棒棒哒:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           2.4G  2.3M  2.4G   1% /run
/dev/sda1       146G  7.3G  138G   6% /
tmpfs            12G     0   12G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/sda15       98M  6.3M   92M   7% /boot/efi
tmpfs           2.4G  4.0K  2.4G   1% /run/user/1001
onedrive:       5.0T  1.7M  5.0T   1% /onedrive

总结


Rclone简直就是神仙软件,不止是支持OneDrive,像是常见的网盘服务和对象存储服务统统都能支持。不得不说,这款软件实在是过于强大。更重要的是,挂载好网盘之后,配合其他开源程序还能有更多实用的玩法,比如自动备份、图床支持等等。本次配置过程中,遇到的问题主要是配置服务器端config_token会遇到bug,首次部署时极易踩坑。

参考资料