備份&恢復之五:歸檔模式下丟失或損壞一個資料檔案

mengzhaoliang發表於2008-05-07
備份&恢復之五:歸檔模式下丟失或損壞一個資料檔案

來自piner:
http://www.itpub.net/viewthread.php?tid=126320&extra=page%3D4%26amp%3Bfilter%3Ddigest
在自己機器測試成功

測試環境:
1      作業系統:Redhat Linux 5
[oracle@mzl proc]$ cat /proc/version
Linux version 2.6.18-8.el5 (brewbuilder@ls20-bc2-14.build.redhat.com) (gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)) #1 SMP Fri Jan 26 14:15:21 EST 2007

2     資料庫版本:Oracle10g
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production


3  設定成歸檔模式.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1218868 bytes
Variable Size              88082124 bytes
Database Buffers          171966464 bytes
Redo Buffers                7168000 bytes
Database mounted.
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     16
Current log sequence           18
SQL> alter database archivelog;

Database altered.

SQL> alter database open;

Database altered.

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     16
Next log sequence to archive   18
Current log sequence           18
SQL>


4 建立表插入資料
SQL> drop table test;

Table dropped.

SQL> create table test(a int) tablespace users;

Table created.

SQL> insert into test values(1);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from test;

         A
----------
         1



5  檢視錶test在哪個表空間
SQL> select table_name,tablespace_name from dba_tables
  2  where table_name='TEST';

TABLE_NAME                     TABLESPACE_NAME
------------------------------ ------------------------------
TEST                           USERS



在users表空間內


6   執行熱備份指令碼
[oracle@mzl BackupDatabase]$ pwd
/home/mzl/BackupDatabase
[oracle@mzl BackupDatabase]$ vi hotbak.sql
#rem script.:hotbak.sql
#rem creater:mengzhaoliang
#rem data:2008/2/4
#rem desc:backup all database datafile in archive
#enter database
$ORACLE_HOME/bin/sqlplus "/as sysdba" <alter system archive log current;
#start
alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/orcl/system01.dbf /home/mzl/BackupDatabase/;
alter tablespace system end backup;

alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/orcl/undotbs01.dbf /home/mzl/BackupDatabase/;
alter tablespace system end backup;

alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/orcl/users01.dbf /home/mzl/BackupDatabase/;
alter tablespace system end backup;

alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/orcl/sysaux01.dbf /home/mzl/BackupDatabase/;
alter tablespace system end backup;

alter tablespace system begin backup;
!cp /u01/app/oracle/oradata/orcl/example01.dbf /home/mzl/BackupDatabase/;
alter tablespace system end backup;

#backup control file
#binary
alter database backup controlfile to '/home/mzl/BackupDatabase/controlbinarybackup';

#ascii
alter database backup controlfile to trace;

alter system archive log current;

<
[oracle@mzl BackupDatabase]$ ./hotbak.sql

7  繼續插入資料.
SQL> insert into test values(2);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from test;

         A
----------
         1
         2

8  關閉資料庫,把users01.dbf資料檔案移出
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@mzl orcl]$ pwd
/u01/app/oracle/oradata/orcl
[oracle@mzl orcl]$ ls
control01.ctl  create.sql     redo01.log  risenet.dbf   system01.dbf
control02.ctl  example01.dbf  redo02.log  sqlnet.log    undotbs01.dbf
control03.ctl  perfstat.dbf   redo03.log  sysaux01.dbf  users01.dbf
[oracle@mzl orcl]$ mkdir Old
[oracle@mzl orcl]$ mv users01.dbf Old

9 啟動資料庫
SQL> startup
ORACLE instance started.

Total System Global Area  268435456 bytes
Fixed Size                  1218868 bytes
Variable Size              88082124 bytes
Database Buffers          171966464 bytes
Redo Buffers                7168000 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file
ORA-01110: data file 4: '/u01/app/oracle/oradata/orcl/users01.dbf'



其中alert_ORCL.log資訊為:
ALTER DATABASE OPEN
Wed May  7 19:42:59 2008
Errors in file /u01/app/oracle/admin/orcl/bdump/orcl_dbw0_3972.trc:
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file
ORA-01110: data file 4: '/u01/app/oracle/oradata/orcl/users01.dbf'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
ORA-1157 signalled during: ALTER DATABASE OPEN...


其中orcl_dbw0_3450.trc的資訊為:
*** SERVICE NAME:() 2008-05-07 19:42:59.118
*** SESSION ID:(167.1) 2008-05-07 19:42:59.118
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file
ORA-01110: data file 4: '/u01/app/oracle/oradata/orcl/users01.dbf'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3





檢視哪些檔案需要恢復:
SQL> select * from v$recover_file;

     FILE# ONLINE  ONLINE_ ERROR                             CHANGE# TIME
---------- ------- ------- ------------------------------ ---------- ---------
         4 ONLINE  ONLINE  FILE NOT FOUND                          0


10  離線資料檔案
SQL> alter database datafile 4 offline drop;

Database altered.


11   開啟資料庫,複製備份回來(restore),恢復(recover)該資料檔案,並聯機
SQL> alter database open;

Database altered.

[oracle@mzl orcl]$ pwd
/u01/app/oracle/oradata/orcl
[oracle@mzl orcl]$ cp /home/mzl/BackupDatabase/users01.dbf  .

SQL> recover datafile 4;
Media recovery complete.

SQL> alter database datafile 4 online;

Database altered.

SQL> select * from test;

         A
----------
         1
         2







說明:
1、採用熱備份,需要執行在歸檔模式下,可以實現資料庫的完全恢復,也就是說,從備份後到資料庫崩潰時的資料都不會丟失。
2、可以採用全備份資料庫的方式備份,對於特殊情況,也可以只備份特定的資料檔案,如只備份使用者表空間(一般情況下對於某些寫特別頻繁的資料檔案,可以單獨加大備份頻率)
3、如果在恢復過程中,發現損壞的是多個資料檔案,即可以採用一個一個資料檔案的恢復方法(第5步中需要對資料檔案一一離線,第6步中需要對資料檔案分別恢復),也可以採用整個資料庫的恢復方法。
4、如果是系統表空間的損壞,不能採用此方法
 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12778571/viewspace-263079/,如需轉載,請註明出處,否則將追究法律責任。

相關文章