Mysql增量恢复

mysqldump增量恢复
何时需要使用备份的数据?
备份最牛逼的层次,就是永远都用不上备份。--老男孩
不管是逻辑备份还是物理备份,备份的数据什么时候需要用?
===========================================================
1、恢复数据到测试库的时候。
2、人为通过SQL语句将数据删除的时候。
3、做数据库主从复制的时候。

其它场景(诸如硬件、删了磁盘上文件)几乎都不要使用备份恢复,
而是直接使用主从复制功能,将业务切换到从库。
=============================================================
具备什么条件才能完整恢复数据?
1、全备(mysqldump)
2、binlog数据(开启binlog)

已知数据库数据:
[root@mysql-db02 ~]# mysql -e "select * from oldboy.test;"
+------+---------+
| id | name |
+------+---------+
| 1 | oldboy |
| 2 | oldgirl |
| 3 | inca |
| 4 | zuma |
| 5 | maya |
+------+---------+

1、准备环境:开始备份数据
mkdir /data/backup -p
date -s "2018/03/19"
mysqldump -B --master-data=2 --single-transaction oldboy|gzip>/data/backup/oldboy_$(date +%F).sql.gz

mysql -e "use oldboy;insert into test values(6,'bingbing');"
mysql -e "use oldboy;insert into test values(7,'xiaoting');"
mysql -e "select * from oldboy.test;"

2、模拟误删数据:
date -s "2018/03/19 10:00"
mysql -e "drop database oldboy;show databases;"

2018/03/19 10:10 发现问题

如果是update test set name="xiaoting";
停止对外访问:
iptables -I INPUT ! -p tcp -s 10.0.0.9 -j DROP

3、开始恢复准备
检查全备:
ll /data/backup/oldboy_2018-03-19.sql.gz
cd /data/backup/
zcat oldboy_2018-03-19.sql.gz >oldboy_2018-03-19.sql

检查binlog:
cp -a /application/mysql/data/oldboy-bin.* /data/backup/
cd /data/backup/
sed -n '22p' oldboy_2018-03-19.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='oldboy-bin.000004', MASTER_LOG_POS=2441;

mysqlbinlog -d oldboy --start-position=2441 oldboy-bin.000004 -r bin.sql
###mysqlbinlog -d oldboy oldboy-bin.000005 oldboy-bin.000007 oldboy-bin.000007 -r bin.sql

清理造成故障的drop语句:
grep drop bin.sql
sed -i '/drop database oldboy/d' bin.sql
grep drop bin.sql

4、开始恢复全备。
mysql <oldboy_2018-03-19.sql
mysql -e "show databases;"
[root@mysql-db02 backup]# mysql -e "use oldboy;select * from test;"
+------+---------+
| id | name |
+------+---------+
| 1 | oldboy |
| 2 | oldgirl |
| 3 | inca |
| 4 | zuma |
| 5 | maya |
+------+---------+

5、开始恢复增量
[root@mysql-db02 backup]# mysql oldboy <bin.sql
[root@mysql-db02 backup]# mysql -e "use oldboy;select * from test;"
+------+----------+
| id | name |
+------+----------+
| 1 | oldboy |
| 2 | oldgirl |
| 3 | inca |
| 4 | zuma |
| 5 | maya |
| 6 | bingbing |
| 7 | xiaoting |
+------+----------+
恢复完毕。
对外提供服务。
iptables -d INPUT ! -p tcp -s 10.0.0.9 -j DROP

xtrabackup增量恢复
中小企业MySQL Xtrabackup物理增量恢复案例实战
具备全量备份(xtrabckup备份的全备)
具备全量之后的所有增量备份(xtrabckup备份的增量)
具备最后一次增量备份以后的所有MySQL的Binlog增量日志

1、模拟数据
use oldboy
insert into test values(1,'full01');
insert into test values(2,'full02');
insert into test values(3,'full03');
insert into test values(4,'full04');
insert into test values(5,'full05');
select * from test;
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
+----+--------+
5 rows in set (0.00 sec)

