Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1647014
  • 博文数量: 135
  • 博客积分: 2820
  • 博客等级: 少校
  • 技术积分: 2544
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-16 13:33
文章分类

全部博文(135)

文章存档

2015年(1)

2014年(8)

2013年(16)

2012年(43)

2011年(56)

2010年(11)

分类: Mysql/postgreSQL

2014-07-05 17:21:52


官网地址: 下载地址:downloads/XtraBackup/



原理:
如果是MyISAM引擎只是数据文件的复制过程,全备份,并且加写锁,备份完成后释放写锁。
如果是InnoDB引擎(简单流程如下)
1). 第一次做完整备份,并记录检查点LSN(记录在备份目录下的xtrabackup_checkpoints文件中) ,
2). 增量备份是基于完整备份之上做的,去logfile中查找最后备份时的LSN,如果LSN大于上次备份时的LSN,则备份数据并记录当前的LSN。 循环往复。


什么是LSN?

Log Sequence Number (LSN) : Log Sequence Numbers correspond to given position in the log files and typically incremented for each log record. Innodb uses number of bytes ever written to the log files however it could be something different. LSNs are often extensively used in recovery check pointing and buffer management operations. When checkpoint (both fuzzy and not) happens you get something like “all changes up to LSN=X are now flushed to the data space” this means you can discard or archive logs for LSN earlier than that. When doing log recovery checking LSN in the log record can tell if you this change needs to be applied or it already was applied (when doing recovery you do not know which dirty pages were flushed from the buffer pool).


The LSN do not relate much to transactions – changes from different transactions are intermixed in the log files and many LSNs can correspond to changes from the same transaction.

日志序列号(LSN):日志序列号对应于给定位置为每个日志记录日志文件,通常会增加。Innodb写入日志文件的号码和字节数是不同的东西。  LSNs通常是广泛应用于恢复检查指向和缓冲区管理操作。
检查点(模糊和不)发生时你会得到类似“所有更改LSN = X现在刷新到数据空间”这意味着您可以丢弃或归档日志LSN早些时候。
做日志恢复时检查的LSN日志记录这种变化可以告诉如果你需要应用也已应用(在恢复你不知道哪些脏页刷新缓冲池)。

LSN不涉及太多的事务——变化从不同的事务日志文件和许多LSNs混杂在一起可以从相同的事务对应变化。

模拟备份过程:
注意:如果使用innobackupex备份数据库,一定要在my.cnf里加上datadir=/xxx/xxx/  参数。xtrabackup_55 是根据它去找数据文件的位置。



基本参数说明:(其它参数请查阅官网)

--user=root                        指定用户
--password=xxx                  指定密码xxx,如果没有密码,可不写此参数
--defaults-file=my.cnf          如果mysql的配置文件在默认位置/etc/my.cnf ,可不写此参数。如果在自定义位置要加上这个参数
--databases=sg2                  列出需要备份的databases,如果没有指定该参数,所有包含MyISAM和InnoDB表的database都会被备份
                                         也可指定多个数据库 例如:--databases="sg2 mysql test1 test2"
--no-timestamp                    默认会以时间创建目录,存放备份文件,加此参数不创建此文件
--incremental                       指定此次备份为增量备
--copy-back                         做数据恢复时将备份数据文件拷贝到MySQL服务器的datadir
--use-memory=4G               该参数在prepare的时候使用,控制prepare时innodb实例使用的内存量 
--include=REGEXP               备份包含的库表。支持正则
--slave-info                          如果在从库上做备份,这个记录偏移量到备份目录下的xtrabackup_slave_info文件中
--socket=                            指定socket文件的位置
--compress                           对备份数据进行压缩,目前只支持tar




新建测试表z
create table z (id int(10)) ENGINE=InnoDB;
插入100000条数据
验证数据

root@hf_to_TH (sg2) > select count(*) from z;
+----------+
| count(*) |
+----------+
|   100000 |
+----------+
1 row in set (0.15 sec)


全备
`which innobackupex` --user=root  --no-timestamp --database=sg2 /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all

再插入100000条数据
root@hf_to_TH (sg2) > select count(*) from z;
+----------+
| count(*) |
+----------+
|   200000 |
+----------+



做第一次增量的备份
`which innobackupex` --user=root  --no-timestamp --database=sg2 --incremental --incremental-basedir=/data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/inc-1


在库中新建一个测试表z1

create table z1 (id int(10)) ENGINE=InnoDB;

在测试表z1中插入100条记录
Insert z1 select * from z where id<101;


检查创建的新表
select count(*) from z1;


验证数据
root@hf_from_EN (sg2) > select count(*) from z1;
+----------+
| count(*) |
+----------+
|      100 |
+----------+




再做一个增量的备份
`which innobackupex` --user=root  --no-timestamp --database=sg2 --incremental --incremental-basedir=/data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/inc-1 /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/inc-2


==========================
root@hf_from_EN (sg2) > select count(*) from z;
+----------+
| count(*) |
+----------+
|   200000 |
+----------+
1 row in set (0.00 sec)


root@hf_from_EN (sg2) > select count(*) from z1;
+----------+
| count(*) |
+----------+
|      100 |
+----------+
1 row in set (0.00 sec)


root@hf_from_EN (sg2) > 

====================================
恢复过程:
备份的恢复过程中包括恢复和还原两个部分。
1.使各个数据文件处于一个一致性状态,这个过程叫做"准备(prepare)"。
2.拷贝数据文件到datadir的过程,这个过程叫做"恢复(recovery)"。


service mysql stop
mv /data/mysql/lstx-hf2 /tmp/lstx-hf2_back
rm /data/mysql/lstx-hf2/* -rf


innobackupex --user=root --apply-log  --redo-only /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all

innobackupex --user=root --apply-log  --redo-only /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all --incremental-dir=/data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/inc-1

innobackupex --user=root --apply-log  /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all --incremental-dir=/data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/inc-2

innobackupex --user=root --copy-back /data/mysql_backup/innobackupex_backup/10.52.33.141/20140702/all


cp -r /tmp/lstx-hf2_back/mysql  /data/mysql/lstx-hf2/
chown mysql.mysql /data/mysql/ -R
service mysql start


注意:恢复最后一次增量日志时不要加--redo-only 参数。 
官网说明
Note: --redo-only should be used when merging all incrementals except the last one. That’s why the previous
line doesn’t contain the --redo-only option. Even if the --redo-only was used on the last step, backup would
still be consistent but in that case server would perform the rollback phase


注意:--redo-only应该使用合并所有渐进映像,除了最后一个。这就是为什么前面行不包含--redo-only选项。
即使使用--redo-only最后一步,备份仍然是一致的,但在这种情况下,服务器会执行回滚的阶段。




注意:CentOS6.3 默认yun源安装的xtrabackup版本是xtrabackup-1.6.3-292.rhel5.x86_64  
此版本有bug ,在全备份之后创建的数据对象在恢复时不显示。在官网下载的2.0版本没有些问题。











阅读(2011) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~