首页 > 代码库 > 利用XtraBackup给MYSQL热备(基于数据文件)

利用XtraBackup给MYSQL热备(基于数据文件)

利用XtraBackup给MYSQL热备(基于数据文件)

By JRoBot on 2013 年 11 月 26 日 | Leave a response

利用XtraBackup给MYSQL热备(基于数据文件)
利用 XtraBackup 快速配置主从服务器
这XtraBackup 个主要包含2个命令 xtrabackup 和 innobackupex 第一个是他的主程序 另外个他自己封装的脚本一般用脚本来完成备份
这是工作笔记 比较凌乱 凑合着看吧

获取XtraBackup

获取XtraBackup(其中1.6里面有个WIN版本的 测试后很头疼各种BUG未解决 WIN下的MYSQL还是使用远程备份吧)

http://www.percona.com/downloads/XtraBackup/

1# 例如我的环境是CENTOS 6X 64BIT的
2mkdir -p $HOME/downloads;cd $HOME/downloads

 

1# 多线程下载
2lftp -c "pget -n 30 http://www.percona.com/downloads/XtraBackup/LATEST/RPM/rhel6/x86_64/percona-xtrabackup-2.1.5-680.rhel6.x86_64.rpm"

 

安装

1# 然后安装
2# 安装前可能需要 yum -y install libaio perl-Time-HiRes
3# 然后再。。。
4# 查看安装包信息
5rpm -qip percona-xtrabackup-2.1.5-680.rhel6.x86_64.rpm
6# 安装
7rpm -ivh percona-xtrabackup-2.1.5-680.rhel6.x86_64.rpm
8# 拆卸
9rpm -e percona-xtrabackup

 

