由于刚刚学习Python,利用课余时间写点公司需要的小程序,希望对大家有用。
#!/usr/bin/python
#coding=utf-8
import os
import sys
import datetime
from pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MODIFY
wm = WatchManager()
mask = IN_DELETE | IN_CREATE |IN_MODIFY # watched events
d = datetime.datetime.now()
RSYNC='/usr/bin/rsync'
SRC='/home/xinggw/'
DST='root@192.168.10.127:/home/wangsy/'
class PFilePath(ProcessEvent):
def process_IN_CREATE(self, event):
f = open('/var/log/rsync/rsync.log','a')
sys.stdout=f
print d, "Create file: %s " % os.path.join(event.path, event.name)
f.close()
os.system("%s -ahqzt --delete %s %s" % (RSYNC,SRC,DST))
def process_IN_DELETE(self, event):
f = open('/var/log/rsync/rsync.log','a')
sys.stdout=f
print d, "Delete file: %s " % os.path.join(event.path, event.name)
f.close()
os.system("%s -ahqzt --delete %s %s" % (RSYNC,SRC,DST))
def process_IN_MODIFY(self, event):
f = open('/var/log/rsync/rsync.log','a')
sys.stdout=f
print d, "Modify file: %s " % os.path.join(event.path, event.name)
f.close()
os.system("%s -ahqzt --delete %s %s" % (RSYNC,SRC,DST))
if __name__ == "__main__":
notifier = Notifier(wm, PFilePath())
wdd = wm.add_watch(SRC, mask, rec=True)
while True:
try :
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
阅读(4717) | 评论(0) | 转发(0) |