diff --git a/scripts/archive-binlog.sh b/scripts/archive-binlog.sh index 29bbe98..b9e640a 100755 --- a/scripts/archive-binlog.sh +++ b/scripts/archive-binlog.sh @@ -24,18 +24,21 @@ require_archive_config() { backup_require_env BACKUP_PASSPHRASE >/dev/null } -closed_binlogs() { - local mysql_cid="$1" - docker exec "${mysql_cid}" sh -c \ - 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --protocol=socket -N -B -e "SHOW BINARY LOGS;"' \ - 2>/dev/null | awk 'NR > 1 { print previous } { previous = $1 } END { }' +mysql_binlog_query() { + local mysql_cid="$1" query="$2" output code + output="$(docker exec "${mysql_cid}" sh -c \ + 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --protocol=socket -N -B -e "$1"' \ + sh "${query}" 2>&1)" || { + code=$? + backup_error "MySQL binlog 查询失败(${query}):${output}" + return "${code}" + } + printf '%s\n' "${output}" } master_status() { local mysql_cid="$1" - docker exec "${mysql_cid}" sh -c \ - 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --protocol=socket -N -B -e "SHOW MASTER STATUS;"' \ - 2>/dev/null | awk 'NR == 1 { print $1 "\t" $2 }' + mysql_binlog_query "${mysql_cid}" 'SHOW MASTER STATUS;' | awk 'NR == 1 { print $1 "\t" $2 }' } state_active_binlog() { @@ -127,7 +130,7 @@ archive() { backup_require_cmd openssl require_archive_config local force="${1:-}" backup_dir oss_uri mysql_cid server_uuid state_dir first_binlog last_index first_index expected_index filename index - local current_status current_file current_pos previous_file previous_pos final_status final_file final_pos + local current_status current_file current_pos previous_file previous_pos final_status final_file final_pos binlog_list closed_binlog_list if [[ -n "${force}" && "${force}" != '--force' ]]; then printf '用法: %s archive [--force]\n' "$0" >&2 exit 1 @@ -156,8 +159,8 @@ archive() { else backup_log "活动 binlog 无新增已提交事务,跳过轮转" fi - first_binlog="$(docker exec "${mysql_cid}" sh -c \ - 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --protocol=socket -N -B -e "SHOW BINARY LOGS;"' | head -1 | awk '{print $1}')" + binlog_list="$(mysql_binlog_query "${mysql_cid}" 'SHOW BINARY LOGS;')" + first_binlog="$(awk 'NR == 1 { print $1; exit }' <<< "${binlog_list}")" [[ -n "${first_binlog}" ]] || { backup_error '无法读取 binlog 列表'; exit 1; } last_index="$(state_last_index "${state_dir}" || true)" first_index="$(backup_binlog_index "${first_binlog}")" @@ -166,6 +169,7 @@ archive() { exit 1 fi + closed_binlog_list="$(awk 'NR > 1 { print previous } { previous = $1 }' <<< "${binlog_list}")" expected_index="${last_index}" while IFS= read -r filename; do [[ -n "${filename}" ]] || continue @@ -179,7 +183,7 @@ archive() { fi archive_one "${mysql_cid}" "${state_dir}" "${server_uuid}" "${oss_uri}" "${filename}" expected_index="${index}" - done < <(closed_binlogs "${mysql_cid}") + done <<< "${closed_binlog_list}" final_status="$(master_status "${mysql_cid}")" IFS=$'\t' read -r final_file final_pos <<< "${final_status}" [[ -n "${final_file}" && "${final_pos}" =~ ^[0-9]+$ ]] || { backup_error '无法读取归档后的 binlog 位点'; exit 1; }