SYSTEM 表空间管理及备份恢复

--=============================

-- SYSTEM 表空间管理及备份恢复

--=============================

 

    SYSTEM表空间是Oracle数据库最重要的一个表空间,存放了一些DDL语言产生的信息以及PL/SQL包、视图、函数、过程等,称之为数据字典,

因此该表空间也具有其特殊性,下面描述SYSTEM表空间的相关特性及备份与恢复。   

   

一、SYSTEM表空间的管理

    1.建议不存放用户数据,避免用户错误导致系统表空间不可用

        应当为系统设定缺省的默认表空间来避免用户创建时使用系统表空间

            ALTER DATABASE DEFAULT TABLESPACE tablespace_name

           

        SQL> col property_value format a30

        SQL> select property_name,property_value from database_properties

          2  where property_name like 'DEFAULT%';

 

        PROPERTY_NAME                  PROPERTY_VALUE

        ------------------------------ ------------------------------

        DEFAULT_TEMP_TABLESPACE        TEMP

        DEFAULT_PERMANENT_TABLESPACE   USERS  --此处应当为非SYSTEM表空间

        DEFAULT_TBS_TYPE               SMALLFILE

       

    2.SYSTEM表空间特性

        不能脱机offline

        不能置为只读read only

        不能重命名

        不能删除

            --演示不能脱机

                SQL> alter tablespace system offline;

                alter tablespace system offline

                *

                ERROR at line 1:

                ORA-01541: system tablespace cannot be brought offline; shut down if necessary

 

                SQL> alter database datafile 1 offline;

                alter database datafile 1 offline

                *

                ERROR at line 1:

                ORA-01541: system tablespace cannot be brought offline; shut down if necessary

 

            --不能置为只读状态

                SQL> alter tablespace system read only;

                alter tablespace system read only

                *

                ERROR at line 1:

                ORA-01643: system tablespace can not be made read only

 

            --不能重命名

                SQL> alter tablespace system rename to system2;

                alter tablespace system rename to system2

                *

                ERROR at line 1:

                ORA-00712: cannot rename system tablespace

 

            --不能删除

                SQL> drop tablespace system;

                drop tablespace system

                *

                ERROR at line 1:

                ORA-01550: cannot drop system tablespace

 

                SQL> drop tablespace system including contents and datafiles;

                drop tablespace system including contents and datafiles

                *

                ERROR at line 1:

                ORA-01550: cannot drop system tablespac

               

        总结:system表空间就一句话,普通表空间所具有的更名、删除、只读、脱机不为system表空间所拥有

       

    3.空间管理

        保证空间可用,一般存放单个数据文件。设置为自动扩展

        如果SYSTEM表空间数据文件很大,可以考虑使用bigfile

        使用下面的视图来获取表空间的相关状态,使用空间等等

            dba_data_files

            dba_tablespaces

            dba_free_space

            v$datafiles

            v$tablespace

        --查看表空间的大小及已用大小   

            SQL> select tablespace_name,bytes/1024/1024 cur_size,user_bytes/1024/1024 as user_bytes,status,online_status

              2  from dba_data_files;

 

            TABLESPACE_NAME                  CUR_SIZE USER_BYTES STATUS    ONLINE_

            ------------------------------ ---------- ---------- --------- -------

            USERS                                   5     4.9375 AVAILABLE ONLINE

            SYSAUX                                250   249.9375 AVAILABLE ONLINE

            UNDOTBS1                               35    34.9375 AVAILABLE ONLINE

            SYSTEM                                500   499.9375 AVAILABLE SYSTEM

            EXAMPLE                               100    99.9375 AVAILABLE ONLINE  

       

        --查看表空间的剩余空间

            SQL> select tablespace_name,sum(bytes/1024/1024)

              2  from dba_free_space group by tablespace_name;

 

            TABLESPACE_NAME                SUM(BYTES/1024/1024)

            ------------------------------ --------------------

            UNDOTBS1                                    11.6875

            SYSAUX                                        2.125

            USERS                                             2

            SYSTEM                                       10.125

            EXAMPLE                                       31.75

 

        --查看已用空间的百分比

            select a.tablespace_name, round((sizea-sizeb),2) as used_space, round(sizeb,2) ||' MB' as free_space,

            round((sizea-sizeb)/sizea*100,2) ||'%' as used_percent

            from

            (select tablespace_name,bytes/1024/1024 sizea from dba_data_files) a

            inner join

              (select tablespace_name,sum(bytes/1024/1024) sizeb

               from dba_free_space group by tablespace_name) b

            on a.tablespace_name = b.tablespace_name

            order by a.tablespace_name;

 

