第2章 数据库恢复
这里只做redo,不做undo
2.1 恢复数据前准备
数据库的恢复,是从全备--->1次增量--->2次增量......--->N次增量直到故障点的位置的顺序来恢复的
如果是全备:最近一次的全备----> binlog
2.1.1 创建新的语句为二进制日志恢复做准备
mysql> insert into testbackup(step)values(
'second increment backup'
);
Query OK, 1 row affected (0.01 sec)
mysql>
select
* from testbackup;
+----+-------------------------+
|
id
| step |
+----+-------------------------+
| 1 | firstfull backup |
| 2 | firstincrement backup |
| 3 | secondincrement backup |
+----+-------------------------+
3 rows
in
set
(0.00 sec)
mysql> insert into testbackup(step)values(
'last backup'
);
Query OK, 1 row affected (0.00 sec)
mysql>
select
* from testbackup;
+----+-------------------------+
|
id
| step |
+----+-------------------------+
| 1 | firstfull backup |
| 2 | firstincrement backup |
| 3 | secondincrement backup |
| 4 |last backup |
+----+-------------------------+
4 rows
in
set
(0.00 sec)
mysql> truncate table testbackup;
Query OK, 0 rows affected (0.02 sec)
mysql>
select
* from testbackup;
Empty
set
(0.00 sec)
2.2 恢复第一次的全备数据库
恢复数据库的时候一定会重启一次数据库的,所以我们可以选择停库操作(如果可以的情况下),下面以停库操作练习:
1、 停掉数据库
[root@db01 inc]
# /data/3306/mysql stop
Stoping MySQL...
[root@db01 inc]
# netstat -lntup|grep 3306
[root@db01 inc]
#
2、 恢复最近一次的全备,*****在操作前一定要先备份*****
[root@db01 backups]
# for i in `ls/data/backups/`;do cp -r $i /data/backups/$i.bak;done
[root@db01 backups]
# ls
full full.bak inc inc.bak
[root@db01 backups]
# cp -r /data/3306/data/data/backups/3306_data.bak
[root@db01 backups]
# ls
3306_data.bak full full.bak inc inc.bak
3、 导入全备数据
innobackupex --user=root --password=789 --apply-log--redo-only
/data/backups/full/2016-09-05_20-01-32--socket
=
/data/3306/mysql
.sock
要确认看到OK字样,表示导入成功
2.3 恢复第一次全备后的第一次增量备份
1、 确定第一次增量备份的文件目录
[root@db01 inc]
# ls -lrt
总用量 8
drwxr-x--- 8 root root 4096 9月 5 20:19 2016-09-05_20-19-48
drwxr-x--- 8 root root 4096 9月 5 21:18 2016-09-05_21-18-30
通过时间排序可以看到这里第一次是2016-09-05_20-19-48,工作中可以自定义一些名字
2、 执行增量恢复
innobackupex --user=root --password=789 --apply-log--redo-only
/data/backups/full/2016-09-05_20-01-32
--incremental-
dir
=
/data/backups/inc/2016-09-05_20-19-48--socket
=
/data/3306/mysql
.sock
看到OK字样表示成功
2.4 恢复第一次全备后的第二次增量备份
innobackupex --user=root --password=789 --apply-log--redo-only
/data/backups/full/2016-09-05_20-01-32
--incremental-
dir
=
/data/backups/inc/2016-09-05_21-18-30--socket
=
/data/3306/mysql
.sock
到此Xtrabackup昨晚了redo操作,下面我们要把他写入数据文件中。这一步才是把数据放到数据库中
数据写入数据文件:
innobackupex --user=root --password=789--apply-log
/data/backups/full/2016-09-05_20-01-32
--socket=
/data/3306/mysql
.sock
同样是看到OK字样表示执行成功。
2.5 恢复数据到实例3306的数据中
Xtrabackup恢复是把所有的增量都叠加到了全备的身上,也就是恢复用的全备。它就是数据库data,我们把这个全备目录替换了3306数据库的data数据
[root@db01 full]
# rm -fr /data/3306/data/
[root@db01 full]
# mv 2016-09-05_20-01-32//data/3306/data
[root@db01 full]
# ll /data/3306/data
总用量 163884
-rw-r----- 1 root root 418 9月 5 20:01 backup-my.cnf
-rw-r----- 1 root root 134217728 9月 5 22:12 ibdata1
-rw-r----- 1 root root 4194304 9月 5 22:12 ib_logfile0
-rw-r----- 1 root root 4194304 9月 5 22:12 ib_logfile1
-rw-r----- 1 root root 4194304 9月 5 22:12 ib_logfile2
-rw-r----- 1 root root 12582912 9月 5 22:12 ibtmp1
drwxr-x--- 2 root root 4096 9月 5 22:10 krik
drwxr-x--- 2 root root 4096 9月 5 22:10 mysql
drwxr-x--- 2 root root 4096 9月 5 22:10 oldboy
drwxr-x--- 2 root root 4096 9月 5 22:10 performance_schema
drwxr-x--- 2 root root 4096 9月 5 22:10
test
-rw-r----- 1 root root 22 9月 5 22:10 xtrabackup_binlog_info
-rw-r--r-- 1 root root 33 9月 5 22:12 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 113 9月 5 22:12 xtrabackup_checkpoints
-rw-r----- 1 root root 630 9月 5 22:10 xtrabackup_info
-rw-r----- 1 root root 8388608 9月 5 22:04 xtrabackup_logfile
drwxr-x--- 2 root root 4096 9月 5 22:10 xtr_test
[root@db01 full]
# chown -R mysql.mysql/data/3306/data/
[root@db01 full]
# ll /data/3306/data
总用量 163884
-rw-r----- 1 mysql mysql 418 9月 5 20:01 backup-my.cnf
-rw-r----- 1 mysql mysql 134217728 9月 5 22:12 ibdata1
-rw-r----- 1 mysql mysql 4194304 9月 5 22:12 ib_logfile0
-rw-r----- 1 mysql mysql 4194304 9月 5 22:12 ib_logfile1
-rw-r----- 1 mysql mysql 4194304 9月 5 22:12 ib_logfile2
-rw-r----- 1 mysql mysql 12582912 9月 5 22:12 ibtmp1
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10 krik
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10 mysql
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10 oldboy
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10 performance_schema
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10
test
-rw-r----- 1 mysql mysql 22 9月 5 22:10 xtrabackup_binlog_info
-rw-r--r-- 1 mysql mysql 33 9月 5 22:12 xtrabackup_binlog_pos_innodb
-rw-r----- 1 mysql mysql 113 9月 5 22:12 xtrabackup_checkpoints
-rw-r----- 1 mysql mysql 630 9月 5 22:10 xtrabackup_info
-rw-r----- 1 mysql mysql 8388608 9月 5 22:04 xtrabackup_logfile
drwxr-x--- 2 mysql mysql 4096 9月 5 22:10 xtr_test
重启数据库测试
[root@db01 full]
# cd /data/3306
[root@db01 3306]
# ./mysql start
Starting MySQL...
[root@db01 3306]
# netstat -lntup|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 56714
/mysqld
mysql>
select
* from testbackup;
+----+-------------------------+
|
id
| step |
+----+-------------------------+
| 1 | firstfull backup |
| 2 | firstincrement backup |
| 3 | secondincrement backup |
+----+-------------------------+
3 rows
in
set
(0.00 sec)
测试增量恢复正常,但是我们最后做的一次操作,没有出现,下面使用二进制binglog来恢复
2.6 binlog日志恢复数据
使用binlog恢复数据,我们需要来确定一个“范围”这里我们需要确定从第二次增量备份后到执行删除表的sql语句之前这个范围;查找范围,我们可以使用时间范围、位置点pos来取这个范围。
2.6.1 查找第二次增量的位置点
[root@db01 2016-09-05_21-18-30]
# catxtrabackup_binlog_info
mysql-bin.000010 1201
这里我们知道了第二次增量结束后的位置点事1201,我们将以这个位置点为起止位置点
2.6.2 查找删表语句时候的位置点
由于测试binlog日志文件少,这里很好查找,生产中要根据时间点来截取一段时间范围内,然后在去查找
[root@db01 3306]
# mysqlbinlog--base64-output=decode-rows mysql-bin.000010
# at 1376
#160905 21:46:41 server id 1 end_log_pos 1403 Xid = 164
COMMIT/*!*/;
# at 1403
#160905 21:46:59 server id 1 end_log_pos 1495 Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1473083219/*!*/;
truncate table testbackup
/*!*/;
# at 1495
#160905 21:52:16 server id 1 end_log_pos 1514 Stop
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
通过查找到语句后,我们找到了位置点,我们可以导出一个文件,通过vim编辑来搜索语句
2.6.3 binlog恢复数据
通过上面找出的起止点和终止点来恢复数据
mysqlbinlog --base64-output=decode-rows -
v
--start-position=1201 --stop-position=1403 mysql-bin.000010
来再次确认
恢复:
[root@db01 3306]
# mysqlbinlog --start-position=1201--stop-position=1403 mysql-bin.000010|mysql -uroot -p789 -S/data/3306/mysql.sock
数据中查看:
mysql>
select
* from testbackup;
+----+-------------------------+
|
id
| step |
+----+-------------------------+
| 1 | firstfull backup |
| 2 | firstincrement backup |
| 3 | secondincrement backup |
| 4 |last backup |
+----+-------------------------+
4 rows
in
set
(0.01 sec)
附录---Xtrabackup参数详解
Xtrabackup常用参数选项如下:
--slave-info:
它会记录master服务器的binary log的pos和name。会把记录的信息记录在xtrabackup_slave_info。在备份从库时,可以使用该参数, 加上--slave-info备份目录下会多生成一个xtrabackup_slave_info 文件, 这里会保存主日志文件以及偏移, 文件内容类似于:CHANGE MASTER TOMASTER_LOG_FILE='', MASTER_LOG_POS=0。
这个参数适用的场景:假设现在有主库A和从库B,目前想再添加一台备库C,并让备库C以主库A为master;因为主库A是生产库,压力一般比较大,所以我们就在备库B上备份一个数据库,然后把这个备份拿到C服务器上 并导入到C库,接下来再在C服务器上执行change master的命令:其中 master_host是A的ip,而master_log_file和master_log_pos就是这个xtrabackup_slave_info里面的值
--safe-salve-backup:
它会停止slave SQL 进程,等备份完后,重新打开slave的SQL进程
--force-tar --stream=tar /tmp
这些命令是用来压缩备份为tar文件。具体看官方文档
--defaults-file=#
默认配置文件的路径,如果不该参数,xtrabackup将从依次从以下位置查找配置文件/etc/my.cnf、/etc/mysql/my.cnf、/usr/local/etc/my.cnf、~/.my.cnf,并读取配置文件中的[mysqld]和[xtrabackup]配置段。[mysqld]中只需要指定datadir、innodb_data_home_dir、innodb_data_file_path、innodb_log_group_home_dir、innodb_log_files_in_group、innodb_log_file_size6个参数即可让xtrabackup正常工作。
--defaults-extra-file=#
如果使用了该参数,在读取了全局配置文件之后,会再读取这里指定的配置文件
--target-dir=name
备份文件的存放目录路径
--backup
实施备份到target-dir
--prepare
实施对备份文件进行恢复前的准备(生成InnoDB log file)
--print-param
打印备份或恢复时需要的参数
--use-memory=#
该参数在 prepare 的时候使用,控制prepare时innodb实例使用的内存量
--suspend-at-end
在target-dir目录下产生一个xtrabackup_suspended文件,将xtrabackup进程挂起,不停地将数据文件的变化同步到备份文件,直到用户手工删除xtrabackup_suspended文件
--throttle=#
每秒IO次数,限制backup时使用的I/O操作量,使备份对数据库正常业务的影响最小化
--log-stream
该参数在backup的时候使用,将xtrabackup_logfile的内容输出到标准输出,使用该参数时会自动使用suspend-at-end参数,innobackupex脚本的stream 模式会使用该参数。
--incremental-lsn=name
增量备份时只拷贝LSN比该参数指定值新的ibd pages,前次备份到了哪个LSN可以看前次备份集的xtrabackup_checkpoints文件
--incremental-basedir=name
该参数在backup的时候使用,备份比该参数指定位置的备份集新的idb pages
--incremental-dir=name
该参数在prepare的时候使用,指定prepare时产生的.delta 文件和日志文件的存放路径
--tables=name
在备份file-per-table类型的数据文件时使用,使用正则表达式指定需要备份的innodb表
--datadir=name
MySQL数据库的数据文件目录
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/142477.htm