真知即所以为行,不行不足谓之知。

【转】#教程# 使用Rclone挂载Google Drive到云主机/VPS 扩容硬盘

Spread the love

Rclone是一个 Github 上面的一个开源项目,专门开发用来在 Linux 上面同步文件/文件夹,上传的一个命令行工具。目前很多人喜欢用 VPS 做离线下载,不过由于有的服务器硬盘太小了,我们可以通过挂载Google Drive网盘的方法,实现扩容。(Google Drive有 15G 免费的空间,也可以去某宝购买一个无限空间的账号,理论上能够实现无限存储)

本教程仅在 CentOS 7 下测试成功,其他版本并未测试仅供参考;

首先安装 EPEL 源:

  1. yum y install epelrelease

接下来安装一些基本组件和依赖:

  1. yum y install wget unzip screen fuse fusedevel

下载Rclone-解压-重命名文件夹-进入文件夹

  1. cd /usr/local
  2. wget wget https://downloads.rclone.org/v1.47.0/rclone-v1.47.0-linux-amd64.zip
  3. unzip rclonev1.47.0linuxamd64.zip
  4. mv rclonev1.47.0linuxamd64 rclone
  5. cd rclone

目前最新版本是 1.47.0,rclone 会随时间推移而更新,可以到 rclone 官网 查看最新版本然后替换地址即可;

运行 Rclone 开始配置:

  1. ./rclone config

第一步选择 n(新建),然后回车输入一个 name(名字),例如:这里我创建的名字为 gd

  1. No remotes found make a new one
  2. n)
    New remote
  3. s)
    Set configuration password
  4. q)
    Quit config
  5. n/s/q> n
  6. name> gd

这里选择你需要配置的存储类型,我这里选择 12(Google Drive),根据版本变化选择的序号会不同看清楚Google Drive再选择,如果要挂载其他的选填数字即可:

  1. Type of storage to configure.
  2. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  3. Choose a number from below,
    or type in your own value

  4. 1
    / A stackable unification remote, which can appear to merge the contents of several remotes
  5. \ “union”

  6. 2
    /
    Alias
    for a existing remote
  7. \ “alias”

  8. 3
    /
    Amazon
    Drive
  9. \ “amazon cloud drive”

  10. 4
    /
    Amazon S3 Compliant
    Storage
    Provider
    (AWS,
    Alibaba,
    Ceph,
    Digital
    Ocean,
    Dreamhost, IBM COS,
    Minio, etc)
  11. \ “s3”

  12. 5
    /
    Backblaze B2
  13. \ “b2”

  14. 6
    /
    Box
  15. \ “box”

  16. 7
    /
    Cache a remote
  17. \ “cache”

  18. 8
    /
    Dropbox
  19. \ “dropbox”

  20. 9
    /
    Encrypt/Decrypt a remote
  21. \ “crypt”
  22. 10
    / FTP Connection
  23. \ “ftp”
  24. 11
    /
    Google
    Cloud
    Storage
    (this
    is
    not
    Google
    Drive)
  25. \ “google cloud storage”
  26. 12
    /
    Google
    Drive
  27. \ “drive”
  28. 13
    /
    Hubic
  29. \ “hubic”
  30. 14
    /
    JottaCloud
  31. \ “jottacloud”
  32. 15
    /
    Local
    Disk
  33. \ “local”
  34. 16
    /
    Mega
  35. \ “mega”
  36. 17
    /
    Microsoft
    Azure
    Blob
    Storage
  37. \ “azureblob”
  38. 18
    /
    Microsoft
    OneDrive
  39. \ “onedrive”
  40. 19
    /
    OpenDrive
  41. \ “opendrive”
  42. 20
    /
    Openstack
    Swift
    (Rackspace
    Cloud
    Files,
    Memset
    Memstore, OVH)
  43. \ “swift”
  44. 21
    /
    Pcloud
  45. \ “pcloud”
  46. 22
    /
    QingCloud
    Object
    Storage
  47. \ “qingstor”
  48. 23
    / SSH/SFTP Connection
  49. \ “sftp”
  50. 24
    /
    Webdav
  51. \ “webdav”
  52. 25
    /
    Yandex
    Disk
  53. \ “yandex”
  54. 26
    / http Connection
  55. \ “http”
  56. Storage>
    12

