rman恢復示例

《三思筆記》--讀書筆記

linux下

首先執行備份腳本

如下:

[oracle@cindy rman_backup]$ /u01/oracle/rman_backup/back_jiaoben.sh
RMAN> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 

back_jiaoben.sh的內容如下:

rman target / msglog /u01/oracle/rman_backup/` date +"%Y%m%d" `.log cmdfile=/u01/oracle/rman_backup/back_jiaoben.rman

back_jiaoben.rman的內容如下:

run{
configure retention policy to recovery window of 14 days;
configure controlfile autobackup on;
configure controlfile autobackup format for device type disk to '/u01/oracle/rman_backup/%F';
allocate channel c1 device type disk format '/u01/oracle/rman_backup/bak_%U';
backup database skip inaccessible 
   plus archivelog filesperset 20 
   delete all input;
release channel c1;
}
allocate channel for maintenance device type disk;
crosscheck backupset;
delete noprompt obsolete;

然後查看備份的內容,如下:

[oracle@cindy rman_backup]$ ls
20140902.log  back_jiaoben.rman  back_jiaoben.sh  bak_2pphf9id_1_1  bak_2qphf9if_1_1  bak_2rphf9ok_1_1  c-4252637343-20140902-03
------------------------------------------------------------華麗的分割線--------------------上面是備份------------下面的模擬的各種恢復---------------------------------------
接下來模擬各種情況下恢復數據庫的情況

歸檔模式有備份,丟失數據文件的恢復

1,構造數據

[oracle@cindy ~]$ sqlplus scott/tiger;

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 2 03:53:14 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> create table tmp2(name varchar2(20));

Table created.
SQL> insert into tmp2 values ('test1');

1 row created.

SQL> insert into tmp2 values ('test2');

1 row created.

SQL> insert into tmp2 values ('test3');

1 row created.

SQL> commit;

Commit complete.

2,模擬文件丟失

以sysdba身份登錄並關閉數據庫

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

然後手動刪除數據文件

[oracle@cindy ora11g]$ cd /u02/oradata/ora11g
[oracle@cindy ora11g]$ ls
cindy01.dbf  control01.ctl  example01.dbf  redo01.log  redo02.log  redo03.log  sysaux01.dbf  system01.dbf  temp01.dbf  undotbs01.dbf  users01.dbf
[oracle@cindy ora11g]$ rm users01.dbf

重啓數據庫

[oracle@cindy ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 2 06:17:51 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size                  2228944 bytes
Variable Size             322964784 bytes
Database Buffers           88080384 bytes
Redo Buffers                4272128 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 4 - see DBWR trace file
ORA-01110: data file 4: '/u02/oradata/ora11g/users01.dbf'

此時報錯,找不到數據文件,此時數據庫處於mount狀態

3,執行恢復

重新打開一個窗口,執行rman

[oracle@cindy ~]$ rman target /

Recovery Manager: Release 11.2.0.3.0 - Production on Tue Sep 2 06:24:19 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORA11G (DBID=4252637343, not open)

RMAN> restore datafile 4;

Starting restore at 02-SEP-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00004 to /u02/oradata/ora11g/users01.dbf
channel ORA_DISK_1: reading from backup piece /u01/oracle/rman_backup/bak_2qphf9if_1_1
channel ORA_DISK_1: piece handle=/u01/oracle/rman_backup/bak_2qphf9if_1_1 tag=TAG20140902T034518
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
Finished restore at 02-SEP-14

RMAN> recover datafile 4;

Starting recover at 02-SEP-14
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:00

Finished recover at 02-SEP-14

RMAN> alter database open;

database opened

切換到sql,查詢數據

[oracle@cindy ~]$ sqlplus scott/tiger

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 2 06:26:32 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from tmp2;

NAME
--------------------
test1
test2
test3

數據都回來了

ps:用來查詢表空間及其關聯的數據文件

SELECT A.ABLESPACE_NAME, B.FILE_ID, B.FILE_NAME, B.STATUS
  FROM DBA_TABLESPACES A,
       (SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME, STATUS
          FROM DBA_DATA_FILES
        UNION
        SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME, STATUS
          FROM DBA_TMP_FILES) B
 WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME;


歸檔模式無備份,丟失數據文件的恢復

如system表空間的數據文件丟失或損壞,除非藉助備份,否則無法直接恢復,如果是臨時表空間的數據文件丟失,不需要恢復,重建一個即可

1,創建新的表空間cindy02

確認處於歸檔模式

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u02/arch_log
Oldest online log sequence     9
Next log sequence to archive   11
Current log sequence           11

SQL> create tablespace cindy04 datafile '/u02/oradata/ora11g/cindy04.dbf' size 100m;

Tablespace created.

SQL> create user cindy04 identified by cindy default tablespace cindy04;

User created.

SQL> grant connect,resource to cindy04;

Grant succeeded.

SQL> conn cindy04/cindy;
Connected.
SQL> create table test(name varchar2(20));

Table created.

SQL> insert into test values ('acd');

1 row created.

SQL> commit;

Commit complete.

2,關閉數據庫,手動刪除數據文件

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@cindy ~]$ cd /u02
[oracle@cindy u02]$ ls
arch_log  oradata  rmanbak
[oracle@cindy u02]$ cd oracle
-bash: cd: oracle: No such file or directory
[oracle@cindy u02]$ cd oradata
[oracle@cindy oradata]$ ls
ora11g
[oracle@cindy oradata]$ cd ora11g
[oracle@cindy ora11g]$ ls
cindy01.dbf  cindy03.dbf  control01.ctl  redo01.log  redo03.log    system01.dbf  undotbs01.dbf
cindy02.dbf  cindy04.dbf  example01.dbf  redo02.log  sysaux01.dbf  temp01.dbf    users01.dbf
[oracle@cindy ora11g]$ rm  cindy04.dbf 
[oracle@cindy ora11g]$ ls
cindy01.dbf  cindy03.dbf    example01.dbf  redo02.log  sysaux01.dbf  temp01.dbf     users01.dbf
cindy02.dbf  control01.ctl  redo01.log     redo03.log  system01.dbf  undotbs01.dbf
,3,打開數據庫,報錯

