0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

line monitor > 付随Tool > USBメモリコピー(v0.1, v0.2)

Last updated at Posted at 2015-12-12

line monitor
http://qiita.com/7of9/items/028556c5a819a6a8de96

動作確認
Raspberry Pi2 + raspbian

シリアルラインモニターのログファイルはSFTPで接続すれば取得できる。

では、ネットワークがつかえない時はどうか。

pythonでUSBフォルダコピー機能を実装した。

プログラム

v0.1 @ github

logFolderCopier.py
# !/usr/bin/env python

'''
v0.1  2015 Dec. 12
  - add USB memory insertion recognition
  - add folder copy feature
  - add main 
'''

'''
Usage:
# sudo python logFolderCopier.py [LogFolderPath]
e.g.
# sudo python logFolderCopier.py /home/pi/linemonitor/Log/
'''


import shutil
import os.path
import sys
import time

param = sys.argv

srcpath=param[1] # xxx/Log/
dstpath="/media/pi/COPYTO"

chk1=False
chk2=False
chk3=False
while True:
	chk1 = chk2
	chk2 = chk3
	chk3 = os.path.isdir(dstpath)
	if os.path.isdir(srcpath) and os.path.isdir(dstpath):
		if chk1==False and chk2==False and chk3==True:
			print "inserted"
			shutil.copytree(srcpath, dstpath + "/Log/")
			print "Copied"
	time.sleep(0.5)

やっていること

  • 500m秒ごとにコピー先フォルダの有無をチェック。
  • フォルダ有無が(False -> False -> True)となった時に、USBメモリが差し込まれたと認識する。SWを押下した時のチェックと同じ。
  • ログフォルダをコピー (shutil.copytree())

参考
http://www.gesource.jp/programming/python/code/0008.html
http://www.gesource.jp/programming/python/code/0015.html

使い方

chmod +xで実行権限をつけておく。

/etc/rc.localなどで以下のように実行しておく。(Logフォルダの場所は環境依存)

nohup ./logFolderCopier /home/pi/linemonitor/Log/ &

ボリューム名 (COPYTO : 全部大文字)としたUSBメモリを挿して2秒程度待てばログフォルダがコピーされる。

実装 v0.2 > umount + 上書き対応

(追記 2019/02/22)

2015年12月13日に下記を追加した。

  • 対象メディアのumount
  • 上書きを考慮したコピー処理

v0.2 @ GitHub

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?