1、生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库一个合格的运维工程师或者dba工程师,如果有从事数据库方面的话,首先需要做的就是备份,如果没有备份,出现问题的话,你的业务就会出问题,你的工作甚至会。所以备份是重要的,但光有备份还不行,备份后如果出现问题,你还得使用备份数据来恢复,但恢复数据的时间一般都是很长的,不符合业务需求,所以一个快速备份与恢复的软件就很有必要。之前我在维护mysql数据库的时候,使用mysqldump来进行备份与恢复,在备份的时候锁住表,然后全部备份,在数据少的时候
2、没问题,但如果数据很多,不允许锁表,同时需要恢复数据块的情况,mysqldump就不适合了,我在恢复一个4G数据文件的数据库的时候,恢复的数据是使用mysqldump的数据,恢复了3个小时还没有反应,造成的影响很严重,所以我开始寻找其他的别发软件来满足以上的需求,幸好找到了,就是使用xtrabackup来进行备份与恢复,恢复4G数据文件的数据库,仅需要14秒,同时在备份的时候不会锁表,而且支持增量备份,所以把我的比较分享给大家,希望对大家有益!Xtrabackup 是percona公司的开源项目,用以实现类似innodb官方的热备份工具InnoDB Hot Backup的功能,能够非常快速地备
3、份与恢复mysql数据库。 Xtrabackup中包含两个工具:xtrabackup是用于热备份innodb, xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;innobackupex是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。由于innobackupex的功能更为全面和完善,所以,本文以innobackupex作为基础进行研究描述。下面介绍xtrabackup的全部、增量的备份与恢复。一、下载与安装1、下载wget 2、安装依赖库如果是debian系列的话apt-get install debhelper autotools-dev
4、 libaio-dev wget automake libtool bison libncurses-dev libz-dev cmake bzr 如果是redhat系列的话yum install cmake gcc gcc-c+ libaio libaio-devel automake autoconf bzr bison libtool ncurses-devel zlib-devel 3、解压tar zxvf xtrabackup-1.6.7.tar.gz 4、进入目录cd xtrabackup-1.6.7 5、复制cd bin cp * /usr/bin 然后就安装完成了,下面开始备份
5、 其中,innobackupex是我们要使用的备份工具;xtrabackup是被封装在innobackupex之中的,innobackupex运行时需要调用它;xtrabackup_51是xtrabackup运行时需要调用的工具;tar4ibd是以tar流的形式产生备份时用来打包的工具。6、对某个数据库进行全部备份的命令介绍innobackupex -user=root -password=123456 -defaults-file=/etc/mysql/f -database=test -stream=tar /tmp/data/ 2/tmp/data/err.log|gzip 1/tmp/
6、data/test.tar.gz 说明: -database=test 单独对test数据库做备份 ,若是不添加此参数那就那就是对全库做备份 2/tmp/data/err.log 输出信息写入日志中 1/tmp/data/test.tar.gz 打包压缩存储到该文件中 二、对数据库的全部备份与恢复下面开始测试xtrabackup的全部备份(1)先进入mysql里创建一个新的test数据库 rootclient2:/tmp# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or
7、g. Your MySQL connection id is 40 Server version: 5.5.28-0ubuntu0.12.04.3-log (Ubuntu) Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
8、Type help; or h for help. Type c to clear the current input statement. mysql drop database test; Query OK, 3 rows affected (0.13 sec) mysql create database test; Query OK, 1 row affected (0.00 sec) mysql use test; Database changed mysql create table test (id int); Query OK, 0 rows affected (0.06 sec
9、) mysql insert into test values(1); Query OK, 1 row affected (0.04 sec) mysql insert into test values(2); Query OK, 1 row affected (0.01 sec) mysql insert into test values(3); Query OK, 1 row affected (0.00 sec) mysql insert into test values(4); Query OK, 1 row affected (0.00 sec) mysql insert into
10、test values(5); Query OK, 1 row affected (0.01 sec) mysql select * from test; +-+ | id | +-+ | 1 | | 2 | | 3 | | 4 | | 5 | +-+ 5 rows in set (0.00 sec) mysql flush privileges; Query OK, 0 rows affected (0.00 sec) (2)然后备份test的整个数据库使用下面的backup.sh脚本rootclient2:/tmp# cat backup.sh #!/bin/bash user=root
11、passwd=123456 database=test my_config=/etc/mysql/f log=$database-$(date +%Y%m%d%H%M).log str=$database-$(date +%Y%m%d%H%M).tar.gz backup_dir=/tmp/data echo Start to backup at $(date +%Y%m%d%H%M) if ! -d $backup_dir ;then mkdir $backup_dir fi innobackupex -user=$user -password=$passwd -defaults-file=
12、$my_config -database=$database -stream=tar $backup_dir 2$backup_dir/$log | gzip 1$backup_dir/$str if $? -eq 0 ;then echo Backup is finish! at $(date +%Y%m%d%H%M) exit 0 else echo Backup is Fail! at $(date +%Y%m%d%H%M) exit 1 fi 现在开始运行此脚本rootclient2:/tmp# sh backup.sh Start to backup at 201303072101
13、Backup is finish! at 201303072102 然后到data里查看结果rootclient2:/tmp# cd data rootclient2:/tmp/data# ll total 3272 drwxr-xr-x 2 root root 4096 Mar 7 21:01 ./ drwxrwxrwt 13 root root 4096 Mar 7 21:02 ./ -rw-r-r- 1 root root 3780 Mar 7 21:02 test-201303072101.log -rw-r-r- 1 root root 3336909 Mar 7 21:02 tes
14、t-201303072101.tar.gz rootclient2:/tmp/data# cat test-201303072101.log InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy and Percona Inc 2009-2012. All Rights Reserved. This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. 130307 21:01:39 innob
15、ackupex: Starting mysql with options: -defaults-file=/etc/mysql/f -password=xxxxxxxx -user=root -unbuffered - 130307 21:01:39 innobackupex: Connected to database with mysql child process (pid=12441) 130307 21:01:45 innobackupex: Connection to database server closed IMPORTANT: Please check that the backup run completes successfully. At the end of a successful backup run innobackupex p
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1