MySQL master-slave binlog timed deletion is very important, generally through expire_logs_days = 10来 set binlog reserved Days (mysql5.0 version is not supported), but sometimes this is not enough, if a few days of log volume is very large, the hard disk may be full, so not only to set the number of days to keep, but also to monitor the space usage of the hard disk. Wrote a script, suitable for each version of MySQL, keep the 3-day log, when the log is stored in more than 80% of the hard disk, it is retained for 2 days, but at least one day of binlog log files are retained.
- #!/bin/env python
- #-*-Coding:utf-8-*-
- ##############################################################
- #查看存在binlog的目录位置并找出3天前的最后一个bin-log file name
- #删除3天以前的binlog文件, after the data1 directory is removed, the drive usage over
- #过的80%, continue to delete the log file 2 days ago, keeping at least one day's log.
- ##############################################################
- Import Os,sys,time,mysqldb
- def log_w (text):
- LogFile = "/usr/local/zabbix/bin/delet.log"
- now = Time.strftime ("%y-%m-%d%h:%m:%s")
- tt = now + "\ T" + str (text) + "\ n"
- f = open (logfile,' A + ')
- F.write (TT)
- F.close ()
- def mysql_conn (port,lastlog,days):
- Try:
- Center_ip = ' 127.0.0.1 '
- Center_user = ' Repl_monitor '
- center_passwd = ' Vqmqlgwtaw3k0uv8 '
- sql = "PURGE MASTER LOGS to '%s ';"% Lastlog
- conn = MySQLdb.connect (host = Center_ip,port = Int (port), user = CENTER_USER,PASSWD = center_passwd,connect_timeout=5 )
- cursor = Conn.cursor ()
- Cursor.execute (SQL)
- AllData = Cursor.fetchall ()
- Cursor.close ()
- Conn.close ()
- Text = "Deltet before%s days Binlog,deltet%s before!"% (Days,lastlog)
- Log_w (text)
- except Exception,e:
- Log_w (e)
- Def find_logdir ():
- conn = "Find/-name binlog|grep-v usr"
- Logdir_list = Os.popen (conn). ReadLines ()
- If Len (logdir_list)! = 0:
- For LogDir in logdir_list:
- DataDir = Logdir.strip (). Split ("/") [1]
- if "Mysql_log" in Logdir.strip ():
- Port = 3306
- Else:
- Port = Logdir.strip (). Split ("/") [3].split ("-") [-1]
- Days = 3
- while 1:
- conn = "Find%s-mtime%s|sort"% (Logdir.strip (), days)
- Count = Os.popen (conn). ReadLines ()
- If Len (count)! = 0:
- Lastlog = count[-1].strip (). Split ("/") [-1]
- Mysql_conn (Port,lastlog,days)
- DF = "Df-h|grep-e '%s$ ' |awk ' {print $} ' |awk-f ' percent ' ' {print '} '"% DataDir
- Disk = Os.popen (DF). Read (). Strip ()
- if not disk:
- Break
- Else:
- if int (disk) < :
- Break
- Else:
- Days = days- 1
- if days = = 1:
- Break
- Else:
- Sys.exit ()
- if __name__ = = "__main__":
- Find_logdir ()
This article is from "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1123088
Regular cleanup of MySQL master-slave logs