#注意,备份机到远程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. javascript原生dom操作方法

    一.节点层次属性 考虑空白符的相关层次关系属性: 1.childNodes属性 包含 2.parentNode属性 3.previouseSibling属性和nextSibling属性 4.first ...

  2. ubuntu14.04安装eclipse

    1.安装jdk7.x,参考上篇<Linux14.04安装JDK> 2下载eclipse.tar 3.sudo tar -zxvf eclipse-x-x-.tar.gz mv eclips ...

  3. linux 缺少动态连接库.so--cannot open shared object file: No such file or directory

    error while loading shared libraries的解決方法  执行行程式時,如此遇到像下列這種錯誤: ./tests: error while loading shared l ...

  4. C++Primer 第三章

    //1.位于头文件中的代码一般不应该使用using声明.这是因为头文件的内容会拷贝到所有引用它的文件中,可能会产生始料未及的命名空间冲突. // 三种使用命名空间中的名字的方法 using names ...

  5. Climbing Stairs - Print Path

    stair climbing, print out all of possible solutions of the methods to climb a stars, you are allowed ...

  6. Lintcode: Maximum Subarray II

    Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...

  7. [转]Hibernate不能自动建表解决办法及Hibernate不同数据库的连接及SQL方言

    最近开始学Hibernate,看的是李刚的那本<轻量级java ee企业应用实战>.头一个hibernate程序,我原原本本的按照书上例子写下来,同时只是改动了些mysql的连接参数,并且 ...

  8. 如何彻底卸载MySQL

    如何彻底卸载MySQL 下面给出完整的卸载MySQL 5.1的卸载方法: 1.控制面板里的增加删除程序内进行删除 2.删除MySQL文件夹下的my.ini文件,如果备份好,可以直接将文件夹全部删除 3 ...

  9. 利用MyEclipes的反转工程来配置Hibernate各种配置

    首先需要有设计好的数据库,然后创建一个Web Project然后右键点击项目选择MyEclipse→add Hibernate Capabilities →→ →→,然后如果没有管理员的话需要在选择M ...

  10. Codeforces Round #312 (Div. 2) E. A Simple Task

    题目大意就是给一个字符串,然后多个操作,每次操作可以把每一段区间的字符进行升序或者降序排序,问最终的字符串是多少. 一开始只考虑字符串中字符'a'的情况,假设操作区间[L,R]中有x个'a',那么一次 ...