二、SYSTEM表空间的冷备与恢复

        关于数据库的冷备份请参考: Oracle 冷备份

        关于如何切换归档模式请参考:Oracle 归档日志

        因仅仅涉及SYSTEM表空间的备份与恢复,在此仅仅备份SYSTEM表空间

 

    1.非归档模式下的备份与恢复

        --查看归档模式

            SQL> select log_mode from v$database;

 

            LOG_MODE

            ------------

            NOARCHIVELOG

 

        --关闭数据库后备份system01.dbf文件到指定备份目录

            SQL> shutdown immediate;

            SQL> ho cp $ORACLE_BASE/oradata/orcl/system01.dbf $ORACLE_BASE/coolbak/

 

        --基于非系统表空间创建表tb

            SQL> startup

            SQL> create table tb tablespace users as select * from scott.emp;

 

            SQL> select count(1) from tb;

 

              COUNT(1)

            ----------

                    14

       

        --多次切换日志直到日志组被清空

            SQL> alter system switch logfile;

 

        --删除system01.dbf文件

            SQL> ho rm $ORACLE_BASE/oradata/orcl/system01.dbf

           

        --强制启动后出现错误提示

            SQL> startup force;

            ORACLE instance started.

 

            Database mounted.

            ORA-01157: cannot identify/lock data file 1 - see DBWR trace file

            ORA-01110: data file 1: '/u01/app/oracle/oradata/orcl/system01.dbf'

 

        --还原system表空间的数据文件   

            SQL> ho cp $ORACLE_BASE/coolbak/system01.dbf $ORACLE_BASE/oradata/orcl/

           

        --基于放弃的恢复system01.dbf   

            SQL> recover database until cancel;

            ORA-00279: change 677850 generated at 08/15/2010 14:00:36 needed for thread 1

            ORA-00289: suggestion : /u01/app/oracle/flash_recovery_area/ORCL/archivelog/2010_08_15/o1_mf_1_10_%u_.arc

            ORA-00280: change 677850 for thread 1 is in sequence #10

 

            Specify log: {<RET>=suggested | filename | AUTO | CANCEL}

            cancel        --系统提示成功恢复需要使用RESETLOGS打开数据库

            ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below

            ORA-01194: file 2 needs more recovery to be consistent--提示数据文件需要执行一致性恢复,有些数据在undo表空间丢失

            ORA-01110: data file 2: '/u01/app/oracle/oradata/orcl/undotbs01.dbf'  

 

            ORA-01112: media recovery not started      

       

        --由于没有备份datafile 2,因此启用隐藏参数_allow_resetlogs_corruption

            SQL> alter system set "_allow_resetlogs_corruption" = true scope = spfile;  --此参数为静态参数,需要设置scope

 

            SQL> startup force;

            ORACLE instance started.

 

            Database mounted.

            ORA-01589: must use RESETLOGS or NORESETLOGS option for database open

 

        --使用resetlogs打开数据库时系统异常终止

            SQL> alter database open resetlogs; 

            alter database open resetlogs

            *

            ERROR at line 1:

            ORA-00603: ORACLE server session terminated by fatal error

 

            SQL> exit;  --退出

   

        --重新登录后可以正常启动

            [oracle@robinson ~]$ sqlplus / as sysdba

            SQL> startup

            ORACLE instance started.

 

            Database mounted.

            Database opened.

 

        --关闭隐藏参数_allow_resetlogs_corruption

            SQL> alter system reset "_allow_resetlogs_corruption" scope =spfile sid ='*';

 

        --提交的事务因日志被清空,数据丢失

            SQL> select count(1) from tb;

            select count(1) from tb

                                 *

            ERROR at line 1:

            ORA-00942: table or view does not exist

               

    2.归档模式下的备份与恢复   

        --获得system表空间的路径后一致性关闭数据库并复制system表空间数据文件到备份路径     

            SQL> col name format a55

            SQL> select name from v$datafile where file# = 1;

 

            NAME

            -------------------------------------------------------

            /u01/app/oracle/oradata/orcl/system01.dbf

 

            SQL> shutdown immediate;

            SQL> ho cp $ORACLE_BASE/oradata/orcl/system01.dbf $ORACLE_BASE/coolbak/

 

        --查看日志归档模式

            SQL> select log_mode from v$database;

 

            LOG_MODE

            ------------

            ARCHIVELOG

   

        --删除system01.dbf

            SQL> ho rm $ORACLE_BASE/oradata/orcl/system01.dbf;

   

        --system01.dbf文件丢失后,下面可以查询users表空间的数据

            SQL> select count(1) from scott.emp;

 

              COUNT(1)

            ----------

                    14 

       

        --基于system表空间创建表并插入数据,提示system表空间的数据文件丢失,无法创建该表

            SQL> create table tb_test tablespace system as select * from dba_segments ;

            create table tb_test as select * from dba_segments

               *

            ERROR at line 1:

            ORA-01116: error in opening database file 1

            ORA-01110: data file 1: '/u01/app/oracle/oradata/orcl/system01.dbf'

            ORA-27041: unable to open file

            Linux Error: 2: No such file or directory

            Additional information: 3

 

        --可以基于非system表创建对象,如下在users表空间创建表tb_test

            SQL> create table tb_test tablespace users as select * from scott.emp;

 

            SQL> select count(1) from tb_test;

 

              COUNT(1)

            ----------

                    14

   

        --下面给帐户解锁时收到system01.dbf丢失的错误,因为帐户信息存储在system表空间

            SQL> alter user hr account unlock;

            alter user hr account unlock

            *

            ERROR at line 1:

            ORA-00604: error occurred at recursive SQL level 2

            ORA-01116: error in opening database file 1

            ORA-01110: data file 1: '/u01/app/oracle/oradata/orcl/system01.dbf'

            ORA-27041: unable to open file

            Linux Error: 2: No such file or directory

            Additional information: 3

       

        --开启另外一个会话,可以连接,但出现以下错误提示

            SQL> conn scott/tiger;

            ERROR:

            ORA-00604: error occurred at recursive SQL level 1

            ORA-01116: error in opening database file 1

            ORA-01110: data file 1: '/u01/app/oracle/oradata/orcl/system01.dbf'

            ORA-27041: unable to open file

            Linux Error: 2: No such file or directory

            Additional information: 3

           

        --无法一致性关闭数据库

            SQL> shutdown immediate;  

            ORA-01116: error in opening database file 1

            ORA-01110: data file 1: '/u01/app/oracle/oradata/orcl/system01.dbf'

            ORA-27041: unable to open file

            Linux Error: 2: No such file or directory

            Additional information: 3

            SQL> shutdown abort;    --强制关闭数据库

            ORACLE instance shut down.

 

        --还原system表空间的数据文件

            SQL> ho cp $ORACLE_BASE/coolbak/system01.dbf $ORACLE_BASE/oradata/orcl/

 

        --恢复system表空间的数据文件

            SQL> recover datafile 1;

            Media recovery complete.

            SQL> alter database open;

 

        --由于日志没有丢失,因此已提交的事务保持一致性

            SQL> select count(1) from tb_test;

 

              COUNT(1)

            ----------

                    14

   

    总结:

        a.非归档模式下,由于联机重做日志的循环使用规则,一些已经写入日志但尚未及时更到数据文件的数据信息在故障发生后将丢失

          因此仅仅能恢复到表空间(此处为system表空间)备份的时刻,而归档模式下可以恢复到指定或最新时刻

        b.system表空间丢失后,涉及到system表空间的数据字典将不可用,也不可在system表空间添加对象

        c.使用了resetlogs重置了日志文件,建议一致性关闭数据库后,立即全备数据库

   

