使用Rclone掛載Google Drive到雲主機/VPS 擴容硬盤
Rclone是一個Github 上面的一個開源項目,專門開髮用來在Linux 上面同步文件/文件夾,上傳的一個命令行工具。目前很多人喜歡用VPS 做離線下載,不過由於有的服務器硬盤太小了,我們可以通過掛載Google Drive網盤的方法,實現擴容。(Google Drive 有15G 免費的空間,也可以去某寶購買一個無限空間的賬號,理論上能夠實現無限存儲)
本教程僅在CentOS 7 下測試成功,其他版本並未測試僅供參考;
首先安裝EPEL 源:
- yum – y install epel – release
接下來安裝一些基本組件和依賴:
- yum – y install wget unzip screen fuse fuse – devel
下載Rclone-解壓-重命名文件夾-進入文件夾
- cd / usr / local
- wget wget https : //downloads.rclone.org/v1.47.0/rclone-v1.47.0-linux-amd64.zip
- unzip rclone-v1.47.0-linux-amd64.zip
- mv rclone-v1.47.0-linux-amd64 rclone
- cd rclone
目前最新版本是 1.47.0,rclone 会随时间推移而更新,可以到 rclone 官网 查看最新版本然后替换地址即可;
运行 Rclone 开始配置:
- chmod +x rclone
- ./rclone config
第一步选择 n(新建),然后回车输入一个 name(名字),例如:这里我创建的名字为 gd
- No remotes found – make a new one
- n) New remote
- s) Set configuration password
- q) Quit config
- n/s/q> n
- name> gd
这里选择你需要配置的存储类型,我这里选择 12(Google Drive),根据版本变化选择的序号会不同看清楚 Google Drive 再选择,如果要挂载其他的选填数字即可:
- 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 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 / 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
接下来会叫选填 Google Application Client ID 和,这里 2 项都留空回车即可:
- 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>
rclone 在请求从驱动器访问时应使用的范围,这里选 1 即可
- 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
选填文件夹 ID 和帐户凭据 JSON 文件路径,这里我都留空回车的
- 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>
问你是否要编辑高级配置,这里我选择 N
- 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
接下来会给出 Google Drive 的授权地址,把它复制到浏览器打开,按提示登陆,复制获取到的代码,然后返回 SSH 粘贴后回车
- 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
- Log in and authorize rclone for access
- Enter verification code> 4/UQGiRz375eb-OixO5EUtZMxBhJwAQ4zOyvA1wtJWKq2Ocmzh3zNYE
问你是不是将其配置为团队网盘,这里选 N
- Configure this as a team drive?
- y) Yes
- n) No
- y/n> n
列出你上面修改的配置信息,问你是否删除、编辑或者确定配置信息,这里选择确定 y
- ——————–
- [gd]
- type = drive
- scope = drive
- 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″}
- ——————–
- y) Yes this is OK
- e) Edit this remote
- d) Delete this remote
- y/e/d> y
问你是否修改、删除、退出配置编辑,这里选择退出 q
- Current remotes:
- Name Type
- ==== ====
- gd 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
到这里结束配置 rclone,下面要把 Google Drive 网盘挂载到云主机/Vps 上和设置开机自启,自动挂载 Google Drive 网盘
新建一个你要挂载的目录,例如我要挂载到/home/gdrive
- mkdir -p /home/gdrive
再执行挂载命令:
- ./rclone mount gd: /home/gdrive –allow-other –allow-non-empty –vfs-cache-mode writes
gd 为 Rclone 的配置名称,比如你在创建配置 rclone 的时候 Name 填的 gd,/home/gdrive 为本地路径;
这里还可以自定义设置网盘里的文件夹路径,例如:
- ./rclone mount gd:backup /home/gdrive –allow-other –allow-non-empty –vfs-cache-mode writes
gd:backup gd 为 Rclone 的配置名称:backup 为网盘里的目录名
卸载 Google Drive 磁盘
- fusermount -qzu /home/gdrive
挂载只要几秒钟,但终端不会返回成功信息,关闭 SSH 重连即可。
重连后查看是否挂载成功:
- df -h
有看到 gd,15G 硬盘,使用量和本本地主机路径即成功挂载
- gd: 15G 1.1M 15G 1% /home/gdrive
接下来设置开机自动挂载,先把 rclone 的可执行文件复制到/usr/bin:
- cp /usr/local/rclone/rclone /usr/bin/rclone
新建一个/usr/lib/systemd/system/rclone.service 文件内容为:
- [Unit]
- Description=rclone
- [Service]
- User=root
- ExecStart=/usr/bin/rclone mount gd: /home/gdrive –allow-other –allow-non-empty –vfs-cache-mode writes
- Restart = on – abort
- [ Install ]
- WantedBy = multi – user . target
注意修改掛載命令為你主機的路徑
重載daemon,讓新的服務文件生效:
- systemctl daemon – reload
用systemctl 來啟動rclone
- systemctl start rclone
設置開機啟動:
- systemctl enable rclone
停止、查看狀態可以用:
- systemctl stop rclone
- systemctl status rclone
重啟你的雲主機/VPS:
- reboot
重啟後查看一下rclone 的服務是否啟動,GoogleDrive 是否正常掛載
- systemctl status rclone
- df – h
OK ! 完成.
把文件上傳到Google Drive
為什麼不直接把文件目錄設置成掛載目錄?如果直接把文件目錄指定到掛載目錄,會出現各種莫名其妙的錯誤,比如:文件無法寫入、讀取、保存到Googlr Drive 的文件不完整等等奇葩的問題。
這裡可以用同步命令,本地目錄/home/backup 同步到網盤的backup 目錄
- rclone sync / home / backup gd : backup
相反,把目錄調整下,就是把網盤backup 目錄同步到VPS 目錄/home/backup
- rclone sync gd : backup / home / backup
通過添加此參數–ignore-existing 可以忽略在網盤上已備份的文件,這相當於增量備份
- rclone copy — ignore – existing / home / backup gd : backup
如果掛載2 個網盤的話,可以同步配置名gd2 的網盤裡的backup 目錄,到配置名為gd 的網盤的backup 目錄,反之亦然
- rclone sync gd : backup gd : backup
其他問題請參考rclone幫助文檔 https://rclone.org/googlecloudstorage/