なんて不親切なんだろう・・・という、設定記事しかなく
最終的に、
rcloneでGoogle Driveへのリモートバックアップを自動化する【rsyncみたいなやつ】
を見つけて、ようやくGoogle Driveの接続が出来ました。
動いてしまえば、どの記事を読んでも「ああ~」という感じなんですが、
悩ましいかったところを強調して、記載してみました。
rcloneのインストール
curl https://rclone.org/install.sh | sudo bash
これだけ。あっけないぐらい、
あとは、つらつらと勝手にインストールしてくれます。
rcloneの設定
インストール完了後、入力するところを、強調しています。
※当然ながら、以降は入力せんでいいやつです。
[ec2-user@ip-10-0-0-0 ~]$ rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n ※新規
name> GoogleDrive ※任意
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
(省略)
11 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
12 / Google Drive
\ "drive"
(省略)
Storage> 12
** See help for drive backend at: https://rclone.org/drive/ **
Google Application Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> 000000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com ※Google APIs 承認情報
Google Application Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> xxxxxxxxxxxxxxxxxxxxxxxxx ※Google APIs 承認情報
Google Driveを使うには、認証が必要です。
- client_id
- client_secret
は、まず、
https://console.developers.google.com/apis/credentials
に、読み書きしたいGoogleDriveのアカウントでログインします。
規約云々は、やり過ごしてもらって、
1. プロジェクトを作成
※場所は、「組織なし」で、作成を押します。
2. 認証情報
「OAuth クライアント ID の作成」をしていきます。
++ 以下、作成中 ++
では、続きを。
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.
(省略)
scope> 2 ※readonly(EC2から書き込む予定があれば、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> xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxx ※ルート直下の場合はなにも入力しない
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> ※なにも入れず、Enterを押す
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 or Y didn't work
y) Yes
n) No
y/n> n
If your browser doesn't open automatically go to the following link:
https://accounts.google.com/o/oauth2/auth?(省略)
※このURLをブラウザーに貼り付け、認証の手続きを行う。
Log in and authorize rclone for access
Enter verification code> xxxxxxxxxxxxxxxxxxxxxxx ※承認後に表示されるコード
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[GoogleDrive] ※最初に設定した名前
type = drive
client_id = 000000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxx
scope = drive.readonly
root_folder_id = xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxx
token = {"access_token":"xxxx","token_type":"Bearer","refresh_token":"1/xxxx","expiry":"2018-10-28T18:54:53.172455517Z"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:
Name Type
==== ====
GoogleDrive 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 ※終了
rcloseの動作確認
rclone ls GoogleDrive:/
の実行結果↓

無事、GoogleDrive上の一覧を取得することが出来ました!
Amazon Cloudですが、こちらも参考に。
>> https://qiita.com/_akisato/items/f4b36e86fcf7ae665b4d
rclone 導入経緯
クライアントで入力された値を、直接WEBのURLを叩いて、データを送り込むのが普通なんですが、
「インターネット非接続状態で動かない」という
現場からのクレームを考えた結果、
ファイルとしてログを保存しておけば、
オンラインになれば、GoogleDriveが勝手に、
ファイルを吸い上げてくれるので、
オフライン・オンライン時の処理を簡素化するために、GoogleDriveを使いました。
Gmailやリモートデスクトップも使えるので、
ホントGoogleアカウントは便利ですネ?!
***
そんなわけで、どうしても、EC2からGoogleDriveにアクセスする必要があり、
まず試したのが、「google-drive-ocamlfuse」なんですが、
インストールで失敗します。
リポジトリー先にファイルがなく、開発終了?のため断念。
参考 > https://qiita.com/wapa5pow/items/b91be008810331c4dc59
次に、PHPでやろうとしたのですが、コンソールでないと動かないようで、
参考 > https://t.co/fyUsZ8TBx0
(後で思えば、execで呼び出せばよかった)
Javascript版でGoogleDriveを読み込んで、AJAXでDBに書き込もうかとしたのですが、
それも面倒だったので、
rcloneにたどり着いたわけです。
せっかくなので、
WEBサーバーのファイルバックアップの設定もしておきたいと思います。
