Oracle備份與恢復系列(三)alter tablspace begin backup

snowdba發表於2014-08-01
完全恢復試驗

此次試驗需要以下步驟
1 產生資料庫活動
2 建立備份指令碼
3 執行熱備份
破壞資料庫
5 還原丟失的資料檔案
6 恢復還原的資料檔案
7 確認資料庫恢復

每個資料檔案在其首部都有一個序號,該序號給出了系統檔案的最新資料庫狀態。
在一個資料庫可以被開啟前,所有聯機資料檔案必須擁有同樣的系統變更號(SCN)。
當這些聯機資料檔案擁有相同的SCN時,它們就是一致的。在資料庫open狀態下進行的備份不會產生一致的資料檔案。
這樣,在使用不一致的資料檔案還原資料庫之前必須進行介質恢復。一旦所有必須的重做資訊都被應用而保持SCN一致時,就可以開啟資料庫了。

step1 產生資料庫活動

insert into snow.date_log values(sysdate,'Elle');
commit;
alter system switch logfile;
insert into snow.date_log values(sysdate,'Flow');
commit;
alter system switch logfile;
insert into snow.date_log values(sysdate,'Gigi');
commit;
alter system switch logfile;

建立備份目錄
mkdir -p /backup/open_backup
chown oracle:oinstall /backup/open_backup

step2 建立備份指令碼
vi /backup/scripts/open_backup.sql

set feedback off set pagesize 0 set heading off set verify off set linesize 100 set trimspool on define dir = '/backup/open_backup'
define fil = '/backup/scripts/open_backup_commands.sql'
define spo = '&dir/open_backup_output.lst' set serveroutput on spool &fil prompt spool &spo prompt archive log list;; prompt alter system switch logfile;; prompt alter system archive log all;; DECLARE CURSOR cur_tablespace IS   SELECT tablespace_name     FROM dba_tablespaces where tablespace_name<>'TEMP';
CURSOR cur_datafile (tn VARCHAR) IS   SELECT file_name     FROM dba_data_files    WHERE tablespace_name = tn; BEGIN   FOR ct IN cur_tablespace LOOP     dbms_output.put_line ('alter tablespace '||ct.tablespace_name||' begin backup;');     FOR cd IN cur_datafile (ct.tablespace_name) LOOP        dbms_output.put_line ('host cp '||cd.file_name||' &dir');     END LOOP;     dbms_output.put_line ('alter tablespace '||ct.tablespace_name||' end backup;');   END LOOP; END; /

prompt alter system switch logfile;; prompt alter database backup controlfile to '&dir./control01.ctl' REUSE;; prompt archive log list;; prompt spool off spool off;--@&fil

備份pfile檔案
create pfile='/backup/open_backup/init.ora' from spfile;

step3 執行熱備份
@/backup/scripts/open_backup.sql

step4 破壞資料庫
rm /oradata/PRACTICE/tools01.dbf
rm /oradata/PRACTICE/users01.dbf 
alter system checkpoint;

step5 還原丟失的資料檔案
檢視需要恢復的資料檔案

當資料檔案需要恢復時,資料檔案必須離線。或者將其表空間離線。
select * from v$recover_file;

alter database datafile 5 offline; alter database datafile 6 offline;

col FILE_NAME for a60
select FILE_ID,FILE_NAME,ONLINE_STATUS from dba_data_files;
         4 /oradata/PRACTICE/users01.dbf                                ONLINE
         3 /oradata/PRACTICE/undotbs01.dbf                              ONLINE
         2 /oradata/PRACTICE/sysaux01.dbf                               ONLINE
         1 /oradata/PRACTICE/system01.dbf                               SYSTEM
         5 /oradata/PRACTICE/example01.dbf                              RECOVER
         6 /oradata/PRACTICE/tools01.dbf                                RECOVER
         7 /oradata/PRACTICE/indx.dbf                                   ONLINE
         8 /oradata/PRACTICE/users02.dbf                                ONLINE

cp /backup/open_backup/users01.dbf /oradata/PRACTICE/
cp /backup/open_backup/tools01.dbf /oradata/PRACTICE/

發現檢查點SCN
col error for a20
set linesize 200
select * from v$recover_file;

     FILE# ONLINE                ONLINE_STATUS         ERROR                   CHANGE# TIME
---------- --------------------- --------------------- -------------------- ---------- -------------------
         5 OFFLINE               OFFLINE                                       1307778 2014-08-01 12:57:34
         6 OFFLINE               OFFLINE                                       1306321 2014-08-01 12:26:27

