暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

基于percona xtrabackup之xtrabackup实现mysql增量备份及恢复数据库

lovedb 2019-11-05
280

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


采用 percona xtrabackup之命令innobackupex实现基于增量备份及恢复数据库





前言

在上文中,简要介绍了全量备份及恢复的操作过程。在真正的生产系统中,数据库是持续有数据变化的。因此除了全量备份,必须结合基于全量备份(或基准备份)增量备份,才能拐持续高效构建数据库的备份体系,增强数据库高可用性及应对故障的能力。

为何产生增量备份呢,有几个方面:

  • 减少每次备份的资源消耗,包括IO资源和网络资源和时间资源

  • 减少每次备份的空间占用,增量备份仅是备份自上次基准备份变化了数据


大家要是接触过oracle,percona xtrabackup的增量备份类似于rman,当然二者在实现机制是有差异的。可以参与oracle rman的概念学习percona xtrabackup.

   

大家可能在网上看到多半是基于percona xtrabackup之innobackupdex实现增量备份及恢复数据库的一些文章,这里全程采用percona xtrabackup之extrabackup实现增量备份及恢复数据库的详细操作,供大家参考。



操作步骤


1,数据库的当前数据

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)


2,执行全库备份

[root@standbygtid ~]# mkdir -p backup_full_dir

[root@standbygtid ~]# xtrabackup -uroot -psystem --backup --target-dir=/backup_full_dir


3,数据库变更

mysql> insert into zxydb.t_go select 11,11;