接下来会叫选填 Google Application Client ID 和,这里 2 项都留空回车即可:

  1. Google
    Application
    Client
    Id
  2. Leave blank normally.
  3. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  4. client_id>

  5. Google
    Application
    Client
    Secret
  6. Leave blank normally.
  7. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  8. client_secret>

rclone 在请求从驱动器访问时应使用的范围,这里选 1 即可

  1. Scope that rclone should use
    when requesting access from drive.
  2. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  3. Choose a number from below,
    or type in your own value

  4. 1
    /
    Full access all files, excluding Application
    Data
    Folder.
  5. \ “drive”

  6. 2
    /
    Readonly access to file metadata and file contents.
  7. \ “drive.readonly”

  8. /
    Access to files created by rclone only.

  9. 3
    |
    These are visible in the drive website.

  10. |
    File authorization is revoked when the user deauthorizes the app.
  11. \ “drive.file”

  12. /
    Allows read and write access to the Application
    Data folder.

  13. 4
    |
    This
    is
    not visible in the drive website.
  14. \ “drive.appfolder”

  15. /
    Allows readonly access to file metadata but

  16. 5
    | does not allow any access to read or download file content.
  17. \ “drive.metadata.readonly”
  18. scope>
    1

选填文件夹 ID 和帐户凭据 JSON 文件路径,这里我都留空回车的

  1. ID of the root folder
  2. Leave blank normally.
  3. Fill
    in to access “Computers” folders.
    (see docs).
  4. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  5. root_folder_id>

  6. Service
    Account
    Credentials JSON file path
  7. Leave blank normally.
  8. Needed only if you want use SA instead of interactive login.
  9. Enter a string value.
    Press
    Enter
    for the default
    (“”).
  10. service_account_file>

问你是否要编辑高级配置,这里我选择 N

  1. Edit advanced config?
    (y/n)
  2. y)
    Yes
  3. n)
    No
  4. y/n> n

是否自动远程配置,这里选择 N,因为你在手动远程配置

  1. Remote config
  2. Use
    auto config?

  3. *
    Say Y if
    not sure

  4. *
    Say N if you are working on a remote or headless machine
  5. y)
    Yes
  6. n)
    No
  7. y/n> n

接下来会给出Google Drive的授权地址,把它复制到浏览器打开,按提示登陆,复制获取到的代码,然后返回 SSH 粘贴后回车

  1. 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=55663e7e07382e3dddb9025c86de4f
  2. Log in and authorize rclone for access
  3. Enter verification code> 4/UQGiRz375eb-OixO5EUtZMxBhJwAQ4zOyvA1wtJWKq2Ocmzh3zNYE

问你是不是将其配置为团队网盘,这里选 N

  1. Configure
    this
    as a team drive?
  2. y)
    Yes
  3. n)
    No
  4. y/n> n

列出你上面修改的配置信息,问你是否删除、编辑或者确定配置信息,这里选择确定 y

  1. ——————–
  2. [gd]
  3. type = drive
  4. scope = drive
  5. token =
    {“access_token”:“ya29.GlsQByNiBURlXoPpe-bDpa2kF99Jo4rrmjicBXdWIT6loPUhS7SJ9XWUIk2LP4vO231nra_zpUwHn6no0Y_LBbXYFZvyVf0gRthepF2VuPFdhBFEKY7XYJaelt”,“token_type”:“Bearer”,“refresh_token”:“1/ry1JGhRiqqE6-PqRN-S2icZ_Oz9uOTXfSNxWA85zUnjU5gEm-6TejL6o-hjyuY”,“expiry”:“2019-05-21T04:36:23.300542043-04:00”}
  6. ——————–
  7. y)
    Yes
    this
    is OK
  8. e)
    Edit
    this remote
  9. d)
    Delete
    this remote
  10. y/e/d> y

问你是否修改、删除、退出配置编辑,这里选择退出 q

  1. Current remotes:
  2. Name
    Type
  3. ====
    ====
  4. gd drive
  5. e)
    Edit existing remote
  6. n)
    New remote
  7. d)
    Delete remote
  8. r)
    Rename remote
  9. c)
    Copy remote
  10. s)
    Set configuration password
  11. q)
    Quit config
  12. e/n/d/r/c/s/q> q

到这里结束配置 rclone,下面要把 Google Drive 网盘挂载到云主机/Vps 上和设置开机自启,自动挂载 Google Drive 网盘

