当前位置:网站首页>Several ways to recover tidb data from accidental deletion

Several ways to recover tidb data from accidental deletion

2022-06-11 17:33:00 On the way to data communication

One 、 background

In the daily operation, it is unavoidable to make some mistakes , What can I do at this time , There are different ways for a database that doesn't work , Most of them are restored by backup , There are other unique methods , Such as mysql Of binlog, that tidb What is the unique method , Let's introduce

Two 、 Environmental preparation

#  Create tables and prepare data 
create table recover_test(id int(10));
insert into recover_test values(1),(2),(3),(4);

#  see gc Related parameters ,tikv_gc_life_time,tikv_gc_safe_point,tikv_gc_safe_point
select * from mysql.tidb where VARIABLE_NAME like '%gc%';
tikv_gc_life_time

3、 ... and 、 Recovery method

1.flashback

describe : DROP or TRUNCATE The deleted table is in tikv_gc_safe_point After time , Can be used FLASHBACK TABLE Syntax to restore .
Be careful : Downstream is mysql Because of mysql This syntax is not supported to cause cdc perhaps binlog Synchronization failure

#  Clear the table 
truncate table recover_test;
#  Restore table data 
flashback table recover_test to recover_test_1;
#  Delete table 
drop table recover_test;
#  Restore table 
flashback table recover_test;
#  Second recovery table , You can see an error report below , because  FLASHBACK  Of the recovered table  table ID  Or the deleted table  table ID, and  TiDB  All remaining tables are required  table ID  It has to be globally unique , So delete the table once , Cannot recover twice 
flashback table recover_test to recover_test_2;
ERROR 1050 (42S01): Table 'recover_test' already been flashback to 'recover_test', can't be flashback repeatedly
 

2.recover

describe : DROP The deleted table is in tikv_gc_safe_point After time , use recover Syntax to restore .
Be careful : Downstream is mysql Because of mysql This syntax is not supported to cause cdc perhaps binlog Synchronization failure

#  Delete table 
drop table recover_test;
#  Restore table 
recover table recover_test;

3.dumpling --snapshot

describe : dml Update table or drop,truncaate Is in tikv_gc_safe_point After time , use snapshot Syntax to restore .

#  Delete data 
delete from recover_test where id = 3;
#  Export snapshot time , Note that this time should be greater than tikv_gc_safe_point
dumpling --snapshot "2022-06-02 17:12:45"
#  Import the exported file 
lighting ...

4.select into outfile

describe : dml Update table or drop,truncaate Is in tikv_gc_safe_point After time , use snapshot Syntax to restore .

#  Delete data 
delete from recover_test where id = 3;
#  Set the snapshot time 
set @@tidb_snapshot="2022-06-02 15:50:26";
# select into outfile export , Be careful , Which is connected tidb, Where is the location of the exported file tidb On 
select * from recover_test into outfile '/data/tidb/a.txt';
#  Import data 
load data ... 

5. Historical backup recovery

lighting
br

原网站

版权声明
本文为[On the way to data communication]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111719373661.html