mysql几种备份恢复_mysql多种备份与恢复方式一

通过binlog时间区间/偏移量进行恢复

mysqlbinlog [选项] binlog日志文件名 | mysql -uroot -p

选项

--start-datetime="yyyy-mm-dd hh:mm:ss"

--stop-datetime="yyyy-mm-dd hh:mm:ss"

--strat-position=偏移量

--stop-position=偏移量

恢复思路:

第一步首先通过上次的完整备份恢复到完整备份之前的状态。

第二步通过binlog定位到完整备份之后到当前删库之前的偏移量得到起始偏移量和结束偏移量(或者时间)。

第三步准确恢复校对数据。

思路校验:

先创建测试库测试表然后通过循环进行插入数据。

mysql -uroot -p12345 -e "create database testdb charset=utf8;"

mysql -uroot -p12345 -e "create table testdb.testtb1(id int);"

for i in {1..1000};do mysql -uroot -p12345 -e "insert into testdb.testtb1(id) values($i);" ;done

然后通过mysqldump对数据库testdb进行完整备份。

mysqldump -uroot -p12345 --flush-logs -B testdb > /tmp/testdb.sql

#参数-B和不加有何区别?

#1.区别就在于加参数B后可以指定备份数据库列表。

#2.生成的数据库备份文件中含有创建数据库和选择数据库的指令。

#CREATE DATABASE /*!32312 IF NOT EXISTS*/ `testdb` /*!40100 DEFAULT CHARACTER SET utf8 */;

#USE `testdb`;

模拟用户写入数据1001条。

for i in {3000..4000};do mysql -uroot -p12345 -e "insert into testdb.testtb1(id) values($i);" ;done && mysql -uroot -p12345 -e "insert into testdb.testtb1(id) values(666);" ;

然后测试删库

mysql -uroot -p12345 -e "drop database testdb;"

然后使用完整备份记录进行恢复。

mysql -uroot -p12345 < testdb.sql

使用上次完整备份的记录恢复后,中间还有用户写入的1000条数据,可以通过binlog偏移量(时间差)进行恢复,先定位上次备份结束的位置

[root@db-master tmp]# mysql -uroot -p12345 -e "select count(*) from testdb.testtb1"

mysql: [Warning] Using a password on the command line interface can be insecure.

+----------+

| count(*) |

+----------+

| 1000 |

+----------+

# at 869709

#191015 0:19:55 server id 1 end_log_pos 875708 CRC32 0x9b4fd429 Querythread_id=3032exec_time=0error_code=0

SET TIMESTAMP=1571069995/*!*/;

******************************************************************************************

内容过多省略

******************************************************************************************

# at 875708

#191015 0:19:56 server id 1 end_log_pos 296078 CRC32 0x84203c10 Querythread_id=4040exec_time=0error_code=0

SET TIMESTAMP=1571071195/*!*/;

insert into testdb.testtb1(id) values(666)

/*!*/;

通过偏移量进行恢复(也可以指定binlog文件和结束偏移量恢复到删库前一条,不带开始偏移量。)

mysqlbinlog --strat-position=869709 --stop-position=875708 /logfile/lqh.000001 | mysql -uroot -p12345

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值