新建一个你要挂载的目录,例如我要挂载到/home/gdrive

  1. mkdir p /home/gdrive

再执行挂载命令:

  1. ./rclone mount gd: /home/gdrive allowother allownonempty vfscachemode writes

gd 为 Rclone 的配置名称,比如你在创建配置 rclone 的时候 Name 填的 gd,/home/gdrive 为本地路径;

这里还可以自定义设置网盘里的文件夹路径,例如:

  1. ./rclone mount gd:backup /home/gdrive allowother allownonempty vfscachemode writes

gd:backup gd Rclone 的配置名称:backup 为网盘里的目录名

卸载 Google Drive 磁盘

  1. fusermount qzu /home/gdrive

挂载只要几秒钟,但终端不会返回成功信息,关闭 SSH 重连即可。

重连后查看是否挂载成功:

  1. df h

有看到 gd,15G 硬盘,使用量和本本地主机路径即成功挂载

  1. gd:
    15G
    1.1M
    15G
    1%
    /home/gdrive

接下来设置开机自动挂载,先把 rclone 的可执行文件复制到/usr/bin:

  1. cp /usr/local/rclone/rclone /usr/bin/rclone

新建一个/usr/lib/systemd/system/rclone.service 文件内容为:

  1. [Unit]
  2. Description=rclone
  3. [Service]
  4. User=root
  5. ExecStart=/usr/bin/rclone mount gd:
    /home/gdrive allowother allownonempty vfscachemode writes
  6. Restart=onabort
  7. [Install]
  8. WantedBy=multiuser.target

注意修改挂载命令为你主机的路径

如果需要挂载多个网盘,按上面的教程新运行个 Rclone 配置,再新建一个/etc/systemd/system/rclone@.service 文件

  1. # cat /etc/systemd/system/rclone@.service
  2. [Unit]
  3. Description=rclone mount %I drive
  4. After=network.target
  5. [Service]
  6. #Type=notify
  7. Type=simple
  8. #PrivateTmp=true
  9. ExecStart=/usr/bin/rclone mount %i:
    /home/%i allowother config /root/.config/rclone/rclone.conf
  10. [Install]
  11. WantedBy=multiuser.target

/home/%i 为挂载目录,可以自行设置路径例如:/root/%i

然后用这个启动命令:

  1. for a in
    `grep ‘^\[‘ /root/.config/rclone/rclone.conf`
  2. do
  3. b=${a:1:-1}

  4. [
    !
    d “/root/${b}”
    ]
    && mkdir /root/${b}
  5. systemctl enable rclone@${b}
  6. systemctl start rclone@${b}
  7. done

重载 daemon,让新的服务文件生效:

  1. systemctl daemonreload

用 systemctl 来启动 rclone

  1. systemctl start rclone

设置开机启动:

  1. systemctl enable rclone

停止、查看状态可以用:

  1. systemctl stop rclone
  2. systemctl status rclone

重启你的云主机/VPS:

  1. reboot

重启后查看一下 rclone 的服务是否启动,GoogleDrive 是否正常挂载

  1. systemctl status rclone
  2. df h

OK ! 完成.

把文件上传到 Google Drive

为什么不直接把文件目录设置成挂载目录?如果直接把文件目录指定到挂载目录,会出现各种莫名其妙的错误,比如:文件无法写入、读取、保存到 Googlr Drive 的文件不完整等等奇葩的问题。

这里可以用同步命令,本地目录/home/backup 同步到网盘的 backup 目录

  1. rclone sync /home/backup gd:backup

相反,把目录调整下,就是把网盘 backup 目录同步到 VPS 目录/home/backup

  1. rclone sync gd:backup /home/backup

通过添加此参数 –ignore-existing 可以忽略在网盘上已备份的文件,这相当于增量备份

  1. rclone copy ignoreexisting /home/backup gd:backup

如果挂载 2 个网盘的话,可以同步配置名 gd2 的网盘里的 backup 目录,到配置名为 gd 的网盘的 backup 目录,反之亦然

  1. rclone sync gd2:backup gd:backup

其他问题请参考 rclone 帮助文档 https://rclone.org/googlecloudstorage/


Spread the love
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:Living in the flow » 【转】#教程# 使用Rclone挂载Google Drive到云主机/VPS 扩容硬盘

分享到: 更多 (0)
avatar

评论 抢沙发

  • QQ号
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活