AWS RDS binlog 读取

下载 aws rds binlog 并转换成 sql 的脚本

# download_and_convert_binlog.sh

#!/bin/bash

logFile=$1

mysqlbinlog \
    --read-from-remote-server \
    --host=xxx.ap-east-1.rds.amazonaws.com \
    --port=3306  \
    --user admin \
    --password \
    --raw \
    --result-file=/data1/ \
    $logFile

function getBinlogStartTime() {
    theFile="$1"
    #取出文件头做分析
    binlogHead=`hexdump ${theFile} | head -1`

    #binlog文件校验
    binlogCrc=`echo $binlogHead | awk '{print $1$2$3}'`
    if [ "${binlogCrc}" != '000000062fe6e69' ]; then
        echo '${theFile} is damage.'
        exit 1
    fi

    #计算binlog的开始时间
    binlogBeginTimeInt=`echo $binlogHead | awk '{print $5$4}' | gawk ' { printf strtonum("0x" $0)}'`

    binlogBeginTime=`date -d "1970-01-01 UTC $binlogBeginTimeInt seconds" "+%F %T"`
    echo $binlogBeginTime
}

getBinlogStartTime $logFile

# binlog 转换成 sql
mysqlbinlog $logFile -r $logFile.sql

 

脚本执行

bash download_and_convert_binlog.sh mysql-bin-changelog.057167

 

查看 binlog 文件名

SHOW BINARY LOGS;

 

posted @ 2022-12-14 13:27  运维工作栈  阅读(210)  评论(0编辑  收藏  举报