使用使用rclone挂载OneDrive到服务器

综述

此文又臭又长,如果不是实在闲的蛋疼/刚需建议还是跳过吧

老王混迹网络这么多年,因为各种各样的原因使用过不少服务器(VPS),从国内到国外从早期的服务器合租网到现在的阿里云,当然国外的樱花、Vultr、狗爹也用过不少。

但是都有各种各样的不同缺点(主要是钱没到位),国内的限制带宽不限制流量,国外的限制流量不限制带宽(其实也限制,只是不严格),除此之外,硬盘空间也是个大问题,早期的VPS还好,偶尔有运营商给出便宜点(5-10$/月)的1T或者500G的HDD,大家基本上都是一哄而上抢光,现在的VPS一言难尽啊,基本上都是20-50G的空间了。

那么怎么解决这个问题,运营商开始卖其他的,比如阿里云的OSS,NSA;腾讯、百度也有类似的产品。

但是我们如果因为一些开飞机等需求购买的国外VPS又没有单独的磁盘可购买,怎么办呢?挂载阿里的OSS去交磁盘和流量费?

老王这种牛(qiong)人才不想呢,于是百度谷歌搜了一通,发现一个好东西,rclone!可以挂载N多的网盘作为本地磁盘使用,现在已经支持了

1 / 1Fichier

2 / Alias for an existing remote

3 / Amazon Drive

4 / Amazon S3 Compliant Storage Provider

5 / Backblaze B2

6 / Box

7 / Cache a remote

8 / Citrix Sharefile

9 / Dropbox

10 / Encrypt/Decrypt a remote

11 / FTP Connection

12 / Google Cloud Storage (this is not Google Drive)

13 / Google Drive

14 / Google Photos

15 / Hubic

16 / JottaCloud

17 / Koofr

18 / Local Disk

19 / Mail.ru Cloud

20 / Mega

21 / Microsoft Azure Blob Storage

22 / Microsoft OneDrive

23 / OpenDrive

24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)

25 / Pcloud

26 / Put.io

27 / QingCloud Object Storage

28 / SSH/SFTP Connection

29 / Transparently chunk/split large files

30 / Union merges the contents of several remotes

31 / Webdav

32 / Yandex Disk

33 / http Connection

34 / premiumize.me

软件优缺点

优点

1、 支持众多网络磁盘

2、 操作简单

3、 支持多个平台Windows、Linux(x86、x64、arm)等等

4、 多种安装方式:源码包、安装包等

缺点

1、 操作稍微复杂,如果是远程Linux你还需要一台Windows

2、 Linux系统的虚拟机需要支持fuse才行,也就是说绝大部分openvz不能用了。

判断系统是否支持

如果你购买vps时候已经明确购买的是KVM架构的,那么这一段你可以跳过去了,因为你肯定是支持的。

我们登录系统,输入“ls /dev/fuse”,如果返回的是下图,显示已经找到,那么是可以使用的。

如果系统返回的是下图,那么你需要跟厂商的客户工单沟通下了。

当然沟通的结果可能是我这样的,字里行间透漏出几个大字“穷逼买了openvz不行,你得买KVM架构的去”

当然了,如果你的客服给你开通了fuse的内核,那么太棒了,继续搞下。

安装rclone软件

我们以centos7和onedrive为例讲,其他Linux类似

既然我们这里讲的是挂载onedrive(我真不是可着微软的羊毛使劲撸,只是微软的撸起来顺手……),那么首先我们需要注册一个onedrive,怎么注册一个5T的onedrive呢?看我前面的文章。

