一、完全恢复
控制文件,数据文件,表空间丢失,且是物理上的丢失
二、不完全恢复
联机日志,表空间丢失使用居于时间点的不完全恢复
三、克隆技术
数据库是7*24小时运行,drop table,drop user
四、闪回技术
误dml操作
一、完全恢复
数据库发生结构上的丢失,丢表空间,数据文件,控制文件丢失,参数文件丢失,可将数据库恢复到误操作最近的时刻点
不适用与7*24小时数据库,因为需要当机
1.参数文件丢失
在rman中可以在没有参数文件的状态下,启动到nomount状态
rman>>crosscheck backup ; //检查备份是否可用
rman>>list backup of spfile; //不能查找到spfile的路径,我们只能在物理位置上找到
rman>>restore spfile from '/备份参数文件的路径' ;//还原参数文件
rman>>startup force nomount;
rman>>sql 'alter database mount';
rman>>alter database open ;
可以将上面的恢复步骤写到一个run里面
run
{
restore spfile from '/';
startup foece nomount ;
sql 'alter database mount ';
sql 'alter database open ';
}
2.控制文件丢失
rman>>startup nomount;
rman>>restore controlfile from '/';
rman>>alter database mount ;
rman>>alter database open;
也可以写入run 当中
如果用alter database open reserlogs 打开了数据库,一定要再进行备份。
3.数据文件丢失
1)不可离线表空间丢失
只能在mount状态下恢复
rman>>startup mount
rman>>restore tablespace users ; //使用于数据文件较多的表空间
restore datafile <编号>; //适用于表空间下的数据文件较少的情况
rman>>recover database ;
rman>>later database open;
2)可离线表空间丢失
在数据库open:
>>>alter database datafile 4/路径 offline; //将表空间下的数据文件离线
rman>>restore datafile <>;
rman>>recover datafile <>;
>>>alter database datafile <> online;
在数据库mount:---可以与不能离线的表空间一样的操作
>>>startup mount;
>>>alter database datafile 4 offline immediate;
>>>alter database open;
>>>restore datafile <>;
>>>recover datafile <>;
>>>alter database datafile <> online;
二、不完全恢复
1.基于时间点不完全恢复
误操作模拟:
drop table <>
drop user
drop tablespace test --- 11:00 误删---<此间有操作>---14:00发现
a>确认有误操作之前的备份,在关机之后做数据库的冷备
b>启动到nomount ,使用以前的控制文件恢复,使用基于时间点的不玩全恢复,恢复到11点
restore controlfile from '/u01/oracle/fast_recovery_area/MAXDB/backupset/2016'
c> 使用导出的方式将表空间导出,使用冷备份恢复数据库,再将表空间导入
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';//转化时间格式
set until time '2016-12-13 10:56:46'; //基于时间点的不完全恢复
restore database ;
recover database ;
SQL> create tablespace test datafile '/opt/u01/oracle/oradata/maxdb/test01.dbf' size 20m;
Tablespace created.
SQL> create table test1 tablespace test as select * from dept;
SQL> create table test2 tablespace test as select * from emp;
[oracle@up12 maxdb]$ rman target /
rman>backup database //备份数据库
[oracle@up12 2016_12_13]$ pwd
/opt/u01/oracle/fast_recovery_area/MAXDB/backupset/2016_12_13 /在目录下会生成相应的备份文件
[oracle@up12 2016_12_13]$ ls
o1_mf_ncsnf_TAG20161213T164210_d4zf4o47_.bkp o1_mf_nnndf_TAG20161213T164210_d4zf34kt_.bkp
SQL> select group#,member from v$logfile;
GROUP# MEMBER
---------- --------------------------------------------------
4 /opt/u01/oracle/oradata/maxdb/redo04.log
5 /opt/u01/oracle/oradata/maxdb/redo05.log
6 /opt/u01/oracle/oradata/maxdb/redo06.log
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; //记下删除的时间
TO_CHAR(SYSDATE,'YY
-------------------
2016-12-13 16:50:40
SQL> drop tablespace test including contents and datafiles;
Tablespace dropped.
再进行一些相应的dml操作
将我们的数据库,控制文件进行冷备份
RMAN> startup nomount
restore controlfile from '/opt/u01/oracle/fast_recovery_area/MAXDB/backupset/2016_12_13/o1_mf_ncsnf_TAG20161213T164210_d4zf4o47_.bkp‘
RMAN> startup mount
RMAN>
run
{
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
set until time '2016-12-13 14:05:35';
restore database;
recover database;
}
SQL> alter tablespace test read only;
[oracle@up12 maxdb]$ exp \'/ as sysdba\' file='/tmp/test33.dump' transport_tablespace=y tablespaces=test;
关闭数据库
还原我们刚才冷备的控制文件和数据文件
打开数据库
[oracle@up12 maxdb]$ imp \'/ as sysdba \' file='/tmp/test33.dump' tablespaces=test transport_tablespace=y datafile='/opt/u01/oracle/oradata/maxdbbak/test01.dbf'; //这里的数据文件来自于我们冷备之后的数据文件
SQL> alter tablespace test read write;
到这一步我们的数据库就完全恢复了
基于scn号的恢复
run{
set until scn 1580608;
restore database;
recover database;
}
基于日志序号的恢复
run{
set until sequece 2;
restore database;
recover database;
}
drop table <>purge;
drop user <> cascade;
2.数据克隆
解决对user、table、dml的误操作,还有备库的搭建
10g:--克隆步骤
1) 目标库做备份
2)新库中
a.创建密码文件,参数文件 (可从目标库备份过来,或手动创建)
b.准备要存放数据库的目录
c.目标库所做的备份同步到数据库中,包括归档
rsync -avLR -e ssh ip:/ ./ //不能使用拷贝的方式
d.使用备份还原一个数据出来
启动到nomount ,恢复控制文件,
启动到mount ,恢复数据文件
alter database open resetlogs ;
11g---在线克隆,但需要配置网络
两个数据库都需要准备密码文件
duplicate target database <> to <> nofilenamecheck from active database; //支持在线克隆 from active database --完全克隆
目标库
1).配置监听 listener.ora ---放在grid目录下
vim /opt/u01/11.2.0/grid/network/admin/listener.ora
#listener.ora Network Configuration File: /opt/u01/11.2.0/grid/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = max.com)
(ORACLE_HOME = /opt/u01/oracle/11g)
(SID_NAME = maxdb)
)
(SID_DESC =
(GLOBAL_DBNAME = dupdb)
(ORACLE_HOME = /opt/u01/oracle/11g)
(SID_NAME = dupdb)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = up12.up.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /opt/u01/oracle
ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER=ON # line added by Agent
2).配置tnsnames.ora ----放在oracle目录下
vim /opt/u01/oracle/11g/network/admin/tnsnames.ora
MAXDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.153)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER=DEDICATED)
(SERVICE_NAME = max.com)
)
)
DUPDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.153)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = dupdb)
)
)
3).重启监听
. .grid //在grid环境变量下重启
lsnrctl stop
lsnrctl start
新库
1).准备参数文件
vim /opt/u01/oracle/11g/dbs/initdupdb.ora
control_files='/opt/u01/oracle/oradata/dupdb/control01.ctl','/opt/u01/oracle/fast_recovery_area/dupdb/control02.ctl'
db_block_size=8192
db_name='dupdb'
db_recovery_file_dest='/opt/u01/oracle/fast_recovery_area'
db_recovery_file_dest_size=4g
undo_tablespace='UNDOTBS2' //这里的表空间必须和源库的表空间位置一致
2).准备密码文件
cd /u01/oracle/11g/dbs
orapwd file=orapwdupdb password=oracle
3).创建存放数据库的目录
mkdir /u01/oracle/oradata/dupdb
mkdir /u01/oracle/fast_recovery_area/dupdb
mkdir /u01/oracle/admin/dupdb/{a,dp}dump -p //创建审计文件
4).配置监听,tnsname.ora
同目标数据库一致
tnsping dupdb //检验监听是否启动
tnsping maxdb
两个库都要可以远程登录才可以用
rlwrap sqlplus sys/oracle@dupdb as sysdba
rlwrap sqlplus sys/oracle@maxdb as sysdba
5).执行在线克隆 ---在线克隆时,两个库都是打开的
启动到nomount状态
export ORACLE_SID=dupdb
sqlplus / as sysdba
startup nomount
rman target sys/oracle@updb auxiliary sys/oracle@dupdb
rman>>duplicate target database to dupdb nofilenamecheck from active database db_file_name_convert=('/opt/u01/oracle/oradata/maxdb','/opt/u01/oracle/oradata/dupdb') logfile group 1 ('/opt/u01/oracle/oradata/dupdb/redo01.log') size 100m,group 2 ('/opt/u01/oracle/oradata/dupdb/redo02.log') size 100m;
//db_file_name_convert 进行文件路径的转化
居于时间克隆(使用于7*24小时的数据库)----
倘若是丢了结构的我们就不可以使用可能,只能够使用完全恢复,或者不完全恢复
日志挖掘---logmnr
dbms_logmnr //这个工具主要用来分析日志文件记录的数据
SQL> desc dbms_logmnr
1>将日志传到logmnr
2>分析日志
3>释放内存
日志分析完后的结果放在v$logmnr_contents
打开附加日志supplemental,将用户所做的所有操作都记录到日志当中,不做日志挖掘是不会打开的
附加日志的分类
supplemental:min //用户所做的DML操作,所有字段都可以记录
primary key //记录用户对主键的操作
unique
foregin key
all //上面的所有信息都可以记录
开打附加日志
alter database add supplemental log data all ; //以所有方式打开
alter database add supplemental log data ; //以最小的方式打开 ,工作中一般用这种
alter database add supplemental log data (primary key)columns;
select supplemental_log_data_pk from v$database
关闭附加日志
alter database drop supplemental log data
exec dbms_logmnr.add_logfile(logfilename=>'/u01/oracle/oradata/maxdb/redo06.log'); //11g才可以使用
exec dbms_logmnr.add_logfile('/u01/oracle/oradata/maxdb/redo06.log');
exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog); //分析日志
select * from v$logmnr_contents; //查看我们挖掘的内容
select sql_redo ,sql_undo,scn ,start_scn ,to_char(timestamp,'yyyy-mm-dd hh24:mi:ss') timestamp from v$logmgr_contents where table_name='表名' ;//sql_undo 里记录的是反操作,sql_redo里面记录的是用户的操作
exec dbms_logmnr.end_logmnr //结束后释放内存
模拟删表误操作,通过基于时间点的方式恢复
先备份数据库
SQL> alter database add supplemental log data; //打开我们的最小附加日志文件
Database altered.
SQL> select supplemental_log_data_min from v$database; //查看我们的附加日志文件是否打开
SUPPLEME
--------
YES
可以删表了,我们删除的是A10,A11,A12
drop table <>purge ;
SQL> select group# ,status,member from v$logfile;
GROUP# STATUS MEMBER
---------- ------- ----------------------------------------
4 /opt/u01/oracle/oradata/maxdb/redo04.log
5 /opt/u01/oracle/oradata/maxdb/redo05.log
6 /opt/u01/oracle/oradata/maxdb/redo06.log
SQL> select group# ,status ,members from v$log; //工作中因为不知道当前使用的是哪个日志文件,我们只能加入归档日志分析。查看我们当前使用的日志主要是为节约时间,工作中就一个一个的去找吧
GROUP# STATUS MEMBERS
---------- ---------------- ----------
4 CURRENT 1
5 INACTIVE 1
6 INACTIVE 1
SQL> exec dbms_logmnr.add_logfile(logfilename=>'/opt/u01/oracle/oradata/maxdb/redo04.log'); //传入日志文件
PL/SQL procedure successfully completed.
SQL> exec dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog); //进行日志分析
PL/SQL procedure successfully completed.
SQL> select sql_redo ,scn,start_scn,commit_scn from v$logmnr_contents where table_name='A10' ; //找到我们删除表的scn号
SQL_REDO
--------------------------------------------------------------------------------
SCN START_SCN COMMIT_SCN
---------- ---------- ----------
ALTER TABLE "SCOTT"."A10" RENAME TO "BIN$Q5oqz6hbP6XgU5kBqMCOIw==$0" ;
2022773
drop table A10 AS "BIN$Q5oqz6hbP6XgU5kBqMCOIw==$0" ;
2022775
执行克隆数据库的准备工作
目标库 (监听的库也可以用netmgr配置)
1).配置监听 listener.ora ---放在grid目录下
vim /opt/u01/11.2.0/grid/network/admin/listener.ora
# listener.ora Network Configuration File: /opt/u01/11.2.0/grid/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = max.com)
(ORACLE_HOME = /opt/u01/oracle/11g)
(SID_NAME = maxdb)
)
(SID_DESC =
(GLOBAL_DBNAME = dupdb)
(ORACLE_HOME = /opt/u01/oracle/11g)
(SID_NAME = dupdb)
)
(SID_DESC =
(GLOBAL_DBNAME = nupdb)
(ORACLE_HOME = /opt/u01/oracle/11g)
(SID_NAME = nupdb)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = up12.up.com)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /opt/u01/oracle
ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER=ON # line added by Agent
2).配置tnsnames.ora ----放在oracle目录下
vim /opt/u01/oracle/11g/network/admin/tnsnames.ora
MAXDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.153)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER=DEDICATED)
(SERVICE_NAME = max.com)
)
)
DUPDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.153)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = dupdb)
)
)
NUPDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.153)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = nupdb)
)
)
3).重启监听
. .grid //在grid环境变量下重启
lsnrctl stop
lsnrctl start
新库
1).准备参数文件
vim /opt/u01/oracle/11g/dbs/initnupdb.ora
control_files='/opt/u01/oracle/oradata/nupdb/control01.ctl','/opt/u01/oracle/fast_recovery_area/nupdb/control02.ctl'
db_block_size=8192
db_name='nupdb'
db_recovery_file_dest='/opt/u01/oracle/fast_recovery_area'
db_recovery_file_dest_size=4g
undo_tablespace='UNDOTBS2'
2).准备密码文件
cd /u01/oracle/11g/dbs
orapwd file=orapwnupdb password=oracle
3).创建存放数据库的目录
mkdir /u01/oracle/oradata/nupdb
mkdir /u01/oracle/fast_recovery_area/nupdb
mkdir /u01/oracle/admin/nupdb/{a,dp}dump -p //创建审计文件
4).配置监听,tnsname.ora
同目标数据库一致
tnsping nupdb //检验监听是否启动,要在.db环境变量下验证
tnsping maxdb
两个库都要可以远程登录才可以用
rlwrap sqlplus sys/oracle@nupdb as sysdba
rlwrap sqlplus sys/oracle@maxdb as sysdba
5).执行在线克隆 ---在线克隆时,两个库都是打开的
启动到nomount状态
export ORACLE_SID=nupdb
sqlplus / as sysdba
startup nomount
rlwrap rman target sys/oracle@maxdb auxiliary sys/oracle@nupdb
run
{
set until scn 2022774; //也可以用时间,这次我们用从日志挖掘里找到的scn号,回到误操作的前一步即scn-1
duplicate target database to nupdb nofilenamecheck db_file_name_convert=('/opt/u01/oracle/oradata/maxdb','/opt/u01/oracle/oradata/nupdb') logfile group 1 ('/opt/u01/oracle/oradata/nupdb/redo01.log') size 100m,group 2 ('/opt/u01/oracle/oradata/nupdb/redo02.log') size 100m;
}
再来进行我们的表恢复
[oracle@up12 ~]$ . .db
[oracle@up12 ~]$ export ORACLE_SID=nupdb
[oracle@up12 ~]$ exp scott/tiger file=/tmp/A121.dmp tables=A12
[oracle@up12 ~]$ . .db
[oracle@up12 ~]$ imp scott/tiger file=/tmp/A121.dmp tables=A12
来源:oschina
链接:https://my.oschina.net/u/2918364/blog/805674