Linux VPS使用Rclone挂载OneDrive为本地硬盘使用

如果需要挂载Google Drive网盘,可以看这里:Linux VPS使用Rclone挂载Google Drive为本地硬盘使用

这篇主要记录下Rclone挂载OneDrive的详细过程,方便以后使用;

获取OneDrive授权

在Windows电脑上下载Rclone,下载地址:https://rclone.org/downloads 然后解压至D盘根目录,将文件夹重新命名为rclone,按下键盘Win+R,然后输入cmd,确定;在出现的命令框中依次输入以下命令:

cd /d d:rclone
rclone authorize "onedrive"

输入完成后会弹出网页,选择或输入你需要挂载的OneDrive网盘账号和密码,然后等待授权完成;

授权完成后回到刚才的命令符界面,此时会出现一大串授权字符;

Paste the following into your remote machine --->
{"access_token":"xxx"}       #这一大串就是我们的授权码,全部都要复制下来
<---End paste

我们需要将得到的授权码记录下来,后面会用到(提示:授权码有效期为一小时)

注意:记录的授权码需要包含字符头{"和字符尾"}都要复制下来;

Linux安装Rclone

此脚本只适用于Debian/Ubuntu系统

安装脚本:

wget https://www.moerats.com/usr/shell/rclone_debian.sh && bash rclone_debian.sh

开始配置:

rclone config

详细配置过程:

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新建挂载任务
name> SUN           #这里输入新挂载任务的名称,自行设定
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 Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
 "s3"
5 / Backblaze B2
 "b2"
6 / Box
 "box"
7 / Cache a remote
 "cache"
8 / Citrix Sharefile
 "sharefile"
9 / Dropbox
 "dropbox"
10 / Encrypt/Decrypt a remote
 "crypt"
11 / FTP Connection
 "ftp"
12 / Google Cloud Storage (this is not Google Drive)
 "google cloud storage"
13 / Google Drive
 "drive"
14 / Google Photos
 "google photos"
15 / Hubic
 "hubic"
16 / JottaCloud
 "jottacloud"
17 / Koofr
 "koofr"
18 / Local Disk
 "local"
19 / Mail.ru Cloud
 "mailru"
20 / Mega
 "mega"
21 / Microsoft Azure Blob Storage
 "azureblob"
22 / Microsoft OneDrive
 "onedrive"
23 / OpenDrive
 "opendrive"
24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
 "swift"
25 / Pcloud
 "pcloud"
26 / Put.io
 "putio"
27 / QingCloud Object Storage
 "qingstor"
28 / SSH/SFTP Connection
 "sftp"
29 / Transparently chunk/split large files
 "chunker"
30 / Union merges the contents of several remotes
 "union"
31 / Webdav
 "webdav"
32 / Yandex Disk
 "yandex"
33 / http Connection
 "http"
34 / premiumize.me
 "premiumizeme"
Storage> 22          #选择你需要挂载的网盘,这里OneDrive是22
** See help for onedrive backend at: https://rclone.org/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               #选择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> 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":"xxx"}      #这里粘贴刚才得到的授权码
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
 "onedrive"
2 / Root Sharepoint site
 "sharepoint"
3 / Type in driveID
 "driveid"
4 / Type in SiteID
 "siteid"
5 / Search a Sharepoint site
 "search"
Your choice> 1          #选择1回车
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=xxx
Chose drive to use:> 0  #这里选择给出的编号后回车,我的是0
Found drive 'root' of type 'business', URL: https://pqj-my.sharepoint.com/personal/xxx
Is that okay?
y) Yes
n) No
y/n> y                  #选择y回车
--------------------
[SUN]
type = onedrive
token = {"access_token":"xxx"}
drive_id = xxx
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y                #选择y回车
Current remotes:
Name                 Type
====                 ====
SUN                 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保存并退出

新建本地挂载文件夹

mkdir /home/OD

挂载磁盘

说明

#/usr/bin/rclone mount DriveName:Folder LocalFolder 
下面整体代码中最上面的这条需要自己修改,不要直接复制,注意代码中有空格
DriveName       #配置时填写的name
Folder          #网盘里要挂载的文件夹名
LocalFolder     #本地要挂载的文件夹绝对路径
例:/usr/bin/rclone mount SUN:/我的文件 /home/OneDrive 

