




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库一个合格的运维工程师或者dba工程师,如果有从事数据库方面的话,首先需要做的就是备份,如果没有备份,出现问题的话,你的业务就会出问题,你的工作甚至会。所以备份是重要的,但光有备份还不行,备份后如果出现问题,你还得使用备份数据来恢复,但恢复数据的时间一般都是很长的,不符合业务需求,所以一个快速备份与恢复的软件就很有必要。之前我在维护mysql数据库的时候,使用mysqldump来进行备份与恢复,在备份的时候锁住表,然后全部备份,在数据少的时候没问题,但如果数据很多,不允许锁表,同时需要恢复数据块的情况,mysqldum
2、p就不适合了,我在恢复一个4G数据文件的数据库的时候,恢复的数据是使用mysqldump的数据,恢复了3个小时还没有反应,造成的影响很严重,所以我开始寻找其他的别发软件来满足以上的需求,幸好找到了,就是使用xtrabackup来进行备份与恢复,恢复4G数据文件的数据库,仅需要14秒,同时在备份的时候不会锁表,而且支持增量备份,所以把我的比较分享给大家,希望对大家有益!Xtrabackup 是percona公司的开源项目,用以实现类似innodb官方的热备份工具InnoDB Hot Backup的功能,能够非常快速地备份与恢复mysql数据库。 Xtrabackup中包含两个工具:xtrabac
3、kup是用于热备份innodb, xtradb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构;innobackupex是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。由于innobackupex的功能更为全面和完善,所以,本文以innobackupex作为基础进行研究描述。下面介绍xtrabackup的全部、增量的备份与恢复。一、下载与安装1、下载wget 2、安装依赖库如果是debian系列的话apt-get install debhelper autotools-dev libaio-dev wget automake libtool bison
4、 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 然后就安装完成了,下面开始备份 其中,innobackupex是我们要使用的备份工具;xtrabackup是
5、被封装在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/data/test.tar.gz 说明: -database=test 单独对
6、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 g. Your MySQL connection id is 40 Serve
7、r 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. Type help; or h for help. Type c to cle
8、ar 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) mysql insert into test values(1); Que
9、ry 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 test values(5); Query OK, 1 row affecte
10、d (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 passwd=123456 database=test my_config=/
11、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=$my_config -database=$database -stream=
12、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 Backup is finish! at 201303072102 然后到da
13、ta里查看结果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 test-201303072101.tar.gz rootclient2:/tmp/
14、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 innobackupex: Starting mysql with options: -
15、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 th
16、e end of a successful backup run innobackupex prints completed OK!. innobackupex: Using mysql Ver 14.14 Distrib 5.5.28, for debian-linux-gnu (x86_64) using readline 6.2 innobackupex: Using mysql server version Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. innobackupex:
17、 Created backup directory /tmp/data 130307 21:01:45 innobackupex: Starting mysql with options: -defaults-file=/etc/mysql/f -password=xxxxxxxx -user=root -unbuffered - 130307 21:01:45 innobackupex: Connected to database with mysql child process (pid=12471) 130307 21:01:47 innobackupex: Connection to
18、database server closed 130307 21:01:47 innobackupex: Starting ibbackup with command: xtrabackup_55 -defaults-file=/etc/mysql/f -backup -suspend-at-end -log-stream -target-dir=/tmp innobackupex: Waiting for ibbackup (pid=12478) to suspend innobackupex: Suspend file /tmp/xtrabackup_suspended xtrabacku
19、p: suspend-at-end is enabled. xtrabackup: uses posix_fadvise(). xtrabackup: cd to /var/lib/mysql xtrabackup: Target instance is assumed as followings. xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabacku
20、p: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 130307 21:01:47 InnoDB: Using Linux native AIO xtrabackup: Stream mode. log scanned up to (59605543) 130307 21:01:49 innobackupex: Continuing after ibbackup has suspended innobackupex: Starting to backup InnoDB tables and in
21、dexes innobackupex: from original InnoDB data directory /var/lib/mysql innobackupex: Backing up as tar stream ibdata1 130307 21:01:52 innobackupex: Starting mysql with options: -defaults-file=/etc/mysql/f -password=xxxxxxxx -user=root -unbuffered - 130307 21:01:52 innobackupex: Connected to database
22、 with mysql child process (pid=12494) log scanned up to (59605543) 130307 21:01:54 innobackupex: Starting to lock all tables. log scanned up to (59605543) log scanned up to (59605543) 130307 21:02:04 innobackupex: All tables locked and flushed to disk 130307 21:02:04 innobackupex: Starting to backup
23、 .frm, .MRG, .MYD, .MYI, innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in innobackupex: subdirectories of /var/lib/mysql innobackupex: Backing up file /var/lib/mysql/test/test.frm innobackupex: Backing up file /var/lib/mysql/test/db.opt 130307 21:02:04 innobackupex: Finished backin
24、g up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files innobackupex: Resuming ibbackup xtrabackup: The latest check point (for incremental): 59605543 log scanned up to (59605543) xtrabackup: Transaction log of lsn (59605543) to (59605543) was copied. 130307 21:02:06 innobacku
25、pex: All tables unlocked 130307 21:02:06 innobackupex: Connection to database server closed innobackupex: Backup created in directory /tmp/data innobackupex: MySQL binlog position: filename mysql-bin.000022, position 107 innobackupex: You must use -i (-ignore-zeros) option for extraction of the tar
26、stream. 130307 21:02:06 innobackupex: completed OK! 可以看到备份完成了(3)恢复数据库先关闭mysql服务,然后再删除test数据库文件rootclient2:/tmp/data# service mysql stop mysql stop/waiting rootclient2:/tmp/data# cd /var/lib/mysql/ rootclient2:/var/lib/mysql# ll total 77860 drwx- 8 mysql mysql 4096 Mar 7 20:59 ./ drwxr-xr-x 38 root r
27、oot 4096 Mar 7 19:52 ./ -rw-r-r- 1 root root 0 Jan 5 14:22 debian-5.5.flag drwx- 2 mysql mysql 4096 Feb 11 17:39 django/ -rw-rw- 1 mysql mysql 69206016 Mar 7 21:02 ibdata1 -rw-rw- 1 mysql mysql 5242880 Mar 7 21:02 ib_logfile0 -rw-rw- 1 mysql mysql 5242880 Mar 7 21:01 ib_logfile1 drwx- 2 mysql mysql
28、4096 Jan 5 22:55 monitor/ drwx- 2 mysql root 4096 Jan 5 14:22 mysql/ -rw-rw- 1 root root 6 Jan 5 14:22 mysql_upgrade_info drwx- 2 mysql mysql 4096 Jan 5 14:22 performance_schema/ drwx- 2 mysql mysql 4096 Mar 7 21:00 test/ drwxr-xr-x 2 mysql mysql 4096 Mar 7 19:58 xtrbackup/ rootclient2:/var/lib/mysq
29、l# rm -rf test rootclient2:/var/lib/mysql# ll total 77856 drwx- 7 mysql mysql 4096 Mar 7 21:03 ./ drwxr-xr-x 38 root root 4096 Mar 7 19:52 ./ -rw-r-r- 1 root root 0 Jan 5 14:22 debian-5.5.flag drwx- 2 mysql mysql 4096 Feb 11 17:39 django/ -rw-rw- 1 mysql mysql 69206016 Mar 7 21:02 ibdata1 -rw-rw- 1
30、mysql mysql 5242880 Mar 7 21:02 ib_logfile0 -rw-rw- 1 mysql mysql 5242880 Mar 7 21:01 ib_logfile1 drwx- 2 mysql mysql 4096 Jan 5 22:55 monitor/ drwx- 2 mysql root 4096 Jan 5 14:22 mysql/ -rw-rw- 1 root root 6 Jan 5 14:22 mysql_upgrade_info drwx- 2 mysql mysql 4096 Jan 5 14:22 performance_schema/ drw
31、xr-xr-x 2 mysql mysql 4096 Mar 7 19:58 xtrbackup/ 开始恢复数据库先把之前/tmp/data里的数据库压缩版给解压rootclient2:cd /tmp/data rootclient2:/tmp/data# tar -izxvf test-201303072101.tar.gz -C /tmp/restore/ backup-f ibdata1 xtrabackup_binlog_info test/test.frm test/db.opt xtrabackup_logfile xtrabackup_checkpoints xtrabackup
32、_binary 注意:这里tar解包必须使用-i参数,否则解压出来的文件只有一个backup-f查看一下restore的内容rootclient2:/tmp/data# cd /tmp/restore/ rootclient2:/tmp/restore# ll total 67616 drwxr-xr-x 3 root root 4096 Mar 7 21:03 ./ drwxrwxrwt 14 root root 4096 Mar 7 21:03 ./ -rw-r-r- 1 root root 260 Mar 7 21:01 backup-f -rw-rw- 1 mysql mysql 69
33、206016 Mar 7 21:01 ibdata1 drwxr-xr-x 2 root root 4096 Mar 7 21:03 test/ -rw-r-r- 1 root root 13 Mar 7 21:02 xtrabackup_binary -rw-r-r- 1 root root 23 Mar 7 21:02 xtrabackup_binlog_info -rw-r-r- 1 root root 79 Mar 7 21:02 xtrabackup_checkpoints -rw-r-r- 1 root root 2560 Mar 7 21:02 xtrabackup_logfil
34、e 然后将备份文件中的日志应用到备份文件中的数据文件上rootclient2:/tmp/restore# innobackupex -user=root -password=123456 -apply-log /tmp/restore/ 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 LIC
35、ENSE Version 2, June 1991. IMPORTANT: Please check that the apply-log run completes successfully. At the end of a successful apply-log run innobackupex prints completed OK!. 130307 21:04:18 innobackupex: Starting ibbackup with command: xtrabackup_55 -defaults-file=/tmp/restore/backup-f -prepare -tar
36、get-dir=/tmp/restore xtrabackup_55 version 1.6.7 for Percona Server 5.5.16 Linux (x86_64) (revision id: undefined) xtrabackup: cd to /tmp/restore xtrabackup: This target seems to be not prepared yet. xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(59605543) xtrabackup: Temporary in
37、stance for recovery is set as followings. xtrabackup: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 1 xtrabackup: innodb_log_file_size = 2097152 130307 21:04:19 InnoDB: Using Lin
38、ux native AIO xtrabackup: Starting InnoDB instance for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by -use-memory parameter) 130307 21:04:19 InnoDB: The InnoDB memory heap is disabled 130307 21:04:19 InnoDB: Mutexes and rw_locks use GCC atomic builtins 130307 21:04:19 InnoDB: Co
39、mpressed tables use zlib 1.2.3 130307 21:04:19 InnoDB: Using Linux native AIO 130307 21:04:19 InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads and innodb_write_io_threads instead 130307 21:04:19 InnoDB: Initializing buffer pool, size = 100.0M 130307 21:04:19 I
40、nnoDB: Completed initialization of buffer pool 130307 21:04:19 InnoDB: highest supported file format is Barracuda. InnoDB: The log sequence number in ibdata files does not match InnoDB: the log sequence number in the ib_logfiles! 130307 21:04:19 InnoDB: Database was not shut down normally! InnoDB: S
41、tarting crash recovery. InnoDB: Reading tablespace information from the .ibd files. InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000022 130307 21:04:20 InnoDB: Waiting for the background threads to start 130307 21:04:21 Percona XtraDB () 1.1.8-20.1 started; log s
42、equence number 59605543 notice (again) If you use binary log and dont use any hack of group commit, the binary log position seems to be: InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000022 xtrabackup: starting shutdown with innodb_fast_shutdown = 1 130307 21:04:2
43、1 InnoDB: Starting shutdown. 130307 21:04:25 InnoDB: Shutdown completed; log sequence number 59606758 130307 21:04:25 innobackupex: Restarting xtrabackup with command: xtrabackup_55 -defaults-file=/tmp/restore/backup-f -prepare -target-dir=/tmp/restore for creating ib_logfile* xtrabackup_55 version
44、1.6.7 for Percona Server 5.5.16 Linux (x86_64) (revision id: undefined) xtrabackup: cd to /tmp/restore xtrabackup: This target seems to be already prepared. xtrabackup: notice: xtrabackup_logfile was already used to -prepare. xtrabackup: Temporary instance for recovery is set as followings. xtraback
45、up: innodb_data_home_dir = ./ xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend xtrabackup: innodb_log_group_home_dir = ./ xtrabackup: innodb_log_files_in_group = 2 xtrabackup: innodb_log_file_size = 5242880 130307 21:04:25 InnoDB: Using Linux native AIO xtrabackup: Starting InnoDB instance
46、 for recovery. xtrabackup: Using 104857600 bytes for buffer pool (set by -use-memory parameter) 130307 21:04:25 InnoDB: The InnoDB memory heap is disabled 130307 21:04:25 InnoDB: Mutexes and rw_locks use GCC atomic builtins 130307 21:04:25 InnoDB: Compressed tables use zlib 1.2.3 130307 21:04:25 Inn
47、oDB: Using Linux native AIO 130307 21:04:25 InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads and innodb_write_io_threads instead 130307 21:04:25 InnoDB: Initializing buffer pool, size = 100.0M 130307 21:04:25 InnoDB: Completed initialization of buffer pool 130
48、307 21:04:25 InnoDB: Log file ./ib_logfile0 did not exist: new to be created InnoDB: Setting log file ./ib_logfile0 size to 5 MB InnoDB: Database physically writes the file full: wait. 130307 21:04:25 InnoDB: Log file ./ib_logfile1 did not exist: new to be created InnoDB: Setting log file ./ib_logfi
49、le1 size to 5 MB InnoDB: Database physically writes the file full: wait. 130307 21:04:25 InnoDB: highest supported file format is Barracuda. InnoDB: The log sequence number in ibdata files does not match InnoDB: the log sequence number in the ib_logfiles! 130307 21:04:25 InnoDB: Database was not shu
50、t down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files. InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000022 130307 21:04:26 InnoDB: Waiting for the background threads to start 130307 21:04:27 Percona XtraDB ()
51、 1.1.8-20.1 started; log sequence number 59607052 notice (again) If you use binary log and dont use any hack of group commit, the binary log position seems to be: InnoDB: Last MySQL binlog file position 0 107, file name /var/log/mysql/mysql-bin.000022 xtrabackup: starting shutdown with innodb_fast_s
52、hutdown = 1 130307 21:04:27 InnoDB: Starting shutdown. 130307 21:04:31 InnoDB: Shutdown completed; log sequence number 59607052 130307 21:04:31 innobackupex: completed OK! 这里的-apply-log指明是将日志应用到数据文件上,完成之后将备份文件中的数据恢复到数据库中:然后再查看一下当前目录内容rootclient2:/tmp/restore# ll total 79904 drwxr-xr-x 3 root root 40
53、96 Mar 7 21:04 ./ drwxrwxrwt 14 root root 4096 Mar 7 21:04 ./ -rw-r-r- 1 root root 260 Mar 7 21:01 backup-f -rw-rw- 1 mysql mysql 69206016 Mar 7 21:04 ibdata1 -rw-r-r- 1 root root 5242880 Mar 7 21:04 ib_logfile0 -rw-r-r- 1 root root 5242880 Mar 7 21:04 ib_logfile1 drwxr-xr-x 2 root root 4096 Mar 7 21:0
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年高铁站维保部考试试题及答案
- 2025年康复医疗学试题及答案
- 11 探究昆虫的奥秘(教学设计)-苏教版科学四年级下册
- 美妆集合店2025年跨界合作与品牌联名案例分析报告
- 模具生产试题题库及答案
- 辽阳市烟草公司2025秋招网申-申论题模板及答案
- 2024-2025学年度邮政行业职业技能鉴定预测复习附答案详解【研优卷】
- 2023年度粮油食品检验人员能力提升B卷题库含答案详解【能力提升】
- 2024年自考专业(计算机网络)过关检测试卷及完整答案详解【名师系列】
- 2025年高级软考考试综合练习附参考答案详解【培优B卷】
- 心理健康和生命教育
- 进口铁矿石的报关流程
- 新苏教版一年级数学上册第一单元《练习一》教案
- 冀教版英语五年级上册单词表
- 医院感染在眼科医疗中的预防与控制
- 园区废气与噪音综合治理管理制度
- 催收专业知识培训课件
- 2025华电(海西)新能源限公司面向华电系统内外公开招聘高频重点提升(共500题)附带答案详解
- 医疗器械冷链培训
- 冠脉微循环功能障碍评估
- 《褐藻酸寡糖含量的测定》编制说明
评论
0/150
提交评论