利用闪回恢复truncate表的数据
2017-01-07 18:03
369 查看
因为truncate不是DML语句,是DDL语句,不能使用闪回查询的方式恢复表数据,这里介绍一种通过flashback database的方式恢复数据的方法。
1. 创建测试表
SQL> create table t as select * from all_objects where rownum<11; Table created. SQL> select object_id from t where rownum<11; OBJECT_ID ---------- 100 116 117 280 365 367 368 370 371 373 10 rows selected.
2. 开始数据库闪回
SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount; ORACLE instance started. Total System Global Area 776646656 bytes Fixed Size 2257272 bytes Variable Size 486542984 bytes Database Buffers 285212672 bytes Redo Buffers 2633728 bytes Database mounted. SQL> archive log list Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 1 Next log sequence to archive 4 Current log sequence 4 SQL> alter database flashback on; Database altered. SQL> alter database open; Database altered.
3. 检查闪回数据库的必要条件
SQL> show parameter db_recovery NAME TYPE ------------------------------------ --------------------------------- VALUE ------------------------------ db_recovery_file_dest string /u01/app/oracle/fast_recovery_ area db_recovery_file_dest_size big integer 4182M SQL> archive log list Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 1 Next log sequence to archive 4 Current log sequence 4 SQL> select flashback_on from v$database; FLASHBACK_ON ------------------------------------------------------ YES
4. 模拟truncate丢失数据
SQL> select count(*) from t; COUNT(*) ---------- 10 SQL> select sysdate from dual; SYSDATE ------------------- 2017-01-07 15:00:18 SQL> truncate table t; Table truncated.
5. 闪回数据库到数据仍然存在的时间点
QL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 776646656 bytes Fixed Size 2257272 bytes Variable Size 486542984 bytes Database Buffers 285212672 bytes Redo Buffers 2633728 bytes Database mounted. SQL> flashback database to timestamp to_timestamp('2017-01-07 15:00:18','yyyy-mm-dd hh24:mi:ss'); Flashback complete.
6. 闪回数据库之后,可以通过alter database open resetlogs的方式打开,但闪回时间之后的数据就会全部丢失。这里用read only的模式打开数据库
SQL> alter database open read only; Database altered.
7. 导出被truncate表的数据
[oracle@centos6 ~]$ exp test_fb/oracle file=t.dmp tables=t; Export: Release 11.2.0.4.0 - Production on Sat Jan 7 15:07:10 2017 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Rele 4000 ase 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options Export done in UTF8 character set and AL16UTF16 NCHAR character set server uses ZHS16GBK character set (possible charset conversion) About to export specified tables via Conventional Path ... . . exporting table T 10 rows exported Export terminated successfully without warnings.
8. 重启数据库,并恢复到闪回之前的时间点
SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount; ORACLE instance started. Total System Global Area 776646656 bytes Fixed Size 2257272 bytes Variable Size 486542984 bytes Database Buffers 285212672 bytes Redo Buffers 2633728 bytes Database mounted. SQL> recover database; Media recovery complete. SQL> alter database open; Database altered. SQL> select count(*) from t; #此时t表仍然没有数据 COUNT(*) ---------- 0
9. 导入t表的数据
[oracle@centos6 ~]$ imp test_fb/oracle file=t.dmp tables=t ignore=y; Import: Release 11.2.0.4.0 - Production on Sat Jan 7 15:09:54 2017 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options Export file created by EXPORT:V11.02.00 via conventional path import done in UTF8 character set and AL16UTF16 NCHAR character set import server uses ZHS16GBK character set (possible charset conversion) . importing TEST_FB's objects into TEST_FB . importing TEST_FB's objects into TEST_FB . . importing table "T" 10 rows imported Import terminated successfully without warnings.
10. 验证数据已经恢复
SQL> select count(*) from t; COUNT(*) ---------- 10
相关文章推荐
- http协议(十一)http与https
- QT Webkit 多线程执行 javascript 崩溃问题
- 用python读取cifar-10与cifar-100图像数据
- supervisor 一句话安装
- 读取联系人信息
- spring+hibernate 大批量处理数据
- centos 32位下安装gitlab
- 安卓设计模式之_观察者模式
- kibana运行的时候找不到node命令
- kibana一个最简单的过滤
- C#中基于GDI+(Graphics)图像处理系列之文字或者图片水印(透明、任意角度旋转)
- yii2实现分页,带搜索的分页功能示例
- Eclipse实现Android混淆打包注意事项
- background-position 百分比问题,精灵图的实现
- CSS中background:url(图片) 不能显示的问题
- python fabric简单的配置账号和密码
- 生产者实现应用内分布式事务管理
- Linux基础介绍【第九篇】
- Android 开发工程师面试指南
- JXL和POI两种导出excel方式