The oracle recovery table deletes data by mistake, and restores the deleted table and its data
Preparatory work (all steps 1 and 2 must be operated by system users):
1. Open the archive log
#查看归档日志是否开启
su - oracle
sqlplus / as sysdba
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 28
Next log sequence to archive 30
Current log sequence 30
#开启归档日志
su - oracle
sqlplus / as sysdba
SQL>shutdown immediate;
SQL>startup mount;
SQL>alter database archivelog;
SQL>alter database open;
SQL>archive log list;
2. Turn on flashback
su - oracle
sqlplus / as sysdba
#检查是否开启闪回
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
NO
#开启闪回
SQL> alter database flashback on;
1. Accidentally deleted data and small data recovery
#查询表tableA被删除的数据,这里时间'xxxx-xx-xx xx:xx:xx'指该时间点前tableA的表数据。
#注意:该时间不能是未来时间。举个栗子,现在时间是2020-10-26 09:12:00。执行查询2020-10-26 10:12:00前的数据,
#就会报ORA-08186: 指定的时间戳无效,因为2020-10-26 10:12:00是未来时间
select * from tableA as of timestamp
to_timestamp('xxxx-xx-xx xx:xx:xx','yyyy-mm-dd hh24:mi:ss')
where tableA中字段 not in (
select tableA中字段 from tableA
)
#恢复tableA中被删除的数据(将'xxxx-xx-xx xx:xx:xx'前不存在于现在时间tableA表的数据插入到tableA中)
INSERT INTO tableA select * from tableA as of timestamp
to_timestamp('xxxx-xx-xx xx:xx:xx','yyyy-mm-dd hh24:mi:ss')
where tableA中字段 not in (
select tableA中字段 from tableA
)
#实例
#查询B_IMAGE_LOG 中被删除的数据('2020-10-26 09:12:00'前的B_IMAGE_LOG中数据和现在的B_IMAGE_LOG做对比)
select * from B_IMAGE_LOG as of timestamp
to_timestamp('2020-10-26 09:12:00','yyyy-mm-dd hh24:mi:ss')
where busi_num not in (
select busi_num from B_IMAGE_LOG
)
#恢复B_IMAGE_LOG 中被删除的数据
INSERT INTO B_IMAGE_LOG select * from B_IMAGE_LOG as of timestamp
to_timestamp('2020-10-26 09:12:00','yyyy-mm-dd hh24:mi:ss')
where busi_num not in (
select busi_num from B_IMAGE_LOG
)
Two, flashback table (update/insert/delete)
#闪回表就是对表的数据做回退,回退到之前的某个时间点,其利用的是undo的历史数据,与undo_retention设置有关,默认是900s
su - oracle
sqlplus / as sysdba
SQL> show parameter undo_
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
#修改undo_retention参数未10800
ALTER SYSTEM SET undo_retention=10800 SCOPE=BOTH;
#示例(以下操作为新建普通用户)
#启用行移动功能
alter table TEST enable row movement;
#恢复TEST表的数据到2020-10-26 14:20:00前的数据
flashback table TEST to timestamp to_timestamp('2020-10-26 14:20:00','yyyy-mm-dd hh24:mi:ss');
3. Recover deleted tables and their data. Flashback DROP (drop table)
su - oracle
sqlplus / as sysdba
SQL> conn 用户名/用户密码;
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
TEST BIN$souMg34DMq3gUAEKfAFufA==$0 TABLE 2020-10-26:11:39:23
#法一恢复删掉的表
flashback table TEST to before drop;
#法二按RECYCLEBIN NAME恢复删掉的表
flashback table "BIN$souMg34DMq3gUAEKfAFufA==$0" to before drop;
#恢复删掉的表,并且重命名
flashback table TEST to before drop rename to TEST2;
Reference materials: https://www.jb51.net/article/130703.htm