rs, files in os.walk(path):?
? ? ? ? size += sum([getsize(join(root, name)) for name in files])?
? ? return (size)?
?
?
def statement(path, backup_host, isMaster):?
? ? '''''
? ? 功能: 从 metadata 读取change master to 信息
? ? 1. 备份过程: 会先生成: metadata.partial, 完成后metadata.partial会重名为: metadata 并写入备份完成时间
? ? 2. metadata 分3段:
? ? ? (1) Started dump: 备份开始时间.
? ? ? (2) master 的log-file 和 log-pos 信息 (必有); slave 的host、log-file 和 log-pos 信息 (备机是slave 才有)
? ? ? (3) Finished dump: 备份结束时间
? ? 3. 返回错码, master_host 和 change master to 信息
? ? '''?
? ? path += "/metadata"; sMetadata = ""; master_host = ""; er_code = 654321; er_info = "%s not exists !!!" % (path)?
?
?
? ? if (os.path.exists(path)):?
? ? ? ? if (isMaster != 1):?
? ? ? ? ? ? # 备机是 slave?
? ? ? ? ? ? num = 3?
? ? ? ? ? ? sFinds = "SLAVE STATUS"?
? ? ? ? else:?
? ? ? ? ? ? num = 2?
? ? ? ? ? ? sFinds = "MASTER STATUS"?
?
?
? ? ? ? f = open(path, 'r')?
? ? ? ? rows = f.readlines(); i = 100; lst =[]?
? ? ? ? for s in rows:?
? ? ? ? ? ? if (s.find(sFinds) > 0):?
? ? ? ? ? ? ? ? i = 1; continue?
?
?
? ? ? ? ? ? if (i <= num):?
? ? ? ? ? ? ? ? lst.append(s.split(':')[1].strip())?
? ? ? ? ? ? ? ? i += 1?
?
?
? ? ? ? if (isMaster == 1):?
? ? ? ? ? ? # 备机是 master?
? ? ? ? ? ? master_host = backup_host?
? ? ? ? ? ? log_file, log_pos = lst;?
? ? ? ? else:?
? ? ? ? ? ? # 备机是 slave?
? ? ? ? ? ? master_host, log_file, log_pos = lst;?
?
?
? ? ? ? er_code = 0?
? ? ? ? er_info = ""?
? ? ? ? sMetadata = "CHANGE MASTER TO MASTER_HOST='%s',MASTER_LOG_FILE='%s',MASTER_LOG_POS=%s,MASTER_USER='rep_user',MASTER_PASSWORD='meizu.com'" % (master_host, log_file, log_pos )?
?
?
? ? return (er_code, er_info, master_host, sMetadata)?
?
?
def execute(cmd):?
? ? '''''
? ? 1.执行 shell 命令
? ? 2.返回执行信息 (returncode = 0 则执行成功, std_err 为报错的错误信息)
? ? '''?
? ? try:?
? ? ? ? returncode, std_err = commands.getstatusoutput(cmd)?
? ? ? ? return (returncode, std_err)?
? ? except os.error, e:?
? ? ? ? # 异常返回 1001 错误?
? ? ? ? return (1001, e)?
?
?
def call_proc(my_args):?
? ? # 备份信息写入数据库?
? ? try:?
? ? ? ? conn = mydb.connect(host = '127.0.0.1', user = 'test', passwd = 'zxc/213?', db = 'meizu_item')?
? ? ? ? cur? = conn.cursor()?
?
?
? ? ? ? cur.callproc('sp_backup_i',[my_args[0], my_args[1], my_args[2], my_args[3], my_args[4], my_args[5], my_args[6], my_args[7], my_args[8], my_args[9], my_args[10], my_args[11], my_args[12]])?
? ? ? ? conn.commit()?
? ? except mydb.Error, e:?
? ? ? ? pass?
? ? ? ? # print "Mysql Error %d: %s" % (e.args[0], e.args[1])?
? ? finally:?
? ? ? ? cur.close(); conn.close()?
?
?
if __name__ == '__main__':?
? ? main()?
#!/usr/bin/phthon
import os
import time
import commands
import shutil
import threading
from os.path import join, getsize
import MySQLdb as mydb
# 备份目录
baseDir = "/data2/backup/backup_data/"
# ns 或 wx;? 备份后是否要压缩(mydumper 自带压缩功能),要压缩 True,否则 False.
idc = 'ns'; isZip = True
# 备份失败是否重试 ,True 则重试,不重试 设 False, retry_sleep 多久后才开始重试(秒)
is_errRetryBackup = True; retry_sleep = 300
# 备份日期
backup_date = time.strftime("%Y%m%d")
# 备份命令
cmd? = "/usr/local/bin/mydumper -h %s -u root -p password? -P %s %s -t 5 -o %s"
'''
功能描述:
? 1. mydumper 远程批量备份, 备份列表由配置文件提供
? 2. 可按要求对备份是否压缩(mydumper 自动压缩)
? 3. 备份失败允许再尝试备份一次
? 4. 备份信息写入数据库
'''
def main():
? ? thread_pool = []
? ? # 是否启用压缩
? ? zip = '-c' if isZip == True else ''
? ? # 从配置文件读取 ip, 名称, 端口, 并拼凑备份语句
? ? #f = open('/data2/backup/cnf/other_list.cnf', 'r')
? ? f = open('/data/other_list.cnf', 'r')
? ? for lines in f.readlines():
? ? ? ? if (not lines.startswith('#') and len(lines.strip()) > 0):
? ? ? ? ? ? str = lines.split()
? ? ? ? ? ? host, businessName, port, isMaster = str[0], str[1], str[2], str[3]
? ? ? ? ? ? # 业务文件夹不存在则创建
? ? ? ? ? ? dir = baseDir + '/' + businessName;
? ? ? ? ? ? if (not os.path.exists(dir)):
? ? ? ?