现在的位置: 首页 > 综合 > 正文

什么是备份数据库?什么是数据库一致性备份和非一致性备份?

2014年11月21日 ⁄ 综合 ⁄ 共 10181字 ⁄ 字号 评论关闭
导读:

  读书操作记录

  - 什么是备份数据库?

  备份数据库是指备份数据库的所有数据文件和控制文件,另外还应该备份参数文件和口令文件,注意,当备份数据库时,

  不要备份重做日志,备份重做日志“有弊无利”

  - 什么是数据库一致性备份?

  数据库一致性备份是指关闭了数据库后备份所有数据文件和控制文件的方法。当使用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.

本文转自

http://zhulch.itpub.net/post/17395/287880

【上篇】
【下篇】

抱歉!评论已关闭.