挂载

#下面代码是一个整体,全部复制后粘贴运行
/usr/bin/rclone mount DriveName:Folder LocalFolder 
--umask 0000 
--default-permissions 
--allow-non-empty 
--allow-other 
--transfers 1 
--buffer-size 64M 
--low-level-retries 200
#如果你还涉及到读取使用,比如使用H5ai等在线播放,就在后面多加上以下三条参数
--dir-cache-time 12h
--vfs-read-chunk-size 32M
--vfs-read-chunk-size-limit 1G

参数说明

--transfers:该参数是最大同时传输任务数量,如果经常传输大文件,或CPU性能不佳,建议设置为单线程,也就是设置为“1”
--buffer-size:该参数为读取每个文件时的内存缓冲区大小,控制rclone上传和挂载的时候的内存占用
--low-level-retries:该参数为传输文件没速度的时候重试次数,没速度的时候,单个会自动睡眠10ms起,然后再重试

挂载成功后,输入df -h命令查看是否挂载成功;

卸载磁盘

fusermount -qzu LocalFolder

LocalFolder为本地挂载文件夹绝对路径

设置自启动

下载并编辑脚本

wget https://www.moerats.com/usr/shell/rcloned && nano rcloned

修改以下内容:

NAME=""  #rclone name名,及配置时输入的name
REMOTE=''  #远程文件夹,Google Drive网盘里的挂载的一个文件夹
LOCAL=''  #挂载地址,VPS本地挂载目录

设置自启

apt-get install sudo -y
mv rcloned /etc/init.d/rcloned
chmod +x /etc/init.d/rcloned
update-rc.d -f rcloned defaults
bash /etc/init.d/rcloned start

常用Rclone命令

### 文件上传
rclone copy /home/backup gdrive:backup  # 本地路径 配置名字:网盘文件夹路径
### 文件下载
rclone copy gdrive:backup /home/backup  # 配置名字:网盘文件夹路径 本地路径
### 新建文件夹
rclone mkdir gdrive:backup              # 配置名字:网盘文件夹路径
### 获取文件夹大小
rclone size gdrive:backup               # 配置名字:网盘文件夹路径
### 列表
rclone ls gdrive:backup
rclone lsl gdrive:backup # 比上面多一个显示上传时间
rclone lsd gdrive:backup # 只显示文件夹
### 挂载
rclone mount gdrive:mm /root/mm &
### 卸载
fusermount -u  /root/mm
#### 其他 ####
#### https://softlns.github.io/2016/11/28/rclone-guide/
rclone config - 以控制会话的形式添加rclone的配置,配置保存在.rclone.conf文件中。
rclone copy - 将文件从源复制到目的地址,跳过已复制完成的。
rclone sync - 将源数据同步到目的地址,只更新目的地址的数据。   –dry-run标志来检查要复制、删除的数据
rclone move - 将源数据移动到目的地址。
rclone delete - 删除指定路径下的文件内容。
rclone purge - 清空指定路径下所有文件数据。
rclone mkdir - 创建一个新目录。
rclone rmdir - 删除空目录。
rclone check - 检查源和目的地址数据是否匹配。
rclone ls - 列出指定路径下所有的文件以及文件大小和路径。
rclone lsd - 列出指定路径下所有的目录/容器/桶。
rclone lsl - 列出指定路径下所有文件以及修改时间、文件大小和路径。
rclone md5sum - 为指定路径下的所有文件产生一个md5sum文件。
rclone sha1sum - 为指定路径下的所有文件产生一个sha1sum文件。
rclone size - 获取指定路径下,文件内容的总大小。.
rclone version - 查看当前版本。
rclone cleanup - 清空remote。
rclone dedupe - 交互式查找重复文件,进行删除/重命名操作。
重启:systemctl restart rclone
停止:systemctl stop rclone
状态:systemctl status rclone

将命令挂载到后台运行

挂载后台运行,Rclone的上传下载命令需要开着SSH,SSH断开后任务会中断,对于大文件不太方便,我们可以挂载到后台运行

nohup 这里填写需要执行的命令,注意前后有空格 &

查看进程PID

ps -ef

结束进程PID

kill -6 任务的PID

参考链接:

https://www.moerats.com/archives/491/

全部为采集文章,文中的 联系方式 均不是 本人 的!

发表评论