Rclone挂载OneDrive

日常数据备份占用空间过大,磁盘空间不够用?想要将备份的数据打包到各大云端?Rclone或许能满足你的需求。

说明

环境:Debian12

挂载盘:Microsoft E5 OneDrive

Windows运行rclone

在本地Windows电脑上下载rclone,下载地址:https://rclone.org/downloads/ 。然后解压出来,比如我解压到D盘,文件夹命名rclone,此时点击Win+R,然后输入cmd,确定。再输入以下命令:

cd /d d:\rclone 
rclone authorize "onedrive"

然后浏览器会弹出窗口,按提示登录要挂载OneDrive的Office账户,登录完成后选择同意授权,接着回到cmd窗口中,复制{"access_token":"XXXX"} 这一串Token信息,后续需要用到。(如果cmd窗口中没有输出Token信息,可能是失去鼠标焦点而停止运行了,在cmd窗口中多回车几下可解决。)

Linux安装rclone

root用户下直接执行(非root权限将下面的bash替换为 sudo bash ):

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

配置rclone

rclone config

 

会出现以下信息(rclone各个版本可能有所出入,自行判断):

点击展开查看内容

root@debian:~# rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n #1.选择新建
name> Cimen #2.名称随意
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, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Compress a remote
   \ "compress"
10 / Dropbox
   \ "dropbox"
11 / Encrypt/Decrypt a remote
   \ "crypt"
12 / Enterprise File Fabric
   \ "filefabric"
13 / FTP Connection
   \ "ftp"
14 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
15 / Google Drive
   \ "drive"
16 / Google Photos
   \ "google photos"
17 / Hadoop distributed file system
   \ "hdfs"
18 / Hubic
   \ "hubic"
19 / In memory object storage system.
   \ "memory"
20 / Jottacloud
   \ "jottacloud"
21 / Koofr
   \ "koofr"
22 / Local Disk
   \ "local"
23 / Mail.ru Cloud
   \ "mailru"
24 / Mega
   \ "mega"
25 / Microsoft Azure Blob Storage
   \ "azureblob"
26 / Microsoft OneDrive
   \ "onedrive"
27 / OpenDrive
   \ "opendrive"
28 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
29 / Pcloud
   \ "pcloud"
30 / Put.io
   \ "putio"
31 / QingCloud Object Storage
   \ "qingstor"
32 / SSH/SFTP Connection
   \ "sftp"
33 / Sugarsync
   \ "sugarsync"
34 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
35 / Transparently chunk/split large files
   \ "chunker"
36 / Union merges the contents of several upstream fs
   \ "union"
37 / Webdav
   \ "webdav"
38 / Yandex Disk
   \ "yandex"
39 / Zoho
   \ "zoho"
40 / http Connection
   \ "http"
41 / premiumize.me
   \ "premiumizeme"
42 / seafile
   \ "seafile"
Storage> 26 #3.选择Microsoft OneDrive,不同版本序号可能会不同!
** See help for onedrive backend at: https://rclone.org/onedrive/ **

OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>    #4.直接回车留空
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> #5.同样回车留空
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 #6.由于我挂载的是E5的OneDrive,因此选择1,世纪互联请选择4,不同版本序号可能不同!
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> #7.直接回车默认
Remote config
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 #8.这里请选择n,后续能手动输入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 below:
result> #9.这里粘贴你获取到的Token信息并回车,格式大致为{"access_token":"XXXX"} 
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"
Your choice> 1 #10.选1挂载OneDrive
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!o44VutxxxxxxxxxxxxxxxxxxxxxztxqCMZG-
Chose drive to use:> 0 #11.输入冒号前的序号,这里输入0
Found drive 'root' of type 'business', URL: https://xxx-my.sharepoint.com/personal/xxx_xxx_onmicrosoft_com/Documents
Is that okay?
y) Yes (default)
n) No
y/n> #11.直接回车,默认确认
--------------------
[Cimen] #你最初输入的名称
type = onedrive #挂载的类型
region = global #你选择的区域
token = {"access_token":""} #这里是你之前粘贴的Token信息
drive_id = b!o44VutxxxxxxxxxxxxxxxxxxxxxztxqCMZG-  #这里是磁盘ID
drive_type = business #挂载的云盘属性(默认识别),如不正确请选择e进行修改,或者选择d删除,重新配置
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y #12.确认信息无误后输入y,或直接默认值回车
Current remotes:

Name                 Type
====                 ====
Cimen                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 #13.退出rclone配置

 

安装Fuse

如果是很久远很久远的Rclone版本/很旧的系统,请安装 apt install fuse ,否则请安装:

apt install fuse3

挂载测试

#新建本地文件夹,路径自己定,即下面的LocalFolder
mkdir /OneDrive
# 为VPS上的本地文件夹,
LocalFolder=/OneDrive
# Rclone 初始化配置填的名称
DriveName=
# OneDrive里的文件夹
RemoteFolder=

#挂载为磁盘,下面的三个参数需要替换,详细看下面的说明
rclone mount $DriveName:$RemoteFolder $LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

确认测试没问题之后,按Ctrl+c退出挂载。

开机自启

新建systemd配置文件

同样修改三个参数,然后全部粘贴到ssh中执行。

# 修改下面的三个变量,一起复制到SSH窗口中运行
# [1]Rclone 添加挂载创建时填写的Onedrive名称,具体可以使用rclone config进行查看
DriveName=
# [2]OneDrive 中需要保存的文件夹的完整路径,如 / 、 sub 、 sub1/sub2/...
RemoteFolder=
# [3]Linux 本地实际存在的,且为空的文件夹,如:/OneDrive
LocalFolder=

# 设置完上面的参数后,一起复制到SSH窗口中运行
command="mount $DriveName:$RemoteFolder $LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000"
cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

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

[Install]
WantedBy=default.target
EOF
systemctl daemon-reload
systemctl enable rclone
systemctl restart rclone
systemctl status rclone

如果你想挂载多个网盘,那么将systemd配置文件的rclone.service改成rclone1.service即可,重启动什么的同样换成rclone1

相关命令

#开始启动:systemctl start rclone
#设置开机自启:systemctl enable rclone
#重启:systemctl restart rclone
#停止:systemctl stop rclone
#状态:systemctl status rclone

碎碎念

如果挂载过程中出现NOTICE: One drive root 'vpsbackup': --vfs-cache-mode writes or full is recommended for this remote as it can't stream 错误,可以无视该错误。

可能用到的命令

卸载磁盘

当发生意外无法再次挂载时,可以使用卸载。

fusermount -qzu LocalFolder  #LocalFolder为你vps本地文件夹

其它参数

#如果你还涉及到读取使用,比如使用H5ai等在线播放,就在后面多加上以下三条参数
--dir-cache-time 12h
--vfs-read-chunk-size 32M
--vfs-read-chunk-size-limit 1G
阅读剩余
THE END