1,分批次通过远程的binlog来进行数据加载

业务新需求,线上数据库数据拉到本次,但是不允许开启slave服务,不建立直接外网的数据库账号,也不能打通数据库对外网的网络,所以我们测试环境无法通过普通的slave主从复制机制来获得实时数据。

想了一个办法,将binlog放在一个可获取的安全服务器上,然后把binlog拉到测试环境,在测试环境解析binlog加载数据。

2,远程获取binlog日志记录

写一个远程能获取binlog日志记录的脚本,一个小时拉一次binlog记录

[root@db12162bright]# more get_binlog.sh
#!/bin/bash

loadlogfile=/home/mysql/binlogs/bright/get_binlogs.log
dir=$(date +%Y%m%d%H)
echo "---- "$dir" begin-----------------------------------------------------------------------------------------">> $loadlogfile
mkdir -p /home/mysql/binlogs/bright/$dir
echo "begin to get bright binlogs ......"  >> $loadlogfile
ftp -n<<!
open 120.132.27.91 21
user downdata RakudespuH3bAk+ruy
binary
cd bright/$dir
lcd /home/mysql/binlogs/bright/$dir
prompt
mget mysql-bin*
close
bye
!

ls -l /home/mysql/binlogs/bright/$dir >> $loadlogfile

echo $(date +%Y-%m-%d-%H:%M:%S) >> $loadlogfile
echo "get binlogs end ..." >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile

3,加载远程过来的binlog

在上个step2之后,立即加载binlog,获得数据显示

#sh load_data_frombinlog.sh $dir

if [ ! -n "$1" ]; then
        echo -e "需输入日期参数如:2017060909\n"
        echo -e "需输入日期参数如:2017060909\n"  >> $loadlogfile

#        exit
fi

loadlogfile=/home/mysql/binlogs/bright/bright_load_data.log
filename=`date '+%Y%m%d_%H%M%S'`.txt
#find /home/mysql/binlogs/bright -amin -1300 -name mysql-bin.*.zip |sort -n> $filename
find /home/mysql/binlogs/bright/$dir -name mysql-bin.*.zip |sort -n> $filename
echo `date '+%Y-%m-%d %H:%M:%S'`  >> $loadlogfile
echo "begin to load data from binlog ......" >> $loadlogfile
echo $filename >> $loadlogfile
for binlogfile in `cat $filename`
do
    echo ""
    echo "begin to load binlog"  >> $loadlogfile
    echo $binlogfile
    chmod +x  $binlogfile
    dir_binlog=`/usr/bin/dirname $binlogfile`
    realbinlogfile=${binlogfile%.zip}
    rm -rf $realbinlogfile
    unzip $binlogfile -d  $dir_binlog
    echo "unzip binlog $binlogfile"  >> $loadlogfile
    echo "binlog file is: $realbinlogfile" >> $loadlogfile
    rm -rf $realbinlogfile.sql
    /usr/local/mysql3309/bin/mysqlbinlog $realbinlogfile > $realbinlogfile.sql
    echo "create sql file $realbinlogfile.sql from $realbinlogfile" >> $loadlogfile
    echo "begin to load data" >> $loadlogfile
    #mysqlruninfo=`/usr/local/mysql3309/bin/mysql -uroot --password="bright_yueworld" -S /usr/local/mysql3309/mysql.sock < $realbinlogfile.sql`
    #$mysqlruninfo >> $loadlogfile
    /usr/local/mysql3309/bin/mysql -uroot --password="bright_yueworld" -S /usr/local/mysql3309/mysql.sock < $realbinlogfile.sql
    #echo $mysqlruninfo >> $loadlogfile
    echo "load $realbinlogfile data end" >> $loadlogfile
    echo "" >> $loadlogfile
    echo "" >> $loadlogfile
done
echo "---- "$dir" end----------------------------------------------------------------------------------------------" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
[root@db12162bright]# 
05-24 09:47