mysql全备和增量备份以及恢复过程(percona工具)
实验环境
系统环境,内核版本和xtrabackup工具版本
[root@linux-node1 mysql]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@linux-node1 mysql]# uname -rm
3.10.0-229.el7.x86_64 x86_64
[root@linux-node1 mysql]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock -e 'select version();'
+------------+
| version() |
+------------+
| 5.6.30-log |
+------------+
[root@linux-node1 mysql]# rpm -qa | grep percona
percona-xtrabackup-2.3.4-1.el7.x86_64
[root@linux-node1 mysql]#
全备和增量备份
先查看当前的数据,t1库的tab1表里只有1和2这2条数据
[root@linux-node1 opt]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.6.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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 |
| t1 |
| test |
+--------------------+
5 rows in set (0.00 sec) mysql> use t1;
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_t1 |
+--------------+
| tab1 |
+--------------+
1 row in set (0.00 sec) mysql> select * from tab1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 19
Current database: t1 +------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.01 sec) mysql>
执行第一次全备
[root@linux-node1 ~]# innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock /opt/full/
结尾提示OK
170405 02:00:21 [00] Writing backup-my.cnf
170405 02:00:21 [00] ...done
170405 02:00:21 [00] Writing xtrabackup_info
170405 02:00:21 [00] ...done
xtrabackup: Transaction log of lsn (1645798) to (1645798) was copied.
170405 02:00:21 completed OK!
查看备份的数据
[root@linux-node1 incr]# ll /opt/full/2017-04-05_02-00-19/
total 12316
-rw-r----- 1 root root 387 Apr 5 02:00 backup-my.cnf
-rw-r----- 1 root root 12582912 Apr 5 02:00 ibdata1
drwx------ 2 root root 4096 Apr 5 02:00 mysql
drwx------ 2 root root 4096 Apr 5 02:00 performance_schema
drwx------ 2 root root 49 Apr 5 02:00 t1
drwx------ 2 root root 19 Apr 5 02:00 test
-rw-r----- 1 root root 69 Apr 5 02:00 xtrabackup_binlog_info
-rw-r----- 1 root root 113 Apr 5 02:00 xtrabackup_checkpoints
-rw-r----- 1 root root 603 Apr 5 02:00 xtrabackup_info
-rw-r----- 1 root root 2560 Apr 5 02:00 xtrabackup_logfile
[root@linux-node1 incr]#
添加点数据
[root@linux-node1 ~]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock -e 'insert into t1.tab1 values (4);'
[root@linux-node1 ~]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock -e 'select * from t1.tab1;'
+------+
| id |
+------+
| 1 |
| 2 |
| 4 |
+------+
[root@linux-node1 ~]#
执行第1次增量备份,在第1次全备的基础上执行增量
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --incremental /opt/incr/ --incremental-basedir=/opt/full/2017-04-05_02-00-19/ 查看第1次增量 备份目录
[root@linux-node1 incr]# ll /opt/incr/
total 4
drwx------ 6 root root 4096 Apr 5 02:02 2017-04-05_02-02-08
[root@linux-node1 incr]# [root@linux-node1 incr]# ll /opt/incr/2017-04-05_02-02-08/
total 160
-rw-r----- 1 root root 387 Apr 5 02:02 backup-my.cnf
-rw-r----- 1 root root 131072 Apr 5 02:02 ibdata1.delta
-rw-r----- 1 root root 44 Apr 5 02:02 ibdata1.meta
drwx------ 2 root root 4096 Apr 5 02:02 mysql
drwx------ 2 root root 4096 Apr 5 02:02 performance_schema
drwx------ 2 root root 75 Apr 5 02:02 t1
drwx------ 2 root root 19 Apr 5 02:02 test
-rw-r----- 1 root root 69 Apr 5 02:02 xtrabackup_binlog_info
-rw-r----- 1 root root 117 Apr 5 02:02 xtrabackup_checkpoints
-rw-r----- 1 root root 676 Apr 5 02:02 xtrabackup_info
-rw-r----- 1 root root 2560 Apr 5 02:02 xtrabackup_logfile
[root@linux-node1 incr]#
继续插入一条数据
[root@linux-node1 ~]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock -e 'insert into t1.tab1 values (5);'
[root@linux-node1 ~]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock -e 'select * from t1.tab1;'
+------+
| id |
+------+
| 1 |
| 2 |
| 4 |
| 5 |
+------+
[root@linux-node1 ~]#
执行第2次增量备份(在第一次增量的基础上)
这里只须要把最后的目录改为第一次增量备份的数据目录即可
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --incremental /opt/incr/ --incremental-basedir=/opt/incr/2017-04-05_02-02-08/ 查看第2次的增量目录
[root@linux-node1 ~]# ll /opt/incr/
total 8
drwx------ 6 root root 4096 Apr 5 02:02 2017-04-05_02-02-08
drwx------ 6 root root 4096 Apr 5 02:03 2017-04-05_02-03-42
[root@linux-node1 ~]# ll /opt/incr/2017-04-05_02-03-42/
total 128
-rw-r----- 1 root root 387 Apr 5 02:03 backup-my.cnf
-rw-r----- 1 root root 98304 Apr 5 02:03 ibdata1.delta
-rw-r----- 1 root root 44 Apr 5 02:03 ibdata1.meta
drwx------ 2 root root 4096 Apr 5 02:03 mysql
drwx------ 2 root root 4096 Apr 5 02:03 performance_schema
drwx------ 2 root root 75 Apr 5 02:03 t1
drwx------ 2 root root 19 Apr 5 02:03 test
-rw-r----- 1 root root 69 Apr 5 02:03 xtrabackup_binlog_info
-rw-r----- 1 root root 117 Apr 5 02:03 xtrabackup_checkpoints
-rw-r----- 1 root root 676 Apr 5 02:03 xtrabackup_info
-rw-r----- 1 root root 2560 Apr 5 02:03 xtrabackup_logfile
[root@linux-node1 ~]#
模拟故障场景,数据磁盘严重损坏。文件无法修复
[root@linux-node1 ~]# cd /usr/local/mysql/data/
[root@linux-node1 data]# ll
total 110724
-rw-rw---- 1 mysql mysql 56 Apr 4 15:25 auto.cnf
-rw-rw---- 1 mysql mysql 81970 Apr 5 01:59 error.log
-rw-rw---- 1 mysql mysql 12582912 Apr 5 02:03 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Apr 5 02:03 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Apr 4 14:42 ib_logfile1
-rw-rw---- 1 mysql mysql 174 Apr 4 15:34 linux-node1-bin.000001
-rw-rw---- 1 mysql mysql 2457 Apr 5 01:33 linux-node1-bin.000002
-rw-rw---- 1 mysql mysql 244 Apr 5 01:55 linux-node1-bin.000003
-rw-rw---- 1 mysql mysql 214 Apr 5 01:55 linux-node1-bin.000004
-rw-rw---- 1 mysql mysql 931 Apr 5 01:57 linux-node1-bin.000005
-rw-rw---- 1 mysql mysql 655 Apr 5 02:03 linux-node1-bin.000006
-rw-rw---- 1 mysql mysql 150 Apr 5 01:59 linux-node1-bin.index
-rw-rw---- 1 mysql mysql 6 Apr 4 16:54 linux-node1.nmap.com.pid
-rw-rw---- 1 mysql mysql 6 Apr 5 01:59 linux-node1.pid
drwx------ 2 mysql mysql 4096 Apr 4 14:42 mysql
drwx------ 2 mysql mysql 4096 Apr 4 14:42 performance_schema
drwx------ 2 mysql mysql 49 Apr 4 17:11 t1
drwx------ 2 mysql mysql 6 Apr 4 14:42 test
[root@linux-node1 data]# [root@linux-node1 data]# mkdir /data-tmp/
[root@linux-node1 data]# mv * /data-tmp/
[root@linux-node1 data]# ll
total 0
[root@linux-node1 data]#
利用备份恢复过程
利用全部备份和增量备份恢复数据
停止mysql进程
[root@linux-node1 data]# /usr/local/mysql/bin/mysqladmin shutdown -S /usr/local/mysql/mysql.sock
[root@linux-node1 data]# ps -ef | grep mysql
root 40382 40009 0 01:33 pts/0 00:00:00 grep --colour=auto mysql
[root@linux-node1 data]#
先应用全备日志,
(1)在备份完成后, 数据尚且不能用于恢复操作, 因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。
因此,此时数据文件仍处于不一致状态。–apply-log选项可用于实现此功能
(2)–redo-only是只将已提交的事务同步到数据文件中, 未提交的事务日志不在进行回滚了.可以让那增备之间日志可以无缝衔接起来
(3)--use-memory 可以指定内存大小,加快应用日志的速度
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --apply-log --redo-only --use-memory=1G /opt/full/2017-04-05_02-00-19/ 执行结尾部分输出
InnoDB: from the doublewrite buffer...
xtrabackup: Last MySQL binlog file position 908, file name linux-node1-bin.000005 xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1645798
170405 02:06:22 completed OK!
[root@linux-node1 ~]#
将全备和第1个增备合并
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --apply-log --redo-only --use-memory=1G \
/opt/full/2017-04-05_02-00-19/ --incremental-dir=/opt/incr/2017-04-05_02-02-08/ 执行结尾部分输出
170405 02:07:51 [01] Copying /opt/incr/2017-04-05_02-02-08/test/db.opt to ./test/db.opt
170405 02:07:51 [01] ...done
170405 02:07:51 [00] Copying /opt/incr/2017-04-05_02-02-08//xtrabackup_binlog_info to ./xtrabackup_binlog_info
170405 02:07:51 [00] ...done
170405 02:07:51 [00] Copying /opt/incr/2017-04-05_02-02-08//xtrabackup_info to ./xtrabackup_info
170405 02:07:51 [00] ...done
170405 02:07:51 completed OK!
[root@linux-node1 ~]#
把第2个增备整合到上面已经整合了第一个增量的全备目录中
这里可以把--redo-only 参数取消了。这样未提交的事务被回滚。也符合事务特性
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --apply-log --use-memory=1G \
/opt/full/2017-04-05_02-00-19/ --incremental-dir=/opt/incr/2017-04-05_02-03-42/ 执行结尾部分输出
nnoDB: Highest supported file format is Barracuda.
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.24 started; log sequence number 1646604
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1646614
170405 02:13:00 completed OK!
[root@linux-node1 ~]#
把整合了全备和2次增量备份的数据恢复到data目录下。要先保证data目录下没有数据文件
innobackupex --defaults-file="/usr/local/mysql/my.cnf" --user=root --socket=/usr/local/mysql/mysql.sock --copy-back /opt/full/2017-04-05_02-00-19/ 执行结尾部分输出
170405 02:16:28 [01] Copying ./performance_schema/session_account_connect_attrs.frm to /usr/local/mysql/data/performance_schema/session_account_connect_attrs.frm
170405 02:16:28 [01] ...done
170405 02:16:28 [01] Copying ./test/db.opt to /usr/local/mysql/data/test/db.opt
170405 02:16:28 [01] ...done
170405 02:16:28 [01] Copying ./xtrabackup_binlog_pos_innodb to /usr/local/mysql/data/xtrabackup_binlog_pos_innodb
170405 02:16:28 [01] ...done
170405 02:16:28 [01] Copying ./xtrabackup_info to /usr/local/mysql/data/xtrabackup_info
170405 02:16:28 [01] ...done
170405 02:16:28 completed OK!
查看文件已经恢复过去了,但是里面的文件属组是root。需要改为mysql用户和组
[root@linux-node1 mysql]# ll data/ -d
drwxr-xr-x 6 mysql mysql 134 Apr 5 02:16 data/
[root@linux-node1 mysql]# ll data/
total 12304
-rw-r----- 1 root root 12582912 Apr 5 02:16 ibdata1
drwx------ 2 root root 4096 Apr 5 02:16 mysql
drwx------ 2 root root 4096 Apr 5 02:16 performance_schema
drwx------ 2 root root 49 Apr 5 02:16 t1
drwx------ 2 root root 19 Apr 5 02:16 test
-rw-r----- 1 root root 27 Apr 5 02:16 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 676 Apr 5 02:16 xtrabackup_info
[root@linux-node1 mysql]# 改变属组
[root@linux-node1 mysql]# chown -R mysql:mysql data/
[root@linux-node1 mysql]# ll data/
total 12304
-rw-r----- 1 mysql mysql 12582912 Apr 5 02:16 ibdata1
drwx------ 2 mysql mysql 4096 Apr 5 02:16 mysql
drwx------ 2 mysql mysql 4096 Apr 5 02:16 performance_schema
drwx------ 2 mysql mysql 49 Apr 5 02:16 t1
drwx------ 2 mysql mysql 19 Apr 5 02:16 test
-rw-r----- 1 mysql mysql 27 Apr 5 02:16 xtrabackup_binlog_pos_innodb
-rw-r----- 1 mysql mysql 676 Apr 5 02:16 xtrabackup_info
[root@linux-node1 mysql]#
启动mysql
[root@linux-node1 mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf &
[1] 41106
[root@linux-node1 mysql]# 2017-04-05 02:19:20 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.30-log) starting as process 41106 ... [root@linux-node1 mysql]#
登录检查数据,数据都还在
[root@linux-node1 mysql]# /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> select * from t1.tab1;
+------+
| id |
+------+
| 1 |
| 2 |
| 4 |
| 5 |
+------+
4 rows in set (0.01 sec) mysql>
上述步骤仅仅是简化版的利用全备和增量备份恢复的过程。假如在最后一次增量备份和数据库损坏之间有一些binlog日志,最后还需要把这些binlog恢复了。当然了,这些binlog肯定要在恢复数据之前先备份出来
mysql全备和增量备份以及恢复过程(percona工具)的更多相关文章
- mysql全备、增量备份脚本
1.mysql全量备份及定时删除备份文件脚本 #!/bin/bash v_user="root" v_password="mysql" backup_date ...
- mysql数据库的增量备份和全备
还有一种简单的方法 参考 https://blog.csdn.net/u010098331/article/details/50932064 (注意:5.6版本以上新加了gtid 功能,gtid开启之 ...
- [MySQL Reference Manual] 7 备份和恢复
7. 备份和恢复 本章主要会介绍: 1.备份的类型:逻辑备份,物理备份,全备和增量4种 2.创建备份的方法 3.还原方法,包括还原到时间点 4.备份计划,压缩和加密 5.表维护,恢复损坏的表 7. 备 ...
- xtrabackup之Innobackupex增量备份及恢复
演示增量备份 #启动一个全备 innobackupex \ > --defaults-/my.cnf \ > --host=127.0.0.1 \ > --user=xtrabk \ ...
- XtraBackup全备与增量备份
一.XtraBackup安装 下载地址:http://www.percona.com/downloads/XtraBackup/XtraBackup-2.2.8/source/ 安装步骤: ===== ...
- 第五章 MySQL事务,视图,索引,备份和恢复
第五章 MySQL事务,视图,索引,备份和恢复 一.事务 1.什么是事务 事务是一种机制,一个操作序列,它包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求.要么都执行 ...
- ceph中rbd的增量备份和恢复
ceph中rbd的增量备份和恢复 ceph的文档地址:Ceph Documentation 在调研OpenStack中虚机的备份和恢复时,发现OpenStack和ceph紧密结合,使用ceph做O ...
- MySQL日志管理、备份、恢复
目录: 一.MySQL 日志管理 二.数据库备份的重要性与分类 三.常见的备份方法 四.MySQL完全备份 五.数据库完全备份分类 六.MySQL增量备份 七.MySQL数据库增量恢复 八.MySQL ...
- mysql 利用binlog增量备份,还原实例
mysql 利用binlog增量备份,还原实例 张映 发表于 2010-09-29 分类目录: mysql 标签:binlog, mysql, mysqldump, 增量备份 一,什么是增量备份 增量 ...
随机推荐
- freemarker学习 (servlet + freemarker -> Struts2+freemarker -> springMVC+freemarker)
什么是freemarker? freemarker类似于jsp,但不是jsp!怎么说呢?freemarker文件后缀是.ftl,它不像jsp本质是servlet,它将构建模板.解析模板.使用模板分离开 ...
- java基础学习之单例设计模式学习
最近狂补java基础的我重新学习了下单例,下面直接贴出代码,以作备忘 package com.darling.single; /** * 单例模式 * 单例即在内存中只存在该类的一个实例,要想实现这个 ...
- css 伪类: 1)a:link , a:visited, a:hover, a:active 2):first-child
1. 链接伪类: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...
- Python 面向对象和面向过程对比
# 大象装冰箱 # 脚本, 此时代码是最简单的. 不需要构思整个程序的概况 print("开门") print("装大象") print("关门&qu ...
- Kafka的安装 -- 未完成
1. 官网下载软件 2. linux服务器上, 安装上传和下载的工具 yum install -y lrzsz rz : 上传 sz + 文件名 : 下载 3.解压文件 pwd: 查看当前路径 解压到 ...
- 2.7 多窗口、句柄(handle)
2.7 多窗口.句柄(handle) 前言 有些页面的链接打开后,会重新打开一个窗口,对于这种情况,想在新页面上操作,就得先切换窗口了.获取窗口的唯一标识用句柄表示,所以只需要切换句柄,我们就能在 ...
- 精确率、召回率、准确率与ROC曲线
精确率表示的是预测为某类样本(例如正样本)中有多少是真正的该类样本,一般用来评价分类任务模型. 比如对于一个分类模型,预测结果为A类的所有样本中包含A0个真正的A样本,和A1个不是A样本的其他类样本, ...
- php获取真实ip地址(转)
REMOTE_ADDR只能获取访问者本地连接中设置的IP,如中南民族大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址, 如果访问者使用代理服务器,将不 ...
- html播放音乐
如何在网站网页中添加音乐代码 告诉你多种格式文件的详细使用代码. width_num——指定一个作为宽度的数字: height_num——指定一个作为高度的数字: 1.mp3 ...
- CodeForces - 441D: Valera and Swaps(置换群)
A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permu ...