我們經常會遇到監控一個檔案或目錄的變化,如果有變化,把檔案上傳備份至備份主機,並且我們還要監控上傳過程是否有問題等,根據此需求,查閱了相關的一些材料,編寫如下指令碼實現這個功能:
#!/usr/bin/env python #coding=utf-8 ####################### # #Status wd gs/ccs sql file changed #date:2013-08-26 王偉 #檔案有變化上傳至備份主機,上傳之後驗證檔案是否正確 # #######################import paramiko,os,sys,datetime,time,MySQLdbfrom pyinotify import WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MODIFY\'\'\'CREATE TABLE `wddel_log.status_sql` ( `ip` varchar(16) NOT NULL COMMENT \'機器IP\', `tar_name` varchar(50) NOT NULL COMMENT \'備份檔名字\', `md5` varchar(50) NOT NULL COMMENT \'備份檔案 MD5\', `flag` int(2) NOT NULL COMMENT \'0:成功;1:失敗\', `error_log` varchar(100) NOT NULL COMMENT \'錯誤日誌\', `uptime` datetime NOT NULL COMMENT \'更新時間\', KEY `ip` (`ip`), KEY `uptime` (`uptime`)) ENGINE=InnoDB DEFAULT CHARSET=utf8\'\'\'#日誌表建立指令碼GM_path=\'/home/asktao/\'center_hostname=\'192.168.1.100\'center_username=\'root\'center_password=\'123456\'center_port=63008def log2db(ip,tar_name,md5,flag,error=\'0\'):#刪除日誌入庫 try:tar_name = os.path.split(tar_name)[1]now = time.strftime("%Y-%m-%d %H:%M:%S")conn = MySQLdb.connect(host = \'192.168.1.104\',user = \'root\',passwd = \'1q2w3e4r\',charset=\'utf8\',connect_timeout=20)cursor = conn.cursorsql = "SELECT ip FROM wddel_log.status_sql WHERE ip=\'%s\'" % ipcursor.execute(sql)res = cursor.fetchallif len(res)==0: inster_sql = "insert into wddel_log.status_sql VALUES(\'%s\',\'%s\',\'%s\',%s,\'%s\',\'%s\')" % (ip,tar_name,md5,flag,error,now) cursor.execute(inster_sql) conn.commitelse: update_sql = "UPDATE wddel_log.status_sql SET md5=\'%s\',flag=\'%s\',error_log=\'%s\',uptime=\'%s\' WHERE ip=\'%s\'" % (md5,flag,error,now,ip) cursor.execute(update_sql) conn.commitcursor.closeconn.close except Exception,e:print edef find_ip:#獲取本地 eth0 的 IP 地址 ip = os.popen("/sbin/ip a|grep \'global eth0\'").readlines[0].split[1].split("/")[0] if "192.168." in ip:ip = os.popen("/sbin/ip a|grep \'global eth1\'").readlines[0].split[1].split("/")[0] return ipdef md5sum(file_name):#驗證 sql 打包檔案的 MD5 if os.path.isfile(file_name):f = open(file_name,\'rb\')py_ver = sys.version[:3]if py_ver == "2.4": import md5 as hashlibelse: import hashlib md5 = hashlib.md5(f.read).hexdigest f.close return md5 else:return 0def center_md5(file_name):#上傳至備份中心的檔案的 MD5 try:s=paramiko.SSHClients.set_missing_host_key_policy(paramiko.AutoAddPolicy)s.connect(hostname = center_hostname,port=center_port,username=center_username, password=center_password)conm = "/usr/bin/md5sum %s" % file_namestdin,stdout,stderr=s.exec_command(conm)result = stdout.readlines[0].split[0].strips.closereturn result except Exception,e:return edef back_file(ip,tar_name,tar_md5):#上傳檔案到備份中心 remote_dir=\'/data/sql\' file_name=os.path.join(remote_dir,os.path.split(tar_name)[1]) try:t=paramiko.Transport((center_hostname,center_port))t.connect(username=center_username,password=center_password)sftp=paramiko.SFTPClient.from_transport(t)sftp.put(tar_name,file_name)t.close#print "%s back_file OK" % tar_nameos.remove(tar_name)remot_md5=center_md5(file_name)if remot_md5 == tar_md5: log2db(ip,tar_name,tar_md5,0)else: log2db(ip,tar_name,tar_md5,1,\'remot_md5!=tar_md5\') except Exception,e:#print "connect error!"log2db(ip,tar_name,tar_md5,1,e)os.remove(tar_name)def back_sql:#執行備份 ip = find_ip tar_name = "/tmp/%s.tar.gz" % ip sql_conn = "/usr/bin/find %s -type f -name \'*.sql\'|/usr/bin/xargs /bin/tar zcvPf %s" % (GM_path,tar_name) sql_tar = os.popen(sql_conn).readlines tar_md5 = md5sum(tar_name) if tar_md5 != 0:back_file(ip,tar_name,tar_md5) else:error_log = "%s not find" % tar_namelog2db(ip,tar_name,tar_md5,0,error_log)class PFilePath(ProcessEvent):#檔案變化的觸發 def process_IN_CREATE(self, event):if os.path.splitext(event.name)[1] == ".sql": text = "Create file: %s " % os.path.join(event.path, event.name) #print text back_sql def process_IN_MODIFY(self, event):if os.path.splitext(event.name)[1] == ".sql": text = "Modify file: %s " % os.path.join(event.path, event.name) #print text back_sqldef FSMonitor:#主監控函式 back_sql#執行指令碼先備份 sql 檔案 wm = WatchManager mask = IN_CREATE |IN_MODIFY notifier = Notifier(wm, PFilePath) wdd = wm.add_watch(GM_path, mask, rec=True) print \'now starting monitor %s\' % (GM_path) while True:try : notifier.process_events if notifier.check_events:notifier.read_eventsexcept KeyboardInterrupt: notifier.stop breakif __name__ == "__main__": FSMonitor
Loading...
未載入完,嘗試【重新整理】or【關閉小說模式】or【關閉廣告遮蔽】。
嘗試更換【Firefox瀏覽器】or【Chrome谷歌瀏覽器】開啟多多收藏!
移動流量偶爾打不開,可以切換電信、聯通、Wifi。
收藏網址:www.peakbooks.cc
(>人<;)