1# 否则可能出现以下提示
2error: Failed dependencies:
3        libaio.so.1()(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
4        libaio.so.1(LIBAIO_0.1)(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
5        libaio.so.1(LIBAIO_0.4)(64bit) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64
6        perl(Time::HiRes) is needed by percona-xtrabackup-2.1.5-680.rhel6.x86_64

 

1# 好 这次安装顺利过去了
2xtrabackup --version
3xtrabackup version 2.1.5 for Percona Server 5.1.70 unknown-linux-gnu (x86_64) (revision id: 680)

 

1#
2开始备份(如果在MY.CNF里面配置了[XtraBackup]节的用户和密码就可以使用不带密码的方式备份)
3BACKUP_DIR="/home/ftp/b/backup_data/wwwroot/2013/11/23/2"
4mkdir -p $BACKUP_DIR
5# xtrabackup --backup --datadir=Mysql源数据目录 --target-dir=Mysql备份数据存放目录
6xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=$BACKUP_DIR
7xtrabackup --backup --user=root --password=密码 --datadir=/var/lib/mysql/ --target-dir=/home/ftp/b/backup_data/wwwroot/2013/11/23/-4

 

1innobackupex --user=root --password=密码 --defaults-file=/etc/my.cnf /home/ftp/b/backup_data/wwwroot/2013/11/23/2.1
2# 我晕 真是折腾啊
3innobackupex: Connected to MySQL server
4innobackupex: Error: Support for MySQL 5.1 with builtin InnoDB (not the plugin) was removed in Percona XtraBackup 2.1. The last version to support MySQL 5.1 with builtin InnoDB was Percona XtraBackup 2.0.

 

1# 拆卸
2rpm -e percona-xtrabackup

 

1# 重新下载
2mkdir -p $HOME/downloads;cd $HOME/downloads
3lftp -c pget -n 30 http://www.percona.com/redir/downloads/XtraBackup/XtraBackup-2.0.0/RPM/rhel6/x86_64/percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm

 

1# 查看安装包信息
2rpm -qip percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm

 

1# 安装
2rpm -ivh percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm
3warning: percona-xtrabackup-2.0.0-417.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
4Preparing...                ########################################### [100%]
5   1:percona-xtrabackup     ########################################### [100%]
6xtrabackup --version
7xtrabackup version 2.0.0 for Percona Server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 417)

 

测试备份

1innobackupex --user=root --password=密码 --defaults-file=/etc/my.cnf /home/ftp/b/backup_data/wwwroot/

 

好 这下对鸟。。。

01InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
02and Percona Inc 2009-2012.  All Rights Reserved.
03 
04This software is published under
05the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
06 
07131123 09:22:25  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
08131123 09:22:25  innobackupex: Connected to database with mysql child process (pid=30774)
09131123 09:22:31  innobackupex: Connection to database server closed
10IMPORTANT: Please check that the backup run completes successfully.
11           At the end of a successful backup run innobackupex
12           prints "completed OK!".
13 
14innobackupex: Using mysql  Ver 14.14 Distrib 5.1.69, for redhat-linux-gnu (x86_64) using readline 5.1
15innobackupex: Using mysql server version Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
16 
17innobackupex: Created backup directory /home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31
18131123 09:22:31  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
19131123 09:22:31  innobackupex: Connected to database with mysql child process (pid=30841)
20131123 09:22:33  innobackupex: Connection to database server closed
21 
22131123 09:22:33  innobackupex: Starting ibbackup with command: xtrabackup_51  --defaults-file="/etc/my.cnf" --backup --suspend-at-end --target-dir=/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31
23innobackupex: Waiting for ibbackup (pid=30861) to suspend
24innobackupex: Suspend file ‘/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31/xtrabackup_suspended‘
25 
26xtrabackup_51 version 2.0.0 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: 417)
27xtrabackup: uses posix_fadvise().
28xtrabackup: cd to /var/lib/mysql
29xtrabackup: Target instance is assumed as followings.
30xtrabackup:   innodb_data_home_dir = ./
31xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend
32xtrabackup:   innodb_log_group_home_dir = ./
33xtrabackup:   innodb_log_files_in_group = 2
34xtrabackup:   innodb_log_file_size = 5242880
35>> log scanned up to (0 44243)
36[01] Copying ./ibdata1 to /home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31/ibdata1
37[01]        ...done
38 
39131123 09:22:35  innobackupex: Continuing after ibbackup has suspended
40131123 09:22:35  innobackupex: Starting mysql with options:  --defaults-file=‘/etc/my.cnf‘ --password=xxxxxxxx --user=‘root‘ --unbuffered --
41131123 09:22:35  innobackupex: Connected to database with mysql child process (pid=30874)
42131123 09:22:37  innobackupex: Starting to lock all tables...
43>> log scanned up to (0 44243)
44>> log scanned up to (0 44243)
45131123 09:22:47  innobackupex: All tables locked and flushed to disk
46 
47131123 09:22:47  innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,
48innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in
49innobackupex: subdirectories of ‘/var/lib/mysql‘
50innobackupex: Backing up files ‘/var/lib/mysql/blog/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
51innobackupex: Backing up file ‘/var/lib/mysql/info/db.opt‘
52innobackupex: Backing up files ‘/var/lib/mysql/bd_2kdp_com/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
53>> log scanned up to (0 44243)
54innobackupex: Backing up file ‘/var/lib/mysql/www_2kdp_com/db.opt‘
55innobackupex: Backing up files ‘/var/lib/mysql/mysql/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (69 files)
56innobackupex: Backing up file ‘/var/lib/mysql/clean_cache/db.opt‘
57innobackupex: Backing up file ‘/var/lib/mysql/union/db.opt‘
58innobackupex: Backing up files ‘/var/lib/mysql/movie/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (49 files)
59innobackupex: Backing up file ‘/var/lib/mysql/link_2kdp/db.opt‘
60innobackupex: Backing up files ‘/var/lib/mysql/silkime_com/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (295 files)
61>> log scanned up to (0 44243)
62innobackupex: Backing up file ‘/var/lib/mysql/copy_site/db.opt‘
63innobackupex: Backing up files ‘/var/lib/mysql/sell_bdl/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}‘ (133 files)
64131123 09:22:59  innobackupex: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files
65 
66innobackupex: Resuming ibbackup
67 
68xtrabackup: The latest check point (for incremental): ‘0:44243‘
69xtrabackup: Stopping log copying thread.
70.>> log scanned up to (0 44243)
71>> log scanned up to (0 44243)
72 
73xtrabackup: Transaction log of lsn (0 44243) to (0 44243) was copied.
74131123 09:23:01  innobackupex: All tables unlocked
75131123 09:23:01  innobackupex: Connection to database server closed
76 
77innobackupex: Backup created in directory ‘/home/ftp/b/backup_data/wwwroot/2013-11-23_09-22-31‘
78# 这一句我特别喜欢 真的。。。折腾那么久 就为了这个。。。。。
79innobackupex: MySQL binlog position: filename ‘mysql-bin.000001‘, position 25959
80131123 09:23:01  innobackupex: completed OK!

 

