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

-- 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 归档日志

        因仅仅涉及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/