こんにちはtsuyuhです。
世間は夏真っ盛りという感じですが、私はテレワーク続きで日中外に出ることが少なく、冷房の下でモヤシのようになっております。
さて、今回はサーバとクラウドストレージ間のデータ移動の話をしようと思います。
クラウドストレージにあるデータをサーバに移動させたいと思ったとき、一回自分のPCにダウンロードしてきて、それを再びサーバにアップロードする、というのはちょっと面倒です。
特に、秘匿性の高いデータや、サイズの大きなデータの場合にはそういうことはしたくありません。
なので、ローカルを経由することなく、クラウドストレージからデータをサーバへ直接移動させる方法 "rclone" をご紹介します。
なお、今回はOneDriveからデータ転送してみます。
ちなみに、"rclone" はrsync for cloud storageという意味だそうです。
rsyncは、コマンドラインでファイルやディレクトリを遠隔同期するコマンドなので、それと同様のことをクラウドストレージでやってくれるということですね。

作業環境
Windows PCを使い、Tera TermでLinuxサーバにリモートアクセス
(Tera Termについてはこちらの記事をどうぞ↓)
Windowsユーザのためのあれこれ① -サーバ接続編 - アメリエフの技術ブログ(Linuxサーバ) rclone v1.51.0
(ローカルマシン) rclone v1.55.1
利用クラウドストレージ:OneDrive
rcloneのインストール (Linuxサーバ)
インストール方法は公式サイトを参照してください。
色んな手段があるようです。お好みで。
curl https://rclone.org/install.sh | sudo bash
など。
rcloneのインストール (ローカルマシン)
rcloneを使って、OneDriveのアクセストークンを取得する必要があるのですが、Linuxサーバではウェブブラウザを開けないので、ローカルマシンにもrcloneをインストールしておきます。
https://rclone.org/downloads/
このページから、ご自身の環境にあったものをダウンロードしてくればOKです。
筆者はWindowsの64bit版をダウンロードしました。
ダウンロードしたファイルを解凍したら、適当なフォルダに移動させておきます。
わかりやすいので今回はDesktopに。
PowerShellで確認するとこんな感じですね。↓
> ls
ディレクトリ: C:\Users\[自分のユーザ名]\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2021/07/20 15:49 rclone-v1.55.1-windows-amd64
> ls .\rclone-v1.55.1-windows-amd64\
ディレクトリ: C:\Users\[自分のユーザ名]\Desktop\rclone-v1.55.1-windows-amd64
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021/07/20 15:49 8028 git-log.txt
-a---- 2021/07/20 15:49 1398558 rclone.1
-a---- 2021/07/20 15:49 43068416 rclone.exe
-a---- 2021/07/20 15:49 1489225 README.html
-a---- 2021/07/20 15:49 1220996 README.txt
この中のrclone.exeを後で使います。
OneDriveのアクセストークンを取得する (ローカルマシン)
WindowsならPowerShellでrclone.exeを実行します。
OneDriveのアクセストークンを取得するには rclone.exe authorize "onedrive" です。
> cd \Users\[自分のユーザ名]\Desktop\rclone-v1.55.1-windows-amd64 > ./rclone.exe authorize "onedrive"
すると、以下の表示がでて、ウェブブラウザが自動的に起動します。
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=j82Dn4lCW-demc1c2Ntujw Log in and authorize rclone for access Waiting for code...

なので、
自分のMicrosoftアカウントでサインインして、
Success! という表示に変わればOKです。

