Regular cleanup of MySQL master-slave logs

Source: Internet
Author: User

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.

  1. #!/bin/env python
  2. #-*-Coding:utf-8-*-
  3. ##############################################################
  4. #查看存在binlog的目录位置并找出3天前的最后一个bin-log file name
  5. #删除3天以前的binlog文件, after the data1 directory is removed, the drive usage over
  6. #过的80%, continue to delete the log file 2 days ago, keeping at least one day's log.
  7. ##############################################################
  8. Import Os,sys,time,mysqldb
  9. def log_w (text):
  10. LogFile = "/usr/local/zabbix/bin/delet.log"
  11. now = Time.strftime ("%y-%m-%d%h:%m:%s")
  12. tt = now + "\ T" + str (text) + "\ n"
  13. f = open (logfile,' A + ')
  14. F.write (TT)
  15. F.close ()
  16. def mysql_conn (port,lastlog,days):
  17. Try:
  18. Center_ip = ' 127.0.0.1 '
  19. Center_user = ' Repl_monitor '
  20. center_passwd = ' Vqmqlgwtaw3k0uv8 '
  21. sql = "PURGE MASTER LOGS to '%s ';"% Lastlog
  22. conn = MySQLdb.connect (host = Center_ip,port = Int (port), user = CENTER_USER,PASSWD = center_passwd,connect_timeout=5 ) 
  23. cursor = Conn.cursor ()
  24. Cursor.execute (SQL)
  25. AllData = Cursor.fetchall ()
  26. Cursor.close ()
  27. Conn.close ()
  28. Text = "Deltet before%s days Binlog,deltet%s before!"% (Days,lastlog)
  29. Log_w (text)
  30. except Exception,e:
  31. Log_w (e)
  32. Def find_logdir ():
  33. conn = "Find/-name binlog|grep-v usr"
  34. Logdir_list = Os.popen (conn). ReadLines ()
  35. If Len (logdir_list)! = 0:
  36. For LogDir in logdir_list:
  37. DataDir = Logdir.strip (). Split ("/") [1]
  38. if "Mysql_log" in Logdir.strip ():
  39. Port = 3306
  40. Else:
  41. Port = Logdir.strip (). Split ("/") [3].split ("-") [-1]
  42. Days = 3
  43. while 1:
  44. conn = "Find%s-mtime%s|sort"% (Logdir.strip (), days)
  45. Count = Os.popen (conn). ReadLines ()
  46. If Len (count)! = 0:
  47. Lastlog = count[-1].strip (). Split ("/") [-1]
  48. Mysql_conn (Port,lastlog,days)
  49. DF = "Df-h|grep-e '%s$ ' |awk ' {print $} ' |awk-f ' percent ' ' {print '} '"% DataDir
  50. Disk = Os.popen (DF). Read (). Strip ()
  51. if not disk:
  52. Break
  53. Else:
  54. if int (disk) < :
  55. Break
  56. Else:
  57. Days = days- 1
  58. if days = = 1:
  59. Break
  60. Else:
  61. Sys.exit ()
  62. if __name__ = = "__main__":
  63. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.