01# 现在我们cd到备份目录 看看搞了些什么.
02# 除了数据库本身的文件外 发现还有 另外5个文件 有用的写下备注
03backup-my.cnf # 这个是用作数据库数据文件指向到这个备份目录的时候用的 直接把里面的内容复制到系统的my.cnf
04xtrabackup_binlog_info # 记录了备份完成时候的 二进制日志和 Position指针位置
05xtrabackup_logfile
06xtrabackup_binary
07xtrabackup_checkpoints # 这个里面指明了备份类型backup_type 如 full-backuped 增量...
08# 这个东西可以写个脚本来读取 从服务器那边那个做成自动配置
09cat xtrabackup_binlog_info
10mysql-bin.000001        25959

 

1# 现在再来测试下压缩备份
2# 一些比较有用的选项
3  # --slave-info(slave-info可以记录备份恢 复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。)
4  # --no-lock(备份过程不锁定表)
5  # 2>$BACKUP_DIR/`date +%Y-%m-%d_%H-%M-%S`/mysqlbackup.log(备份过程中的输出信息保存,适合封装到自己的脚本里面去什么的)
6 
7#TIME1=`date +%Y%m%d%H%M%S`;echo 勤劳的Jrobot:我在`date +%Y/%m/%d_%H:%M:%S`开始备份!
8#echo "勤劳的Jrobot:我在`date +%Y/%m/%d_%H:%M:%S`开始备份!" >> /home/ftp/b/backup_data/`date +%Y-%m-%d_%H-%M-%S`/backup.log

 

1BACKUP_DIR="/home/ftp/b/backup_data/wwwroot"
2USER="用户"
3PSWD="密码"
4MY="/etc/my.cnf"
5LOG="MySQL_Last_Backup.LOG"
6LAST_GZIP_BACKUP="LASTBACKUP.TAR.GZ"
7#mkdir -p $BACKUP_DIR/`date +%Y-%m-%d_%H-%M-%S`/

 

记录备份日志

1innobackupex --user=$USER --password=$PSWD --slave-info --defaults-file=$MY $BACKUP_DIR/ 2>$BACKUP_DIR/$LOG

 

gzip压缩备份

1innobackupex --user=$USER --password=$PSWD --slave-info --defaults-file=$MY $BACKUP_DIR/ --stream=tar gzip>$BACKUP_DIR/$LAST_GZIP_BACKUP

 

解压这个包看看或者直接SCP到从机

1tar xvzfi LASTBACKUP.TAR.GZ
2cat xtrabackup_binlog_info # 哈哈开心了吧

 

在从服务器上解压这个后 开始配置同步

1#slave stop;
2#CHANGE MASTER TO MASTER_HOST=‘主机地址‘,MASTER_USER=‘从复制账号‘,MASTER_PASSWORD=‘密码‘,MASTER_LOG_FILE=‘mysql-bin.00000X‘,MASTER_LOG_POS=1;
3#slave start;

 

总结 这个东西的封装后的脚本是很强大 用来做主从配置也很快 但是有个问题 就是不支持远程备份。你比如说 你想从Win服务器备份到Linux的就蛋疼了。这个软件的主要文档和参考资料的环境几乎都是基于LINUX的。WIN下着实蛋疼。。。

利用XtraBackup给MYSQL热备(基于数据文件)