motionの画像ファイルをリモートサーバーに保存する
Raspberry Pi 2でmotionを立ち上げるところまでができたので、motionが吐き出す画像ファイルをリモートのサーバーに送る設定をしてみます。
画像ファイルをどうやって転送しようか悩みましたが、とりあえずsshfsを利用してみたいと思います。
次の記事を参考に進めます。
1. RaspberryPi上のSSHの設定
MacのターミナルからRaspberryPiにログインしてrootになりssh-keygenコマンドを叩いて公開鍵認証用のファイルを作成します。
$ ssh pi@raspberrypi.local pi@raspberrypi ~ $ sudo su - root@raspberrypi:~# ssh-keygen
パスフレーズはなしで、.ssh以下にid_rsaとid_rsa.pubを作ります。 .ssh/configファイルは次のような形にしました。
root@raspberrypi:~# vi .ssh/config Host vagrant-centos User pi HostName 192.168.0.28 IdentityFile ~/.ssh/id_rsa
これを書いておくとssh接続時の設定が楽になります。
2. 画像ファイル転送先リモートサーバーの設定
リモートサーバーはMacのVM上にCentOS7を入れて代用します。 Vagrantについては次の記事を参考にどうぞ。
CentOS7のboxを選んでインストールしました。 同一プライベートネットワーク上の別のマシン(RaspberryPi)からアクセスするにはブリッジの設定が必要なようです。
Vagrantfileを次のように書き換えました。
config.vm.network "public_network", bridge: 'en0: Ethernet (AirPort)', ip: "192.168.0.28"
ブリッジのネットワークインターフェースを固定するのとIPアドレスを指定しました。
先程の.ssh/configファイルの対象がこのサーバーになるわけです。
3. リモートサーバー上のpiユーザーの作成とSSHの設定
VagrantのCentOS上にpiユーザーを作ります。
$ vagrant ssh [vagrant@localhost ~]$ sudo useradd pi
piユーザーにRaspberryPi上で作った公開鍵を設定します。
[pi@localhost ~]$ mkdir .ssh [pi@localhost ~]$ chmod 700 .ssh [pi@localhost ~]$ cd .ssh [pi@localhost ~]$ vi authorized_keys RaspberryPiの公開鍵をコピペして保存 [pi@localhost ~]$ chmod 600 authorized_keys
これでRaspberryPiからsshで接続できるかテストしてみます。
root@raspberrypi:~# ssh vagrant-centos [pi@localhost ~]$ mkdir motion
ログインできることが確認できたらOKです。 motionのファイルを保存する先のディレクトリを作成しておきました。
4. RaspberryPiのsshfsとautofsの設定
次にRaspberryPiにsshfsとautofsをインストールします。
root@raspberrypi:~# apt-get install sshfs autofs
autofsの設定をします。
root@raspberrypi:~# vi /etc/auto.master 以下を最後に追記 /home/pi/mnt /etc/auto.sshfs --timeout 0 --ghost
root@raspberrypi:~# vi /etc/auto.sshfs ファイルを新規に作って以下を記述 motion -fstype=fuse,allow_other :sshfs\#vagrant-centos\:/home/pi/motion
autofsで指定しているディレクトリを用意する必要があります。
root@raspberrypi:~# mkdir /home/pi/mnt
autofsをリスタートします。
root@raspberrypi:~# service autofs restart
これでリモート上のCentOSのpiユーザーのmotionディレクトリをRaspberryPi上の/home/pi/mnt/motionにマウントすることができるはずです。
motionの設定を変更して、画像ファイルなどを /home/pi/mnt/motion に出力するようtarget_dirの値を変更します。
root@raspberrypi:~# vi /etc/motion/motion.conf target_dir /home/pi/mnt/motion root@raspberrypi:~# service motion restart
ここまでやっておくと、RaspberryPiを起動しただけでmotionが勝手にサーバーにファイルを吐いてくれるようになります。
autofsの設定がわからなかったり、sshfsがつながらなかったりといろいろと試行錯誤しましたが、無事に画像ファイルがリモート上のサーバーに転送されるようになりました。
とはいえ、まだサーバーにmotionの画像を転送しただけで、画像の削除をしていないため、24時間以内のファイルだけ残すなどの設定をCentOS側に仕込みたいと思います。
motionの設定もデフォルトのままなので、いろいろと検証していきたいと思います。