数据文件恢复通过数据文件和归档日志和联机重做日志(正在使用中的)达到恢复效果
恢复数据库其实是做两个操作
一restore(恢复数据文件)//完全恢复哪个文件有问题恢复哪个,不完全恢复数据库完全恢复
二recover(写日志)
1. redo(roll forward)//前滚 照着归档日志完全回放
2. undo(roll back) //撤销 回滚没有提交的工作
数据文件
RPO/RTO
数据文件:
不归档方式下丢失一个数据文件://不归档模式下备份直接用cp
SQL> archive log list
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('friday, before backup');
SQL> commit;
备份:
SQL>查询v$datafile, v$logfile, v$tempfile, v$controlfile
SQL> shutdown immediate
$ cd $ORACLE_BASE/oradata/
$ cp -r orcl orcl.bak//复制目录
$ cd $ORACLE_BASE/fast_recovery_area/orcl
$ cp control02.ctl control02.ctl.bak
SQL> startup
备份后工作:
SQL> insert into t1 values ('monday, after backup');
SQL> commit;
故障:
SQL> alter system flush buffer_cache;
$ cd $ORACLE_BASE/oradata/orcl
$ >users01.dbf//输出重定向
SQL> select * from t1; 报错
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
$ dbv file=/u01/app/oracle/oradata/orcl/users01.dbf
恢复:
SQL> shutdown abort
$ cd $ORACLE_BASE/oradata
$ rm -rf orcl
$ mv orcl.bak orcl
$ cd $ORACLE_BASE/fast_recovery_area/orcl
$ mv control02.ctl.bak control02.ctl
SQL> startup
SQL> select * from t1;
归档模式下丢失一个数据文件:
SQL> archive log list
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('friday, before backup');
SQL> commit;
备份:
RMAN> backup tablespace users tag "tbs_users_weekend_backup";
备份后工作:
SQL> select group#, sequence#, status, archived from v$log;
SQL> insert into t1 values ('after backup, logseq 7, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 8, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 9, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 10, current');
SQL> commit;
SQL> insert into t1 values ('after backup, logseq 10, current, uncommitted');
SQL> select * from t1;
故障:
SQL> shutdown abort
$ rm $ORACLE_BASE/oradata/orcl/users01.dbf
SQL> startup 报错
SQL> select open_mode from v$database;//查看当前数据库状态
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log//查看警报日志
恢复:
RMAN> list backup of tablespace users;
RMAN> list archivelog all;
SQL> alter database datafile 4 offline;//暂时离线损坏数据文件 system和undo tbs不能offline
SQL> alter database open;//打开数据库 ,不离线损坏数据文件无法打开数据库 注意 若系统表空间出错,无法离线
RMAN> restore datafile 4;
RMAN> recover datafile 4;
SQL> alter database datafile 4 online;//让离线文件上线
SQL> select * from t1;
通过不完全恢复解决用户的误操作:
SQL> archive log list
备份前工作:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('before backup');
SQL> commit;
备份:
RMAN> backup database tag 'weekend_DB_full_backup';
备份后:
SQL> insert into t1 values ('after backup, before delete');
SQL> commit;
误操作:
SQL> select sysdate from dual;
SQL> select dbms_flashback.get_system_change_number from dual;
SQL> delete t1;
SQL> commit;
SQL> create table after_delete (x int); 正确的操作
SQL> insert into after_delete values (1);
SQL> commit;
恢复:
(shutdown abort
startup mount)= startup force mount;
restore database
recover database until scn/logseq/timestamp //恢复到指定时间点
alter database open resetlogs
//用run包装执行 因为有scn点 所以可以指定恢复时间点
logminer //搜索用户误操作命令所在
RMAN> run {
startup force mount;
set until scn= 1806683;
restore database;
recover database;
alter database open resetlogs;//联机重做日志清零
}
set until time=’2015-10-26 11:13:23’; 基于时间点恢复
SQL> select * from t1;
SQL> select * from after_delete; 丢失
SQL> select group#, sequence#, status, archived from v$log;
通过不完全恢复解决归档日志不连续:
SQL> archive log list
备份前:
SQL> create table t1(x varchar2(50)) tablespace users;
SQL> insert into t1 values ('before backup');
SQL> commit;
备份:
RMAN> backup database tag 'weekend_DB_full_backup';
备份后:
SQL> select GROUP#, SEQUENCE#, STATUS, archived from v$log;
SQL> insert into t1 values ('after backup, logseq 1, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 2, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 3, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 4, archived');
SQL> commit;
SQL> alter system switch logfile;
SQL> insert into t1 values ('after backup, logseq 5, current');
SQL> commit;
SQL> alter system checkpoint;
故障:
SQL> shutdown abort
$ rm /u01/app/oracle/oradata/orcl/users01.dbf
$ rm /u01/app/oracle/fast_recovery_area/ORCL/archivelog/2016_03_01/o1_mf_1_5_cfbcxo84_.arc
恢复:
SQL> startup
先尝试只恢复一个数据文件失败。
RMAN> run {
startup force mount;
set until sequence 5;//写5则恢复到4 写n即恢复到
restore database;
recover database;
alter database open resetlogs;
}
SQL> select * from t1;
控制文件
丢失部分控制文件:
SQL> select * from v$controlfile;
$ >/u01/app/oracle/oradata/orcl/control01.ctl
SQL> select * from v$tablespace; 报错
SQL> alter system checkpoint; 报错
$ vi /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
SQL> shutdown abort
SQL> startup nomount
SQL> show parameter control_files
$ cp /u01/app/oracle/fast_recovery_area/orcl/control02.ctl /u01/app/oracle/oradata/orcl/control01.ctl
SQL> alter database mount;
SQL> alter database open;
丢失全部控制文件(有自动备份):
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> backup datafile 4;
$ >/u01/app/oracle/oradata/orcl/control01.ctl
$ >/u01/app/oracle/fast_recovery_area/orcl/control02.ctl
SQL> select * from v$tablespace; 报错
SQL> alter system checkpoint; 报错
SQL> shutdown abort
SQL> startup nomount
RMAN> restore controlfile from autobackup;
RMAN> alter database mount;
RMAN> recover database;
RMAN> alter database open resetlogs;
丢失全部控制文件(没有自动备份):
SQL> alter database backup controlfile to '/home/oracle/control.bak';
SQL> alter database backup controlfile to trace;
SQL> select * from v$diag_info;
//不管在oracle中遇到任何问题,只要数据库还没关,就不要手动去关闭,尽量在数据库中解决问题
spfile
有自动备份:
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> backup datafile 4;
联机恢复:
$ mv $ORACLE_HOME/dbs/spfileorcl.ora $ORACLE_HOME/dbs/spfileorcl.ora.bak
SQL> alter system set resource_limit=true; 报错
SQL> create spfile='/home/oracle/spfile.bak' from memory;//当数据库还在连接中发现问题时,可用此语句恢复
$ mv /home/oracle/spfile.bak $ORACLE_HOME/dbs/spfileorcl.ora
脱机恢复:
SQL> shutdown immediate
$ mv $ORACLE_HOME/dbs/spfileorcl.ora $ORACLE_HOME/dbs/spfileorcl.ora.bak
RMAN> startup//在rman下执行startup 使之启动实例但不通过配置参数
RMAN> restore spfile from '/u01/app/oracle/fast_recovery_area/ORCL/autobackup/2015_10_26/o1_mf_s_894118741_c2vkgo8x_.bkp';
RMAN> startup force //重启
没有备份:
利用alert_orcl.log中的参数值,构造initorcl.ora //在最后1次启动下找到参数复制保存到initorcl.ora
SQL> create spfile='/home/oracle/spfile.bak' from pfile;
利用备份init.ora:
$ vi /u01/app/oracle/product/11.2.0/db_1/dbs/init.ora 完善参数
SQL> startup pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/init.ora'
SQL> create spfile from pfile;