在Linux上使用rclone掛載Google Drive 和 Onedrive

在Linux上使用rclone掛載Google Drive 和 Onedrive

rclone可以幫助我們在Linux上掛載一些儲存服務,包括Google drive, onedrive, box, AWS S3等等.同時不會佔用硬盤空間
簡直就是小容量vps的福音

1.0 安裝 Rclone

參考rclone Linux 安裝描述

1.1 穩定版安裝

Script installation
To install rclone on Linux/macOS/BSD systems, run:

curl https://rclone.org/install.sh | sudo bash

1.2 Beta安裝

For beta installation, run:

curl https://rclone.org/install.sh | sudo bash -s beta

1.3 源碼安裝

從預編譯二進制文件安裝Linux 獲取並解壓縮
Linux installation from precompiled binary
Fetch and unpack

curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
cd rclone-*-linux-amd64

複製bin文件到/usr/bin/
Copy binary file

sudo cp rclone /usr/bin/
sudo chown root:root /usr/bin/rclone
sudo chmod 755 /usr/bin/rclone

安裝對應的man幫助
Install manpage

sudo mkdir -p /usr/local/share/man/man1
sudo cp rclone.1 /usr/local/share/man/man1/
sudo mandb 

Note:

“ 官方同時給出了docker方式部署,利用docker -v 掛載到OS上。個人感覺意義不大就介紹了

2.0 本地PC 上下載rclone (可選)

爲什麼要在本地win10 上下載rclone ?
若果你的VPS 可以開啓圖形化的可以忽略這個步驟

主要問題是客戶端授權,在運行rclone需要先授權。授權其中會跳轉到瀏覽器,google drive 授權是給出網址,可以直接複製到chrome上進行授權獲取認證,但是onedrive 通常是給出的網址是http://127.0.0.1:53682/auth 這個很操蛋vps沒有圖形化就不無法解決了

所以直接利用本地pc win10上獲取auth就省事多了.

2.1 在本地window下載rclone

https://downloads.rclone.org/rclone-current-windows-amd64.zip

解壓出來後,進入cmd,輸入

rclone authorize "onedrive"

之後會彈出窗口認證,然後複製token 記錄好,後面在linux 上會用到

Paste the following into your remote machine --->
{"access_token":"xxxx"}  #請複製{xx}整個內容(包括花括號)
<---End paste

3.0 初始化配置rclone

回到vps上

執行rclone config 配置google drive

[root@lab-test ~]# rclone config
2019/09/11 10:18:28 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  #選擇新建一個配置
name> test #配置名稱
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 / 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 / Google Photos
   \ "google photos"
14 / Hubic
   \ "hubic"
15 / JottaCloud
   \ "jottacloud"
16 / Koofr
   \ "koofr"
17 / Local Disk
   \ "local"
18 / Mega
   \ "mega"
19 / Microsoft Azure Blob Storage
   \ "azureblob"
20 / Microsoft OneDrive
   \ "onedrive"
21 / OpenDrive
   \ "opendrive"
22 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
23 / Pcloud
   \ "pcloud"
24 / Put.io
   \ "putio"
25 / QingCloud Object Storage
   \ "qingstor"
26 / SSH/SFTP Connection
   \ "sftp"
27 / Union merges the contents of several remotes
   \ "union"
28 / Webdav
   \ "webdav"
29 / Yandex Disk
   \ "yandex"
30 / http Connection
   \ "http"
31 / premiumize.me
   \ "premiumizeme"
Storage> 12 #選擇對應的標號 這裏選擇的是Google Drive
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> #一般留空
Google Application Client Secret
Setting your own is recommended.
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 #選擇完全訪問所有文件
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 #是否修改客戶端配置,沒有特殊就不改了
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
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=cdadc76b05c67ecf139a03d81e3c8e2c
Log in and authorize rclone for access 
Enter verification code> #複製上面給出的認證地址到chrome,得到的認證碼粘貼到這裏