Query OK, 1 row affected (0.02 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> commit;

Query OK, 0 rows affected (0.00 sec)


4,执行第1次增量备份

[root@standbygtid ~]# mkdir -p backup_incr_dir/incr1

[root@standbygtid ~]# xtrabackup -uroot -psystem --backup --target-dir=/backup_incr_dir/incr1  --incremental-basedir=/backup_full_dir


5,数据库变更

mysql> insert into zxydb.t_go select 12,12;

Query OK, 1 row affected (0.02 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> commit;

Query OK, 0 rows affected (0.00 sec)


6,执行第2次增量备份

[root@standbygtid backup_incre_dir]# pwd

/backup_incre_dir

[root@standbygtid backup_incre_dir]# ll

总用量 156

-rw-r----- 1 root root    418 11月  4 23:08 backup-my.cnf

drwxr-x--- 2 root root   4096 11月  4 23:08 completedb

-rw-r----- 1 root root 114688 11月  4 23:08 ibdata1.delta

-rw-r----- 1 root root     44 11月  4 23:08 ibdata1.meta

drwxr-x--- 2 root root   4096 11月  4 23:08 mysql

drwxr-x--- 2 root root   4096 11月  4 23:08 performance_schema

drwxr-x--- 2 root root   4096 11月  4 23:08 test

-rw-r----- 1 root root     18 11月  4 23:08 xtrabackup_binlog_info

-rw-r----- 1 root root    120 11月  4 23:08 xtrabackup_checkpoints

-rw-r----- 1 root root    557 11月  4 23:08 xtrabackup_info

-rw-r----- 1 root root   2560 11月  4 23:08 xtrabackup_logfile

drwxr-x--- 2 root root   4096 11月  4 23:08 zxydb

[root@standbygtid backup_incre_dir]# mkdir -p backup_incr_dir/incr2

[root@standbygtid backup_incre_dir]# xtrabackup -uroot -psystem --backup --target-dir=/backup_incr_dir/incr2  --incremental-basedir=/backup_incr_dir/incr1

[root@standbygtid backup_incre_dir]# pwd

/backup_incre_dir

[root@standbygtid backup_incre_dir]# ll

总用量 160

-rw-r----- 1 root root    418 11月  4 23:08 backup-my.cnf

drwxr-x--- 2 root root   4096 11月  4 23:08 completedb

-rw-r----- 1 root root 114688 11月  4 23:08 ibdata1.delta

-rw-r----- 1 root root     44 11月  4 23:08 ibdata1.meta

drwxr-xr-x 7 root root   4096 11月  4 23:12 incr2

drwxr-x--- 2 root root   4096 11月  4 23:08 mysql

drwxr-x--- 2 root root   4096 11月  4 23:08 performance_schema

drwxr-x--- 2 root root   4096 11月  4 23:08 test

-rw-r----- 1 root root     18 11月  4 23:08 xtrabackup_binlog_info

-rw-r----- 1 root root    120 11月  4 23:08 xtrabackup_checkpoints

-rw-r----- 1 root root    557 11月  4 23:08 xtrabackup_info

-rw-r----- 1 root root   2560 11月  4 23:08 xtrabackup_logfile

drwxr-x--- 2 root root   4096 11月  4 23:08 zxydb

[root@standbygtid backup_incre_dir]# cd incr2

[root@standbygtid incr2]# ll

总用量 140

-rw-r----- 1 root root   418 11月  4 23:12 backup-my.cnf

drwxr-x--- 2 root root  4096 11月  4 23:12 completedb

-rw-r----- 1 root root 98304 11月  4 23:12 ibdata1.delta

-rw-r----- 1 root root    44 11月  4 23:12 ibdata1.meta

drwxr-x--- 2 root root  4096 11月  4 23:12 mysql

drwxr-x--- 2 root root  4096 11月  4 23:12 performance_schema

drwxr-x--- 2 root root  4096 11月  4 23:12 test

-rw-r----- 1 root root    18 11月  4 23:12 xtrabackup_binlog_info

-rw-r----- 1 root root   120 11月  4 23:12 xtrabackup_checkpoints

-rw-r----- 1 root root   564 11月  4 23:12 xtrabackup_info

-rw-r----- 1 root root  2560 11月  4 23:12 xtrabackup_logfile

drwxr-x--- 2 root root  4096 11月  4 23:12 zxydb


7,准备应用全量备份

--未准备应用全量备份前

[root@standbygtid incr2]# cd backup_full_dir/

[root@standbygtid backup_full_dir]# ll

总用量 77864

-rw-r----- 1 root root      418 11月  4 23:06 backup-my.cnf

drwxr-x--- 2 root root     4096 11月  4 23:06 completedb

-rw-r----- 1 root root 79691776 11月  4 23:06 ibdata1

drwxr-x--- 2 root root     4096 11月  4 23:06 mysql

drwxr-x--- 2 root root     4096 11月  4 23:06 performance_schema

drwxr-x--- 2 root root     4096 11月  4 23:06 test

-rw-r----- 1 root root       18 11月  4 23:06 xtrabackup_binlog_info

-rw-r----- 1 root root      115 11月  4 23:06 xtrabackup_checkpoints

-rw-r----- 1 root root      510 11月  4 23:06 xtrabackup_info

-rw-r----- 1 root root     2560 11月  4 23:06 xtrabackup_logfile

drwxr-x--- 2 root root     4096 11月  4 23:06 zxydb

准备应用全量备份

[root@standbygtid backup_full_dir]# xtrabackup -uroot -psystem --prepare --apply-log-only --target-dir=/backup_full_dir


准备应用全量备份后

(可见全量备份的数据变化了,大家注意日期及时间)

[root@standbygtid backup_full_dir]# ll

总用量 86056

-rw-r----- 1 root root      418 11月  4 23:06 backup-my.cnf

drwxr-x--- 2 root root     4096 11月  4 23:06 completedb

-rw-r----- 1 root root 79691776 11月  4 23:15 ibdata1   --变化,系统表空间,数据字典

drwxr-x--- 2 root root     4096 11月  4 23:06 mysql

drwxr-x--- 2 root root     4096 11月  4 23:06 performance_schema

drwxr-x--- 2 root root     4096 11月  4 23:06 test

-rw-r----- 1 root root       18 11月  4 23:06 xtrabackup_binlog_info

-rw-r--r-- 1 root root       18 11月  4 23:15 xtrabackup_binlog_pos_innodb   --增加了这个文件

-rw-r----- 1 root root      113 11月  4 23:15 xtrabackup_checkpoints   --变化

-rw-r----- 1 root root      510 11月  4 23:06 xtrabackup_info

-rw-r----- 1 root root  8388608 11月  4 23:15 xtrabackup_logfile  --变化

drwxr-x--- 2 root root     4096 11月  4 23:06 zxydb


8,准备应用第1次增量备份

[root@standbygtid backup_full_dir]# xtrabackup -uroot -psystem --prepare --apply-log-only --target-dir=/backup_full_dir  --incremental-dir=/backup_incr_dir/incr1


9,准备应用第2次增量备份(最后一次增量备份)

[root@standbygtid backup_full_dir]# xtrabackup -uroot -psystem --prepare  --target-dir=/backup_full_dir  --incremental-dir=/backup_incr_dir/incr2


10,关闭数据库

mysqladmin -uroot -psystem shutdown


11,物理删除数据库的数据文件

[root@standbygtid backup_full_dir]# cd var/lib/mysql

[root@standbygtid mysql]# rm -rf *


12,把上述已经应用过2次增量备份的全量备份(一个一致性的数据库备份),恢复到数据库的数据文件目录

[root@standbygtid mysql]# xtrabackup  --defaults-file=/tmp/my.cnf  --copy-back --target-dir=/backup_full_dir/


13,删除数据库的数据文件目录中与xtrabackup相关的文件

(注:因为这些是xtrabackup相关的中间件)

[root@standbygtid mysql]# ll

总用量 188444

drwxr-x--- 2 root root     4096 11月  4 23:48 completedb

-rw-r----- 1 root root 79691776 11月  4 23:48 ibdata1

-rw-r----- 1 root root 50331648 11月  4 23:48 ib_logfile0

-rw-r----- 1 root root 50331648 11月  4 23:48 ib_logfile1

-rw-r----- 1 root root 12582912 11月  4 23:48 ibtmp1

drwxr-x--- 2 root root     4096 11月  4 23:48 mysql

drwxr-x--- 2 root root     4096 11月  4 23:48 performance_schema

drwxr-x--- 2 root root     4096 11月  4 23:48 test

-rw-r----- 1 root root       19 11月  4 23:48 xtrabackup_binlog_pos_innodb  --需要删除

-rw-r----- 1 root root      569 11月  4 23:48 xtrabackup_info  --需要删除

drwxr-x--- 2 root root     4096 11月  4 23:48 zxydb

[root@standbygtid mysql]# rm -rf xtrabackup_*



14,变更数据库目录权限

[root@standbygtid mysql]# pwd

/var/lib/mysql

[root@standbygtid mysql]# chown -Rf mysql:mysql *


15,重启数据库

nohup mysqld_safe  --user=mysql&


16,验证数据库

mysql> select * from zxydb.t_go;

+----+------+

| a  | b    |

+----+------+

|  1 |    1 |

|  2 |    2 |

|  3 |    3 |

|  5 |    5 |

|  8 |    8 |

| 10 |   10 |

| 11 |   11 |

| 12 |   12 |

+----+------+

8 rows in set (0.00 sec)





数据库水平提升

(收费20元,可通过文末联系方式与我联系

相关文件

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

http://blog.itpub.net/9240380/viewspace-2662613/


mysql 5.6备份工具mysqldump在生产环境的使用思考



联系方式


文章转载自lovedb,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论