step6 恢復還原的資料檔案
recover datafile 5;
recover datafile 6;

alter database datafile 5 online; alter database datafile 6 online;

如果歸檔日誌的位置發生變化可以指定其位置
recover from '/archive' datafile 5
recover from '/archive' tablespace tools;

step7 確認資料庫恢復

select * from snow.date_log order by create_time;
2014-03-22 10:12:44 2014-03-22 10:28:58


不完全恢復

1- 插入一條記錄作為參照,後面的恢復中要能查詢的到
insert into snow.date_log values (sysdate,'Iceman');
commit;
select * from snow.date_log order by create_time;

2-切換日誌,從v$datafile中的change#提取資料檔案的checkpoint scn
alter system switch logfile;

3-檢視當前日誌
col status for a10
select GROUP#,SEQUENCE#,ARCHIVED,STATUS,FIRST_CHANGE#,FIRST_TIME,NEXT_CHANGE#,NEXT_TIME from v$log;

    GROUP#  SEQUENCE# ARCHIVED  STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME
---------- ---------- --------- ---------- ------------- ------------------- ------------ -------------------
         1          7 NO        CURRENT          1309379 2014-08-01 13:16:12   2.8147E+14
         2          5 YES       INACTIVE         1306249 2014-08-01 12:26:03      1306349 2014-08-01 12:26:28
         3          6 YES       ACTIVE           1306349 2014-08-01 12:26:28      1309379 2014-08-01 13:16:12

select sequence#,first_change#,to_char(first_time,'yyyy-mm-dd hh24:mi:ss') first_time from v$log_history;

SEQUENCE# FIRST_CHANGE# FIRST_TIME
---------- ------------- ---------------------------------------------------------
         1       1299137 2014-08-01 10:36:27
         2       1306113 2014-08-01 12:22:18
         3       1306118 2014-08-01 12:22:22
         4       1306123 2014-08-01 12:22:30
         5       1306249 2014-08-01 12:26:03
         6       1306349 2014-08-01 12:26:28  <==該SCN為恢復參考點

4-檢視資料檔案的SCN
set linesize 200
select FILE# , CHECKPOINT_CHANGE#  ,CHECKPOINT_TIME,LAST_CHANGE# ,LAST_TIME   from v$datafile;
         FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     LAST_CHANGE# LAST_TIME
---------- ------------------ ------------------- ------------ -------------------
         1            1307778 2014-08-01 12:57:34
         2            1307778 2014-08-01 12:57:34
         3            1307778 2014-08-01 12:57:34
         4            1307778 2014-08-01 12:57:34
         5            1308231 2014-08-01 13:02:10
         6            1308238 2014-08-01 13:02:14
         7            1307778 2014-08-01 12:57:34
         8            1307778 2014-08-01 12:57:34

5-刪除表
alter system switch log file;
alter system switch log file;
alter system switch log file;

drop table scott.emp;

7-關閉資料庫
shutdown immediate;

ORA-01122: database file 4 failed verification check
ORA-01110: data file 4: '/oradata/PRACTICE/users01.dbf'
ORA-01208: data file is an old version - not accessing current version

shutdown abort;

8-還原資料檔案
cp /backup/open_backup/* /oradata/PRACTICE/
cp /backup/open_backup/control01.ctl /u01/app/oracle/fast_recovery_area/PRACTICE/control02.ctl

9-載入資料庫,檢視需要恢復的資料檔案
startup mount;
select * from v$recover_file;

10-恢復資料庫到SCN1306349

由於控制檔案也是從備份中還原的,沒有記錄之後發生的變化,所以使用using backup controlfile字句來使其應用之後產生的歸檔日誌。

recover database until change 1306349 using backup controlfile;

ORA-00279: change 1306340 generated at 08/01/2014 12:26:28 needed for thread 1
ORA-00289: suggestion : /archive/1_5_854447787.arc
ORA-00280: change 1306340 for thread 1 is in sequence #5

Specify log: {=suggested | filename | AUTO | CANCEL}

Log applied.
Media recovery complete.

檢驗資料檔案的SCN恢復狀況
select * from v$recover_file;

     FILE# ONLINE                ONLINE_STATUS         ERROR         CHANGE# TIME
---------- --------------------- --------------------- ---------- ---------- -------------------
         1 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         2 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         3 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         4 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         5 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         6 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         7 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28
         8 ONLINE                ONLINE                              1306349 2014-08-01 12:26:28

11-開啟資料庫

alter database open resetlogs;

檢視scott.emp是否存在
select * from scott.emp;

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

相關文章