#注意,备份机到远程mysql服务器需要免密钥登录,此脚本放到计划任务中每五分钟执行一次,避免mysqlbinlog server进程长时间挂掉无人知晓
 
cat backup_binlog.sh
 
#!/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
 
 
#使用
sh backup_binlog.sh start  #开启binlog server开始备份,会启动一个mysqlbinlog server守护进程一直监听远程服务器的binlog,有变动会同步到备份机
 
sh backup_binlog.sh stop #关闭binlog server守护进程,停止备份远程服务器binlog
 
 

使用mysqlbinlog server远程备份binlog的脚本的更多相关文章

  1. sql server远程备份和恢复

    sql server远程备份和恢复 SQLSERVER服务实例名称:192.168.0.2需要备份的数据库名称: a备份机器名称(Client端):192.168.0.3备份机用户:zf 密码:123 ...

  2. 远程备份binlog服务

    Ⅰ.bonlog server介绍 对于binlog的备份,之前文章里说的是有从机,一般不备份,那现在人家就是 要备份嘛,怎么办嘛, 写个脚本每天夜里去把前一天产生的binlog拷贝出来可以不? 行啊 ...

  3. SQL Server 远程备份详解

    例1: 有A数据库服务器,B本机: 我现在想通过在B机器上通过代码调用SQL来执行A数据库的备份到B机器上 调用的SQL语句为:Backup Database MYDATABASE To Disk=' ...

  4. SQL Server 远程备份数据库

    在一些涉及到数据库的项目中,经常会有定时备份数据库的需要.定时备份到本机的话,还是挺容易的,计划任务就可以完成,但如果是整机挂了,那备份到本机是没意义的,那么就需要来考虑备份到局域网中,其它电脑里. ...

  5. sql server 远程备份 bak 删除

    前言: 管理一个公司的一个服务器,最近有一些维护SQLserver数据库活弄,写下防止忘了. 因为公司采用SQL\Redis\MongoDB共用,SQL用来存储基础的结构\权限\等一些杂七杂八的东西. ...

  6. PC Server远程管理卡用户管理脚本实现

    1.IPMI工作原理图: 2.脚本实现流程图: 3.适配服务器机型: 4.演示效果: 5.实现代码: #!/usr/bin/env bash # Author : JACK ZHAO # Date : ...

  7. SQL SERVER 远程备份DB

    --检查sqlserver所在服务的运行账号是否有权限访问共享文件夹,没有的话右键添加写权限 --开启权限sp_configure 'show advanced options', 1;gorecon ...

  8. SQL Server存储过程备份数据库的脚本-干货

    拿AAAAAAAA数据库为例子,除了汉字标注的替换真实的数据,其他都不动        CREATE proc [dbo].[p_log_backupAAAAAAAA]    @dbname sysn ...

  9. [SQLServer]NetCore中将SQLServer数据库备份为Sql脚本

    NetCore中将SQLServer数据库备份为Sql脚本 描述: 最近写项目收到了一个需求, 就是将SQL Server数据库备份为Sql脚本, 如果是My Sql之类的还好说, 但是在网上搜了一大 ...

随机推荐

  1. 网站购物,使用pickle

    主程序, #!/usr/bin/env python #_*_ coding:utf- _*_ import pickle import os import sys import time impor ...

  2. 用仿ActionScript的语法来编写html5——第七篇,自定义按钮

    第七篇,自定义按钮这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ ...

  3. iOS - (调用系统本机发送短信功能)

    今天处理一个订单详情的界面,在订单详情页面上有联系方式(电话号码),在电话号码的右边有两图片,一个是电话样式的,一个是短信样式的,做为一名程序员(号称攻城师)一眼看下去就应该是一个 button,而不 ...

  4. 使用dbms_system追踪其它session

    dbms_system是内部包,建议在官方指导下使用该包. SQL> desc dbms_system PROCEDURE ADD_PARAMETER_VALUE Argument Name T ...

  5. Make Rules

    target: components ls TAB rule main:main.o mytool1.o mytool2.o gcc -o main main.o mytool1.o mytool2. ...

  6. 定制对ArrayList的sort方法的自定义排序

    java中的ArrayList需要通过collections类的sort方法来进行排序 如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法 调用sort方法时将Arr ...

  7. java collections读书笔记(8)collection框架总览(1)

  8. NABCD模式

    各位用户:       我们的“昵妆”是为了帮助不会化妆的用户解决困难, 他们需要有适合他们的优质的化妆品和 正确的视频或者化妆师来指导他们,但是现有的方案并没有很好地解决这些需求,我们有独特的办法, ...

  9. PostgreSQL单机、同步复制、异步复制性能测试对比

    测试环境: •测试机:PC •内存:8GB •CPU:Intel(R) Core(TM) i5-3450 3.10GHz •硬盘:HDD •数据量:20GB •测试工具:pgbench •Postgr ...

  10. 使用visual studio 2012 编译opencv2.4.9

    最近,由于需要从opencv源码部分对opencv中的某个函数进行修改,以提升算法的速度,因此一直在尝试使用vs2012来编译opencv.期间不乏多次的失败.今天通过实验发现了自己编译的opencv ...