mysql binlog server_使用mysqlbinlog server远程备份binlog的脚本

#!/bin/bash

[ -e /etc/profile ] && source /etc/profile || exit 0

#本地binlog路径

local_binlog_dir=/data/3306/247binlog

[ ! -d "$local_binlog_dir" ] && mkdir -p "$local_binlog_dir"

cd "$local_binlog_dir"

#远程服务器ssh端口

ssh_port=22

#远程服务器ip

remote_host=192.168.0.68

#本地binlog文件名

local_logfile=`ls -al "$local_binlog_dir" | grep 'mysql-bin\.[0-9]\+' |tail -n 1 | awk '{print $NF}'`

#远程服务器binlog路径

remote_binlog_dir=/data/mysql3306/

#远程服务器第一个binlog文件名

first_remote_lofile=`ssh -p ${ssh_port} -o StrictHostKeyChecking=no ${remote_host} " cat \${remote_binlog_dir}/mysql-bin.index | head -n 1 | awk -F'/' '{print \\$NF}'"`

last_remote_logfile=`ssh -p ${ssh_port} -o StrictHostKeyChecking=no ${remote_host} " cat \${remote_binlog_dir}/mysql-bin.index | tail -n 1 | awk -F'/' '{print \\$NF}'"`

#远程mysql用户

remote_user=root

#远程mysql用户密码

remote_password=xx

function start() {

running=`ps uax | grep 'mysqlbinlog -R --raw' | grep -v grep|grep raw | awk '{print $2}'`

if [ "$running" != "" ];then

echo "mysqlbinlog server is running"

exit

fi

if [ "$local_logfile" == "" ];then

#echo "the binlogserver is first start "

mysqlbinlog -R --raw --host=$remote_host --user="$remote_user" --password="$remote_password" --stop-never  $first_remote_lofile &

else

if ! ssh -p ${ssh_port} -o StrictHostKeyChecking=no ${remote_host} "ls -lh ${remote_binlog_dir}/${local_logfile}" &> /dev/null;then

local_logfile_num=`ll /data/3306/247binlog/ |tail -1 |awk '{print $NF}' |grep -o '\([1-9]\)\+\([0-9]\)\+'`

binlogs=(`ssh -p ${ssh_port} -o StrictHostKeyChecking=no ${remote_host} "ls -lh ${remote_binlog_dir}/mysql-bin.* |grep -v index |awk -F'/' '{print \\$NF}' |wc -l"`)

for binlog in `seq 1 $binlogs`

do

local_logfile_num=`expr $local_logfile_num + 1`

if [ "$local_logfile_num" -lt 10 ];then

local_logfile=mysql-bin.00000${local_logfile_num}

elif [ "$local_logfile_num" -lt 100 ];then

local_logfile=mysql-bin.0000${local_logfile_num}

elif [ "$local_logfile_num" -lt 1000 ];then

local_logfile=mysql-bin.000${local_logfile_num}

elif [ "$local_logfile_num" -lt 10000 ];then

local_logfile=mysql-bin.00${local_logfile_num}

elif [ "$local_logfile_num" -lt 100000 ];then

local_logfile=mysql-bin.0${local_logfile_num}

else

local_logfile=mysql-bin.${local_logfile_num}

fi

if ssh -p ${ssh_port} -o StrictHostKeyChecking=no ${remote_host} "ls -lh ${remote_binlog_dir}/${local_logfile}" &> /dev/null;then

break

fi

done

mysqlbinlog -R --raw --host=$remote_host --user="$remote_user" --password="$remote_password" --stop-never  $local_logfile &

else

mysqlbinlog -R --raw --host=$remote_host --user="$remote_user" --password="$remote_password" --stop-never  $local_logfile &

fi

fi

}

function stop() {

ps uax | grep mysqlbinlog | grep raw | awk '{print $2}' | xargs kill

}

case $1 in

start)

start

;;

stop)

stop

;;

*)

# usage

basename=`basename "$0"`

echo "Usage: $basename  {start|stop}  [ MySQL BinlogServer options ]"

exit 1

;;

esac

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值