#注意,备份机到远程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. Lua xavante WEB server实现xmlrpc服务器端

    xavante xavante是一个使用lua实现的遵守http1.1的web server,支持wsapi. 依赖库: xavante核心 -- lua, copas(纯lua编写,网络连接coro ...

  2. PostgreSQL Cascade Replication

    PostgreSQL Cascade Replication node1:master:10.2.208.10:repclia(user) node2:upstreamnode:10.2.208.11 ...

  3. Leetcode: Alien Dictionary && Summary: Topological Sort

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  4. weiphp---------图灵机器人存在的bug。

    1.很多人下载下来weiphp源码以后,配置好了图灵机器人却不能使用.原因是因为他源码里面存在一个小bug 上图红色框框内是他的源码,问题就出在这里. 修改方法: if($result ['code' ...

  5. RESTful架构详解(转)

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...

  6. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  7. C++之路进阶——codevs1036(商务旅行)

    1036 商务旅行 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇 ...

  8. linux第5天 socket api

    IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中 通用地址结构用来指定与套接字关联的地址.以socka ...

  9. HDU 3681 Prison Break(BFS+二分+状态压缩DP)

    Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...

  10. 1021: A除以B

    1021: A除以B 时间限制: 1 Sec  内存限制: 128 MB提交: 263  解决: 189[提交][状态][讨论版] 题目描述 本题要求计算A/B,其中A是不超过1000位的正整数,B是 ...