RcloneでGoogleドライブをローカルPCにマウント

2022.06.06

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

以前S3をローカルPCにマウントしてみましたが、今回はGoogleドライブをマウントしてきます。

Rcloneでs3をローカルPCにマウントしてみる

インストール方法は上記記事にも記載していますので、今回は割愛します。

セットアップ

以下のコマンドを実行して設定していきます。

rclone config

インタラクティブなセットアッププロセスがガイドされますが、 初期設定では、ブラウザで行う必要のあるGoogleドライブのトークンを取得することができます。

設定例

Current remotes:

Name                 Type
====                 ====
s3                   s3

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> n
name> gdrive

新規の接続を作成するので、nを入力します。

ドライブににアクセスするための名前を入力します。今回はgdriveとしました。

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
 2 / Akamai NetStorage
   \ (netstorage)
~~~~
省略
~~~~
Storage> drive

ストレージタイプの選択では、driveを入力します。

Option client_id.
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 value. Press Enter to leave empty.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>

client_idclient_secretを空白にすることも可能です(デフォルト構成)。

デフォルト構成でGoogleドライブでrcloneを使用する場合、rcloneのclient_idを使用しています。 これは、すべてのrcloneユーザー間で共有されます。各client_idがGoogleによって設定できる1秒あたりのクエリ数にはグローバルなレート制限があります。

ここはデフォルト構成ではなく、独自のclient_idを使用しましょう。

Making your own client_id を参考にし作成していってください。・

Option scope.
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value.
Press Enter to leave empty.
 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

ドライブからのアクセスを要求する際にrcloneが使用すべきスコープは、今回はフルアクセスである 1 を入力します。

用途に応じてここは変更します。

Option root_folder_id.
ID of the root folder.
Leave blank normally.
Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.
Enter a value. Press Enter to leave empty.
root_folder_id>

ルートフォルダーのIDを入力します。(rcloneがドライブのルートと見なすディレクトリです) 通常、これは空白のままにし、rcloneはそれ自体を使用する正しいルートを決定します。

これを設定して、rcloneを特定のフォルダー階層に制限したり、ドライブWebインターフェイス(Googleのバックアップおよび同期デスクトッププログラムからのファイルが移動する場所)の[コンピューター]タブ内のデータにアクセスしたりできます。

※ ルートフォルダーのIDは、ドライブWebインターフェイスで関連するフォルダーを開いたときのURLの最後のセグメント

Option service_account_file.
Service Account Credentials JSON file path.
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.
Enter a value. Press Enter to leave empty.
service_account_file>

サービスアカウント認証情報のJSONファイルパスを指定できますが、通常は空白にしてください。 対話型ログインの代わりにSAを使用する場合のみ必要です。

高度なオプション

Googleドライブに固有の詳細オプション

Edit advanced config?
y) Yes
n) No (default)
y/n> y

その他の高度な設定を行う場合は yを入力します。

 oogleドライブの高度な設定 を参照してください。

Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n>y

オートコンフィグを使うか? ですが、よくわからない場合はYと答えてください。

リモートマシンやヘッドレスマシンで作業している場合、またはYがうまくいかなかった場合、Nにします。

Configure this as a Shared Drive (Team Drive)?
y) Yes
n) No
y/n> n

Google共有ドライブを指すようにリモートを構成する場合はyを入力します。

これにより、Googleから共有ドライブのリストが取得され、使用する共有ドライブを構成できるようになります。必要に応じて、共有ドライブIDを入力することもできます

参考 : 共有ドライブ(チームドライブ)

--------------------
[gdrive]
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

yを入力してセットアップ終了となります

注意点

自動構成モードを使用する場合、rcloneはローカルマシンでWebサーバーを実行して、Googleから返されたトークンを収集します。

これは、ブラウザを開いた瞬間から確認コードを取得する瞬間までのみ実行されます。

http://127.0.0.1:53682/になっており、ホストファイアウォールを実行している場合は一時的にブロックを解除するか、手動モードを使用する必要がある場合があります

コマンドを実行してみる

rcloneコマンドを実行し、Googleドライブの情報が取得できるか確認してみます。

rclone about gdrive:

Used:    17.305 GiB
Trashed: 1.631 GiB
Other:   15.982 GiB

現在の割り当てを表示するには、rclone about {設定した名前} を実行します。

使用制限(quota)、Googleドライブでの使用、ゴミ箱内のすべてのファイルのサイズ、Gmailなどの他のGoogleサービスで使用されている容量を表示するコマンドです。

$ rclone lsd gdrive:
          -1 2022-06-06 13:02:03        -1 home
$ rclone ls gdrive:home
        9 hoge.txt
        0 .hoge.txt.swx
       -1 .zsh_plugins.docx
     2699 .zsh_plugins.sh
     3514 .zshrc
     1604 .zprofile
       21 .zshenv

rclone lsdでGoogleドライブのディレクトリ一覧、rclone ls {設定した名前}:{ディレクトリ名} でファイル一覧が参照できるはずですので、 設定後は合わせて確認してみましょう。

マウントする

マウントのコマンドは以下

rclone mount {remote}:{path/to/files} {/path/to/local/mount}

{/path/to/local/mount} は空の既存のディレクトリですので、マウントするディレクトリは作成しておきましょう。

ルートディレクトが上記の画像の場合で、homeフォルダをマウントするのであれば

rclone mount gdrive:home /backup/home/ --daemon

となります。

ll /backup/home/
total 13
drwxr-xr-x 1 root root    0 Jun  6 04:33 ./
drwxr-xr-x 3 root root 4096 Jun  6 04:32 ../
-rw-r--r-- 1 root root 1604 May 25 06:59 .zprofile
-rw-r--r-- 1 root root    0 Jun  6 03:53 .zsh_plugins.docx
-rw-r--r-- 1 root root 2699 Jun  6 00:04 .zsh_plugins.sh
-rw-r--r-- 1 root root   21 May 24 09:24 .zshenv
-rw-r--r-- 1 root root 3514 Jun  4 11:32 .zshrc

ローカルのディレクトリを確認し、Googleドライブにあるファイルと同じものがリストされているかどうかみておきましょう。

マウントしたローカルディレクトリ上にファイルをコピーしたり、新規ファイルを作成したりもできるようになります。

アンマウント

umountコマンドを実行します。

アンマウント実行後、ディレクトリリストには何もないことを確認します。

umount /backup/home/

ll /backup/home/
total 8
drwxr-xr-x 2 root root 4096 Jun  6 04:32 ./
drwxr-xr-x 3 root root 4096 Jun  6 04:32 ../

参考