if (returncode == 0):
# 备份 std_err 返回不为空也视为出错。
if (std_err.strip() != ""):
returncode = 123456
else:
# 获取 change master to 信息,再次校验备份是否有效
returncode, std_err, master_host, slave_statement = statement(backupDir, backup_host, isMaster)
if (returncode == 0):
file = backupDir
if (returncode != 0):
# 异常备份标记为: 日期 + _ERR
errDir = backupDir + "_ERR"
os.rename(backupDir, errDir)
file = errDir
# 获取备份大小
file_size = getDirsize(file)
if (len(std_err) > 255):
std_err = std_err[:250] + "..."
my_args = [idc, master_host, backup_host, name, port, backup_type, file, start_time, stop_time, returncode, file_size, slave_statement, std_err]
# 写入数据库
call_proc(my_args)
# 备份失败是否需要重备? 重备允许一次.
if (is_Retry == True and returncode != 0):
time.sleep(retry_sleep)
oldfile = sCmd.split('-o')[1]
pos = oldfile.rfind("/") + 1
# 获取备份全路径, 备份文件标记为重备 字样
retry_file = oldfile[:pos] + "ReBackup-" + oldfile[pos:]
retryCmd = sCmd.replace(oldfile, retry_file)
# 重备开始
mydumper(retryCmd, retry_file.strip(), name, host, port, False, isMaster)
def getDirsize(path):
# 获取备份文件夹大小
size = 0L
for root, dirs, 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;