#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import time
import urllib
import pyinotify
import logging
import subprocess
from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
sync_dirs = {'/mnt/video/pvmsvideo/2013' : 'pvms_video_pvmsvideo',\
'/mnt/video/pvmsimage/2013' : 'pvms_video_pvmsimage'}
def Monitor():
class Ptmp(ProcessEvent):
"""docstring for Ptmp"""
def process_IN_CREATE(self,event):
pass
def process_IN_CLOSE_WRITE(self,event):
print "Modify : %s " % os.path.join(event.path,event.name)
#notify_change(event)
def process_IN_DELETE(self,event):
fp = open("/var/log/lostvideo.log","w+")
fp.write("Remove : %s " % os.path.join(event.path,event.name))
fp.close()
wm = WatchManager()
mask = pyinotify.IN_CREATE|pyinotify.IN_DELETE|pyinotify.IN_CLOSE_WRITE
notifier = Notifier(wm,Ptmp())
pathes = sync_dirs.keys()
for path in pathes:
wdd = wm.add_watch(path,mask,rec=True,auto_add=True)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
def notify_change(event):
for key in sync_dirs.keys():
if event.path.find(key) >= 0 :
ps_cmd = "ps -ef|grep -i \"rsync.*%s.*\"|wc -l" % (key)
print "%s %d" % (ps_cmd,int(os.popen(ps_cmd).read()))
if int(os.popen(ps_cmd).read()) == 2:
cmd = "rsync -avrP --chmod=Du=rwx,Dg=rx,Do=rx,Fu=rw,Fg=r,Fo=r --password-file=/etc/backup.pass %s --exclude-from=/etc/rsyncexclude.list backup@10.211.1.115::%s & " % (key,sync_dirs[key])
print cmd
#os.system(cmd)
subprocess.Popen(cmd,shell=True)
if __name__ == "__main__":
Monitor()
能不能抽时间帮忙看看 怎么加个定时器到上边代码,
需求是定时每20分钟同步一次修改过的文件到另一台服务器。