什麼是備份資料庫?什麼是資料庫一致性備份和非一致性備份?

zhulch發表於2007-05-15
讀書操作記錄[@more@]

- 什麼是備份資料庫?
備份資料庫是指備份資料庫的所有資料檔案和控制檔案,另外還應該備份引數檔案和口令檔案,注意,當備份資料庫時,
不要備份重做日誌,備份重做日誌“有弊無利”
- 什麼是資料庫一致性備份?
資料庫一致性備份是指關閉了資料庫後備份所有資料檔案和控制檔案的方法。當使用SHUTDOWN 命令正常
關閉了資料庫之後,所有資料庫檔案的當前SCN 值完全一致,所以關閉後的資料庫備份被稱為資料庫
一致性備份或者冷備份。資料庫一致性備份既適用於ARCHIVELOG 也適用於NOARCHIVELOG


實驗操作記錄:
- 列出要備份的資料檔案和控制檔案。當進行資料庫一致性備份時,不需要備份重做日誌。但是在
NOARCHIVELOG 模式下,DBA 可以考慮備份重做日誌(不備份也可以),而在ARCHIVELOG 模式下切記不要
備份重做日誌。
[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 11:46:50 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SYS on 15-MAY-07 at testdb>select name from v$datafile
2 union
3 select name from v$controlfile;
/testdata/oradata/testdb/control01.ctl
/testdata/oradata/testdb/control02.ctl
/testdata/oradata/testdb/control03.ctl
/testdata/oradata/testdb/example01.dbf
/testdata/oradata/testdb/sysaux01.dbf
/testdata/oradata/testdb/system01.dbf
/testdata/oradata/testdb/system02.dbf
/testdata/oradata/testdb/undotbs01.dbf
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/users02.dbf

10 rows selected.
- 關閉資料庫
SYS on 15-MAY-07 at testdb>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
- 複製資料檔案和控制檔案,引數檔案和口令檔案
SYS on 15-MAY-07 at testdb>!cp /testdata/oradata/testdb/control01.ctl /testdat
a/back
!cp /testdata/oradata/testdb/control02.ctl /testdata/back
!cp /testdata/oradata/testdb/control03.ctl /testdata/back
!cp /testdata/oradata/testdb/example01.dbf /testdata/back
!cp /testdata/oradata/testdb/sysaux01.dbf /testdata/back
!cp /testdata/oradata/testdb/system01.dbf /testdata/back
!cp /testdata/oradata/testdb/system02.dbf /testdata/back
!cp /testdata/oradata/testdb/undotbs01.dbf /testdata/back
!cp /testdata/oradata/testdb/users01.dbf /testdata/back
!cp /testdata/oradata/testdb/users02.dbf /testdata/back
! cp /oradev2/app/oracle/product/10.2.0/dbs/orapwtestdb /testdata/back

! cp /oradev2/app/oracle/product/10.2.0/dbs/spfiletestdb.ora /testdata/back

- 正常啟動資料庫
SYS on 15-MAY-07 at testdb>startup
ORACLE instance started.
Total System Global Area 599785472 bytes
Fixed Size 2073096 bytes
Variable Size 234884600 bytes
Database Buffers 356515840 bytes
Redo Buffers 6311936 bytes
Database mounted.
Database opened.


- 什麼是資料庫非一致性備份?
資料庫非一致性備份是指在OPEN 狀態下備份資料庫所有資料檔案和控制檔案的方法。
因為在OPEN 狀態下資料庫內容隨時都會改變,從而導致不同資料庫檔案的當前SCN值完全不同
,所以開啟時的資料庫備份被稱為資料庫非一致性備份。進行資料庫一致性備份會中斷業務操作。
而資料庫非一致性備份不會影響業務操作,因此建議你在實際環境中應該使用資料庫非一致性備份。
注意,資料庫非一致性備份只適用於ARCHIVELOG模式,不適合於NOARCHIVELOG模式。

實驗操作記錄:

- 列出要備份的資料檔案。

[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 13:24:32 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SYS on 15-MAY-07 at testdb>select name from v$datafile;
/testdata/oradata/testdb/system01.dbf
/testdata/oradata/testdb/undotbs01.dbf
/testdata/oradata/testdb/sysaux01.dbf
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/example01.dbf
/testdata/oradata/testdb/users02.dbf
/testdata/oradata/testdb/system02.dbf

7 rows selected.

SYS on 15-MAY-07 at testdb>select FILE_NAME from dba_data_files;
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/sysaux01.dbf
/testdata/oradata/testdb/undotbs01.dbf
/testdata/oradata/testdb/system01.dbf
/testdata/oradata/testdb/example01.dbf
/testdata/oradata/testdb/users02.dbf
/testdata/oradata/testdb/system02.dbf

7 rows selected.

- 開始資料庫備份,在列出要備份的資料檔案之後,將資料庫設定為備份模式。當將資料庫設定為備份
模式之後,會在資料庫上發出全域性檢查點,並且會在所有資料檔案頭快上加鎖,以保證在備份期間資料檔案
的頭塊內容不會改變
SYS on 15-MAY-07 at testdb>alter database begin backup;

Database altered.

- 備份所有資料檔案和控制檔案
SYS on 15-MAY-07 at testdb>!cp /testdata/oradata/testdb/example01.dbf /test
data/back1
!cp /testdata/oradata/testdb/sysaux01.dbf /testdata/back1
!cp /testdata/oradata/testdb/system01.dbf /testdata/back1
!cp /testdata/oradata/testdb/system02.dbf /testdata/back1
!cp /testdata/oradata/testdb/undotbs01.dbf /testdata/back1
!cp /testdata/oradata/testdb/users01.dbf /testdata/back1
!cp /testdata/oradata/testdb/users02.dbf /testdata/back1

SYS on 15-MAY-07 at testdb>alter database backup controlfile to '/testdata/back1
/test.ctl';

Database altered.

- 結束資料庫備份,在備份了所有資料檔案和控制檔案之後,結束資料庫備份。在結束了資料庫備份之後,
為了確保資料檔案備份的同步性,還應該歸檔當前日誌組。
SYS on 15-MAY-07 at testdb>alter database end backup;

Database altered.

SYS on 15-MAY-07 at testdb>alter system archive log current;

System altered.


備份表空間

- 什麼是備份表空間?
備份表空間是指在資料庫處於OPEN 狀態時備份起資料檔案的方法。
注意,表空間備份只適用於ARCHIVELOG 模式,不適用NOARCHIVELOG
另外,當備份表空間時,即可以備份表空間的所有資料檔案,也可以
備份表空間的某個資料檔案。

- 什麼是離線備份?
離線備份是指當表空間處於OFFLINE 狀態時,備份表空間所有資料檔案
或單個資料檔案的過程。與聯機備份相比,這種備份方法的優點是會生成
較少的重做資訊,但缺點是會影響表空間上的業務操作。因為SYSTEM 和正
使用的UNDO 表空間都不能被離線,所以這種方法不能備份SYSTEM和正在用的UNDO

實驗記錄:
- 確定表空間所包含的資料檔案
[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 13:46:01 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SYS on 15-MAY-07 at testdb>select file_name from dba_data_files
2 where tablespace_name='USERS';
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/users02.dbf

-設定表空間為離線狀態。
SYS on 15-MAY-07 at testdb>alter tablespace users offline;

Tablespace altered.

- COPY 資料檔案

SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users01.dbf /testdata/b
ack2

SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users02.dbf /testdata/b
ack2

- 設定表空間為聯機狀態。
SYS on 15-MAY-07 at testdb>alter tablespace users online;

Tablespace altered.

- 再次檢查表空間的狀態

SYS on 15-MAY-07 at testdb>select TABLESPACE_NAME,status from dba_tablespaces;
SYSTEM ONLINE
UNDOTBS1 ONLINE
SYSAUX ONLINE
TEMP ONLINE
USERS ONLINE
EXAMPLE ONLINE

6 rows selected.


- 什麼是聯機備份
聯機備份是指當表空間處於ONLINE 狀態時,備份表空間所有資料檔案或單個資料檔案的
過程。使用這種方法可以備份資料庫的所有表空間和資料檔案。聯機備份的優點是不影響
表空間上的業務操作。但缺點是會生成更多的REDO 資訊和歸檔資訊。

實驗操作記錄:

- 確定表空間所包含的資料檔案
[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 13:57:00 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SYS on 15-MAY-07 at testdb>select file_name from dba_data_files
2 where tablespace_name='USERS';
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/users02.dbf

- 設定表空間為備份模式

SYS on 15-MAY-07 at testdb>alter tablespace users begin backup;

Tablespace altered.

- 複製資料檔案
SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users01.dbf /testdata/b
ack2

SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users02.dbf /testdata/back2

-設定表空間為正常模式
SYS on 15-MAY-07 at testdb>alter tablespace users end backup;

Tablespace altered.


- 模仿 聯機備份失敗

[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 14:03:15 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

- 改變表空間為備份模式
SYS on 15-MAY-07 at testdb>alter tablespace users begin backup;

Tablespace altered.

SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users02.dbf /testdata/b
ack2

SYS on 15-MAY-07 at testdb>

[testdb:oratest] /testdata/arch> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 14:02:48 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

- 開啟另外一個視窗 ,不正常關閉DB
SYS on 15-MAY-07 at testdb>shutdown abort
ORACLE instance shut down.

-啟動資料庫。。。報錯
[testdb:oratest] /oradev2/home> sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.2.0 - Production on Tue May 15 14:05:40 2007

Copyright (c) 1982, 2005, Oracle. All Rights Reserved.

Connected to an idle instance.

SYS on 15-MAY-07 at testdb>startup
ORACLE instance started.
Total System Global Area 599785472 bytes
Fixed Size 2073096 bytes
Variable Size 234884600 bytes
Database Buffers 356515840 bytes
Redo Buffers 6311936 bytes
Database mounted.
ORA-01113: file 4 needs media recovery
ORA-01110: data file 4: '/testdata/oradata/testdb/users01.dbf'


- 啟動到資料到MOUNT 狀態
SYS on 15-MAY-07 at testdb>startup force mount
ORACLE instance started.
Total System Global Area 599785472 bytes
Fixed Size 2073096 bytes
Variable Size 234884600 bytes
Database Buffers 356515840 bytes
Redo Buffers 6311936 bytes
Database mounted.
SYS on 15-MAY-07 at testdb>desc v$backup
Name
Null? Type
-------------------------------------------------------------------------------
---------------------------- -------- ------------------------------------------
------------------------------
FILE#
NUMBER
STATUS
VARCHAR2(18)
CHANGE#
NUMBER
TIME
DATE

- 檢視還處在備份狀態的資料檔案
SYS on 15-MAY-07 at testdb>select file#,status from v$backup;
1 NOT ACTIVE
2 NOT ACTIVE
3 NOT ACTIVE
4 ACTIVE
5 NOT ACTIVE
6 ACTIVE
7 NOT ACTIVE

7 rows selected.

- 方法1,結束備份模式

SYS on 15-MAY-07 at testdb>alter database end backup;

Database altered.

- 方法2 ,也可以用DATAFILE
SYS on 15-MAY-07 at testdb>alter database datafile 4 end backup;
alter database datafile 4 end backup
*
ERROR at line 1:
ORA-01235: END BACKUP failed for 1 file(s) and succeeded for 0
ORA-01199: file 4 is not in online backup mode
ORA-01110: data file 4: '/testdata/oradata/testdb/users01.dbf'

- RECOVER DATAFILE
SYS on 15-MAY-07 at testdb>recover datafile 4;
Media recovery complete.
SYS on 15-MAY-07 at testdb>recover datafile 6;
Media recovery complete.

- 開啟資料庫
SYS on 15-MAY-07 at testdb>alter database open;

Database altered.

- 備份只讀表空間。
當表空間轉變為只讀之後,使用者只能查詢表空間內物件的資料,而不能
在表空間的物件上執行DML/DDL 操作(DROP 除外)


- 改變表空間為只讀
SYS on 15-MAY-07 at testdb>alter tablespace users read only;

Tablespace altered.

- 顯示只讀狀態的表空間

SYS on 15-MAY-07 at testdb>select tablespace_name from dba_tablespaces
2 where status='READ ONLY';
USERS

- 查詢只讀表空間對應的DATAFILE
SYS on 15-MAY-07 at testdb>select file_name from dba_data_files
2 where tablespace_name='USERS'
3 /
/testdata/oradata/testdb/users01.dbf
/testdata/oradata/testdb/users02.dbf

- 複製表空間
SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users01.dbf /testdata/b
ack2

SYS on 15-MAY-07 at testdb>! cp /testdata/oradata/testdb/users02.dbf /testdata/b
ack2

- 恢復只讀表空間為正常讀寫狀態

SYS on 15-MAY-07 at testdb>alter tablespace users read write;

Tablespace altered.

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

相關文章