Takuya71 のぶろぐ

外資系ソフトウェア会社で働いてます、認定スクラムマスター

Rclone で Linux(ubuntu) と Dropbox を同期

以前から課題に思っていた Linux とのファイルの受け渡しについて Dropbox を使った方法で解決したいと思います。

私は メインでは Mac (最近はWindowsもたまに)を使っています。 検証機である CUI環境の Linux のマシンとのファイル交換にいつも手軽な方法がないかと思いながらも scp を使ってファイルのやり取りをしていました。

ファイルの保管には Dropbox を使っているので、Dropboxを使うことができればもっと簡単にファイルの受け渡しができると思っていたので 今日は Linux と Dropbox を使った ファイル共有を設定したいと思います。

前提

  • Dropbox のアカウントがある。
  • Linux は ubuntu 18.04

Dropbox にファイルを送る/取得するには

Dropbox にも 公式から提供されている Linux 用の 同期アプリケーションはあります。ただ MacやWindowsのデスクトップ用のものと同じで 基本的に Dropbox ディレクトリを同期します。 除外はできますが 私の用途としては Dropbox 配下の特定のディレクトリだけが同期されれば 十分で 逆に全部が同期されることは 容量のこともあり望みません。 ということで 今回は Dropbox から提供されてる公式のアプリケーションは使わないということにしました。

Rclone とは

そこで 他の候補を探してみたところ Rclone という クラウドストレージと同期することができる アプリケーションを見つけました。 こちらは Dropbox 以外の クラウドサービスもサポートしています。 今回は こちらの Rclone を使ってみることにします。

Rclone インストール

ubuntu に Rclone をインストールします。 はじめに apt でも インストールできるのでそちらを試したのですが、結果的には バージョンが古いためか 以下のようなエラーが出て Dropbox と同期をすることはできませんでした。

$ rclone sync test dropbox:test1/test2
2020/07/25 17:01:18 ERROR : Attempt 1/3 failed with 1 errors and: v1_retired
2020/07/25 17:01:18 ERROR : Attempt 2/3 failed with 1 errors and: v1_retired
2020/07/25 17:01:19 ERROR : Attempt 3/3 failed with 1 errors and: v1_retired
2020/07/25 17:01:19 Failed to sync: v1_retired

apt でインストールされる rclone のバージョンは rclone v1.36 でした。 公式の インストール手順に従って最新の Rclone をインストールしましょう。

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

を ターミナルから実行すると rclone というコマンドがインストールされます。 その後

$ rclone config

を実行して Dropbox アカウントとの連携を行います。

$ rclone config
2020/07/25 17:27:55 NOTICE: Config file "xxxxxxx/.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> dropbox <== 名前は dropbox としました。
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 / Citrix Sharefile
   \ "sharefile"
 9 / Dropbox
   \ "dropbox"
10 / Encrypt/Decrypt a remote
   \ "crypt"
11 / FTP Connection
   \ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"
14 / Google Photos
   \ "google photos"
15 / Hubic
   \ "hubic"
16 / In memory object storage system.
   \ "memory"
17 / Jottacloud
   \ "jottacloud"
18 / Koofr
   \ "koofr"
19 / Local Disk
   \ "local"
20 / Mail.ru Cloud
   \ "mailru"
21 / Mega
   \ "mega"
22 / Microsoft Azure Blob Storage
   \ "azureblob"
23 / Microsoft OneDrive
   \ "onedrive"
24 / OpenDrive
   \ "opendrive"
25 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
26 / Pcloud
   \ "pcloud"
27 / Put.io
   \ "putio"
28 / QingCloud Object Storage
   \ "qingstor"
29 / SSH/SFTP Connection
   \ "sftp"
30 / Sugarsync
   \ "sugarsync"
31 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
38 / seafile
   \ "seafile"
Storage> 9
** See help for dropbox backend at: https://rclone.org/dropbox/ **

Dropbox App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>  ここはブランクのままエンター
Dropbox App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> ここもブランクのままエンター
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n No を選択
Remote config
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> n CUIしか使えないので Noを選択
For this to work, you will need rclone available on a machine that has
a web browser available.

For more help and alternate methods see: https://rclone.org/remote_setup/

Execute the following on the machine with the web browser (same rclone
version recommended):

        rclone authorize "dropbox"

Then paste the result below:
result>

ここで Dropbox から access_token, token_type を取得しないといけないのですが、 この ubuntu マシンは CUI しか使えないので Browser でアクセスして取得ができないため、他の Windows マシンから コマンドを実行します。 はじめに Windows マシンに Rclone をインストールします。 こちらは Rclone の公式さいとから Windows 用のバイナリをダウンロードして 展開します。 展開すると そこに rclone.exe という アプリケーションがありますので これを使って rclone authorize "dropbox" を実行します。

Windows マシン上で トークンを取得する

cd <Rclone展開先>
rclone authorize "dropbox"

そうしますと ブラウザが起動します。 アクセスの許可を求められますので 許可をおこないます。 その後 コマンドに戻り access_token が取得されます。

> rclone authorize "dropbox"          
2020/07/25 17:28:29 NOTICE: Config file "C:\\Users\xxxxxxx\\.config\\rclone\\rclone.conf" not found - using defaults    If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=xxxxxxxxxxxxxxxxxxxx
Log in and authorize rclone for access
Waiting for code... 
Got code
Paste the following into your remote machine --->
{"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","token_type":"bearer","expiry":"0001-01-01T00:00:00Z"} 
<---End paste                                                                                                                                                                                                                                   

この {...} の内容を result> に貼り付けます。

Dropbox と設定は以上になります。

Dropbox へファイル同期

次に Dropbox にファイルを同期したいと思います。 今回は ubuntu上の test ディレクトリを Dropbox の Lin1 ディレクトリの下の test1 ディレクトリに同期したいと思います。 この場合 以下のコマンドを実行します。

$ rclone  sync test dropbox:Lin1/test1

これで ubuntu から Dropbox にファイルを同期することができます。 同期用の sync コマンドだけではなく copy, move というコマンドもあります。

これで Linux とのファイルのやり取りも楽にできるようになったと思います。 しばらく使っていきたいと思います。