三、SYSTEM表空间的热备与恢复   

    由于非归档模式容易造成数据的丢失,而且生产数据库一般采用归档模式,因此在此不再考虑非归档模式下的备份与恢复处理

 

    --查看日志归档模式

        SQL> select log_mode from v$database;

 

        LOG_MODE

        ------------

        ARCHIVELOG 

       

    --session1中开始system表空间的热备模式

        SQL> alter tablespace system begin backup;

 

    --session2中创建表tb_seg

        SQL> show user;

        USER is "LION" 

        SQL> create table tb_seg tablespace users as select * from dba_segments;

   

    --session1中进行热备到指定路径

        SQL> ho cp $ORACLE_BASE/oradata/orcl/system01.dbf $ORACLE_BASE/hotbak/

   

    --session2中清空刚刚创建的tb_seg

        SQL> delete from tb_seg;

 

        SQL> commit;

   

    --session1中关闭热备模式,并删除system01.dbf

        SQL> alter tablespace system end backup;

 

        SQL> ho rm $ORACLE_BASE/oradata/orcl/system01.dbf

   

    --session2中继续会话并执行下列操作

        SQL> insert into tb_seg select * from dba_segments where rownum<6;

 

        SQL> commit;

 

        SQL> select count(1) from tb_seg;

 

          COUNT(1)

        ----------

                 5

   

    --强制启动数据

        SQL> startup force;

        ORACLE instance started.

 

        Database mounted.

        ORA-01157: cannot identify/lock data file 1 - see DBWR trace file

        ORA-01110: data file 1: '/u01/oracle/oradata/orcl/system01.dbf'

                 

    --还原system表空间的数据文件

        SQL> ho cp $ORACLE_BASE/hotbak/system01.dbf $ORACLE_BASE/oradata/orcl/

   

    --执行介质恢复

        SQL> recover datafile 1;

        Media recovery complete.

        SQL> alter database open ;

   

    --验证恢复,表tb_seg的数据正确

        SQL> select count(1) from lion.tb_seg;

 

          COUNT(1)

        ----------

                 5

 