[oracle@cindy ora11g]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Tue Sep 2 07:10:41 2014

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size                  2228944 bytes
Variable Size             322964784 bytes
Database Buffers           88080384 bytes
Redo Buffers                4272128 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 9 - see DBWR trace file
ORA-01110: data file 9: '/u02/oradata/ora11g/cindy04.dbf'

由於此時沒有備份,我們便從歸檔中恢復數據文件,如下

通過alter database create datafile命令重建一個該文件

SQL>  alter database create datafile '/u02/oradata/ora11g/cindy04.dbf' as '/u02/oradata/ora11g/cindy04.dbf'
  2  ;

Database altered.

SQL> recover datafile 9;
Media recovery complete.
SQL> alter database open;

Database altered.

SQL> conn cindy04/cindy
Connected.
SQL> select * from test;

NAME
--------------------
acd

可見,建完文件之後,恢復該文件,此處的關鍵點是,丟失的數據文件,從其創建時刻起所有的重做日誌文件都還在,因此纔可以在重建該數據文件後,通過recover命令應用所有的重做日誌的方式,重建該數據文件中的內容

此次恢復沒有使用rman,因爲,在這種情況下(有歸檔無備份),上述恢復方式爲最佳。


丟失控制文件的恢復

新增一條數據

SQL> insert into test values('ert');

1 row created.

SQL> commit;

Commit complete.

SQL> shutdown immediate   
ORA-01031: insufficient privileges
SQL> conn / as sysdba
Connected.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

手動刪除控制文件

[oracle@cindy ora11g]$ cd /u02/oradata/ora11g
[oracle@cindy ora11g]$ ls
cindy01.dbf  cindy03.dbf  control01.ctl  redo01.log  redo03.log    system01.dbf  undotbs01.dbf
cindy02.dbf  cindy04.dbf  example01.dbf  redo02.log  sysaux01.dbf  temp01.dbf    users01.dbf
[oracle@cindy ora11g]$ rm control01.ctl
[oracle@cindy ora11g]$ ls
cindy01.dbf  cindy03.dbf  example01.dbf  redo02.log  sysaux01.dbf  temp01.dbf     users01.dbf
cindy02.dbf  cindy04.dbf  redo01.log     redo03.log  system01.dbf  undotbs01.dbf

由於控制文件丟失,數據庫無法啓動到mount狀態,只能到nomount

SQL> startup nomount
ORACLE instance started.

