基于percona xtrabackup 2.4.14的增量备份恢复还原mysql 5.6

来源:这里教程网 时间:2026-03-01 12:47:01 作者:

使用percona xtrabackup 2.4.14备份工具备份mysql 5.6并进行恢复

https://mp.weixin.qq.com/s?__biz=MzIwMjU2MjI1OQ==&mid=2247484161&idx=1&sn=4c4d0f4a6f134505fa38c5cfbabc4989&chksm=96dd8cbda1aa05aba6b21505aecaa21bfeef16fb351ee77a3dd8f0b450dae092d6d2ecc6c867&token=274104940&lang=zh_CN#rd 由于生产系统数据库是持续有业务变更操作,于是引入这篇文章,具体见下:

1,数据库的数据

mysql> use zxydb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t_go; +---+------+ | a | b    | +---+------+ | 1 |    1 | | 2 |    2 | | 3 |    3 | | 5 |    5 | +---+------+ 4 rows in set (0.00 sec) 2,首次全量备份 [root@standbygtid back_full_dir]# mkdir -p /backfup_full_dir 3, 首次全量备份后变更数据库 mysql> use zxydb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> insert into t_go select 8,8; Query OK, 1 row affected (0.01 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from t_go; +---+------+ | a | b    | +---+------+ | 1 |    1 | | 2 |    2 | | 3 |    3 | | 5 |    5 | | 8 |    8 | +---+------+ 5 rows in set (0.00 sec) 4,基于首次全量备份进行第1次增量备份 --获取全量备份时的日志文件的最后的LSN [root@standbygtid back_full_dir]# cd /backup_full_dir/ [root@standbygtid backup_full_dir]# more xtrabackup_checkpoints  backup_type = full-prepared from_lsn = 0 to_lsn = 28517559 last_lsn = 28517559 compact = 0 recover_binlog_info = 0 [root@standbygtid backup_full_dir]#  --构建增量备份的目录 [root@standbygtid backup_full_dir]# mkdir -p /backup_incre_dir --incremental选项后跟的值为存储增量备份的目录 --incremental-basedir选项后跟的为构建增量备份的基准备份(就是说,这个是前者备份的的基础) [root@standbygtid backup_full_dir]# innobackupex --defaults-file=/usr/my.cnf   -uroot -psystem --incremental /backup_incre_dir  --incremental-basedir=/backup_full_dir 5,继续进行数据库变更 mysql> insert into zxydb.t_go select 10,10; Query OK, 1 row affected (0.02 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from zxydb.t_go; +----+------+ | a  | b    | +----+------+ |  1 |    1 | |  2 |    2 | |  3 |    3 | |  5 |    5 | |  8 |    8 | | 10 |   10 | +----+------+ 6 rows in set (0.00 sec) 6,基于上述第一次增量备份为基准,进行第二次增量备份 --同上理,获取上次备份的最后的日志文件的LSN [root@standbygtid backup_full_dir]# cd /backup_incre_dir/ [root@standbygtid backup_incre_dir]# ll 总用量 4 drwxr-x--- 7 root root 4096 11月  4 17:53 2019-11-04_17-53-05 [root@standbygtid backup_incre_dir]# cd 2019-11-04_17-53-05/ [root@standbygtid 2019-11-04_17-53-05]# ll 总用量 156 -rw-r----- 1 root root    418 11月  4 17:53 backup-my.cnf drwxr-x--- 2 root root   4096 11月  4 17:53 completedb -rw-r----- 1 root root 114688 11月  4 17:53 ibdata1.delta -rw-r----- 1 root root     44 11月  4 17:53 ibdata1.meta drwxr-x--- 2 root root   4096 11月  4 17:53 mysql drwxr-x--- 2 root root   4096 11月  4 17:53 performance_schema drwxr-x--- 2 root root   4096 11月  4 17:53 test -rw-r----- 1 root root     18 11月  4 17:53 xtrabackup_binlog_info -rw-r----- 1 root root    120 11月  4 17:53 xtrabackup_checkpoints -rw-r----- 1 root root    579 11月  4 17:53 xtrabackup_info -rw-r----- 1 root root   2560 11月  4 17:53 xtrabackup_logfile drwxr-x--- 2 root root   4096 11月  4 17:53 zxydb [root@standbygtid 2019-11-04_17-53-05]# more xtrabackup_checkpoints  backup_type = incremental from_lsn = 28517559 to_lsn = 28518260 last_lsn = 28518260 compact = 0 recover_binlog_info = 0 [root@standbygtid 2019-11-04_17-53-05]#  [root@standbygtid 2019-11-04_17-53-05]# innobackupex --defaults-file=/usr/my.cnf   -uroot -psystem --incremental /backup_incre_dir  --incremental-basedir=/backup_incre_dir/2019-11-04_17-53-05 7,可见 第2次增量备份是基于第1次增量备份 [root@standbygtid backup_incre_dir]# pwd /backup_incre_dir [root@standbygtid backup_incre_dir]# ll 总用量 12 drwxr-x--- 7 root root 4096 11月  4 17:53 2019-11-04_17-53-05 drwxr-x--- 7 root root 4096 11月  4 17:59 2019-11-04_17-59-40 [root@standbygtid backup_incre_dir]# cd 2019-11-04_17-59-40 [root@standbygtid 2019-11-04_17-59-40]# ll 总用量 140 -rw-r----- 1 root root   418 11月  4 17:59 backup-my.cnf drwxr-x--- 2 root root  4096 11月  4 17:59 completedb -rw-r----- 1 root root 98304 11月  4 17:59 ibdata1.delta -rw-r----- 1 root root    44 11月  4 17:59 ibdata1.meta drwxr-x--- 2 root root  4096 11月  4 17:59 mysql drwxr-x--- 2 root root  4096 11月  4 17:59 performance_schema drwxr-x--- 2 root root  4096 11月  4 17:59 test -rw-r----- 1 root root    18 11月  4 17:59 xtrabackup_binlog_info -rw-r----- 1 root root   120 11月  4 17:59 xtrabackup_checkpoints -rw-r----- 1 root root   600 11月  4 17:59 xtrabackup_info -rw-r----- 1 root root  2560 11月  4 17:59 xtrabackup_logfile drwxr-x--- 2 root root  4096 11月  4 17:59 zxydb [root@standbygtid 2019-11-04_17-59-40]#  从下可知,第1次增量备份和第2次增量备份的数据就连上了,因为日志文件的LSN是连接上了 [root@standbygtid 2019-11-04_17-59-40]# more xtrabackup_checkpoints backup_type = incremental from_lsn = 28518260 --这个就是上次备份最后的LSN to_lsn = 28518547 last_lsn = 28518547   compact = 0 recover_binlog_info = 0 [root@standbygtid 2019-11-04_17-59-40]#  8,下来测试下 数据库损坏了,看可否基于上述的增量备份把数据完整的还原恢复出来 --数据库正在运行,直接把数据文件目录中内容全部删除掉 [root@standbygtid 2019-11-04_17-59-40]# cd /var/lib/mysql [root@standbygtid mysql]# ll 总用量 188500 -rw-rw---- 1 mysql mysql       56 11月  4 15:53 auto.cnf -rw-rw---- 1 mysql mysql      143 11月  4 15:53 binlog.000001 -rw-rw---- 1 mysql mysql      143 11月  4 17:42 binlog.000002 -rw-rw---- 1 mysql mysql      143 11月  4 17:43 binlog.000003 -rw-rw---- 1 mysql mysql      558 11月  4 17:56 binlog.000004 -rw-rw---- 1 mysql mysql       64 11月  4 17:45 binlog.index drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 completedb -rw-r----- 1 mysql mysql 79691776 11月  4 17:56 ibdata1 -rw-r----- 1 mysql mysql 50331648 11月  4 17:56 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 11月  4 15:53 ib_logfile1 -rw-r----- 1 mysql mysql 12582912 11月  4 15:53 ibtmp1 drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 mysql srwxrwxrwx 1 mysql mysql        0 11月  4 17:45 mysql.sock -rw------- 1 mysql mysql      649 11月  4 17:45 nohup.out drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 performance_schema -rw-r----- 1 mysql mysql    19245 11月  4 17:45 standbygtid.err -rw-rw---- 1 mysql mysql        5 11月  4 17:45 standbygtid.pid drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 test drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 xtrabackup_backupfiles -rw-r----- 1 mysql mysql       23 11月  4 15:53 xtrabackup_binlog_pos_innodb -rw-r----- 1 mysql mysql      544 11月  4 15:53 xtrabackup_info drwxr-x--- 2 mysql mysql     4096 11月  4 15:53 zxydb [root@standbygtid mysql]# rm -rf * 9,首先进行全量恢复,但注意须添加选项参数 --apply-log  只前滚已经提交的事务,并不回滚那些未提交的事务(注:因为这个时侯数据库是处于一个中间状态,可能有提交也有不提交了的事务) [root@standbygtid mysql]# innobackupex --defaults--file=/usr/my.cnf -uroot --psystem --apply-log --redo-only /backup_full_dir 10,再次进行第一次增量备份的恢复 选项参数同上, --read-only 后跟值为 全量备份目录 --incremental-dir后跟 第一次增量备份目录 [root@standbygtid mysql]# innobackupex --defaults--file=/usr/my.cnf -uroot --psystem --apply-log --redo-only /backup_full_dir  --incremental-dir=/backup_incre_dir/2019-11-04_17-53-05 11,继续进行第2次增量备份的恢复 [root@standbygtid mysql]# innobackupex --defaults--file=/usr/my.cnf -uroot --psystem --apply-log --redo-only /backup_full_dir  --incremental-dir=/backup_incre_dir/2019-11-04_17-59-40 12,最后进行整个数据库的恢复 最后一次整个数据库的恢复,不用上述的选项参数 --read-only,表明 回滚未提交的事务 [root@standbygtid mysql]# innobackupex --defaults--file=/usr/my.cnf -uroot --psystem --apply-log  /backup_full_dir 13,关闭数据库 14,物理复制已经已经恢复完毕全量备库目录的内容到 数据库的数据目录/var/lib/mysql [root@standbygtid backup_full_dir]# pwd /backup_full_dir [root@standbygtid backup_full_dir]#  [root@standbygtid backup_full_dir]# cp -Rf * /var/lib/mysql [root@standbygtid backup_full_dir]# chown -Rf mysql:mysql /var/lib/mysql ---清除数据文件目录/var/lib/mysql中的与xtrabackup相关的一些临时文件 [root@standbygtid backup_full_dir]# cd /var/lib/mysql [root@standbygtid mysql]# rm -rf xtrabackup_* [root@standbygtid mysql]# nohup mysqld_safe  --user=mysql& 15,可见基于增量备份的数据库恢复正常 mysql> select * from zxydb.t_go; +----+------+ | a  | b    | +----+------+ |  1 |    1 | |  2 |    2 | |  3 |    3 | |  5 |    5 | |  8 |    8 | | 10 |   10 | +----+------+ 6 rows in set (0.00 sec) 联系方式

相关推荐