#保留最后一个备份中的binlog,以及其后面的binlog,这部分binlog会在还原的时候用到
if (int(binlog.split(".")[-1]) >= int(backup_position_binlog_file.split(".")[-1])):
binlog_files_for_restore.append(binlog)
binlog_file_count = 0
#第一个文件,从上最后一个差异备份的position位置开始,最后一个文件,需要stop_at到指定的时间
for binlog in binlog_files_for_restore:
if not os.path.isdir(binlog):
#binlog物理文件的最后修改时间
binlog_file_updatetime = datetime.datetime.strptime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(binlog_path+"/"+binlog).st_mtime)),'%Y-%m-%d %H:%M:%S')
#判断binlog的生成时间,是否大于stop_at,对于修改时间大于stop_at的日志,需要全部还原,不需要stop_at指定截止点
if stop_at_time > binlog_file_updatetime :
if (binlog_file_count < 1):
if (len(binlog_files_for_restore) == 1):
# 找到差异备份之后的第一个binlog,需要根据差异备份的position,来过来第一个binlog文件
restore_commond = '''mysqlbinlog {0} --skip-gtids=true --start-position={1} --stop-datetime="{2}" | mysql mysql -h{3} -u{4} -p{5} -P{6}''' \
.format(binlog, backup_position, stop_at, host, user, password, port)
print(restore_commond)
binlog_file_count = binlog_file_count + 1
else:
# 找到差异备份之后的第一个binlog,需要根据差异备份的position,来过来第一个binlog文件
restore_commond = '''mysqlbinlog {0} --skip-gtids=true --start-position={1} | mysql mysql -h{2} -u{3} -p{4} -P{5}''' \
.format(binlog, backup_position, host, user, password, port)
print(restore_commond)
binlog_file_count = binlog_file_count + 1
else:
# 从第二个文件开始,binlog需要全部还原
restore_commond = '''mysqlbinlog {0} --skip-gtids=true | mysql mysql -h{1} -u{2} -p{3} -P{4}''' \
.format(binlog, host, user, password, port)
print(restore_commond)
binlog_file_count = binlog_file_count + 1
else:
if (binlog_file_count < 1):
restore_commond = '''mysqlbinlog {0} --skip-gtids=true --start-position={1} --stop-datetime={2} | mysql -h{3} -u{4} -p{5} -P{6}'''.format(binlog, backup_position,stop_at,host,user,password,port)
print(restore_commond)
binlog_file_count = binlog_file_count + 1
else:
if (binlog_file_count >= 1):
restore_commond = '''mysqlbinlog {0} --skip-gtids=true --stop-datetime="{1}" | mysql -h{2} -u{3} -p{4} -P{5}'''.format(binlog, stop_at,host,user,password,port)
print(restore_commond)
binlog_file_count = binlog_file_count + 1
break
def apply_log_for_backup():
list_restore_backup = get_restorefile_list()
start_flag = 1
full_backup_path = None