python 脚本多线程备份MySQL数据:
#!/usr/bin/python
# coding=utf-8
import sys
sys.path.append("/data/yw/server/model")
import datetime
import commands
import logging, logging.config
import threading
reload(sys)
sys.setdefaultencoding('utf8')
logging.config.fileConfig("/data/yw/log.conf")
logger = logging.getLogger('dbbackup')
nowtime = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
bakdir = '/data/backup/db_backup'
rsyport = 52873
rsyuser = 'rsync_user'
rsypass = '/etc/rsync_user.pass'
mysql_cmd = '/usr/local/mysql/bin/mysqldump'
MaxThreads = 10
rsypw = "xxxxx"
class ThreadTask(threading.Thread):
def __init__(self, func, func_args):
threading.Thread.__init__(self)
self.func = func
self.args = func_args
def run(self):
apply(self.func, self.args)
#列表说明: 因为多线程备份,避免服务器io不足,ip打乱执行
#备份数据库ip,ssh端口,数据库端口,数据库名,数据库用户名,数据库密码,异地备份ip
dblist = [['192.168.xxxx', 'xxx', '3306', 'zabbix', 'root', 'xxxxx','192.168.xxx'],
['192.168.xxxx', 'xxx', '3306', 'xxxx', 'root', 'xxxxx', '192.168.xxx']]
def check_rsypass(ip, sshport):
'''检查是否有rsync用户密码文件,没则添加'''
cmd = "ssh -p%s %s \"[ ! -f %s ] && echo '%s' > %s && chmod 600 %s \""%(sshport, ip, rsypass, rsypw, rsypass, rsypass)
logger.info("执行检查rsync 密码命令:\n%s" %cmd)
s, r = commands.getstatusoutput(cmd)
if (s==0):
logger.info("%srsync密码配置成功!"%ip)
def dbbackup(ip, sshport, dbport, dbname, dbuser, dbpass, remoteip):
'''备份函数'''
bakfile = '%s/%s-%s.sql.gz'%(bakdir, dbname, nowtime)
#创建备份目录并备份文件
cmd = "ssh -p%s %s '[ ! -d %s ] && mkdir -p %s;%s -u%s -P %s -p%s %s |gzip > %s'"%(sshport, ip, bakdir, bakdir, mysql_cmd, dbuser, dbport, dbpass, dbname, bakfile)
logger.info("执行备份命令:\n%s"%cmd)
s, r = commands.getstatusoutput(cmd)
if (s == 0):
#检查rsync密码文件并异地备份
check_rsypass(ip, sshport)
cmd = "ssh -p%s %s 'rsync -av --port %s --password-file=%s %s %s@%s::dbbackup/%s/'"%(sshport, ip, rsyport, rsypass, bakfile, rsyuser, remoteip, ip)
logger.info("[%s] 备份成功,开始执行异地备份命令:\n%s"%(dbname, cmd))
status, result = commands.getstatusoutput(cmd)
if (status == 0):
logger.info("[%s] 异地备份成功!\n%s"%(dbname, result))
cmd = "/bin/find %s -name \"*.sql.gz\" -mtime +4 | xargs rm -f "%(bakdir)
logger.info("[%s] 删除过期文件\n%s" % (dbname, result))
s, r = commands.getstatusoutput(cmd)
else:
logger.error("[%s] 异地备份失败:\n%s"%(dbname, result))
else:
logger.error("[%s] 备份失败: \n%s"%(dbname, r))
def delfile():
'''删除异地旧备份文件'''
cmd = "ssh -p4902 192.168.xxx '/bin/find /data3/backup/dbbackup/ -name \"*.sql.gz\" -mtime +4 | xargs rm -f '"
logger.info("删除异地备份文件: \n%s"%cmd)
s, r = commands.getstatusoutput(cmd)
if (s == 0):
logger.info("删除异地备份过期文件成功!")
else:
logger.error("删除异地备份过期文件失败: %s")
def main():
logger.info("========================================START=================================================")
num = 0
threads = []
for dbinfo in dblist:
ip = dbinfo[0]
sshport = dbinfo[1]
dbport = dbinfo[2]
dbname = dbinfo[3]
dbuser = dbinfo[4]
dbpass = dbinfo[5]
remoteip = dbinfo[6]
t = ThreadTask(dbbackup, (ip, sshport, dbport, dbname, dbuser, dbpass, remoteip))
threads.append(t)
for thread in threads:
thread.start()
num += 1
while num >= MaxThreads:
num = 0
thread.join()
for thread in threads:
thread.join()
#删除过期文件
delfile()
logger.info("========================================END=================================================")
if __name__ == '__main__':
main()