本文继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
  1. full: 文件中存放全量备份
  2. 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)

还原数据操作

还原步骤:

  1. 先把xtrabackup备份下来的目录进行再次备份,防止在还原的时候误操作,导致备份不可用;
  2. 关闭要回复的数据库实例;
  3. 移除需要恢复数据的MySQL实例下的data目录,建议mv移动到别的目录下;
  4. 使用--apply-log 参数进行恢复前准备操作,如事物回滚等操作;
  5. 使用--copy-back--rsync 参数进行恢复;
  6. 授权MySQL实例下的data目录后,启动数据库
  7. 验证恢复数据

第一步 备份备份文件

[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 全备和还原以及增量备份和还原的更多相关文章

  1. 利用xtrabackup8完全,增量备份及还原MySQL8

    利用xtrabackup8完全,增量备份及还原MySQL8 1.环境准备 服务器 作用 数据库版本 xtrabackup版本 10.0.0.8 数据备份 mysql8.0.26 8.0.28 10.0 ...

  2. Mysql性能优化三(分表、增量备份、还原)

    接上篇Mysql性能优化二 对表进行水平划分 如果一个表的记录数太多了,比如上千万条,而且需要经常检索,那么我们就有必要化整为零了.如果我拆成100个表,那么每个表只有10万条记录.当然这需要数据在逻 ...

  3. 通过innobackupex实现对MySQL的增量备份与还原

    备份 增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --password=test /backup/ 备注:test是我的MySQL服务的root用户的密码,/b ...

  4. mysql 利用binlog增量备份,还原实例

    mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...

  5. 瞧一瞧!这儿实现了MongoDB的增量备份与还原(含部署代码)

    一 需求描述 我们知道数据是公司的重要资产,业务的系统化.信息化就是数字化.数据高效的存储与查询是系统完善和优化的方向,而数据库的稳定性.可靠性是实现的基础.高可用和RPO(RecoveryPoint ...

  6. Mysql大数据备份和增量备份及还原

    目前主流的有两个工具可以实现物理热备:ibbackup和xtrabackup ;ibbackup是需要授权价格昂贵,而xtrabackup功能比ibbackup强大而且是开源的 Xtrabackup提 ...

  7. 挖一挖MongoDB的备份与还原(实现指定时间点还原和增量备份还原)

    一  研究背景需求 目前作者所在公司的MongoDB数据库是每天凌晨做一次全库完整备份,但数据库出现故障时,只能保证恢复到全备时间点,比如,00:30 做的完整备份,而出现故障是下午18:00,那么现 ...

  8. innobackupex实现对MySQL的增量备份与还原

    备份增量备份是基于完整备份的,所以我们需要先做一次完整备份: innobackupex --defaults-file=/etc/my.cnf --user root --password cheng ...

  9. mysql备份与还原,增量备份;使用ibd和frm文件恢复数据

    主要用的:binlog.mysqldump.mysqlbinlog 参考: https://www.cnblogs.com/Cherie/p/3309456.html https://blog.csd ...

随机推荐

  1. ulua、tolua原理解析

    在聊ulua.tolua之前,我们先来看看Unity热更新相关知识. 什么是热更新 举例来说: 游戏上线后,玩家下载第一个版本(70M左右或者更大),在运营的过程中,如果需要更换UI显示,或者修改游戏 ...

  2. 解决移动端touch事件与click冲突的问题

    最简单的办法,就只绑定一个事件不就行了: 第二种,我觉得和第一种也没啥区别.. const Button = document.getElementById("targetButton&qu ...

  3. 学习笔记42_SpringMVC

    SpringMVC中,Global.axas发生变化,其中 1.原来是 public class MvcApplication:System.web.HttpApplication 现在是 publi ...

  4. [UWP] 为WinRT组件创建Nuget包

    Nuget 是 dotnet 开发中必不可少的包管理工具,但不仅仅局限于 dotnet 项目,在 VS 中使用 C++ 开发的时候,也可以使用 Nuget 来引用第三方组件.同样也可以用 Nuget ...

  5. js基础总结01 --操作DOM

    1.选择对象 通过id来选择绑定一个dom节点 :document.getElementById('p1'): 通过类名来绑定一个类数组的对象集合,:document.getElementsByCla ...

  6. IM里“附近的人”功能实现原理是什么?如何高效率地实现它?

    1.引言 基本上以陌生人社交为主的IM产品里,都会增加“附近的人”.“附近的xxx”这种以LBS(地理位置)为导向的产品特色(微信这个熟人社交产品里为啥也有“附近的人”?这当然是历史原因了,微信当初还 ...

  7. 通过ISO镜像简单搭建本地yum仓库

    本文参考链接:https://segmentfault.com/a/1190000015155966 *有时候在我们本地搭建一些Linux上的程序运行环境或者安装一些软件的时候,难免会遇到需要使用yu ...

  8. Spring Cloud gateway 六 Sentinel nacos存储动态刷新

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  9. CSS3解决字母不换行的方法

    CSS3解决字母不换行的方法 <pre>word-wrap: break-word;</pre>

  10. 关于mysql驱动cj与不是cj的问题

    1. 带cj的是mysql驱动6.0以上的,不带的是6.0以下的. 2.6.0以上的需要注意有时区纠正