LoginSignup
7
13

More than 3 years have passed since last update.

rclone で google ドライブにデータ転送

Last updated at Posted at 2019-12-22

※ 以下の設定内容は2019/4時点のものなので、情報が古い可能性がありますので、参考までにご参照いただけると幸いです

インストール

Macはhomebrewから
brew install rclone

Tips

google driveへのアクセス設定

google drive にアクセスするには2つの手順が必要
・rclone をOAUTH クライアントとして設定
・Driveのアクセス権限を rclone にOAuthで渡す。

Google の API Consoleにログインして

新規プロジェクト作成(右上の「新しいプロジェクト」)
適宜、プロジェクト名を入力して「作成」
ページを更新したら左上を確認して、作成したプロジェクトが選択されているか確認する(複数のプロジェクトを作成している場合)

・作ったプロジェクトで Google drive api を有効にする。
RPI API →「API の概要に移動」→ 「+ API とサービスを有効化」
→ 「Google Drive API」→ 「有効にする」
→ 「(左メニューから)認証情報」
→ 「同意画面を設定」 → 「(アプリケーション名を適宜入力)」
→ 「作成」(名前が適当すぎると拒否される)


・作ったプロジェクトのoauthクライアントIDを作る
→ 「認証情報を作成」→ 「OAuth クライアント ID」
→ 「その他 にチェックを入れる」→ 「適宜、名前(OAuthクライアント名)を入力」→ 「作成」

・作ったプロジェクトのoauthシークレットを取り出す。
作成されたOAuth情報が表示されるので、
「クライアントID」 と 「クライアントシークレット」をコピーして控えておく。
→ 「OK」

つまり
誰のGoogle アカウントのAPI権限で
rclone を動かすのか
って言う設定

rclone の設定

コンソールから操作
$ rclone config

2019/04/21 06:05:40 NOTICE: Config file "$HOME/.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
n/s/q> n
名前を入力
name> basket_1

ストレージタイプを選択
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

#12 / Google Drive
#   \ "drive"

#Storage> 12

** See help for drive backend at: https://rclone.org/drive/ **

:warning: 最近、rcloneをアップデートしたら以下のようにストレージタイプの番号が変更されていたので

12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"

GoogleDriveの設定をする場合は
13 を選択する

Storage> 13

ClientId
コピーしたメモからペースト

ClientIdを入力
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"

scope> 1

以下、特に気にしなければ未入力でEnter

ルートディレクトリIDの入力(指定のディレクトリを基底にしたい場合は設定)
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>

# ディレクトリを開いた際にURLに表示されるID

サービスアカウント認証情報JSONファイルパス
利用しないので未入力でEnter

認証情報JSONファイルパス(そのままEnter)
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

ブラウザで認証画面が開くので、画面指示に従ってGoogleアカウントを選択して進める

URLが自動オープンされるのでアカウントログインしてIDを確認後、入力する
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Log in and authorize rclone for access
Enter verification code>
チームかどうか
Configure this as a team drive?
y) Yes
n) No
y/n> n

問題なければ y でEnter

確認
[basket_1]
type = drive
client_id = xxxxxxxxxxxxxxxx
client_secret = xxxxxxxxxxxxxxxx
scope = drive
root_folder_id = xxxxxxxxxxxxxxxx
token = {"access_token":"xxxxxxxxxxxxxxxx","token_type":"xxxxxxxxxxxxxxxx","refresh_token":"xxxxxxxxxxxxxxxx","expiry":"2019-04-21T07:22:44.285574+09:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d>  y
再度最初の画面が表示されるので、qで終了
Current remotes:

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

client_id
空白でもいいっていうメッセージが出て来るが、これを空白にすると、403 Rate Limite Exceeded で苦しむことになる。端的に言うと、rclone を使ういろいろな人と クライアントIDを共有するので、転送上限も共有することとなって、上限になっちゃうよってことでしょうね。→ https://github.com/ncw/rclone/issues/1591


rclone コマンドの利用

ファイル表示

設定した名前を入力して、ファイルを表示できるか確認する
(ここでは例でbasket_1が設定したGdriveの名称とする)

#rclone ls 設定した名称:

rclone ls basket_1:

Googleドライブの共有フォルダへコピー

--drive-shared-with-meオプションを付ける。
これが無いと、マイドライブへのコピーになる

「./hoge.txt」を、Gdriveのルートディレクトリへコピー」
rclone --drive-shared-with-me copy ./hoge.txt basket_1:
「./hoge.txt」を、共有フォルダ「shared_folder/」へコピー」
rclone --drive-shared-with-me copy ./hoge.txt basket_1:shared_folder/

ローカルにGoogleドライブからダウンロード(コピー)

rclone copy basket_1:hoge.txt $HOME/Desktop

懸念

公開Webアプリじゃないので、APIでのアクセスが100回超えたら使えなくなるかも。

使えなくなったのでID再登録して使えるようにしたが
回数によるものなのかは不明
とりあえず、最悪の場合は再登録すれば
サービス自体は繰り返し利用可能らしい。

参考

rclone コマンドで google ドライブにデータを転送する(rcloneインストール方法と使い方) - それマグで!
rcloneを使用したGoogle Driveのバックアップ - Qiita
Memo/Linux/rclone - DEX Lab
awkの使いかた - Qiita
Error listing directory, Google Drive - bug - rclone forum

7
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
13