本文介绍了 Rclone 挂载 OneDrive,挂载服务器的本地目录到 OneDrive 的远程目录。

一、获取授权码

Step1:下载并解压 Rclone

下载地址:https://rclone.org/downloads/

Step2:获取授权码

请按下 Ctrl+R 键,输入 cmd,然后回车,打开命令提示符。

请依次输入下面的命令:

//进入 Rclone 的所在目录
cd /d C:\Users\Jianrry\Downloads\rclone-v1.57.0-windows-amd64

//获取 OneDrive 授权码
rclone authorize "onedrive"

命令提示符中会出现以下的提示信息:

2022/02/05 15:19:06 NOTICE: Config file "C:\\Users\\Jianrry\\AppData\\Roaming\\rclone\\rclone.conf" not found - using defaults
2022/02/05 15:19:07 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=KwmAtsj5QMDb3N_yCs_EYw
2022/02/05 15:19:07 NOTICE: Log in and authorize rclone for access
2022/02/05 15:19:07 NOTICE: Waiting for code...

请复制 http://127.0.0.1:53682/auth?state= 开头的链接,到浏览器中打开。

如果这个过程中出现了错误,则重复上一步的操作即可。

命令提示符中会出现类似的提示信息:

{} 之间的全部内容就是授权码,请保存授权码,下文中会用到。

二、安装 Rclone

Step1:安装 Rclone

请输入下面的命令,安装 Rclone,适用于 Linux/macOS/BSD。

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

Step2:配置 Rclone

请输入下面的命令,配置 Rclone。

rclone config

① 新建一个远程连接。

ubuntu@Azure-HK:~$ rclone config
2022/02/05 09:07:49 NOTICE: Config file "/home/ubuntu/.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

② 输入该连接的名字。

name> onedrive

可以随意输入,请保存该连接名,下文中会用到。

③ 选择该连接的类型。

Option Storage.
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.
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Better checksums for other remotes
   \ "hasher"
 7 / Box
   \ "box"
 8 / Cache a remote
   \ "cache"
 9 / Citrix Sharefile
   \ "sharefile"
10 / Compress a remote
   \ "compress"
11 / Dropbox
   \ "dropbox"
12 / Encrypt/Decrypt a remote
   \ "crypt"
13 / Enterprise File Fabric
   \ "filefabric"
14 / FTP Connection
   \ "ftp"
15 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
16 / Google Drive
   \ "drive"
17 / Google Photos
   \ "google photos"
18 / Hadoop distributed file system
   \ "hdfs"
19 / Hubic
   \ "hubic"
20 / In memory object storage system.
   \ "memory"
21 / Jottacloud
   \ "jottacloud"
22 / Koofr
   \ "koofr"
23 / Local Disk
   \ "local"
24 / Mail.ru Cloud
   \ "mailru"
25 / Mega
   \ "mega"
26 / Microsoft Azure Blob Storage
   \ "azureblob"
27 / Microsoft OneDrive
   \ "onedrive"
28 / OpenDrive
   \ "opendrive"
29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
30 / Pcloud
   \ "pcloud"
31 / Put.io
   \ "putio"
32 / QingCloud Object Storage
   \ "qingstor"
33 / SSH/SFTP Connection
   \ "sftp"
34 / Sia Decentralized Cloud
   \ "sia"
35 / Sugarsync
   \ "sugarsync"
36 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
37 / Transparently chunk/split large files
   \ "chunker"
38 / Union merges the contents of several upstream fs
   \ "union"
39 / Uptobox
   \ "uptobox"
40 / Webdav
   \ "webdav"
41 / Yandex Disk
   \ "yandex"
42 / Zoho
   \ "zoho"
43 / http Connection
   \ "http"
44 / premiumize.me
   \ "premiumizeme"
45 / seafile
   \ "seafile"
Storage> 27

请输入前面的序号,序号随时可能发生变化,请确认无误之后再输入。

我选择的是 OneDrive,输入的是 27。

④ 输入客户端 ID。

Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>

不需要填写,请直接按下回车键。

⑤ 输入客户端密钥。

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
Option region.

不需要填写,请直接按下回车键。

⑥ 选择 OneDrive 区域。

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

请输入前面的序号,选择 OneDrive 区域。