Total System Global Area  417546240 bytes
Fixed Size                  2228944 bytes
Variable Size             322964784 bytes
Database Buffers           88080384 bytes
Redo Buffers                4272128 bytes

打開一個新窗口,連接到rman

首先設置dbid

[oracle@cindy ~]$ rman target /

Recovery Manager: Release 11.2.0.3.0 - Production on Tue Sep 2 07:34:20 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORA11G (not mounted)

RMAN> set dbid = 4252637343

executing command: SET DBID

因爲備份是在nocatalog模式下進行的,因爲備份信息、備份設置等都是存儲在目標數據庫的控制文件中,現在控制文件丟失,相當於前面的一些配置也丟失了,用show all命令查看,可見所有配置均恢復成默認值

RMAN> show all;

using target database control file instead of recovery catalog
RMAN configuration parameters for database with db_unique_name ORA11G are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default

此時恢復控制文件,不能直接使用restore controlfile from autobackup命令,因爲自動備份的設置丟失了,並且此時在nocatalog模式下,無法配置controlfile autobackup的相關屬性,所以此時要顯示指定控制文件備份集的方式恢復控制文件,如下

RMAN> host;   

[oracle@cindy ~]$ cd /u01/oracle/rman_backup
[oracle@cindy rman_backup]$ ls
20140902.log  back_jiaoben.rman  back_jiaoben.sh  bak_2pphf9id_1_1  bak_2qphf9if_1_1  bak_2rphf9ok_1_1  c-4252637343-20140902-03  c-4252637343-20140902-04
[oracle@cindy rman_backup]$ ll
total 1328012
-rw-r--r-- 1 oracle oinstall       4671 Sep  2 03:48 20140902.log
-rw-r--r-- 1 oracle oinstall        484 Aug 31 16:44 back_jiaoben.rman
-rwxr-xr-x 1 oracle oinstall        118 Sep  2 03:44 back_jiaoben.sh
-rw-r----- 1 oracle oinstall     136192 Sep  2 03:45 bak_2pphf9id_1_1
-rw-r----- 1 oracle oinstall 1338613760 Sep  2 03:48 bak_2qphf9if_1_1
-rw-r----- 1 oracle oinstall      35840 Sep  2 03:48 bak_2rphf9ok_1_1
-rw-r----- 1 oracle oinstall    9863168 Sep  2 03:48 c-4252637343-20140902-03
-rw-r----- 1 oracle oinstall    9863168 Sep  2 07:16 c-4252637343-20140902-04
[oracle@cindy rman_backup]$ exit
exit
host command complete

RMAN> restore controlfile from '/u01/oracle/rman_backup/c-4252637343-20140902-04';

Starting restore at 02-SEP-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:07
output file name=/u02/oradata/ora11g/control01.ctl
output file name=/u01/oracle/app/oracle/fast_recovery_area/ora11g/control02.ctl
Finished restore at 02-SEP-14

有了控制文件,數據庫就可置爲mount狀態

RMAN> alter database mount;

database mounted
released channel: ORA_DISK_1

由於只是控制文件丟失,數據文件仍在,因此並不需要對整個數據庫進行修復操作,只需要執行recover 操作,重新應用備份的控制文件後生成的那些重做日誌即可

RMAN> recover database;

Starting recover at 02-SEP-14
Starting implicit crosscheck backup at 02-SEP-14
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
Crosschecked 4 objects
Finished implicit crosscheck backup at 02-SEP-14

Starting implicit crosscheck copy at 02-SEP-14
using channel ORA_DISK_1
Finished implicit crosscheck copy at 02-SEP-14

searching for all files in the recovery area
cataloging files...
no files cataloged

using channel ORA_DISK_1

starting media recovery

archived log for thread 1 with sequence 12 is already on disk as file /u02/oradata/ora11g/redo03.log
archived log file name=/u02/oradata/ora11g/redo03.log thread=1 sequence=12
media recovery complete, elapsed time: 00:00:00
Finished recover at 02-SEP-14

由於是通過備份的控制文件恢復的,所以在打開數據庫的時候,必須執行resetlogs

RMAN> alter database open resetlogs;

database opened

切到sql下,查看數據是否還在

SQL> conn cindy04/cindy
Connected.
SQL> select * from temp;
select * from temp
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> select * from test;

NAME
--------------------
acd
ert



丟失非當前聯機重做日誌文件的恢復,看之前的blog



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章