通过Rclone挂载Google Drive到VPS

Rclone 是一款的命令行工具,支持在不同对象存储、网盘间同步、上传、下载数据。例如它可以轻松挂载Google Drive到你的VPS(当然,他还能够挂载其他服务商的网盘,具体查看官网文档),让你可以方便的使用云盘里的资源。

1. 安装 Rclone

1
2
[root@host ~]# yum -y install unzip
[root@host ~]# curl https://rclone.org/install.sh | sudo bash

2. 配置Rclone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[root@host ~]# rclone config  #输入rclone config命令进行配置
2019/01/02 22:07:50 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 #选择n创建新的远端
name> mydriver #给创建的远端命名,比如mydriver
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 / A stackable unification remote, which can appear to merge the contents of several remotes
\ "union"
2 / Alias for a existing remote
\ "alias"
3 / Amazon Drive
\ "amazon cloud drive"
4 / Amazon S3 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Dropbox
\ "dropbox"
9 / Encrypt/Decrypt a remote
\ "crypt"
10 / FTP Connection
\ "ftp"
11 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
12 / Google Drive
\ "drive"
13 / Hubic
\ "hubic"
14 / JottaCloud
\ "jottacloud"
15 / Local Disk
\ "local"
16 / Mega
\ "mega"
17 / Microsoft Azure Blob Storage
\ "azureblob"
18 / Microsoft OneDrive
\ "onedrive"
19 / OpenDrive
\ "opendrive"
20 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
21 / Pcloud
\ "pcloud"
22 / QingCloud Object Storage
\ "qingstor"
23 / SSH/SFTP Connection
\ "sftp"
24 / Webdav
\ "webdav"
25 / Yandex Disk
\ "yandex"
26 / http Connection
\ "http"
Storage> 12 #选择要挂载到vps上的第三方云盘,由于这里挂载的是谷歌云盘,故选择12
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> #回车
Google Application Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> #回车
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> 1 #选择1,赋予rclone操作云盘可读可写的权限
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders. (see docs).
Enter a string value. Press Enter for the default ("").
root_folder_id> #回车
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> #回车
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 or Y didn't work
y) Yes
n) No
y/n> n #是否使用自动配置,选n不配置


If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=8b3264ffe950d2e850968a091bffd93e
#打开浏览器输入以上https://....链接并以挂载的谷歌云盘对应的账号登陆验证,如下图:

1546488018898

1546487961292

1546487892836

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Log in and authorize rclone for access
Enter verification code> 4/xw**************************nyI #复制以上获得的验证码到此处

Configure this as a team drive?
y) Yes
n) No
y/n> y #输入y确认
Fetching team drive list...
No team drives found in your account--------------------
[mydriver]
type = drive
scope = drive
token = {"access_token":"ya29.GluGBpBlg_NP0xWOUyj75-nT0LH2-kEuVlMtGESyZSxWbMqELF3uB5dAVfl9L8voIaMT2bRBYS2x15mG175TIb5yk08wWqH2DCwLBmhy_podqrtfl5q6TNoIRKRB","token_type":"Bearer","refresh_token":"1/0jso0O697nu0nBYKZZXRwCD50b6o97uCbeB_Sbt2E8aj3ZKDkik9OlzKe-0GvLGh","expiry":"2019-01-02T23:11:47.082006585-05:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y #输入y确认
Current remotes:
#添加名称为mydriver的谷歌云盘到vps成功
Name Type
==== ====
mydriver drive

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退出

3. vps挂载谷歌云盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@host ~]# mkdir -p /data/GoogleDrive #先创建一个目录作为云盘的挂载点

[root@host ~]# rclone mount mydriver:share /data/GoogleDrive --allow-other --allow-non-empty --vfs-cache-mode writes &
#执行挂载命令,其中mydriver是配置Rclone时设置的远端云盘名称;share是谷歌云(mydriver)上的一个目录(确保先创建一个这样类似的目录);/data/GoogleDrive是vps上的挂载点,或者说是共享文件夹

[1] 8327
[root@host ~]# 2019/01/02 22:14:30 Fatal error: failed to mount FUSE fs: fusermount: exec: "fusermount": executable file not found in $PATH

#遇到以上错误执行此命令安装fuse
[root@host ~]# yum -y install fuse

[root@host ~]# rclone mount mydriver:share /data/GoogleDrive --allow-other --allow-non-empty --vfs-cache-mode writes & #再次执行挂载命令
[1] 8337


[root@host ~]# df -h #查看是否挂载成功
Filesystem Size Used Avail Use% Mounted on
devtmpfs 238M 0 238M 0% /dev
...
...
mydriver:share 1.0P 33G 1.0P 1% /data/GoogleDrive #表示挂载成功

📚 更多挂载命令请查看官方文档

4. 设置开机自动挂载

执行完以上操作虽然已经挂载成功,但是只是临时挂载,如果重启服务器或者关机就失效了,所以还有要完成以下任务。

如果rclone不在/usr/bin/目录的话(可以通过which rclone命令查询是否在这个目录),需要先把rclone的可执行文件复制到/usr/bin

新建一个rclone.service文件:

1
vi /usr/lib/systemd/system/rclone.service

写入:

1
2
3
4
5
6
7
8
9
10
[Unit]
Description = rclone

[Service]
User = root
ExecStart = /usr/bin/rclone mount mydriver:share /data/GoogleDrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
Restart = on-abort

[Install]
WantedBy = multi-user.target

注意:以上ExecStart的内容"mydriver:share /data/GoogleDrive"要结合你挂载的情况来定。

1
2
3
4
5
6
#重载daemon,让新的服务文件生效
systemctl daemon-reload
#启动rclone
systemctl start rclone
#设置开机启动:
systemctl enable rclone

重启你的VPS,然后查看一下rclone的服务起来没,接着查看一下盘子挂上去没:

1
2
3
4
5
6
7
8
9
#重启
reboot
#查看状态
systemctl status rclone
#查看磁盘挂载情况
df -h

#如果需要可以通过下面的命令关闭rclone
#systemctl stop rclone

5. 测试

1
2
3
4
[root@host ~]# echo "hello world" > /data/GoogleDrive/hello.txt
[root@host ~]# ls /data/GoogleDrive/hello.txt
/data/GoogleDrive/hello.txt
[root@host ~]#

在 Google Drive 刷新看看

1546490095218



----------- 本文结束 -----------




如果你觉得我的文章对你有帮助,你可以打赏我哦~
0%