我的 OneDrive 是国际版,输入的是 1.

⑦ 编辑高级设置。

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

不需要填写,请直接按下回车键。

⑧ 自动配置。

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> 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"
Then paste the result.
Enter a string value. Press Enter for the default ("").
config_token> ...

请输入上文中提到的授权码。

⑩ 选择 OneDrive 账户的类型。

Option config_type.
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"
   / 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 是个人版,输入的是 1。

⑪ 确认。

Drive OK?

Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=7f2fe84d72080a6b

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

不需要输入,请直接按下回车键。

⑫ 再次确认。

--------------------
[onedrive]
type = onedrive
token = ...
drive_id = 7f2fe84d72080a6b
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d>

不需要输入,请直接按下回车键。

⑬ 退出。

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

请输入 q

三、Rclone 挂载 OneDrive

Step1:新建一个本地目录

请输入下面的命令,新建一个本地目录。

mkdir -p /home/ubuntu/rclone/onedrive

请将 /home/ubuntu/rclone/onedrive 修改为你的本地目录。

Step2:Rclone 挂载 OneDrive

请确保本地目录和 OneDrive 远程目录已存在,然后输入下面的命令,挂载本地目录到 OneDrive 远程目录。

rclone mount onedrive:rclone/onedrive /home/ubuntu/rclone/onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

请将 onedrive 修改为你的连接名,rclone/onedrive 修改为你的 OneDrive 远程目录,/home/ubuntu/rclone/onedrive 修改为你的本地目录。

如果挂载过程中提示 mount helper error: fusermount: option allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf,则请在 /etc/fuse.conf 文件的末尾添加 user_allow_other,然后重新挂载即可。

挂载过程中长时间无响应,这属于正常现象,实际上已经挂载成功了。

Step3:检查是否挂载成功

请输入下面的命令,检查是否挂载成功。

df -h

如果终端中出现了类似的提示信息,则说明已经挂载成功了。

ubuntu@Azure-HK:~$ df -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/root                  62G  1.5G   61G   3% /
devtmpfs                  452M     0  452M   0% /dev
tmpfs                     456M     0  456M   0% /dev/shm
tmpfs                      92M  1.1M   91M   2% /run
tmpfs                     5.0M     0  5.0M   0% /run/lock
tmpfs                     456M     0  456M   0% /sys/fs/cgroup
/dev/loop0                 62M   62M     0 100% /snap/core20/1328
/dev/loop2                 44M   44M     0 100% /snap/snapd/14549
/dev/loop1                 68M   68M     0 100% /snap/lxd/21835
/dev/sda15                105M  5.2M  100M   5% /boot/efi
/dev/sdb1                 3.9G   16M  3.7G   1% /mnt
tmpfs                      92M     0   92M   0% /run/user/1000
onedrive:rclone/onedrive  1.1T  267G  763G  26% /home/ubuntu/rclone/onedrive

四、设置 Rclone 开机启动

Step1:切换到 root 用户。

请输入下面的命令,切换到 root 用户。

sudo su

Step2:新建一个 systemctl 配置文件

请输入下面的命令,新建一个 systemctl 配置文件。

以下的是一个完整的命令,请全部复制并黏贴到终端中。

cat > /etc/systemd/system/rclone.service <<EOF

[Unit]
Description=rclone
        
[Service]
User=ubuntu
ExecStart=rclone mount onedrive:rclone/onedrive /home/ubuntu/rclone/onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
Restart=on-abort
        
[Install]
WantedBy=multi-user.target

EOF

请将 ubuntu 修改为你的用户名,rclone mount onedrive:rclone/onedrive /home/ubuntu/rclone/onedrive ... 修改为你的命令。

Step3:设置 Rclone 开机启动

输入下面的命令,设置 Rclone 开机启动。

systemctl enable rclone

Step4:启动 Rclone

输入下面的命令,启动 Rclone。

systemctl start rclone

Step5:更多命令

//启动 Rclone
systemctl start rclone

//停止 Rclone
systemctl stop rclone

//重启 Rclone
systemctl restart rclone

//查看 Rclone 状态
systemctl status rclone

//设置 Rclone 开机启动
systemctl enable rclone

//禁止 Rclone 开机启动
systemctl disable rclone

//重新加载配置文件
systemctl daemon-reload