千家信息网

mysql数据备份之 xtrabackup

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,上一篇简单介绍了一下mysqldump进行数据库的备份和恢复,这一篇说一下另一种备份工具xtrabackup,在InnoDB事务引擎泛滥的时代,xtrabackup可以很好的支持数据库的热备份,这就很
千家信息网最后更新 2025年01月23日mysql数据备份之 xtrabackup

上一篇简单介绍了一下mysqldump进行数据库的备份和恢复,这一篇说一下另一种备份工具xtrabackup,在InnoDB事务引擎泛滥的时代,xtrabackup可以很好的支持数据库的热备份,这就很讨人喜,

Xtrabackup在epel源中有,所以直接用yum安装即可;

完全备份就是直接连接MySQL服务,给定一个目标目录即可;

[root@www ~]# innobackupex  --user=root --host=localhost --port=3306 --password='123456'  /data/mydata181105 20:31:30 innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully.           At the end of a successful backup run innobackupex           prints "completed OK!".…181105 20:31:39 Executing UNLOCK TABLES181105 20:31:39 All tables unlocked181105 20:31:39 Backup created in directory '/data/mydata/2018-11-05_20-31-30'181105 20:31:39 [00] Writing backup-my.cnf181105 20:31:39 [00]        ...done181105 20:31:39 [00] Writing xtrabackup_info181105 20:31:39 [00]        ...donextrabackup: Transaction log of lsn (9424992) to (9424992) was copied.181105 20:31:39 completed OK!

然后我们就可以在备份的目录中看到几个文件

[root@www ~]# ls /data/mydata/2018-11-05_20-31-30/backup-my.cnf  ibdata1  performance_schema  xtrabackup_checkpoints  xtrabackup_logfilehellodb        mysql    wpsdb               xtrabackup_info         zabbix

除了数据库之外,还有几个特殊的文件:

xtrabackup_binlog_info:记录了整个备份过程中的一些属性信息;

[root@www ~]# cd /data/mydata/2018-11-05_20-31-30/[root@www 2018-11-05_20-31-30]# cat xtrabackup_infouuid = 725248b0-da18-11e8-9fcc-000c29ceaa48name =tool_name = innobackupextool_command = --user=root --host=localhost --port=3306 --password=... /data/mydatatool_version = 2.3.6ibbackup_version = 2.3.6server_version = 5.5.60-MariaDBstart_time = 2018-11-05 20:31:31end_time = 2018-11-05 20:31:39lock_time = 0binlog_pos =innodb_from_lsn = 0innodb_to_lsn = 9424992partial = Nincremental = Nformat = filecompact = Ncompressed = Nencrypted = N

backup-my.cnf:记录与InnoDB存储引擎相关的重要配置参数;

[root@www 2018-11-05_20-31-30]# cat backup-my.cnf# This MySQL options file was generated by innobackupex. # The MySQL server[mysqld]innodb_checksum_algorithm=innodbinnodb_log_checksum_algorithm=innodbinnodb_data_file_path=ibdata1:10M:autoextendinnodb_log_files_in_group=2innodb_log_file_size=5242880innodb_fast_checksum=falseinnodb_page_size=16384innodb_log_block_size=512innodb_undo_directory=.innodb_undo_tablespaces=0

xtrabackup_checkpoints:记录了此次的类型及起始和终止的LSN;

[root@www 2018-11-05_20-31-30]# cat xtrabackup_checkpointsbackup_type = full-backupedfrom_lsn = 0to_lsn = 9424992last_lsn = 9424992compact = 0recover_binlog_info = 0 xtrabackup_binlog_info:记录当前使用的二进制日志的一致性坐标;[root@www 2018-11-05_20-55-04]# cat xtrabackup_binlog_infobinlog.000001       245

完全备份的恢复(mysql的数据目录为空):

恢复数据的过程只需要添加一个--copy-back即可完成;

[root@www 2018-11-05_20-55-04]# innobackupex --copy-back ./181105 20:59:30 innobackupex: Starting the copy-back operation IMPORTANT: Please check that the copy-back run completes successfully.           At the end of a successful copy-back run innobackupex           prints "completed OK!".

增量备份:

相比于之前的完全备份,需要--incremental--incremental-basedir两个选项即可;

[root@www ~]# innobackupex --incremental /data/mydata --incremental-basedir=/data/mydata/2018-11-05_20-31-30181105 20:54:41 innobackupex: Starting the backup operation IMPORTANT: Please check that the backup run completes successfully.           At the end of a successful backup run innobackupex           prints "completed OK!".…181105 20:55:14 Executing UNLOCK TABLES181105 20:55:14 All tables unlocked181105 20:55:14 Backup created in directory '/data/mydata/2018-11-05_20-55-04'MySQL binlog position: filename 'binlog.000001', position '245'181105 20:55:14 [00] Writing backup-my.cnf181105 20:55:14 [00]        ...done181105 20:55:14 [00] Writing xtrabackup_info181105 20:55:14 [00]        ...donextrabackup: Transaction log of lsn (9424992) to (9424992) was copied.181105 20:55:14 completed OK!

通过查看xtrabackup-checkpoints文件内容即可;

[root@www 2018-11-05_20-55-04]# cat xtrabackup_checkpointsbackup_type = incrementalfrom_lsn = 9424992to_lsn = 9424992last_lsn = 9424992compact = 0recover_binlog_info = 0

增量备份的恢复过程同上,也是一个--copy-back即可;


xtrabackup是一个相当好的备份工具,在如今事务引擎遍地的时代,良好的热备份机制和简单化的操作,对我们来说很友好,所以,我很推荐这个工具,而mysqldump大多用来做测试用,因为mysqldump需要锁表,所以温备份机制在这方便不如xtrabackup的热备份机制。但是,在我们实验环境做测试的情况下,这个直接打包成数据库,copy到其他主机就可以释放,还是很不错的。


0