2、2018/03/21 0点全量备份
date -s "2018/03/21"
innobackupex --defaults-file=/etc/my.cnf --user=root --password=oldboy123 --socket=/application/mysql-5.6.34/tmp/mysql.sock --no-timestamp /server/backup/new_base_full

3、2018/03/22 0点增量备份
mysql -e "use oldboy;insert into test values(6,'new_inc_one_1');"
mysql -e "use oldboy;insert into test values(7,'new_inc_one_2');"
mysql -e "select * from oldboy.test;"
+----+---------------+
| id | name |
+----+---------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
+----+---------------+

第一次增量备份
date -s "2018/03/22"
innobackupex --defaults-file=/etc/my.cnf --user=root --password=oldboy123 --socket=/application/mysql-5.6.34/tmp/mysql.sock --no-timestamp --incremental-basedir=/server/backup/new_base_full --incremental /server/backup/new_one_inc

4、2018/03/23 0点增量备份
mysql -e "use oldboy;insert into test values(8,'new_inc_two1');"
mysql -e "use oldboy;insert into test values(9,'new_inc_two2');"
mysql -e "select * from oldboy.test;"

第二次增量备份
date -s "2018/03/23"
innobackupex --defaults-file=/etc/my.cnf --user=root --password=oldboy123 --socket=/application/mysql-5.6.34/tmp/mysql.sock --no-timestamp --incremental-basedir=/server/backup/new_one_inc --incremental /server/backup/new_two_inc

5、2018/03/23 0点以后-10点更新数据
mysql -e "use oldboy;insert into test values(10,'realbinlog_data_3');"
mysql -e "use oldboy;insert into test values(11,'realbinlog_data_4');"
mysql -e "select * from oldboy.test;"

6、2018/03/23上午10点出故障了
mysql -e "use oldboy;update test set name='oldboy';"
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | oldboy |
| 2 | oldboy |
| 3 | oldboy |
| 4 | oldboy |
| 5 | oldboy |
| 6 | oldboy |
| 7 | oldboy |
| 8 | oldboy |
| 9 | oldboy |
| 10 | oldboy |
| 11 | oldboy |
+----+--------+
11 rows in set (0.00 sec)

建议停库:
/etc/init.d/mysqld stop

恢复数据库:
7、合并数据文件
innobackupex --apply-log --use-memory=32M --redo-only /server/backup/new_base_full/
innobackupex --apply-log --use-memory=32M --redo-only --incremental-dir=/server/backup/new_one_inc /server/backup/new_base_full/
innobackupex --apply-log --use-memory=32M --incremental-dir=/server/backup/new_two_inc /server/backup/new_base_full/

8、开始恢复
cd /application/mysql
mv data data.11
cp -a /server/backup/new_base_full data
chown -R mysql.mysql data

9、处理binlog
[root@db02 mysql]# cat /server/backup/new_two_inc/xtrabackup_binlog_info
oldboy-bin.000006 2286

mysqlbinlog -d oldboy --start-position=2286 oldboy-bin.000006 -r bin.sql
mysqlbinlog -d oldboy oldboy-bin.000003 oldboy-bin.000004 oldboy-bin.000005 >>bin.sql

cd /application/mysql/logs/
mysqlbinlog -d oldboy --start-position=2286 oldboy-bin.000006 -r /server/backup/new_bin.sql

删除update一行。update test set name='oldboy'

10、开启防火墙iptables
iptables -I INPUT ! -s 10.0.0.9 -p tcp -j DROP

[root@db02 tools]# mysql oldboy </server/backup/new_bin.sql
[root@db02 tools]# mysql -e "select * from oldboy.test;"
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
| 8 | new_inc_two1 |
| 9 | new_inc_two2 |
| 10 | realbinlog_data_3 |
| 11 | realbinlog_data_4 |
+----+-------------------+

上一篇:HTML&&CSS基础知识点整理


下一篇:HDU-6440-费马小定理