Linuxでrcloneを使用してGoogle DriveとOnedriveをマウント


Linuxでrcloneを使用してGoogle DriveとOnedriveをマウント
rcloneはLinuxにGoogle drive、onedrive、box、AWS 3などのストレージサービスをマウントするのに役立ちます.ハードディスク(HDD)の容量を消費しないのは、小容量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をダウンロード(オプション)
どうしてローカルwin 10でrcloneをダウンロードしますか?もしあなたのVPSがグラフィック化を開くことができるならばこのステップを無視することができます
主な問題はクライアントの授権であり、rcloneを実行するには先に授権する必要がある.授権の中でブラウザにジャンプし、google drive授権はウェブサイトを与え、chromeに直接コピーして授権して認証を得ることができるが、onedriveは通常与えられたウェブサイトはhttp://127.0.0.1:53682/auth これはグラフィック化しないと解决できない
ローカルpc win 10で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を選択します.この場合はWindowsで取得した認証を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.1 MB/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'#マウントアドレスを変更する
しかし、1つのディスクに対応するしかありません.いくつかの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にマウントできるので、あまり使わないファイルを投げ込むとスペースが大幅に節約できます.もちろん重要なファイルを保存しているディスクは転覆しないように注意しましょう