Xtrabackup 全备和还原以及增量备份和还原
本文继Xtrabackup安装和参数详解,本次介绍
全备和还原以及增量备份和还原
MySQL环境介绍
阿里云云主机
系统信息以及数据库版本,数据存放目录具体如下:
[root@node ~]# uname -r
4.18.16-1.el7.elrepo.x86_64
[root@node ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@node ~]# /opt/mysql/bin/mysql -V
/opt/mysql/bin/mysql Ver 14.14 Distrib 5.6.39, for linux-glibc2.12 (x86_64) using EditLine wrapper
[root@node ~]# ls /opt/mysql/data/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
[root@node ~]# ls -ld /opt/mysql/my.cnf
-rw-r--r-- 1 mysql mysql 1468 Dec 6 21:37 /opt/mysql/my.cnf
全备和还原
准备备份目录
[root@node ~]# cd /opt/
[root@node opt]# ls
mysql mysql-5.6 src
[root@node opt]# mkdir backup/{full,incr} -p
[root@node opt]# ls backup/
full incr
- full: 文件中存放全量备份
- incr: 文件中存放增量备份
创建测试数据
创建一个测试数据库,库名为:xtra_test
在xtra_test
库中,创建一个friends
表,字段只有id和name,并插入一条数据
具体操作如下:
[root@node opt]# mysql -uroot -p234567
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database xtra_test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| xtra_test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use xtra_test;
Database changed
mysql> create table friends (
-> id int(3) not null,
-> name varchar(8) not null
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------------+
| Tables_in_xtra_test |
+---------------------+
| friends |
+---------------------+
1 row in set (0.00 sec)
mysql> desc friends;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(3) | NO | | NULL | |
| name | varchar(8) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> INSERT INTO friends( id, name ) VALUES ( 1, 'jack' ) ;
Query OK, 1 row affected (0.00 sec)
mysql> select * from friends;
+----+------+
| id | name |
+----+------+
| 1 | jack |
+----+------+
1 row in set (0.00 sec)
全量备份
这里的具体参数含义请查看:Xtrabackup安装和参数详解
[root@node opt]# innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=234567 /opt/backup/full/
......
181211 11:18:44 [01] ...done
181211 11:18:44 Finished backing up non-InnoDB tables and files
181211 11:18:44 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '1645416'
xtrabackup: Stopping log copying thread.
.181211 11:18:44 >> log scanned up to (1645416)
181211 11:18:44 Executing UNLOCK TABLES
181211 11:18:44 All tables unlocked
181211 11:18:44 Backup created in directory '/opt/backup/full/2018-12-11_11-18-42/'
181211 11:18:44 [00] Writing /opt/backup/full/2018-12-11_11-18-42/backup-my.cnf
181211 11:18:44 [00] ...done
181211 11:18:44 [00] Writing /opt/backup/full/2018-12-11_11-18-42/xtrabackup_info
181211 11:18:44 [00] ...done
xtrabackup: Transaction log of lsn (1645416) to (1645416) was copied.
181211 11:18:44 completed OK!
最后提示 OK
了,查看备份目录中
[root@node opt]# ls /opt/backup/full/
2018-12-11_11-18-42
[root@node opt]# ls /opt/backup/full/2018-12-11_11-18-42/
backup-my.cnf ibdata1 mysql performance_schema xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_info xtrabackup_logfile xtra_test
备份完后,备份目录下有几个文件需要注意:
xtrabackup_binlog_info
记录了binlog的position,若开启了GTID,也会将GTID取出。在用于备份+binlog恢复或建立slave的场景里十分有用
注意 如果MySQL没有开启log-bin
参数,则不会生产次文件
backup-my.cnf
记录了备份时可能涉及到的选项参数,比如系统表空间信息,独立undo表空间信息,redo-log信息等
xtrabackup_checkpoints
记录了此次备份的类型和lsn号的起始值,是否压缩等
xtrabackup_info
记录了备份工具的信息,时间,备份对象(是针对全实例还是某库表),是否是增量,binlog位置等
模拟删除数据
直接删除
xtra_test
数据库;
此操作为模拟,请勿真是在线上操作
[root@node opt]# mysql -uroot -p234567
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> drop database xtra_test;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
还原数据操作
还原步骤:
- 先把xtrabackup备份下来的目录进行再次备份,防止在还原的时候误操作,导致备份不可用;
- 关闭要回复的数据库实例;
- 移除需要恢复数据的MySQL实例下的data目录,建议
mv
移动到别的目录下; - 使用
--apply-log
参数进行恢复前准备操作,如事物回滚等操作; - 使用
--copy-back
和--rsync
参数进行恢复; - 授权MySQL实例下的data目录后,启动数据库
- 验证恢复数据
第一步 备份备份文件
[root@node ~]# cd /opt/backup/full/
[root@node full]# ls
2018-12-11_14-04-15
[root@node full]# cp -a 2018-12-11_14-04-15 2018-12-11_14-04-15_back
[root@node full]# ll
total 8
drwxr-x--- 5 root root 4096 Dec 11 14:04 2018-12-11_14-04-15
drwxr-x--- 5 root root 4096 Dec 11 14:04 2018-12-11_14-04-15_back
第二步 关闭数据库
[root@node full]# /etc/init.d/mysqld stop
Shutting down MySQL..
[root@node full]# ps aux|grep mysql
root 19368 0.0 0.1 112720 2232 pts/1 S+ 15:01 0:00 grep --color=auto mysql
[root@node full]# netstat -lntup | grep 3306
第三步 移除数据库的data目录
[root@node full]# mv /opt/mysql/data /tmp/
[root@node full]# ls -ld /opt/mysql/data
ls: cannot access /opt/mysql/data: No such file or directory
[root@node full]# ls /tmp/data/
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql mysql-bin.000001 mysql-bin.index performance_schema
第四步 恢复前准备
[root@node full]# innobackupex --defaults=/opt/mysql/my.cnf --apply-log /opt/backup/full/2018-12-11_14-04-15/
.......
InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=1654036
InnoDB: Highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 1654284
InnoDB: Doing recovery: scanned up to log sequence number 1654293 (0%)
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: xtrabackup: Last MySQL binlog file position 635, file name mysql-bin.000001
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.7.19 started; log sequence number 1654293
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1654312
181211 14:42:07 completed OK!
[root@node full]# ls 2018-12-11_14-04-15/ -l
total 294956
-rw-r----- 1 root root 481 Dec 11 14:04 backup-my.cnf
-rw-r----- 1 root root 12582912 Dec 11 14:42 ibdata1
-rw-r----- 1 root root 134217728 Dec 11 14:42 ib_logfile0
-rw-r----- 1 root root 134217728 Dec 11 14:42 ib_logfile1
-rw-r----- 1 root root 12582912 Dec 11 14:42 ibtmp1
drwxr-x--- 2 root root 4096 Dec 11 14:04 mysql
drwxr-x--- 2 root root 4096 Dec 11 14:04 performance_schema
-rw-r----- 1 root root 21 Dec 11 14:04 xtrabackup_binlog_info
-rw-r--r-- 1 root root 21 Dec 11 14:42 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 113 Dec 11 14:42 xtrabackup_checkpoints
-rw-r----- 1 root root 509 Dec 11 14:04 xtrabackup_info
-rw-r----- 1 root root 8388608 Dec 11 14:42 xtrabackup_logfile
-rw-r--r-- 1 root root 1 Dec 11 14:42 xtrabackup_master_key_id
drwxr-x--- 2 root root 4096 Dec 11 14:04 xtra_test
注意文件时间的变化,说明准备全备文件的操作只是对备份集本身做了相关处理,所以在准备恢复前最好对备份集再次备份
第五步 恢复数据
[root@node full]# innobackupex --defaults-file=/opt/mysql-5.6/my.cnf --copy-back /opt/backup/full/2018-12-11_14-04-15/
......
181211 15:02:41 [01] ...done
181211 15:02:41 [01] Copying ./performance_schema/events_statements_summary_by_thread_by_event_name.frm to /opt/mysql/data/performance_schema/events_statements_summary_by_thread_by_event_name.frm
181211 15:02:41 [01] ...done
181211 15:02:41 [01] Copying ./performance_schema/mutex_instances.frm to /opt/mysql/data/performance_schema/mutex_instances.frm
181211 15:02:41 [01] ...done
181211 15:02:41 [01] Copying ./performance_schema/events_waits_summary_global_by_event_name.frm to /opt/mysql/data/performance_schema/events_waits_summary_global_by_event_name.frm
181211 15:02:41 [01] ...done
181211 15:02:41 [01] Copying ./xtrabackup_info to /opt/mysql/data/xtrabackup_info
181211 15:02:41 [01] ...done
181211 15:02:41 [01] Copying ./xtrabackup_master_key_id to /opt/mysql/data/xtrabackup_master_key_id
181211 15:02:41 [01] ...done
181211 15:02:41 completed OK!
[root@node full]# ls /opt/mysql/data/ -l
total 286748
-rw-r----- 1 root root 12582912 Dec 11 15:02 ibdata1
-rw-r----- 1 root root 134217728 Dec 11 15:02 ib_logfile0
-rw-r----- 1 root root 134217728 Dec 11 15:02 ib_logfile1
-rw-r----- 1 root root 12582912 Dec 11 15:02 ibtmp1
drwxr-x--- 2 root root 4096 Dec 11 15:02 mysql
drwxr-x--- 2 root root 4096 Dec 11 15:02 performance_schema
-rw-r----- 1 root root 21 Dec 11 15:02 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 509 Dec 11 15:02 xtrabackup_info
-rw-r----- 1 root root 1 Dec 11 15:02 xtrabackup_master_key_id
drwxr-x--- 2 root root 4096 Dec 11 15:02 xtra_test
第六步 授权data目录启动MySQL
[root@node full]# ls -l /opt/mysql/data/
total 286748
-rw-r----- 1 mysql mysql 12582912 Dec 11 15:02 ibdata1
-rw-r----- 1 mysql mysql 134217728 Dec 11 15:02 ib_logfile0
-rw-r----- 1 mysql mysql 134217728 Dec 11 15:02 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Dec 11 15:02 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Dec 11 15:02 mysql
drwxr-x--- 2 mysql mysql 4096 Dec 11 15:02 performance_schema
-rw-r----- 1 mysql mysql 21 Dec 11 15:02 xtrabackup_binlog_pos_innodb
-rw-r----- 1 mysql mysql 509 Dec 11 15:02 xtrabackup_info
-rw-r----- 1 mysql mysql 1 Dec 11 15:02 xtrabackup_master_key_id
drwxr-x--- 2 mysql mysql 4096 Dec 11 15:02 xtra_test
[root@node full]# /etc/init.d/mysqld start
Starting MySQL.. [ OK ]
[root@node full]# ps aux|grep mysql
root 19450 0.0 0.1 113324 3196 pts/1 S 15:04 0:00 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/opt/mysql/data --pid-file=/opt/mysql/mysqld.pid
mysql 20257 2.2 44.1 2299484 901804 pts/1 Sl 15:04 0:00 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/opt/mysql/data --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=/opt/mysql/error.log --open-files-limit=4161 --pid-file=/opt/mysql/mysqld.pid --socket=/tmp/mysql.sock --port=3306
root 20288 0.0 0.1 112720 2244 pts/1 S+ 15:04 0:00 grep --color=auto mysql
[root@node full]# netstat -lntup | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 20257/mysqld
第七步 验证数据恢复操作
[root@node full]# mysql -uroot -p234567
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| xtra_test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use xtra_test;
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> show tables;
+---------------------+
| Tables_in_xtra_test |
+---------------------+
| friends |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from friends;
+----+------+
| id | name |
+----+------+
| 1 | jack |
+----+------+
1 row in set (0.01 sec
至此,全量备份和还原完成;
Xtrabackup 全备和还原以及增量备份和还原的更多相关文章
- 利用xtrabackup8完全,增量备份及还原MySQL8
利用xtrabackup8完全,增量备份及还原MySQL8 1.环境准备 服务器 作用 数据库版本 xtrabackup版本 10.0.0.8 数据备份 mysql8.0.26 8.0.28 10.0 ...
- Mysql性能优化三(分表、增量备份、还原)
接上篇Mysql性能优化二 对表进行水平划分 如果一个表的记录数太多了,比如上千万条,而且需要经常检索,那么我们就有必要化整为零了.如果我拆成100个表,那么每个表只有10万条记录.当然这需要数据在逻 ...
- 通过innobackupex实现对MySQL的增量备份与还原
备份 增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --password=test /backup/ 备注:test是我的MySQL服务的root用户的密码,/b ...
- mysql 利用binlog增量备份,还原实例
mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...
- 瞧一瞧!这儿实现了MongoDB的增量备份与还原(含部署代码)
一 需求描述 我们知道数据是公司的重要资产,业务的系统化.信息化就是数字化.数据高效的存储与查询是系统完善和优化的方向,而数据库的稳定性.可靠性是实现的基础.高可用和RPO(RecoveryPoint ...
- Mysql大数据备份和增量备份及还原
目前主流的有两个工具可以实现物理热备:ibbackup和xtrabackup ;ibbackup是需要授权价格昂贵,而xtrabackup功能比ibbackup强大而且是开源的 Xtrabackup提 ...
- 挖一挖MongoDB的备份与还原(实现指定时间点还原和增量备份还原)
一 研究背景需求 目前作者所在公司的MongoDB数据库是每天凌晨做一次全库完整备份,但数据库出现故障时,只能保证恢复到全备时间点,比如,00:30 做的完整备份,而出现故障是下午18:00,那么现 ...
- innobackupex实现对MySQL的增量备份与还原
备份增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --defaults-file=/etc/my.cnf --user root --password cheng ...
- mysql备份与还原,增量备份;使用ibd和frm文件恢复数据
主要用的:binlog.mysqldump.mysqlbinlog 参考: https://www.cnblogs.com/Cherie/p/3309456.html https://blog.csd ...
随机推荐
- .NET Core 又一杀器! Web Blazor框架横空出世!
多年来,Javascript(及其子框架)已在浏览器中运行DOM(文档对象模型),并且掌握了脚本知识才能真正操作客户端UI.大约2年前,所有这些都随着Web Assembly的引入而发生了变化-Web ...
- svn:"Update item to this version"、"Revert to this version"及"Revert changes from this version"详细说明
背景: 今天在svn分支上做了一些课题性研究,发现当前的环境版本不稳定, 和领导研究后决定还原到前面的版本以继续进行课题. 问题: 因此遇到了问题,是应该选择“Update item to this ...
- vue中组件的data为什么是一个函数
1. 前言 在学习vue的时候,一直纳闷一件事:组件的data数据为什么必须要以函数返回的形式,为什么不是简单的对象形式呢?遂带着问题去翻官方文档,文档中自然也写明了这么做的原因,本篇博文以官方文档给 ...
- 学习笔记之vim的使用
很多刚学习linux编程的人总是对vim有一种恐惧,我自己就是这么回事的. 可是当你努力的去尝试学习使用后,才发现它的精髓所在. 在我看来,让vim变得好用的前提是要安装两个插件,ctags和tagl ...
- 去除word文档页眉处的横杠
如何去除上图word文档页眉处的横杠 wps软件使用者 第一步双击页眉,到页眉页脚: 第一步点击上图页眉横线,点击无线型或者删除横线即可: Microsoft Office 专业增 ...
- PHP判断是否关注微信公众号
PHP判断是否关注微信公众号 1 服务号直接请求这个接口https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839 su ...
- maven的项目结构
1.标准目录结构: src -main –bin 脚本库 –java java源代码文件 –resources 资源库,会自动复制到classes目录里 ...
- T-SQL Part VII: CROSS JOIN
虽然不能确定是不是只有个SQL Server提供了Cross Join的功能,貌似W3School的SQL教程中是没有的 SQL教程.而Wikipedia中倒是有,也是最新的SQL:2011SQL:2 ...
- 019.Kubernetes二进制部署插件dashboard
一 修改配置文件 1.1 下载解压 [root@k8smaster01 ~]# cd /opt/k8s/work/kubernetes/ [root@k8smaster01 kubernetes]# ...
- vue项目iframe的传值问题
前言 项目需要,我需要引入一个已经封装好的浏览器插件.插件只能以html的方式调用, 所以.我把插件的使用封装了一个html页面.vue项目则利用iframe的方式引入. 到这里我就遇到了一个问题,那 ...