2021-01-15に更新

【Raspberry Pi】Raspberry Piをデジタルサイネージ風に使う その2 (rclone導入)

本記事は2019/12/27に作成したものです。
最新の環境ではうまく動作しない可能性がありますのでご注意ください。

その1の記事 で作成したデジタルサイネージを運用しはじめたところ、以下の悩みが発生しました。。。

サイネージのコンテンツ更新のたびに、pdfのスライドを毎度アップロードするのがしんどい!

なので、Google ドライブとrcloneを使って、今まで手動でアップロードしていた作業を自動化しました。
Google ドライブの指定ディレクトリをrcloneで同期させるスクリプトを定期実行させる仕組みです。

動作確認済み環境

Raspberry Pi 3 Model B+
Raspbian 9.4 stretch (日本語環境)
rclone v1.50.2

必要な設定の流れ

  • rcloneのインストールと設定
  • rcloneを使ったGoogle ドライブ同期スクリプト作成
  • Google ドライブ同期スクリプトを定期実行させるためのcron作成

rcloneのインストールと設定

rcloneのインストール

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

rcloneの設定

$ rclone config

以下対話形式で進めます

1. 新規なので、nを選択する

2019/12/27 10:50:07 NOTICE: Config file "/home/pi/.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

2. 適当に名前をつける

name> drive

3. Google ドライブを使うので、13を選択する

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
多いので省略
13 / Google Drive
   \ "drive"
多いので省略
Storage> 13

4. Client Idは、未入力でEnter

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> 

5. client_secretは、未入力でEnter

Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret>

6. Google ドライブからファイルを取り出せればいいので、Read-onlyの2を選択する

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.
 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> 2

7. root_folder_idは、未入力でEnter

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.

Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.

Enter a string value. Press Enter for the default ("").
root_folder_id>

8. service_account_fileは、未入力で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> 

9. advanced configは、編集しないのでnを選択する

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

10. auto configは、使用しないので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

11. rcloneがGoogle ドライブへアクセスするために必要なverification codeを入手する

If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=(省略)
Log in and authorize rclone for access
Enter verification code>

go to the following link: の後のURLをコピーし、WebブラウザでコピーしたURLへ接続します。
画面の指示にしたがってGoogleアカウントの認証とrcloneのアクセス許可を与えてください。
最後に、表示されたコードをコピーして、Enter verification codeに貼り付けます。

12. team driveは使わないのでnを選択する

Configure this as a team drive?
y) Yes
n) No
y/n> n

13. 設定内容が表示されるので、yを選択する

--------------------
[drive]
type = drive
scope = drive.readonly
token = {"access_token":"(省略)"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

14. 設定から抜けるため、qを選択する

Current remotes:

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

15. 動作テスト

ラズパイから、Google ドライブに作成しておいたディレクトリの参照ができることを確認します
(以下例はGoogle ドライブのsinageディレクトリを参照しています)

$ rclone ls drive:sinage
  1565471 slide.pdf

rcloneを使ったGoogle ドライブ同期スクリプト作成

スクリプト置き場の作成

$ mkdir /home/pi/cron

スクリプトの作成

$ vi /home/pi/cron/syncsinage.sh

syncsinage.shの中身
(Google ドライブのsinageディレクトリを/home/pi/sinage/へ同期させています)

#!/bin/bash
rclone sync drive:sinage /home/pi/sinage/

スクリプトの実行権限付与

$ chmod 744 /home/pi/cron/syncsinage.sh

Google ドライブ同期スクリプトを定期実行させるためのcron作成

cron作成エディタ起動

使うエディタの選択はお好みで...

$ crontab -e

cronの中身
(5分おきにsyncsinage.shを実行させています)

SHELL=/bin/bash

### sync gdrive to sinage
*/5 * * * * /home/pi/cron/syncsinage.sh

以上で作業は完了です!
これで面倒なアップロード作業とはお別れできました!
あとは、ラズパイ起動時に自動でスライドショーを始める方法を探したいと思います。
これができたら、もっと運用が楽になりそうです。

ツイッターでシェア
みんなに共有、忘れないようにメモ

arohajiro

しがない元インフラエンジニアです

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント