您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关linux中怎么增量增量传输mysql binlog日志,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
在实际的工作中我们遇到了一个问题,MYSQL 5.5 我们备份使用的mysqldump,然后每天传输的备份到备份机器,但是binlog却不好传输主要要考虑几个问题:
1、binlog 没法取时间,按照什么规则来传?
2、binlog的传输并不是每次都全部传输过去,而是需要增量的。
3、binlog的最后一个日志是在使用的是不能传输的。
4、为了更通用需要使用一个前缀,因为每个数据库的binlog前缀是可以自己指定的
如 mysqlbin.000001
那么有了上面的问题,如何解决?也许shell脚本能够完成,但是我选择使用C/C++ 和LINUX系统函数 opendir readdir lstat来完成
这是我最后挂载的脚本
#!/bin/sh
TIME_TAG=`date +%Y%m%d`
/root/ftptool/tool /dbdata/data/ /root/ftptool/ mysql-bin > tool_${TIME_TAG}.log
if [ $? != 0 ]; then
echo "tool failed"
exit
else
echo "tool sucess!"
fi
cat /root/ftptool/ftphead > /root/ftptool/ftp_${TIME_TAG}.sh
cat /root/ftptool/${TIME_TAG} >> /root/ftptool/ftp_${TIME_TAG}.sh
cat /root/ftptool/ftptail >> /root/ftptool/ftp_${TIME_TAG}.sh
chmod 755 /root/ftptool/ftp_${TIME_TAG}.sh
sh /root/ftptool/ftp_${TIME_TAG}.sh
实际上ftp_${TIME_TAG}.sh来自3个部分
1、ftphead --这个很简单自己写明白ip 用户名密码就好了,如下:
#!/bin/sh
ftp -i -v -n<< EOF
open 192.168.*.*
user mysql ****
cd /backup/mysql
bin
2、 ftptail 这个更简单
quit
EOF
就这点
3、cat ${TIME_TAG}
这部分是我自己写的工具tool 生成的
工具使用如下:
Usage:./tool binlogdir ftpdir binlogprefix
需要一个binlog目录 一个ftp目录和binlong的前缀
binlog目录不用多说是binlog的所在目录,需要以/结尾比如 /mysql/data/不能是/mysql/data 否则抛错error 20 binlogdir Must End With /
ftp目录下存放两个东西一个tmp文件和生成的ftp脚本,ftp脚本生成和当前时间一致比如20160713
tmp文件用于存放当前取到的最大的binlog序号,如果不存在可以新建一个,如果不新建抛错error 15 tmp file not found
如果想再次全部传输删除tmp文件新建一个空的就可以比如touch tmp
这个ftp目录同样必须使用/结尾 否则抛错error 21 ftpdir Must End With /
binlogprefix 就是你的前缀比如mysql-bin
使用就是这么简单,有了这3部分就能够通过shell
cat ftphead > ftp_${TIME_TAG}.sh
cat ${TIME_TAG} >> ftp_${TIME_TAG}.sh
cat ftptail >> ftp_${TIME_TAG}.sh
生成一个全部的脚本。然后运行了。
另外tool工具会输出一些信息,当然可以关闭,这些输出信息我重定向到了 tool_${TIME_TAG}.log
可以查看下面就是一份:
WARNING:Ftpdir Less Than 1000 Char!
PROCESS:Create Chain Now:
INFO:Current Binog Dir Is :/dbdata/
PROCESS:Checkbin Binlogprefix: mysql-bin
................. 这里省略很多
PROCESS:Print chain info
................. 这里省略很多
PROCESS:Now Acc Ftpdir:
INFO:Ftp_scirpt fun time:
Wed Jul 13 14:44:13 2016
20160713
INFO:Ftp_scirpt Fun Tmpfile :/root/ftptool/tmp
INFO:Tmpfile Write Mode Succ!
INFO:Ftp_Scirpt Fun Tmp_max_st:0 Max_st_1:2028:
INFO:Scan Put Counter :163
INFO:Current Max Binlog-1 is:2028
PROCESS:Free Node Is:164
可以清楚看到将会生成163个put脚本信息,最高的传输是2028,实际最大是2029,我在编码的时候做了-1操作,目的在于不要传输当前的binlog,
因为当前的binlog正在使用
最后释放了链表节点164个.同事你的tmp文件目录也会输出,binglog 的目录也会输出.
下面是全部分出错码:
error 10 open bindbr error
error 11 argv parameter error
eroor 12 not find binlog
error 14 fptdir open error
error 15 tmp file not found
error 16 lstat tmp file error
error 18 tmp file write error
error 20 binlogdir Must End With /
error 21 ftpdir Must End With /
当然成功放回 0
编译好的我放到了百度云盘
http://pan.baidu.com/s/1i4WuBB3
下面是全部的源代码
编译使用 g++ main.cpp mybin.cpp -o tool 即可 注意本工具使用了LINUX系统函数所以必须是LINUX系统或者其他满足posix标准的系统不包含windows
在LINUX下编译没有问题。有什么问题可以联系QQ 22389860.
点击(此处)折叠或打开
/*************************************************************************
> File Name: mbin.h
> Author: gaopeng
> Mail: gaopp_200217@163.com
> Created Time: Tue 02 Aug 2016 05:57:27 PM CST
************************************************************************/
#include<iostream>
#define _DEG
using namespace std;
typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef struct data
{
char* file_name;
char* path_name;
char* full_name;
ulong file_st;
uint nuse;
} DATA;
typedef struct f_node
{
struct f_node *next;
DATA* data;
} F_NODE;
typedef struct quence
{
F_NODE* first;
F_NODE* last;
int items;
} QUE;
uint checkbin(const char* file_name,const char* binprefix);
F_NODE* create_que(const char *bindir,const char* binprefix);
QUE* ini_que(void);
void show( F_NODE* const frs_nd);
void ins_chan(const char* f_name,const char* p_name,const char* ful_name,QUE * que);
ulong char_to_num(const char* file_name);
void time_to_time(char* ltime,char* mytime);
void ftp_script(const char* ftp_dir,F_NODE* frs_nd);
int get_max_st(F_NODE* const frs_nd);
ulong readtmp(FILE *fd,const char* tmpdir);
int myfree(F_NODE* const frs_nd);
uint checkdir(const char* mychar);
点击(此处)折叠或打开
/*************************************************************************
> File Name: main.cpp
> Author: gaopeng
> Mail: gaopp_200217@163.com
> Created Time: Tue 02 Aug 2016 05:58:15 PM CST
************************************************************************/
#include<iostream>
#include<iostream>
#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include"mbin.h"
#include <stdlib.h>
using namespace std;
int main(int argc,char *argv[])
{
F_NODE* frs_nd=0;
int freeno =0;
printf("%s\n","Usage:./tool binlogdir ftpdir binlogprefix");
if(argc<4)
{
cout<<"ERROR(9):Parameter Error Like:"<<endl;
printf("%s\n","Usage:./tool binlogdir ftpdir binlogprefix");
exit(9);
}
if(checkdir(argv[1]) == 1)
{
cout<<"ERROR(20):binlogdir Must End With /"<<endl;
exit(20);
}
if(checkdir(argv[2]) == 1)
{
cout<<"ERROR(21):ftpdir Must End With /"<<endl;
exit(21);
}
printf("WARNING:Ftpdir Less Than 1000 Char!\n");
printf("PROCESS:Create Chain Now:\n");
frs_nd = create_que(argv[1],argv[3]);
cout<<"PROCESS:Print chain info"<<endl;
show(frs_nd);
cout<<"PROCESS:Now Acc Ftpdir:"<<endl;
ftp_script(argv[2], frs_nd);
freeno = myfree(frs_nd);
cout<<"PROCESS:Free Node Is:"<<freeno<<endl;
return 0;
}
点击(此处)折叠或打开
/*************************************************************************
> File Name: mybin.cpp
> Author: gaopeng
> Mail: gaopp_200217@163.com
> Created Time: Tue 02 Aug 2016 05:57:34 PM CST
************************************************************************/
#include<iostream>
#include "mbin.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <math.h>
using namespace std;
QUE* ini_que(void)
{
QUE* que = new QUE;
que->first =0;
que->last =0;
que->items =0;
return que;
}
void ins_chan(const char* f_name,const char* p_name,const char* ful_name,QUE * que)
{
F_NODE *add = new F_NODE; //inital node pointer
DATA *name = new DATA; //inital data pointer
name->file_name = new char[strlen(f_name)+5];
name->path_name = new char[strlen(p_name)+5];
name->full_name = new char[strlen(ful_name)+5];
//init mem
memset(name->file_name,0,strlen(f_name)+5);
memset(name->path_name,0,strlen(p_name)+5);
memset(name->full_name,0,strlen(ful_name)+5);
name->file_st = char_to_num(f_name);
name->nuse = 0;
strcpy(name->file_name,f_name);
strcpy(name->path_name,p_name);
strcpy(name->full_name,ful_name);
add->next =0;
add->data = name;
if( que->items==0)
{
que->first =add;
que->last =add;
que->items++;
}
else
{
que->last->next = add;
que->last = add;
que->items++;
}
}
F_NODE* create_que(const char *bindir,const char* binprefix)
{
struct dirent *DirEntry=0;
DIR* Dir=0;
QUE* myq=0;
uint nob=0; //check if binlog find
myq = ini_que();
F_NODE* frs_nd =0;
if( !(Dir = opendir(bindir)))
{
perror("ERROR(10):Open Binlog Dir Error:");
exit(10);
}
else
{
printf("INFO:Current Binog Dir Is :%s\n",bindir);
while( DirEntry = readdir(Dir))
{
if(DirEntry->d_type == DT_REG && checkbin(DirEntry->d_name,binprefix) == 1)
{
nob++;
#ifdef _DEG
printf("%s%s\n",bindir,DirEntry->d_name);
#endif
char *tloop = new char[strlen(bindir) + strlen(DirEntry->d_name)+5];
strcat(strcat(tloop,bindir),DirEntry->d_name); //bindir like /test/test/test/ last / is must
ins_chan(DirEntry->d_name,bindir,tloop,myq);
delete [] tloop;
}
}
closedir(Dir);
if(nob == 0)
{
printf("WARNING:No Bin Log Find!!\n");
delete myq;
exit(12);
}
}
frs_nd = myq->first;
delete myq;
return frs_nd;
}
uint checkbin(const char* file_name,const char* binprefix)
{
uint lenprefix = strlen(binprefix); //exp 5
uint filelen = strlen(file_name);
uint i;
uint t;
char tmpfix[strlen(binprefix)+1]; //exp strlen(binprefix)+1 =6
memset(tmpfix,0,strlen(binprefix)+1);
for(i=0;i<=lenprefix-1;i++) //exp lenprefix-1=4
{
tmpfix[i]=file_name[i]; //5 char copy tmpfix[0-4]
}
tmpfix[lenprefix]='\0'; //tmpfix[5] = '\0'
#ifdef _DEG
cout<<"PROCESS:Checkbin Binlogprefix: "<<tmpfix<<endl;
#endif
if(!strcmp(tmpfix,binprefix)) //check prefix eq tmpfix
{
for(t=(filelen-1);t>=(filelen-6);t--) //exp filelen-1=10 filelen-6=5
{
if(file_name[t]<48 || file_name[t]>57 ) //check 000000
{
return 0;
}
}
if(file_name[filelen-7] !='.') //check . of .000000
{
return 0;
}
}
else
{
return 0;
}
return 1;
}
void show( F_NODE* const frs_nd)
{
F_NODE* scan_nd = frs_nd;
do {
if(!scan_nd->next)
{
break;
}
printf("INFO:%s,%s,%s,%lu,%u\n",scan_nd->data->file_name,scan_nd->data->path_name,scan_nd->data->full_name,scan_nd->data->file_st,scan_nd->data->nuse);
scan_nd = scan_nd->next;
} while(scan_nd->next);
printf("INFO:%s,%s,%s,%lu,%u\n",scan_nd->data->file_name,scan_nd->data->path_name,scan_nd->data->full_name,scan_nd->data->file_st,scan_nd->data->nuse);
}
ulong char_to_num(const char* file_name)
{
uint len = strlen(file_name);
uint i;
ulong num=0;
uint chk=0;
for(i=(len-1);i>0;i--)
{
if(file_name[i]>=48 && file_name[i]<=57)
{
num = (ulong)(file_name[i] - 48)*(ulong)pow(10,chk)+num;
chk++;
}
}
return num;
}
void time_to_time(char* ltime,char* mytime)
{
char buffer[10];
buffer[0] = ltime[4];
buffer[1] = ltime[5];
buffer[2] = ltime[6];
buffer[3] = '\0';
mytime[0]=ltime[20];
mytime[1]=ltime[21];
mytime[2]=ltime[22];
mytime[3]=ltime[23];
if(!strcmp(buffer,"Jan"))
{
mytime[4]='0';
mytime[5]='1';
}
if(!strcmp(buffer,"Feb"))
{
mytime[4]='0';
mytime[5]='2';
}
if(!strcmp(buffer,"Mar"))
{
mytime[4]='0';
mytime[5]='3';
}
if(!strcmp(buffer,"Apr"))
{
mytime[4]='0';
mytime[5]='4';
}
if(!strcmp(buffer,"May"))
{
mytime[4]='0';
mytime[5]='5';
}
if(!strcmp(buffer,"Jun"))
{
mytime[4]='0';
mytime[5]='6';
}
if(!strcmp(buffer,"Jul"))
{
mytime[4]='0';
mytime[5]='7';
}
if(!strcmp(buffer,"Aug"))
{
mytime[4]='0';
mytime[5]='8';
}
if(!strcmp(buffer,"Sep"))
{
mytime[4]='0';
mytime[5]='9';
}
if(!strcmp(buffer,"Oct"))
{
mytime[4]='1';
mytime[5]='0';
}
if(!strcmp(buffer,"Nov"))
{
mytime[4]='1';
mytime[5]='1';
}
if(!strcmp(buffer,"Dec"))
{
mytime[4]='1';
mytime[5]='2';
}
if(ltime[8]<48 ||ltime[8]>57)
{
mytime[6]=48;
}
else
{
mytime[6]=ltime[8];
}
mytime[7]=ltime[9];
mytime[8]='\0';
}
void ftp_script(const char* ftp_dir,F_NODE* frs_nd)
{
time_t now;
F_NODE* scan_nd = frs_nd;
struct tm* nowtm;
FILE* ftp=0;
FILE* tmp=0;
char timebuf[40];
char mytime[25];
char myftpdir[1000];
char tmpdir[1000];
ulong tmp_max_st;
ulong max_st_1;
int i=0;
int freeno=0;
memset(timebuf,0,40);
memset(mytime,0,40);
time(&now);
nowtm = localtime(&now);
asctime_r(nowtm,timebuf);
time_to_time(timebuf,mytime);
#ifdef _DEG
printf("INFO:Ftp_scirpt fun time:\n%s%s\n",timebuf,mytime);
#endif
memset(myftpdir,0,1000);
if((ftp=fopen(strcat(strcat(myftpdir,ftp_dir),mytime),"w"))==NULL)
{
perror("ERROR(14):Ftpdir open error:");
freeno=myfree(frs_nd);
cout<<"PROCESS:Free Mem:Free Node Is:"<<freeno<<endl;
exit(14);
}
memset(tmpdir,0,1000);
if((tmp=fopen(strcat(strcat(tmpdir,ftp_dir),"tmp"),"r"))==NULL)
{
printf("ERROR(15):No Tmp File Found,Please Create it frist!\n");
freeno=myfree(frs_nd);
cout<<"PROCESS:Free Mem:Free Node Is:"<<freeno<<endl;
exit(15);
}
else
{
#ifdef _DEG
printf("INFO:Ftp_scirpt Fun Tmpfile :%s\n",tmpdir);
#endif
tmp_max_st=readtmp(tmp,tmpdir);
}
memset(tmpdir,0,1000);
if((tmp=fopen(strcat(strcat(tmpdir,ftp_dir),"tmp"),"w"))==NULL)
{
printf("ERROR:Have No Write Privi In %s?\n",tmpdir);
perror("ERROR(18):Tmpfile Open W Error:");
freeno=myfree(frs_nd);
cout<<"PROCESS:Free Mem:Free Node Is:"<<freeno<<endl;
exit(18);
}
else
{
printf("INFO:Tmpfile Write Mode Succ!\n");
}
max_st_1 = get_max_st(frs_nd) - 1;
printf("INFO:Ftp_Scirpt Fun Tmp_max_st:%lu Max_st_1:%lu:\n",tmp_max_st,max_st_1);
do{
if(!scan_nd->next)
{
break;
}
if(scan_nd->data->file_st > tmp_max_st && scan_nd->data->file_st <= max_st_1)
{
i++;
fputs("put ",ftp);
fputs(scan_nd->data->full_name,ftp);
fputs(" ",ftp);
fputs(scan_nd->data->file_name,ftp);
fputs("\n",ftp);
}
scan_nd = scan_nd->next;
}while(scan_nd->next);
if(scan_nd->data->file_st > tmp_max_st && scan_nd->data->file_st <= max_st_1)
{
i++;
fputs("put ",ftp);
fputs(scan_nd->data->full_name,ftp);
fputs(" ",ftp);
fputs(scan_nd->data->file_name,ftp);
fputs("\n",ftp);
}
printf("INFO:Scan Put Counter :%d\n",i);
if(i == 0)
{
printf("INFO:Read Max Binlog From Temp is :%lu\n",tmp_max_st);
fprintf(tmp,"%lu",tmp_max_st);
fputs("\n",tmp);
}
else
{
printf("INFO:Current Max Binlog-1 is:%lu\n",max_st_1);
fprintf(tmp,"%lu",max_st_1);
fputs("\n",tmp);
}
fclose(ftp);
fclose(tmp);
}
int get_max_st(F_NODE* const frs_nd)
{
F_NODE* scan_nd = frs_nd;
ulong max_st=0;
do {
if(!scan_nd->next)
{
break;
}
if(scan_nd->data->file_st > max_st)
{
max_st = scan_nd->data->file_st;
}
scan_nd = scan_nd->next;
}while(scan_nd->next);
if(scan_nd->data->file_st > max_st)
{
max_st = scan_nd->data->file_st;
}
return max_st;
}
ulong readtmp(FILE *fd,const char* tmpdir)
{
struct stat buf;
ulong max_st = 0;
if(lstat(tmpdir, &buf) < 0)
{
perror("ERROR(16):Lstat Error:");
exit(16);
}
if(buf.st_size == 0)
{
max_st = 0;
}
else
{
fscanf(fd,"%lu",&max_st);
/*
if(max_st == 0)
{
printf("Tmp File Is Bad ?\n");
exit(17);
}
*/
}
return max_st;
}
int myfree(F_NODE* const frs_nd)
{
F_NODE* scan_nd = frs_nd;
F_NODE* cur_nd = 0;
int i = 0;
do{
cur_nd = scan_nd;
if(!scan_nd->next)
{
free(cur_nd->data->file_name);
free(cur_nd->data->path_name);
free(cur_nd->data->full_name);
free(cur_nd->data);
free(cur_nd);
i++;
return i;
}
scan_nd = scan_nd->next;
free(cur_nd->data->file_name);
free(cur_nd->data->path_name);
free(cur_nd->data->full_name);
free(cur_nd->data);
free(cur_nd);
i++;
}while(scan_nd->next);
free(scan_nd->data->file_name);
free(scan_nd->data->path_name);
free(scan_nd->data->full_name);
free(scan_nd->data);
free(scan_nd);
i++;
return i;
}
uint checkdir(const char* mychar)
{
uint len = strlen(mychar);
if(mychar[len-1] == '/' )
{
return 0;
}
else
{
return 1;
}
}
关于linux中怎么增量增量传输mysql binlog日志就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。