四、基于RMANSYSTEM表空间的备份与恢复

        1.在使用RMAN备份表空间,数据文件时,逻辑上等同于使用Oracle的热备份,只不过RMAN是将表空间、数据文件进行打包封装到一个备

    份集多个备份片之中。事实上,在备份system表空间与备份普通表空间是一样的,但使用RMAN备份system表空间时,RMAN会自动备份控制文件

    以及spfile文件。RMAN使用backup datafile 1(backup database)一条命令来代替将表空间置于热备以及手动复制文件的过程

        2.对于RMANsystem表空间的还原与恢复,与冷备,热备模式所不同的是,还原时并不需要实现手工拷贝备份文件至原始位置,一旦

    发布restore指令,RMAN会自动寻找该表空间最新备份的数据,并完成还原操作。其次使用recover指令来完成介质恢复。

        3.如上描述,使用RMAN备份system表空间与热备模式大同小异,故不再演示,请参考下面给出的相关链接。

       

五、更多参考

       

    有关基于用户管理的备份和备份恢复的概念请参考:

        Oracle 冷备

        Oracle 热备

        Oracle 备份恢复概念

        Oracle 实例恢复

        Oracle 基于用户管理恢复的处理(详细描述了介质恢复及其处理)

       

    有关RMAN的恢复与管理请参考:

        RMAN 概述及其体系结构

        RMAN 配置、监控与管理

        RMAN 备份详解

        RMAN 还原与恢复

       

    有关Oracle体系结构请参考:

        Oracle 实例和Oracle数据库(Oracle体系结构)

        Oracle 表空间与数据文件

        Oracle 密码文件

        Oracle 参数文件

Oracle 数据库实例启动关闭过程

        Oracle 联机重做日志文件(ONLINE LOG FILE)

        Oracle 控制文件(CONTROLFILE)

        Oracle 归档日

 

 

 

       

   

posted @ 2010-11-28 18:14  生活不是用来挥霍的  阅读(510)  评论(0编辑  收藏  举报