Binlog Server python脚本

有问题就是如果nohup python binlog_server.py & ,然后kill 这个脚本,能kill掉,但是ps -ef | grep mysqlbinlog 还是在执行h
还有一个小问题就是如果直接以命令行方式运行明文指定密码,那么通过ps -ef | grep mysqlbinlog会直接看到密码…
密码写到配置文件的话也不太安全,总之,还需要完善

指定不同的dbname,从不同的数据库拉binlog, dbname就是配置文件里的section

创建用户

1
GRANT REPLICATION SLAVE ON *.* TO 'binlog_backup'@'106.3.130.255' IDENTIFIED BY 'xxx'

fw.sh添加

1
2
#备份binlog
$IPTABLES -A INPUT -p tcp -s 106.3.130.255 --dport 3306 -j ACCEPT

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Usage:
binlog_server.py --user=<username> --password=<password> --host=<remote_host> --port=<remote_port> --backup-dir=<backup_dir> --log=<log> [--last-file=<last-file>]
binlog_server.py -h | --help
binlog_server.py --version
binlog_server.py --config=<config_file> --dbname=<database_name> [--last-file=<last-file>]
Options:
-h --help Show help information.
--version Show version.
--user=<username> The user name used to connect to the remote server.
--password=<password> The password used to connect to the remote server.
--host=<remote_host> The remote host IP address.
--port=<remote_port> The remote MySQL server port.
--backup-dir=<backup_dir> The dest to store binlog.
--log=<log> The log.
--last-file=<last-file> Specify the starting binlog.
--config=<config_file> Config file.
--dbname=<database_name> Section name in config file.
"""
from docopt import docopt
import subprocess
import logging
import time
import ConfigParser
import os
arguments = docopt(__doc__, version='Binlog server 1.0')
if arguments['--config']:
cf=ConfigParser.ConfigParser()
cf.read(arguments['--config'])
section_name = arguments['--dbname']
db_host = cf.get(section_name, "db_host")
db_port = cf.get(section_name, "db_port")
db_user = cf.get(section_name, "db_user")
db_passwd = cf.get(section_name, "db_passwd")
backup_dir = cf.get(section_name, "backup_dir")
log = cf.get(section_name, "log")
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=log,
filemode='a')
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=arguments['--log'],
filemode='a')
def dumpBinlog(user,password,host,port,backup_dir,log,last_file=''):
LOCAL_BACKUP_DIR=backup_dir
if backup_dir[-1]!= '/':
os.exit()
#BACKUP_LOG='/data4/binlog_backup/120.27.136.247/BB.log'
BACKUP_LOG=log[log.rfind('/')+1:]
while True:
if not last_file:
cmd="ls -A {LOCAL_BACKUP_DIR} | grep -v {BACKUP_LOG} | grep -v nohup.out |wc -l".format(LOCAL_BACKUP_DIR=LOCAL_BACKUP_DIR,BACKUP_LOG=BACKUP_LOG)
child=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
child.wait()
wc_l=int(child.communicate()[0].strip())
if wc_l != 0:
cmd="ls -l %s | grep -v %s | grep -v nohup.out |tail -n 1 |awk '{print $9}'" % (LOCAL_BACKUP_DIR,BACKUP_LOG)
child=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
child.wait()
LAST_FILE=child.communicate()[0].strip()
else:
LAST_FILE=last_file
logging.info('Last File is %s' % (LAST_FILE))
mysqlbinlog='mysqlbinlog --raw --read-from-remote-server --stop-never --host={REMOTE_HOST} --port={REMOTE_PORT} --user={REMOTE_USER} --password={REMOTE_PASS} --result-file={RESULT_FILE} {LAST_FILE}'.format(REMOTE_HOST=host,REMOTE_PORT=port,REMOTE_USER=user,REMOTE_PASS=password,RESULT_FILE=LOCAL_BACKUP_DIR,LAST_FILE=LAST_FILE)
subprocess.call(mysqlbinlog,shell=True)
logging.info('Binlog server stop!!!,reconnect after 10 seconds')
time.sleep(10)
if __name__ == '__main__':
if arguments['--config']:
lock_file=db_host+"_binlog_server.lock"
else:
lock_file=arguments['--host']+"_binlog_server.lock"
child=subprocess.Popen('ls /tmp|grep %s' % (lock_file),shell=True,stdout=subprocess.PIPE)
child.wait()
lock=child.communicate()[0].strip()
if not lock:
subprocess.call('touch /tmp/%s' % (lock_file),shell=True)
logging.info('Get lock,Binlog server start!!!')
if not arguments['--config']:
dumpBinlog(arguments['--user'],arguments['--password'],arguments['--host'],arguments['--port'],arguments['--backup-dir'],arguments['--log'],arguments['--last-file'])
else:
dumpBinlog(db_user,db_passwd,db_host,db_port,backup_dir,log,arguments['--last-file'])
else:
logging.info('Binlog server already running!!!')
print('Binlog server already running!!!,please check or reomove the lock file')

监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
num_py=`ps -ef | grep binlog_server.py | grep -v grep | grep GN_PT_SLAVE1 | wc -l`
num_mysqlbinlog=`ps -ef | grep mysqlbinlog | grep -v grep | grep 120.27.136.247 | wc -l`
TO_MAIL=xoxoxo@papapa.com
if [ $num_py -eq 0 ] && [ $num_mysqlbinlog -eq 0 ];then
#发邮件,GN_PT_SLAVE1 binlog server宕了
#重启 nohup python /scripts/binlog_server.py --config=/tmp/binlog_server.cnf --dbname=GN_PT_SLAVE1 &
echo "GN_PT_SLAVE1 binlog server宕了" |/usr/bin/mutt -s "Binlog server监控告警" $TO_MAIL
elif [ $num_py -eq 0 ] && [ $num_mysqlbinlog -eq 1 ];then
#发邮件,GN_PT_SLAVE1 python脚本挂了,但是mysqlbinlog还在跑
echo "GN_PT_SLAVE1 python脚本挂了,但是mysqlbinlog还在跑" |/usr/bin/mutt -s "Binlog server监控告警" $TO_MAIL
fi

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost 120.27.143.36]# less /scripts/binlog_server.cnf
[GN_PT_SLAVE1]
db_host=120.27.136.257
db_port=3306
db_user=binlog_backup
db_passwd=xxx
backup_dir=/data1/backup/db_backup/120.27.136.247/ --注意一定要以/结尾
log=/data1/backup/db_backup/120.27.136.247/BB.log
[GN_LOG_MASTER2]
db_host=120.27.143.256
db_port=3306
db_user=binlog_backup
db_passwd=xxx
backup_dir=/data2/backup/db_backup/120.27.143.36/ --注意一定要以/结尾
log=/data2/backup/db_backup/120.27.143.36/BB.log

使用方法

两种方式
1.通过配置文件

1
python /scripts/binlog_server.py --config=/tmp/binlog_server.cnf --dbname=GN_PT_SLAVE1

2.命令行指定
注意backup-dir一定要以’/‘结尾

1
python binlog_server.py --user=binlog_backup --password=xxxx --host=xxxx --port=3306 --backup-dir=/data4/binlog_backup/ --log=/data4/binlog_backup/BB.log

在脚本中 创建了/tmp/IP_binlog_server.lock 文件,为了防止重复运行.
如果有需要停止,需要手动kill binlog_server.py 和 mysqlbinlog, 并且删除/tmp/IP_binlog_server.lock 文件,不然下次起不来

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2018 Fan() All Rights Reserved.

访客数 : | 访问量 :

#