安装软件,登录你的服务器,在命令行一次输入下面的内容,输入完后敲回车运行(#开头的文字不要输入)

yum -y update

#更新系统

yum -y install vim fuse

#安装本次需要的软件

wget https://downloads.rclone.org/rclone-current-linux-amd64.rpm

#下载rclone安装包,其他Linux网页打开找对应的包

rpm -ivh rclone-current-linux-amd64.rpm

#安装rclone到系统

到这里,我们已经安装好软件了。

配置rclone软件

继续在命令行输入

rclone config

系统会提示现在没有远程连接,问你是新建n/设置配置密码s/退出q;输入n新建一个远程,然后输入一个连接名字(随意),在接下来会让你选择网络硬盘,这里每个版本不一样,记得输入Microsoft OneDrive前面对应的序号就行。

rclone config
No remotes found - make a new one
n) New remote

s) Set configuration password
q) Quit config
n/s/q> n
name> laowang
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

接下来会提示输入client_id和client_secret,我们都不需要输入,直接回车跳过,在Edit advanced config? (y/n)选择y进行进阶设置。后面的设置磁盘块大小(drive_id)也可以直接跳过,drive_id也直接跳过,drive_type这里可以根据你的onedrive类型选择输入personal | business | documentLibrary,当然也可以跳过,下一步跳过OneNote文件(expose_onenote_files),直接回车跳过。

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> y/n> y
Chunk size to upload files with - must be multiple of 320k (327,680 bytes).

Above this size files will be chunked - must be multiple of 320k (327,680 bytes). Note
that the chunks will be buffered into memory.
Enter a size with suffix k,M,G,T. Press Enter for the default ("10M").
chunk_size>
The ID of the drive to use
Enter a string value. Press Enter for the default ("").
drive_id>
The type of the drive ( personal | business | documentLibrary )
Enter a string value. Press Enter for the default ("").
drive_type>
Set to make OneNote files show up in directory listings.

By default rclone will hide OneNote files in directory listings because
operations like "Open" and "Update" won't work on them. But this
behaviour may also prevent you from deleting them. If you want to
delete OneNote files or otherwise want them to show up in directory
listing, set this option.
Enter a boolean value (true or false). Press Enter for the default ("false").
expose_onenote_files>
y/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) :
\trclone authorize "onedrive" "\\\\x1b[A" "qtyfaBBYA403=unZUP40~_#"
Then paste the result below:
result>

然后我们需要输入授权,这里我们因为是 远程shell没法使用浏览器,这里选择n,我们从Windows操作

Remote config
Make sure your Redirect URL is set to "http://localhost:53682/" in your custom 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
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) :
\trclone authorize "onedrive" "\\\\x1b[A" "qtyfaBBYA403=unZUP40~_#"
Then paste the result below:
result>

好像到这里一直是跳过啊,没关系,下面开始需要你在Windows上操作了。

在windows上下载https://downloads.rclone.org/rclone-current-windows-amd64.zip,然后解压到任意目录中,然后打开解压后的目录,在目录空白处按住shift点右键,选择在此次打开cmd命令行。

然后在命令行中输入

rclone.exe config

然后按照前面的配置全部重新输入一遍 ,一直到前面将的最后一步操作,不要输入n,输入y,因为我们有浏览器啦(自豪),

系统会自动打开我们的默认浏览器,打开onedrive登录页面,输入账号和密码。

使用使用rclone挂载OneDrive到服务器

使用使用rclone挂载OneDrive到服务器

然后网页会提示你回rclone去看结果,已经返回授权access了。

使用使用rclone挂载OneDrive到服务器

我们返回刚刚的命令行继续操作,首先输入1,确认我们刚刚授权的是OneDrive,接下来的一步会提示选择一个用户,直接输入0啊,继续提示问你这个配置是否正确,输入y继续。

Log in and authorize rclone for access
Waiting for code...
Got code
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
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!******************
Chose drive to use:> 0
Found drive 'root' of type 'business', URL: https://***-my.sharepoint.com/personal/*****_*****_onmicrosoft_com/Documents
Is that okay?
y) Yes
n) No
y/n> y

然后会出现一大堆内容,这里分为几个部分,首先是告诉你这个是onedrive,然后是token,这个是最重要的,继续是id和版本,我们需要复制token =后面一直到drive_id(不包含drive_id)前面的所有内容。

