--首先Oracle日志有归档和非归档模式
SELECT * FROM v$logfile;
-- 并且Oracle的日志文件是循环使用的,下面我们来做个例子:
--向该表中插入数据
INSERT INTO T_TEST_1 SELECT * FROM T_TEST_1;
--再查下当前的日志文件,可以发现日志文件已经切换
SELECT * FROM v$log;
--这时在/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02目录下会生成相应的归档文件
[lubinsu@localhost 2013_06_02]$ ll
total 126720
-rw-r----- 1 oracle oinstall 34198528 Jun 2 10:15 o1_mf_1_154_8tobp66g_.arc
-rw-rw---- 1 oracle oinstall 47694848 Jun 2 10:36 o1_mf_1_155_8tocwsgw_.arc
-rw-rw---- 1 oracle oinstall 47698432 Jun 2 10:37 o1_mf_1_156_8tocz0l8_.arc
--也就是说归档日志是在日志发生切换的时候即生成,并且Oracle通过序列号SEQUENCE#来唯一标识文件的内容:
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 155 52428800 1 YES ACTIVE 1322946393641 2013/6/2 10
2 1 156 52428800 1 NO CURRENT 1322946394098 2013/6/2 10
3 1 154 52428800 1 YES INACTIVE 1322946390294 2013/5/25 1
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 155 52428800 1 YES ACTIVE 1322946393641 2013/6/2 10
2 1 156 52428800 1 NO ACTIVE 1322946394098 2013/6/2 10
3 1 157 52428800 1 NO CURRENT 1322946394167 2013/6/2 10
--我们可以强制切换日志来归档;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 158 52428800 1 YES ACTIVE 1322946394717 2013/6/2 10
2 1 159 52428800 1 NO CURRENT 1322946395548 2013/6/2 10
3 1 157 52428800 1 YES ACTIVE 1322946394167 2013/6/2 10
SQL> alter system switch logfile;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 158 52428800 1 YES ACTIVE 1322946394717 2013/6/2 10
2 1 159 52428800 1 YES ACTIVE 1322946395548 2013/6/2 10
3 1 160 52428800 1 NO CURRENT 1322946395551 2013/6/2 10
--并且已经归档
[lubinsu@localhost 2013_06_02]$ ll
total 214320
-rw-r----- 1 oracle oinstall 34198528 Jun 2 10:15 o1_mf_1_154_8tobp66g_.arc
-rw-rw---- 1 oracle oinstall 47694848 Jun 2 10:36 o1_mf_1_155_8tocwsgw_.arc
-rw-rw---- 1 oracle oinstall 47698432 Jun 2 10:37 o1_mf_1_156_8tocz0l8_.arc
-rw-rw---- 1 oracle oinstall 47693312 Jun 2 10:40 o1_mf_1_157_8tod4rmb_.arc
-rw-rw---- 1 oracle oinstall 41899008 Jun 2 10:44 o1_mf_1_158_8toddjdx_.arc
--如果使用alter system archive log current;也可以进行归档,要归档一定要切换,
--但是切换不一定要归档,所以上面的命令可以使用于归档模式,也可以使用于非归档模式,而下面的
--alter system archive log current;只能使用于归档模式
下面我们来了解一下SCN(SYSTEM CHANGE NUMBER)
我们可以通过下面语句来查询系统的SCN号,SCN号是不断变化的,即使系统没进行任何操作,每3秒钟也会增加一次:
SQL> select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
13229463956339
10G之后可以通过下面的语句来查询:
SELECT a.current_scn FROM v$database a;
备份恢复的概念:
CREATE TABLE t_SCN(ID NUMBER(38), SCN NUMBER);
SELECT * FROM v$log;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 161 52428800 1 NO CURRENT 1322946395561 2013/6/2 10
2 1 159 52428800 1 YES INACTIVE 1322946395548 2013/6/2 10
3 1 160 52428800 1 YES INACTIVE 1322946395551 2013/6/2 10
可知当前日志的开始SCN号是:1322946395561
INSERT INTO t_scn values(1, dbms_flashback.get_system_change_number);
--所以我们进行了插入操作之后,这条语句产生的日志就在161这组日志里
select * from t_scn;--13229463960251
--提交
COMMIT;
保证产生的日志写到日志里面,因为有可能日志存在buffer中;
手动归档
SQL> alter system switch logfile;
System altered.
--查看当前的日志,起始SCN号为:1322946396032
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 161 52428800 1 YES ACTIVE 1322946395561 2013/6/2 10
2 1 162 52428800 1 NO CURRENT 1322946396032 2013/6/2 11
3 1 160 52428800 1 YES INACTIVE 1322946395551 2013/6/2 10
SELECT a.sequence# 日志序号, a.first_change# 日志中最小的SCN号, a.next_change# 日志中最大的SCN号 FROM v$archived_log a WHERE a.sequence# = 161;
--再插入两次值,并切换日志:
INSERT INTO t_scn values(1, dbms_flashback.get_system_change_number);
COMMIT;
alter system switch logfile;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 161 52428800 1 YES INACTIVE 1322946395561 2013/6/2 10
2 1 162 52428800 1 YES ACTIVE 1322946396032 2013/6/2 11
3 1 163 52428800 1 NO CURRENT 1322946396049 2013/6/2 11
INSERT INTO t_scn values(1, dbms_flashback.get_system_change_number);
COMMIT;
alter system switch logfile;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 164 52428800 1 NO CURRENT 1322946396056 2013/6/2 11
2 1 162 52428800 1 YES ACTIVE 1322946396032 2013/6/2 11
3 1 163 52428800 1 YES ACTIVE 1322946396049 2013/6/2 11
SELECT a.sequence# 日志序号, a.first_change# 日志中最小的SCN号, a.next_change# 日志中最大的SCN号 FROM v$archived_log a WHERE a.sequence# >= 161;
--Oracle支持的备份恢复的种类
1.完全脱机备份(适合归档模式和非归档模式)
SHUTDOWN
将Oracle的:
数据文件
控制文件
日志文件备份下来
在对应目录下,备份这些文件即可:
其中:临时文件不需要备份,控制文件只要备份一份即可
[lubinsu@localhost orcl]$ pwd
/home/oracle/oracle/product/10.2.0/oradata/orcl
[lubinsu@localhost orcl]$ ll
total 2855936
-rw-r----- 1 oracle oinstall 7094272 Jun 2 11:19 control01.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 11:19 control02.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 11:19 control03.ctl
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:19 redo01.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:13 redo02.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:15 redo03.log
-rw-r----- 1 oracle oinstall 304095232 Jun 2 11:19 sysaux01.dbf
-rw-r----- 1 oracle oinstall 513810432 Jun 2 11:19 system01.dbf
-rw-r----- 1 oracle oinstall 513810432 Jun 2 11:19 tbs_data.dbf
-rw-r----- 1 oracle oinstall 50339840 May 25 14:20 temp01.dbf
-rw-r----- 1 oracle oinstall 309338112 Jun 2 11:19 undotbs01.dbf
-rw-r----- 1 oracle oinstall 1052516352 Jun 2 11:19 users01.dbf
--备份文件(对于备份,这里只是做个例子,实际上进行异地备份是最安全的方式)
[lubinsu@localhost oradata]$ cp -r orcl /home/lubinsu/
SELECT * FROM user_tables a WHERE a.table_name = 'T_SCN';--在TBS_LUBINSU_DATA表空间中
--再插入几条数据
INSERT INTO t_scn values(1, dbms_flashback.get_system_change_number);
COMMIT;
alter system switch logfile;
SELECT * FROM v$log;
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 164 52428800 1 NO CURRENT 1322946396056 2013/6/2 11
2 1 162 52428800 1 YES INACTIVE 1322946396032 2013/6/2 11
3 1 163 52428800 1 YES INACTIVE 1322946396049 2013/6/2 11
SQL> alter system switch logfile;
System altered
SQL> INSERT INTO t_scn values(2, dbms_flashback.get_system_change_number);
1 row inserted
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 164 52428800 1 YES ACTIVE 1322946396056 2013/6/2 11
2 1 165 52428800 1 NO CURRENT 1322946396143 2013/6/2 11
3 1 163 52428800 1 YES INACTIVE 1322946396049 2013/6/2 11
INSERT INTO t_scn values(2, dbms_flashback.get_system_change_number);
SQL> INSERT INTO t_scn values(2, dbms_flashback.get_system_change_number);
1 row inserted
SQL>
SQL> COMMIT;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 164 52428800 1 YES ACTIVE 1322946396056 2013/6/2 11
2 1 165 52428800 1 YES ACTIVE 1322946396143 2013/6/2 11
3 1 166 52428800 1 NO CURRENT 1322946396145 2013/6/2 11
SQL> INSERT INTO t_scn values(3, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 167 52428800 1 NO CURRENT 1322946396158 2013/6/2 11
2 1 165 52428800 1 YES ACTIVE 1322946396143 2013/6/2 11
3 1 166 52428800 1 YES ACTIVE 1322946396145 2013/6/2 11
SQL> INSERT INTO t_scn values(3, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> INSERT INTO t_scn values(4, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> INSERT INTO t_scn values(4, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> INSERT INTO t_scn values(5, dbms_flashback.get_system_change_number);
1 row inserted
SQL> INSERT INTO t_scn values(6, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL> INSERT INTO t_scn values(7, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit complete
SQL> alter system switch logfile;
System altered
SQL>
--现在插入一条数据,提交,但是不切换日志文件:
SQL> SELECT * FROM v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 170 52428800 1 YES ACTIVE 1322946396162 2013/6/2 11
2 1 171 52428800 1 YES ACTIVE 1322946396163 2013/6/2 11
3 1 172 52428800 1 NO CURRENT 1322946396165 2013/6/2 11
SQL> INSERT INTO t_scn values(8, dbms_flashback.get_system_change_number);
1 row inserted
SQL> commit;
Commit COMPLETE
--最后插入一条数据,但是不提交
INSERT INTO t_scn values(9, dbms_flashback.get_system_change_number);
--这时这条数据可能写到日志文件中也可能没写到日志文件中
--现在我们将数据库的数据文件删除掉
[oracle@localhost orcl]$ ll
total 2855936
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:28 control01.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:28 control02.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:28 control03.ctl
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:54 redo01.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:55 redo02.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 14:28 redo03.log
-rw-r----- 1 oracle oinstall 304095232 Jun 2 14:25 sysaux01.dbf
-rw-r----- 1 oracle oinstall 513810432 Jun 2 14:25 system01.dbf
-rw-r----- 1 oracle oinstall 513810432 Jun 2 12:12 tbs_data.dbf
-rw-r----- 1 oracle oinstall 50339840 May 25 14:20 temp01.dbf
-rw-r----- 1 oracle oinstall 309338112 Jun 2 14:28 undotbs01.dbf
-rw-r----- 1 oracle oinstall 1052516352 Jun 2 12:01 users01.dbf
[oracle@localhost orcl]$ rm sysaux01.dbf
[oracle@localhost orcl]$ rm system01.dbf
[oracle@localhost orcl]$ rm tbs_data.dbf
[oracle@localhost orcl]$ rm undotbs01.dbf
[oracle@localhost orcl]$ rm temp01.dbf
[oracle@localhost orcl]$ rm users01.dbf
[oracle@localhost orcl]$ ll
total 174624
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:29 control01.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:29 control02.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:29 control03.ctl
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:54 redo01.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:55 redo02.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 14:29 redo03.log
这时 SHUTDOWN IMMEDIATE 是无法关闭的,使用SHUTDOWN ABORT 强制关闭数据库
SQL> shutdown immediate
ORA-01116: error in opening database file 1
ORA-01110: data file 1: '/home/oracle/oracle/product/10.2.0/oradata/orcl/system01.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3
SQL> shutdown abort
ORACLE instance shut down.
因为控制文件存在,数据库可以启动到mount状态
但是不能启动到open:
SQL> startup mount
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1218968 bytes
Variable Size 79693416 bytes
Database Buffers 197132288 bytes
Redo Buffers 7168000 bytes
Database mounted.
SQL> alter database open
2 ;
alter database open
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/home/oracle/oracle/product/10.2.0/oradata/orcl/system01.dbf'
读取一号文件system表空间的数据文件时出错了
因为在读取数据库时会通过控制文件上每个数据文件的检查点编号(SCN)与各个数据文件上的与数据文件头的SCN号进行比较,
如果不相等无法打开数据库,另外相等也不一定能打开数据库。
--我们来看看:
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463961652
2 13229463961652
3 13229463961652
4 13229463961652
5 13229463961652
6 13229463961652
7 13229463961652
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 0
2 0
3 0
4 0
5 13229463961652
6 13229463961652
7 0
7 rows selected
这里只有5号文件和6号文件头的SCN号是一致的,显然现在是无法启动数据库到OPEN状态的。
现在将第一个文件拷贝过去:
[oracle@localhost orcl]$ cp /home/lubinsu/orcl/system01.dbf system01.dbf
[oracle@localhost orcl]$ ll
total 676892
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control01.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control02.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control03.ctl
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:54 redo01.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:55 redo02.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 14:35 redo03.log
-rwxr-xr-x 1 oracle oinstall 513810432 Jun 2 14:55 system01.dbf
[oracle@localhost orcl]$ chmod 640 system01.dbf
[oracle@localhost orcl]$ ll
total 676892
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control01.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control02.ctl
-rw-r----- 1 oracle oinstall 7094272 Jun 2 14:56 control03.ctl
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:54 redo01.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 11:55 redo02.log
-rw-r----- 1 oracle oinstall 52429312 Jun 2 14:35 redo03.log
-rw-r----- 1 oracle oinstall 513810432 Jun 2 14:55 system01.dbf
再看看数据文件头的SCN号:
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463960924
2 0
3 0
4 0
5 13229463961652
6 13229463961652
7 0
7 rows selected
现在已经不为0了,但是和控制文件上最新的SCN号不一致:
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 1 needs media recovery
ORA-01110: data file 1: '/home/oracle/oracle/product/10.2.0/oradata/orcl/system01.dbf'
这时报需要介质恢复:
这就需要用到归档日志上的SCN号,恢复的过程其实就是增加SCN号的过程,查询归档日志:
SQL> SELECT a.sequence# 日志序号, a.first_change# 日志中最小的SCN号, a.next_change# 日志中最大的SCN号 FROM v$archived_log a WHERE a.first_change# <= 13229463960924 AND a.next_change# >= 13229463960924;
日志序号 日志中最小的SCN号 日志中最大的SCN号
---------- ----------------- -----------------
164 13229463960567 13229463961439
一号文件头的SCN号介于:164号归档日志的起始和结束SCN号之间:
现在恢复一号数据文件:
SQL> recover datafile 1;
ORA-00279: change 13229463960924 generated at 06/02/2013 11:32:48 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_%u_.arc
ORA-00280: change 13229463960924 for thread 1 is in sequence #164
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
--Oracle建议我们使用164号归档日志来恢复,回车
ORA-00279: change 13229463961439 generated at 06/02/2013 11:51:58 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_165_%u_.arc
ORA-00280: change 13229463961439 for thread 1 is in sequence #165
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_8tojbz0v_.arc' no longer needed for this recovery
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
--Oracle接着提示需要165号文件来恢复
如果归档日志很多,可以直接选择 AUTO 选项
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
ORA-00279: change 13229463961618 generated at 06/02/2013 11:54:14 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_169_%u_.arc
ORA-00280: change 13229463961618 for thread 1 is in sequence #169
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_168_8tojh6p8_.arc' no longer needed for this recovery
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
Log applied.
Media recovery complete.
现在再看看当前数据文件的SCN号:
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 0
3 0
4 0
5 13229463961652
6 13229463961652
7 0
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 13229463961652
3 13229463961652
4 13229463961652
5 13229463961652
6 13229463961652
7 13229463961652
7 rows selected
--可见已经完全恢复了
--打开数据库:
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file
ORA-01110: data file 2: '/home/oracle/oracle/product/10.2.0/oradata/orcl/undotbs01.dbf'
--使用同样的操作,接着恢复2号文件
[oracle@localhost orcl]$ cp /home/lubinsu/orcl/undotbs01.dbf undotbs01.dbf
SQL>
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 2 needs media recovery
ORA-01110: data file 2: '/home/oracle/oracle/product/10.2.0/oradata/orcl/undotbs01.dbf'
SQL> recover datafile 2;
ORA-00279: change 13229463960924 generated at 06/02/2013 11:32:48 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_%u_.arc
ORA-00280: change 13229463960924 for thread 1 is in sequence #164
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 13229463961439 generated at 06/02/2013 11:51:58 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_165_%u_.arc
ORA-00280: change 13229463961439 for thread 1 is in sequence #165
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_8tojbz0v_.arc' no longer needed for this recovery
ORA-00279: change 13229463961457 generated at 06/02/2013 11:52:34 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_166_%u_.arc
ORA-00280: change 13229463961457 for thread 1 is in sequence #166
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_165_8tojd2mx_.arc' no longer needed for this recovery
ORA-00279: change 13229463961587 generated at 06/02/2013 11:53:11 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_167_%u_.arc
ORA-00280: change 13229463961587 for thread 1 is in sequence #167
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_166_8tojf7dd_.arc' no longer needed for this recovery
ORA-00279: change 13229463961601 generated at 06/02/2013 11:53:44 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_168_%u_.arc
ORA-00280: change 13229463961601 for thread 1 is in sequence #168
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_167_8tojg8nw_.arc' no longer needed for this recovery
ORA-00279: change 13229463961618 generated at 06/02/2013 11:54:14 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_169_%u_.arc
ORA-00280: change 13229463961618 for thread 1 is in sequence #169
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_168_8tojh6p8_.arc' no longer needed for this recovery
Log applied.
Media recovery complete.
SQL> alter database open;
--查看2号数据文件的SCN号:
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 13229463966643
3 13229463961652
4 13229463961652
5 13229463961652
6 13229463961652
7 13229463961652
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 13229463966643
3 0
4 0
5 13229463961652
6 13229463961652
7 0
7 rows selected
--如此反复操作,如果我们将重做日志文件删除,可能会提示需要日志文件:
[oracle@localhost orcl]$ cp redo01.log /home/lubinsu/orcl/
[oracle@localhost orcl]$ rm redo01.log
SQL> recover datafile 4;
ORA-00279: change 13229463960923 generated at 06/02/2013 11:19:18 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_%u_.arc
ORA-00280: change 13229463960923 for thread 1 is in sequence #164
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 13229463961439 generated at 06/02/2013 11:51:58 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_165_%u_.arc
ORA-00280: change 13229463961439 for thread 1 is in sequence #165
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_164_8tojbz0v_.arc' no longer needed for this recovery
ORA-00279: change 13229463961457 generated at 06/02/2013 11:52:34 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_166_%u_.arc
ORA-00280: change 13229463961457 for thread 1 is in sequence #166
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_165_8tojd2mx_.arc' no longer needed for this recovery
ORA-00279: change 13229463961587 generated at 06/02/2013 11:53:11 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_167_%u_.arc
ORA-00280: change 13229463961587 for thread 1 is in sequence #167
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_166_8tojf7dd_.arc' no longer needed for this recovery
ORA-00279: change 13229463961601 generated at 06/02/2013 11:53:44 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_168_%u_.arc
ORA-00280: change 13229463961601 for thread 1 is in sequence #168
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_167_8tojg8nw_.arc' no longer needed for this recovery
ORA-00279: change 13229463961618 generated at 06/02/2013 11:54:14 needed for thread 1
ORA-00289: suggestion : /home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_169_%u_.arc
ORA-00280: change 13229463961618 for thread 1 is in sequence #169
ORA-00278: log file '/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_02/o1_mf_1_168_8tojh6p8_.arc' no longer needed for this recovery
ORA-00283: recovery session canceled due to errors
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: '/home/oracle/oracle/product/10.2.0/oradata/orcl/redo01.log'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
ORA-01112: media recovery not started
--提示介质恢复未开始,需要重做日志文件/home/oracle/oracle/product/10.2.0/oradata/orcl/redo01.log
--我们再把该文件拷回来
SQL> recover datafile 4;
Media recovery complete.
SQL>
--恢复完成
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 13229463966643
3 13229463966643
4 13229463966643
5 13229463961652
6 13229463961652
7 13229463966643
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463966643
2 13229463966643
3 13229463966643
4 13229463966643
5 13229463961652
6 13229463961652
7 13229463966643
7 rows selected
SQL> alter database open;
Database altered.
--此时数据库已经可以启动
--查询我们之前的数据是否存在:
SQL> SELECT * FROM lubinsu.t_scn;
ID SCN
--------------------------------------- ----------
9 1322946396
1 1322946396
1 1322946396
1 1322946396
2 1322946396
3 1322946396
3 1322946396
4 1322946396
4 1322946396
5 1322946396
6 1322946396
7 1322946396
8 1322946396
13 rows selected
--其中ID为9的数据不存在,因为我们当时操作的时候没有提交。已经丢失。
2.部分脱机恢复(TABLESPACE OFFLINE)
针对表空间,部分 OFFLINE 恢复
--将表空间OFFLINE
SQL> alter tablespace TBS_LUBINSU_DATA offline;
Tablespace altered.
SQL> SELECT * FROM lubinsu.manager_tm;
SELECT * FROM lubinsu.manager_tm
*
ERROR at line 1:
ORA-00376: file 7 cannot be read at this time
ORA-01110: data file 7: '/home/oracle/oracle/product/10.2.0/oradata/orcl/tbs_data.dbf'
--现在已经无法查询该表空间中的数据文件了。
--备份该表空间对应的数据文件:
[oracle@localhost orcl]$ cp tbs_data.dbf /home/oracle/orcl_backup/
--将数据ONLINE
SQL> alter tablespace TBS_LUBINSU_DATA online;
Tablespace altered
--插入一条数据提交
INSERT INTO t_scn values(9, dbms_flashback.get_system_change_number);
COMMIT;
ALTER SYSTEM SWITCH LOGFILE;
INSERT INTO t_scn values(10, dbms_flashback.get_system_change_number);
COMMIT;
ALTER SYSTEM SWITCH LOGFILE;
INSERT INTO t_scn values(11, dbms_flashback.get_system_change_number);
COMMIT;
ALTER SYSTEM SWITCH LOGFILE;
INSERT INTO t_scn values(12, dbms_flashback.get_system_change_number);
COMMIT;
ALTER SYSTEM SWITCH LOGFILE;
INSERT INTO t_scn values(13, dbms_flashback.get_system_change_number);
COMMIT;
ALTER SYSTEM SWITCH LOGFILE;
--删除数据文件
[oracle@localhost orcl]$ rm tbs_data.dbf
--查看数据文件头的SCN号:
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463987400
2 13229463987400
3 13229463987400
4 13229463987400
5 13229463987400
6 13229463987400
7 13229463987420
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229463987400
2 13229463987400
3 13229463987400
4 13229463987400
5 13229463987400
6 13229463987400
7 0
7 rows selected
--这时我们将原来备份的文件拷贝回来:
[oracle@localhost orcl]$ cp /home/oracle/orcl_backup/tbs_data.dbf tbs_data.dbf
--ONLINE提示需要介质恢复
SQL> alter tablespace tbs_lubinsu_data online;
alter tablespace tbs_lubinsu_data online
*
ERROR at line 1:
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7:
'/home/oracle/oracle/product/10.2.0/oradata/orcl/tbs_data.dbf'
SQL> recover datafile 7;
ORA-00279: change 13229463987253 generated at 06/02/2013 15:53:49 needed for
thread 1
ORA-00289: suggestion :
/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_0
2/o1_mf_1_173_%u_.arc
ORA-00280: change 13229463987253 for thread 1 is in sequence #173
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00279: change 13229463987380 generated at 06/02/2013 15:57:25 needed for
thread 1
ORA-00289: suggestion :
/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_0
2/o1_mf_1_174_%u_.arc
ORA-00280: change 13229463987380 for thread 1 is in sequence #174
ORA-00278: log file
'/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_
02/o1_mf_1_173_8toyq5pz_.arc' no longer needed for this recovery
ORA-00279: change 13229463987395 generated at 06/02/2013 15:57:46 needed for
thread 1
ORA-00289: suggestion :
/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_0
2/o1_mf_1_175_%u_.arc
ORA-00280: change 13229463987395 for thread 1 is in sequence #175
ORA-00278: log file
'/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_
02/o1_mf_1_174_8toyqtt7_.arc' no longer needed for this recovery
ORA-00279: change 13229463987400 generated at 06/02/2013 15:57:50 needed for
thread 1
ORA-00289: suggestion :
/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_0
2/o1_mf_1_176_%u_.arc
ORA-00280: change 13229463987400 for thread 1 is in sequence #176
ORA-00278: log file
'/home/oracle/oracle/product/10.2.0/flash_recovery_area/ORCL/archivelog/2013_06_
02/o1_mf_1_175_8toyqyg1_.arc' no longer needed for this recovery
Log applied.
Media recovery complete.
--恢复之后,我们来查看下SCN号:
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464008626
2 13229464008626
3 13229464008626
4 13229464008626
5 13229464008626
6 13229464008626
7 13229463987420
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464008626
2 13229464008626
3 13229464008626
4 13229464008626
5 13229464008626
6 13229464008626
7 0
7 rows selected
--还是0
因为需要把表空间ONLINE才看得到:
ALTER TABLESPACE tbs_lubinsu_data ONLINE;
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464008626
2 13229464008626
3 13229464008626
4 13229464008626
5 13229464008626
6 13229464008626
7 13229464009374
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464008626
2 13229464008626
3 13229464008626
4 13229464008626
5 13229464008626
6 13229464008626
7 13229464009374
7 rows selected
--可见已经恢复成功了。
--当完全检查点实现的时候所有的SCN号都相同了,Oracle把所有的都同步了。有两种方法实现
1.shutdown的时候(也会执行alter SYSTEM CHECKPOINT)
2. ALTER SYSTEM CHECKPOINT
执行:alter SYSTEM CHECKPOINT:
SQL> select file#, checkpoint_change# from v$datafile;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464009991
2 13229464009991
3 13229464009991
4 13229464009991
5 13229464009991
6 13229464009991
7 13229464009991
7 rows selected
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 13229464009991
2 13229464009991
3 13229464009991
4 13229464009991
5 13229464009991
6 13229464009991
7 13229464009991
7 rows selected
--全部一致了。
但并不是所有的表空间都支持OFFLINE,如SYSTEM,UNDO表空间。
SELECT * FROM dba_temp_files;
SQL> ALTER TABLESPACE TEMP OFFLINE;
ALTER TABLESPACE TEMP OFFLINE
*
ERROR at line 1:
ORA-03217: invalid option for alter of TEMPORARY TABLESPACE
SQL> ALTER TABLESPACE TEMP OFFLINE;
ALTER TABLESPACE TEMP OFFLINE
*
ERROR at line 1:
ORA-03217: invalid option for alter of TEMPORARY TABLESPACE
3.部分联机备份,后面两种只适合归档模式。只有第一种方式适合于非归档模式。
ALTER TABLESPACE tbs_lubinsu_data BEGIN BACKUP;
SQL> SELECT * FROM v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- -----------
1 NOT ACTIVE 0
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 NOT ACTIVE 0
5 NOT ACTIVE 0
6 NOT ACTIVE 0
7 ACTIVE 1322946403 2013/6/3 22
7 rows selected
这时并没有备份,而查询v$backup视图中active的1322946403即备份开始的点。
然后手工对数据文件进行备份(热备份)。
备份完之后:
ALTER TABLESPACE tbs_lubinsu_data END BACKUP;
Oracle 10g开始支持,可以同时热备多个表空间
ALTER DATABASE BEGIN BACKUP;