そして、再びPowerShellを見ると、先ほどのメッセージの続きに、アクセストークンなどの必要な情報が出力されています。
Got code
Paste the following into your remote machine --->
{"access_token":"[省略]","token_type":"Bearer","refresh_token":"[省略]","expiry":"2021-07-27T15:39:50.1671168+09:00"}
<---End paste
このうち、{} の部分をコピーして、メモ帳か何かに貼っておきます。
トークンには有効期限があるので以降の作業はお早めに。
rcloneで接続先のOneDriveの情報を登録する (Linuxサーバ)
まず、rclone config で接続先の情報を登録 (リモート設定ファイルを作成) します。
$ rclone config
# 初めて設定ファイルを作る場合にはこの表示が出ます。
2021/07/27 14:51:50 NOTICE: Config file "/home/[ユーザ名]/.config/rclone/rclone.conf" not found - using defaults
# 新しいリモート設定ファイルを作りたいので、n を入力します。
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
# 名前は適当に決めます。
name> fromonedrive
# ストレージの種類を問われるので、onedriveを選択します。入力は "onedrive" でも "23" でもどちらでもOKです。
# 色々なクラウドストレージに対応していますね。
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 / Transparently chunk/split large files
\ "chunker"
32 / Union merges the contents of several remotes
\ "union"
33 / Webdav
\ "webdav"
34 / Yandex Disk
\ "yandex"
35 / http Connection
\ "http"
36 / premiumize.me
\ "premiumizeme"
Storage> onedrive
** See help for onedrive backend at: https://rclone.org/onedrive/ **
# 次は空欄のままEnterを押せばOKです。
Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
# これも同様に空欄のままEnter。
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
# これはデフォルトでOKです。
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
# 先ほどコピーしておいたアクセストークン等の情報を入力します。
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine (same rclone version recommended) :
rclone authorize "onedrive"
Then paste the result below:
result> {"access_token":"[省略]","token_type":"Bearer","refresh_token":"[省略]","expiry":"2021-07-27T15:39:50.1671168+09:00"}
# アクセストークンを取得したmicrosoftアカウントで何がしたいのか選択します。
# 今回は普通にOneDriveにアクセスしたいだけなので "1" を選択。
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
3 / Type in driveID
\ "driveid"
4 / Type in SiteID
\ "siteid"
5 / Search a Sharepoint site
\ "search"
Your choice> 1
# 接続したいドライブを選択します。個人のアカウントなのでpersonalのみですが。
Found 1 drives, please select the one you want to use:
0: (personal) id=[略]
Chose drive to use:> 0
# デフォルトでOK
Found drive 'root' of type 'personal', URL: https://onedrive.live.com/?cid=[略]
Is that okay?
y) Yes (default)
n) No
y/n> y
# これまで入力した情報が表示されるので、問題がなければ "y" を入力します。
--------------------
[fromonedrive]
type = onedrive
token = {"access_token":"[省略]","token_type":"Bearer","refresh_token":"[省略]","expiry":"2021-07-27T15:39:50.1671168+09:00"}
drive_id = [略]
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
# これまでに作成したすべてのリモート設定が表示されます。
# さらに新しい接続先を登録したり削除したりできます。これでOKなら "q" を入力します。
Current remotes:
Name Type
==== ====
fromonedrive onedrive
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
これで登録完了です。
OneDriveにアクセスする (Linuxサーバ)
rcloneの基本的な使い方は、
rclone [コマンド] [リモート設定ファイル名]:/[フォルダ・ファイル名] です。
例えば、OneDriveの最上層のフォルダを参照するには下記のコマンドを実行します。
$ rclone lsd fromonedrive:/
-1 2021-07-20 11:06:10 1 test
-1 2021-07-20 11:05:14 0 ドキュメント
-1 2021-07-20 11:05:14 0 画像
(筆者が事前に作成したテスト用のフォルダしか入っていません。)
OneDrive内のすべてのファイルを参照するには下記コマンドを実行します。
$ rclone ls fromonedrive:/
1481287 OneDrive を使い始める.pdf
16 test/example.txt
→ OneDriveの最上層に "OneDriveを使い始める.pdf" というファイルがあり、testフォルダ内に "example.txt" というファイルがあることが確認できました。
また、特定のファイルをサーバ側にコピーするには下記コマンドを実行します。
$ rclone copy fromonedrive:test ./ $ ls example.txt
→ これで、OneDrive内のtestフォルダに入っている全てのファイルが、サーバのWorking Directoryにコピーされました。もちろん、ファイルを一つ一つ指定することも可能です。
同期する場合は下記コマンドを実行します。
OneDriveにないフォルダ・ファイルは削除されてしまうので、注意が必要です。
$ rclone sync fromonedrive:test ./ $ ls example.txt
サーバが共用である等の理由で接続先の設定を残しておくのが嫌ならば、最後にリモート設定ファイルを削除します。
$ rclone config Current remotes: Name Type ==== ==== fromonedrive onedrive 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> d Choose a number from below, or type in an existing value 1 > fromonedrive remote> 1 No remotes found - make a new one n) New remote s) Set configuration password q) Quit config n/s/q> q
まとめ
rcloneを使えば、リモートアクセスしているサーバから簡単にクラウドストレージを参照できる!