--------------------
[laowang]
type = onedrive
token = {"access_token":"*********","token_type":"Bearer","refresh_token":"******","expiry":"2019-11-10T15:12:28.3942627+08:00"}
drive_id = b!*********************
drive_type = business
--------------------

复制图片中红色方块之间的内容

使用使用rclone挂载OneDrive到服务器

返回Linux继续操作

然后我们返回Linux继续操作,把这一串东西粘贴上去,回车。

首先输入1,确认我们刚刚授权的是OneDrive,接下来的一步会提示选择一个用户,直接输入0啊,继续提示问你这个配置是否正确,输入y继续。(好像又重复了)。

到了这里,已经配置完成了,输入q退出就可以了。

--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name Type
==== ====
laowang 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

测试挂载

完成上面的操作,所有网络硬盘有关的配置已经完成了,接下来我们需要挂载到服务器使用了。

首先创建一个目录作为挂载点,然后挂载过去,比如我创建的目录就是/data/rclone,你也可以根据自己情况修改。

简单说一下挂载命令:rclone mount laowang:rclone /data/rclone其实需要修改的就是这部分,laowang是刚刚的远程连接名称,rclone是网盘里的文件夹,/data/rclone是服务器上的目录。

mkdir -p /data/rclone
rclone mount laowang:rclone /data/rclone --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

如果你看到了下面的提示,请返回看我判断系统是否支持那一段。如果系统停到了这里没有任何回复,那么恭喜你,完成挂载了。

2019/11/10 14:32:13 mount helper error: fusermount: fuse device not found, try 'modprobe fuse' first
2019/11/10 14:32:13 Fatal error: failed to mount FUSE fs: fusermount: exit status 1

这时候,我们新开一个终端,登录到服务器,输入df-h,看这里,已经挂载了1T到/data/laowang目录下了

[root@atlanta ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 482M 0 482M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda1 25G 2.3G 22G 10% /
tmpfs 99M 0 99M 0% /run/user/0
laowang:rclone 1.0T 0 1.0T 0% /data/laowang

试一下建一个文件,已经可以使用了,虽然速度不是很快。

[root@atlanta ~]# touch /data/laowang/test.txt
[root@atlanta ~]# ls /data/laowang/
test.txt

注册rclone为系统服务并自启动

不说了,开搞,按照下面步骤做就行了

wget https://www.moerats.com/usr/shell/rcloned && vim rcloned
#感谢网友提供的脚本,这是我从别人博客找到的脚本^_^
#NAME="" #rclone name名,及配置时输入的Name
#REMOTE='' #远程文件夹,OneDrive网盘里的挂载的一个文件夹
#LOCAL='' #挂载地址,VPS本地挂载目录
mv rcloned /etc/init.d/rcloned
#复制脚本到 /etc/init.d/
chmod +x /etc/init.d/rcloned
#给脚本运行权限
chmod 755 /etc/init.d/rcloned
#给脚本用户权限
service rcloned start
[信息] rclone 启动成功 !
#启动rcloned服务
service rcloned stop
[信息] rclone 停止成功 !
#停止rcloned服务
service rcloned restart
[信息] rclone 停止成功 !
[信息] rclone 启动成功 !
#重启rcloned服务
chkconfig rcloned on
#设置rcloned服务自启动
chkconfig --list rcloned
#查看rcloned服务状态,如果返回的是下面一行的内容,说明配置没问题了
rcloned \t0:off\t1:off\t2:on\t3:on\t4:on\t5:on\t6:off

然后你可以重启一下服务器测试一下配置了。

总结

远程挂载onedrive到服务器,的确是解决磁盘大小不够的问题,但是这也只是能做一个简单的应用,毕竟读写速度和响应时间都很差,作为备份目录也算是一个好办法了吧。


分享到:


相關文章: