#注意,备份机到远程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. iOS 重写UITableViewCell之动态获取label文字的宽度进行布局

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. 如何修改DBSNMP和SYSMAN用户的密码

    SYSMAN和DBSNMP用户密码过期后OEM无法使用,并报以下错误: SYSMAN用户的密码被加密后存放在不同的地方,这样database control(OMS和agent)可以不用每次访问数据库 ...

  3. 不等高cell的搭建(一)

    一.界面搭建   1.确定开发模式      如果界面是固定的,可以用xib      界面的一些内容不固定,就用纯代码      cell用什么方式去开发(我们采用纯代码和xib结合的方式)   2 ...

  4. JAVA-面向对象-多态

    多态 1.方法重载 2.方法重写 3.对象转型 4.抽象(可以定义类和方法)    (关键字  abstract)   ( 如: public abstract class robot  )(不能修饰 ...

  5. 最长上升子序列(N*log(N))hdu1025

    (HDU1025) Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. DataBase: LeetCode

    Combine Two Tables # Write your MySQL query statement below Select p.FirstName, p.LastName, a.City, ...

  7. bzoj2333 [SCOI2011]棘手的操作

    用set维护每个联通块里的最值,multiset维护所有块里的最值,并查集维护连通性,然后随便搞搞就行了,合并时候采用启发式合并.复杂度O(nlognlogn),大概勉强过的程度,反正跑的很慢就是了. ...

  8. android中在代码中设置margin属性

    1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...

  9. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  10. 南阳oj27题

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上 ...