Configure this as a team drive?
y) Yes
n) No
y/n> n #不用team drive 選擇n
--------------------
[remote]
client_id = 
client_secret = 
scope = drive
root_folder_id = 
service_account_file =
token = {"access_token":"XXX","token_type":"Bearer","refresh_token":"XXX","expiry":"2014-03-16T13:57:58.955387075Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y #確認配置

完成後選擇退出

這時候可以使用再次使用rclone config就可以看到剛新建的配置名稱

[root@lab-test ~]# rclone config
Current remotes:

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

onedrive 是基本一樣的,就是在給出認證鏈接時候給的是http://127.0.0.1:53682/auth。
這時候就直接把在window 上獲得的認證粘貼到Enter verification code> 步驟就行了

4.0 掛載

# 先新建一個目錄,用於掛載
mkdir -p /mnt/google-drive-test
# 掛載命令
rclone mount test: /mnt/google-drive-test --allow-other --allow-non-empty --vfs-cache-mode writes &
# df 查看
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            483M     0  483M   0% /dev
tmpfs            99M   13M   87M  13% /run
/dev/sda1       9.8G  7.6G  1.8G  82% /
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           495M     0  495M   0% /sys/fs/cgroup
test:            7EB     0   7EB   0% /mnt/google-drive-test
# 使用dd 測速一下
dd if=/dev/zero of=/mnt/google-drive-test/1  bs=2048 count=1k
記錄了1024+0 的讀入
記錄了1024+0 的寫出
2097152字節(2.1 MB)已複製,1.88323 秒,1.1 MB/秒

1.1MB/s 速度還是不錯的

Note:

" 這裏需要注意的掛載時候命令默認是前臺展示,退出就卸載了.
使用 rclone mount --help 可以看到
When the program ends, either via Ctrl+C or receiving a SIGINT or SIGTERM signal,
the mount is automatically stopped.
所以使用使用還得加個& 掉到後臺

5.0 卸載

卸載很簡單就是直接umount 掉就行了

#卸載
umount /mnt/google-drive-test
#確認是否已卸載
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            483M     0  483M   0% /dev
tmpfs            99M   13M   87M  13% /run
/dev/sda1       9.8G  7.6G  1.8G  82% /
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           495M     0  495M   0% /sys/fs/cgroup

6.0 設置啓動腳本

這裏提供一個類似server 啓動的腳本

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
NAME_BIN="rclone"
### BEGIN INIT INFO
# Provides:          rclone
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start rclone at boot time
# Description:       Enable rclone by daemon.
### END INIT INFO

NAME="OneDrive" #rclone name名
REMOTE='Download' #遠程文件夾
LOCAL='/home/OneDrive' #掛載地址

Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[錯誤]${Font_color_suffix}"
RETVAL=0

check_running(){
        PID="$(ps -C $NAME_BIN -o pid= |head -n1 |grep -o '[0-9]\{1,\}')"
        if [[ ! -z ${PID} ]]; then
                return 0
        else
                return 1
        fi
}
do_start(){
        check_running
        if [[ $? -eq 0 ]]; then
                echo -e "${Info} $NAME_BIN (PID ${PID}) 正在運行..." && exit 0
        else
                fusermount -zuq $LOCAL >/dev/null 2>&1
                mkdir -p $LOCAL
                sudo /usr/bin/rclone mount $NAME:$REMOTE $LOCAL --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 >/dev/null 2>&1 &
                sleep 2s
                check_running
                if [[ $? -eq 0 ]]; then
                        echo -e "${Info} $NAME_BIN 啓動成功 !"
                else
                        echo -e "${Error} $NAME_BIN 啓動失敗 !"
                fi
        fi
}
do_stop(){
        check_running
        if [[ $? -eq 0 ]]; then
                kill -9 ${PID}
                RETVAL=$?
                if [[ $RETVAL -eq 0 ]]; then
                        echo -e "${Info} $NAME_BIN 停止成功 !"
                else
                        echo -e "${Error} $NAME_BIN 停止失敗 !"
                fi
        else
                echo -e "${Info} $NAME_BIN 未運行"
                RETVAL=1
        fi
        fusermount -zuq $LOCAL >/dev/null 2>&1
}
do_status(){
        check_running
        if [[ $? -eq 0 ]]; then
                echo -e "${Info} $NAME_BIN (PID $(echo ${PID})) 正在運行..."
        else
                echo -e "${Info} $NAME_BIN 未運行 !"
                RETVAL=1
        fi
}
do_restart(){
        do_stop
        do_start
}
case "$1" in
        start|stop|restart|status)
        do_$1
        ;;
        *)
        echo "使用方法: $0 { start | stop | restart | status }"
        RETVAL=1
        ;;
esac
exit $RETVAL

此腳本出自
https://raw.githubusercontent.com/x91270/Centos/master/rcloned

使用時候先改好裏面的變量
NAME=“OneDrive” #rclone name名
REMOTE=‘Download’ #遠程文件夾
LOCAL=’/home/OneDrive’ #掛載地址

但是隻能對應掛載一個盤,當我需要掛載幾個google drive 或者同時掛載onedrive 時候就麻煩極了
盡是些花裏胡哨的玩意
當對應多盤的時候直接編寫一個腳本到/usr/bin下方便多了

cat /usr/bin/rclone-server
rclone mount test: /mnt/google-drive-test --allow-other --allow-non-empty --vfs-cache-mode writes &
rclone mount test1: /mnt/google-drive-test1 --allow-other --allow-non-empty --vfs-cache-mode writes &

添加時候直接往裏面加就完事,不用的時候就直接umount.

總結

如果手裏有大容量的Google Drive或者OneDrive可以使用rclone掛載到VPS上,將不常用的文件丟進去可以很大程度上節約空間。當然要注意的是在保存重要文件的的盤不要翻車哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章