xtrabackup的使用
Posted 步行者811
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xtrabackup的使用相关的知识,希望对你有一定的参考价值。
xtrabackup 包含两个工具 xtrabackup 和innobackupex。
->xtrabackup只能备份innodb和xtradb两种引擎的表,而不能备份myisam引擎的表。
->innobackupex能同时备份innodb和myisam,但对myisam备份时需要加一个全局读锁,增量备份主要是针对innodb而言,对myisam其他存储引擎,innobackupex仍然是一个全拷贝。
innobackupex是一个封装了xtrabackup的 Perl 脚本。
======================================以下是innobackupex的备份,恢复,增量备份恢复的原理===========================================
->innobackupex的备份过程
备份开始时首先会开启一个后台检测进程,实时检测mysql redo的变化,一旦发现redo中有新的日志写入,立刻将日志记入后台日志文件xtrabackup_log中。之后复制innodb的数据文件和系统表空间文件ibdata1,待复制结束后,执行flush tables with read lock操作,复制.frm,MYI,MYD,等文件(执行flush tableswith read lock的目的是为了防止数据表发生DDL操作,并且在这一时刻获得binlog的位置)最后会发出unlock tables,把表设置为可读可写状态,最终停止xtrabackup_log。
->全备恢复
==========================================================================================================================================
进行全备: 步骤:
1.首先创建好存放备份文件的目录,如:/opt
2.root权限下执行: innobackupex -uroot -p123lqh /opt
->备份后无 information_schema表。这是mysql的系统库,不需要备份。 现在可以去/opt目录下查看备份的文件,以时间来命名文件
[root@localhost /]# cd /opt
[root@localhost opt]# ls
[root@localhost opt]# cd 2017-08-31_01-42-08
[root@localhost 2017-08-31_01-42-08]# ll
total 512076
-rw-r----- 1 root root 426 Aug 31 01:43 backup-my.cnf
drwxr-x--- 2 root root 4096 Aug 31 01:42 dbaconfig
drwxr-x--- 2 root root 4096 Aug 31 01:42 dbtest
-rw-r----- 1 root root 7552 Aug 31 01:43 ib_buffer_pool
-rw-r----- 1 root root 524288000 Aug 31 01:42 ibdata1
drwxr-x--- 2 root root 4096 Aug 31 01:42 leochentest
drwxr-x--- 2 root root 4096 Aug 31 01:43 linqiuhua_test
drwxr-x--- 2 root root 4096 Aug 31 01:43 mysql
drwxr-x--- 2 root root 4096 Aug 31 01:42 oldboy
drwxr-x--- 2 root root 4096 Aug 31 01:43 performance_schema
drwxr-x--- 2 root root 4096 Aug 31 01:42 procedure_test01
drwxr-x--- 2 root root 12288 Aug 31 01:42 sys
drwxr-x--- 2 root root 4096 Aug 31 01:42 wang
-rw-r----- 1 root root 25 Aug 31 01:43 xtrabackup_binlog_info
-rw-r----- 1 root root 117 Aug 31 01:43 xtrabackup_checkpoints #记录LSN号文件
-rw-r----- 1 root root 455 Aug 31 01:43 xtrabackup_info
-rw-r----- 1 root root 2560 Aug 31 01:43 xtrabackup_logfile
[root@localhost 2017-08-31_01-42-08]# cat xtrabackup_checkpoints
backup_type = full-backuped #备份类型为全备
from_lsn = 0 #起始LSN号(由于是全备所以起始LSN号为0开始)
to_lsn = 383583464 #截止LSN号
last_lsn = 383583473
compact = 0
recover_binlog_info = 0
破坏数据
-->现在全备已经完成了,我们来模拟破坏数据,然后恢复数据。
首先停掉数据库
[root@localhost mysql]# mysqladmin -uroot -p123lqh -S /data/mysql/data/mysql.sock shutdown 关闭数据库 也可以用 service mysql stop
[root@localhost mysql]# mv /data/mysql/data/ /data/mysql/data_bak/ 对数据库中存放的数据库信息进行移动
[root@localhost mysql]# mkdir -p /data/mysql/data #必须创建一个新的空data文件夹用来恢复,不然恢复会报错
-->此时我们在开启数据库,发现数据库文件被破坏无法启动了
[root@localhost /]# mysqld_safe --defaults-file=/data/3306/my.cnf &
WARNING: Defaults file \'/data/3306/my.cnf\' not found!
170831 02:14:01 mysqld_safe Logging to \'/data/mysql/data/localhost.localdomain.err\'.
170831 02:14:01 mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
170831 02:14:01 mysqld_safe mysqld from pid file /data/mysql/data/localhost.localdomain.pid ended
准备全量恢复
[root@localhost mysql]# innobackupex --defaults-file=/etc/my.cnf -uroot -p123lqh --apply-log /opt/2017-08-31_01-42-08/
InnoDB: 5.7.13 started; log sequence number 383586837
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 383588268
170831 05:07:31 completed OK!
#这里的apply-log参数意思就是在真正恢复数据库数据之前,先将数据库redo日志状态恢复
-->接下来才是将数据文件恢复
首先: 删除arch 里面的文件 ib_logfile0 和 ib_logfile1
[root@localhost mysql]# innobackupex --defaults-file=/etc/my.cnf -uroot --copy-back /opt/2017-08-31_01-42-08/
170831 06:03:20 [01] Copying ./linqiuhua_test/auth_user.frm to /data/mysql/data/linqiuhua_test/auth_user.frm
170831 06:03:20 [01] ...done
170831 06:03:20 [01] Copying ./linqiuhua_test/slow_sql_xczx2.frm to /data/mysql/data/linqiuhua_test/slow_sql_xczx2.frm
170831 06:03:20 [01] ...done
170831 06:03:20 [01] Copying ./xtrabackup_info to /data/mysql/data/xtrabackup_info
170831 06:03:20 [01] ...done
170831 06:03:20 completed OK!
再更改数据文件属组,启动数据库成功
[root@localhost mysql]#chown -R mysql.mysql data
[root@localhost mysql]#chown -R mysql.mysql arch
cd data
[root@localhost data]# service mysql start 启动数据库
[root@localhost ~]# mysql -uroot -p123lqh -S /data/mysql/data/mysql.sock 连接数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbaconfig |
| dbtest |
| leochentest |
| linqiuhua_test |
| mysql |
| oldboy |
| performance_schema |
| procedure_test01 |
| sys |
| wang |
+--------------------+
11 rows in set (0.03 sec) #数据表恢复成功
===========================================================================================================================================
增量备份: 增量备份,前提是之前至少有一次全备份,否则报错。 增量备份要与之前全备的目录要一致,否则报错。
1.先在数据库中插入几条数据
+---------+--------+
| address | num |
+---------+--------+
| 1 | 111 |
| 2 | 222 |
| 3 | 333 |
| 4 | 444 |
| 9 | 999 | #插入的数据
| 10 | 101010 |
| 11 | 111111 |
+---------+--------+
2.创建目录存放增量的文件 比如 /opt/add
[root@localhost /]# innobackupex --defaults-file=/etc/my.cnf -uroot -p123lqh --incremental /opt/add --incremental-basedir=/opt/2017-08-31_01-42-08/
增量备份目录 指定之前全备份目录
170831 07:54:43 Executing UNLOCK TABLES
170831 07:54:43 All tables unlocked
170831 07:54:43 [00] Copying ib_buffer_pool to /opt/add/2017-08-31_07-54-27/ib_buffer_pool
170831 07:54:43 [00] ...done
170831 07:54:43 Backup created in directory \'/opt/add/2017-08-31_07-54-27/\'
MySQL binlog position: filename \'dbnoe05-bin.000023\', position \'440\'
170831 07:54:43 [00] Writing /opt/add/2017-08-31_07-54-27/backup-my.cnf
170831 07:54:43 [00] ...done
170831 07:54:43 [00] Writing /opt/add/2017-08-31_07-54-27/xtrabackup_info
170831 07:54:43 [00] ...done
xtrabackup: Transaction log of lsn (383590366) to (383590375) was copied.
170831 07:54:43 completed OK!
[root@localhost /]# cd /opt/add
[root@localhost add]# cd 2017-08-31_07-54-27
[root@localhost 2017-08-31_07-54-27]# ls
[root@localhost 2017-08-31_07-54-27]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 383583464 #增量备份的起始LSN号是靠读取全备xtrabackup_checkpoints文件得到的
to_lsn = 383590366 #增量结束LSN号
last_lsn = 383590375
compact = 0
recover_binlog_info = 0
增量恢复
增量恢复和全量恢复有着一些不同,需要注意的是:
1)需要在每个备份(包括完全备份和增量备份)上,将已经提交的事务进行“重放”,重放之后,所有的备份数据将合并到完全备份上;
2)基于所有备份将未提交的事务进行“回滚”
关闭数据库
[root@localhost 2017-08-31_07-54-27]# service mysql stop 在任何目录下执行都可以
Shutting down MySQL...... SUCCESS!
执行完全备份的前滚
[root@localhost 2017-08-31_07-54-27]# innobackupex --defaults-file=/etc/my.cnf -uroot -p123lqh --apply-log --redo-only /opt/2017-08-31_01-42-08/
InnoDB: Highest supported file format is Barracuda.
InnoDB: xtrabackup: Last MySQL binlog file position 10247, file name dbnoe05-bin.000022
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 383588277
InnoDB: Number of pools: 1
170831 08:27:09 completed OK!
##--redo-only 在做增量恢复时,全量和增量备份的数据文件在恢复前必须将在重做日志文件中的已提交的事务前滚!此参数将会合并全备和增量备份的数据文件,但不包括最后一次增量备份的数据文件
将全备和增备合并
[root@localhost add]# innobackupex --defaults-file=/etc/my.cnf -uroot -p123lqh --apply-log /opt/2017-08-31_01-42-08/ --incremental-dir=/opt/add/2017-08-31_07-54-27
## /opt/2017-08-31_01-42-08/全备目录
##--incremental-dir=/opt/add/2017-08-31_07-54-27增量目录
##--redo-only 只有一次增备,所以不需要加这个参数,如果有N次增备,依次恢复增备,除了最后一个每次都要加 --redo-only的参数
InnoDB: Page [page id: space=13, page number=3] log sequence number 383589022 is in the future! Current system log sequence number 383588296.
InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 383590391
170831 08:37:34 completed OK!
破坏数据库,将全备和增备恢复回来
[root@localhost mysql]# rm -rf data_bak
[root@localhost mysql]# mv data data_bak
[root@localhost mysql]# cd arch
[root@localhost arch]# rm -rf ib_logfile0 ib_logfile1
[root@localhost mysql]# innobackupex --defaults-file=/etc/my.cnf --copy-back /opt/2017-08-31_01-42-08/
170831 08:47:04 [01] Copying ./linqiuhua_test/auth_user.frm to /data/mysql/data/linqiuhua_test/auth_user.frm
170831 08:47:04 [01] ...done
170831 08:47:04 [01] Copying ./linqiuhua_test/slow_sql_xczx2.frm to /data/mysql/data/linqiuhua_test/slow_sql_xczx2.frm
170831 08:47:04 [01] ...done
170831 08:47:04 [01] Copying ./xtrabackup_info to /data/mysql/data/xtrabackup_info
170831 08:47:04 [01] ...done
170831 08:47:04 completed OK!
赋权限:
[root@localhost mysql]# chown -R mysql:mysql data
[root@localhost mysql]# chown -R mysql:mysql arch
启动数据库并登陆数据库
[root@localhost mysql]# service mysql start
[root@localhost mysql]# mysql -uroot -p123lqh
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbaconfig |
| dbtest |
| leochentest |
| linqiuhua_test |
| mysql |
| oldboy |
| performance_schema |
| procedure_test01 |
| sys |
| wang |
+--------------------+
11 rows in set (0.07 sec)
mysql> use oldboy;
Database changed
mysql> select * from consumer;
+---------+--------+
| address | num |
+---------+--------+
| 1 | 111 |
| 2 | 222 |
| 3 | 333 |
| 4 | 444 |
| 9 | 999 | # 数据恢复
| 10 | 101010 |
| 11 | 111111 |
+---------+--------+
7 rows in set (0.06 sec)
======================================================================================================================================================================
其他一些备份示例
指定数据库备份
1.先建存放备份文件的目录 比如:/backup/each/
2.[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123lqh --databases="oldboy" /backup/each/
指定表备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123lqh --databases="oldboy concumer" /backup/each/
以压缩格式备份
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123lqh --stream=tar /backup/full/|gzip>/backup/full/back_`date +%F`.tar.gz
如果启动报错,则查看是否赋予权限或者查看错误日志,即
tail -f dbnode05.err
如果是启动了双实例端口,则 kill -9 端口号
双实例有双实例的关闭方法,service mysql stop 是关闭默认的端口号,双实例的端口是不会关的
以上是关于xtrabackup的使用的主要内容,如果未能解决你的问题,请参考以下文章