posts - 310, comments - 6939, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          oracle 10g數(shù)據(jù)庫備份與還原總結(jié)
          一.總述

          1.數(shù)據(jù)庫歸檔模式:

                  * 非歸檔模式:當(dāng)數(shù)據(jù)庫數(shù)據(jù)只讀不會改變時(shí),數(shù)據(jù)不會改變,數(shù)據(jù)庫適合用非歸檔模式,                     這樣提高性能

                  * 歸檔模式:對于數(shù)據(jù)庫數(shù)據(jù)經(jīng)常變動,數(shù)據(jù)庫最好用歸檔模式,這樣可以盡量避免數(shù)據(jù)庫                   數(shù)據(jù)丟失,使出現(xiàn)問題時(shí)災(zāi)害降到最低

          2.非歸檔模式與歸檔模式間的切換(在命令窗口下):

                  @檢查當(dāng)前日志操作模式
                        f:\>sqlplus sys/dba as sysdba
                        連接數(shù)據(jù)庫
                        sql>select log_mode from v$database;

                  @關(guān)閉數(shù)據(jù)庫,后裝載數(shù)據(jù)庫,只能在mount狀態(tài)下改變?nèi)罩静僮髂J?br />               sql>startup mount

                  @改變?nèi)罩灸J?br />               sql>alter database achivelog;(變?yōu)闅w檔方式,否則為nochivelog)
                        sql>alter database open;
           
          3.查看歸檔模式相關(guān)信息:

                  @若采用默認(rèn)設(shè)置,歸檔日志位置在快速恢復(fù)區(qū)

                  @配置歸檔日志位置 log_archive_dest_n  (n=[1:10])
                        sql>alter system set
                            log_archive_dest_1 = 'location =c:\demo\archive [optional]';
                        sql>alter system set
                            log_archive_dest_2 = 'service =standby [mandatory] [reopen]';
                   其中:location 是本地
                        service  是遠(yuǎn)程
                        optional 無論歸檔是否成功,都會覆蓋重做日志
                        mandatory只有成功歸檔才會覆蓋重做日志
                        reopen 重新歸檔時(shí)間間隔 默認(rèn)是300s

                  @顯示歸檔信息,即顯示顯示日志操作模式
                        sql>select name,log_mode from v$database;

                  @顯示歸檔日志信息
                        sql>select name,sequence#,first_change# from v$archive_log;
                   sequence#: 歸檔序列號
                   first_change# : 起始SCN值

                  @顯示歸檔日志位置
                        sql>select dest_name,destination,status from v$archive_dest;
                   dest_name 歸檔位置參數(shù)名
                   destination 位置

                  @顯示日志歷史信息
                        sql>select * from v$loghist;
                   threade# : 重做線程號
                   sequence# : 日志序列號
                   first_change# : 起始SCN值
                   first_time : 起始SCN發(fā)生時(shí)間
                   switch_change : 切換SCN值
                 
          二.非歸檔模式下的備份與還原

             . 日志采用覆蓋原有日志的方式存儲

             . 出現(xiàn)介質(zhì)失敗時(shí),只能恢復(fù)到過去的完全備份點(diǎn)
             . 數(shù)據(jù)庫OPEN狀態(tài)是,不能備份數(shù)據(jù)庫

             . 必須先關(guān)閉數(shù)據(jù)庫,再備份,且必須備份所有數(shù)據(jù),控制文件

            1.備份==冷備份

                 @ 先關(guān)閉數(shù)據(jù)庫
                       sql>shutdown immediate

                 @ 拷貝所有數(shù)據(jù)文件,和控制文件

            2.恢復(fù)

                 * 數(shù)據(jù)文件出現(xiàn)丟失,但備份以來的重做日志內(nèi)容尚未被覆蓋,此時(shí)可以完全恢復(fù)

                     @裝載數(shù)據(jù)庫
                         sql>startup force mount

                     @復(fù)制數(shù)據(jù)文件備份

                     @恢復(fù)數(shù)據(jù)文件
                         sql>recover datafile 1

                     @打開數(shù)據(jù)庫
                         sql>alter database open;

                 * 備份文件的重做日志內(nèi)容已經(jīng)被覆蓋,則只能還原到備份點(diǎn)

                     @關(guān)閉數(shù)據(jù)庫

                     @復(fù)制所有數(shù)據(jù)和控制備份文件至原位置

                     @裝載數(shù)據(jù)庫,執(zhí)行恢復(fù)命令
                         sql>startup mount
                         sql>recover database until cancel
                         提示控制文件不再使用原有重做日志

                     @用resetlogs選項(xiàng)打開數(shù)據(jù)庫
                          sql>alter database open resetlogs;


          三.歸檔模式

             . 可以防止數(shù)據(jù)丟失

             . 數(shù)據(jù)處于OPEN狀態(tài)時(shí)可以進(jìn)行備份

             . 當(dāng)出現(xiàn)介質(zhì)失敗時(shí),除了SYSTEM表空間中的數(shù)據(jù)文件外其他的都可以在open狀態(tài)下恢復(fù)

             . 可執(zhí)行完全恢復(fù),也可以恢復(fù)到備份點(diǎn)與失敗點(diǎn)之間的某時(shí)刻
           
            1.備份

                1)冷備份
                      shutdown后拷貝所需數(shù)據(jù)文件和控制文件

                2)熱備份
                      @將數(shù)據(jù)庫設(shè)置為歸檔模式
                      @在數(shù)據(jù)庫上發(fā)出全局檢查點(diǎn),在所有的數(shù)據(jù)文件頭塊上加鎖
                          sql>alter database begin backup ;
                      @備份數(shù)據(jù)文件及控制文件
                          數(shù)據(jù)文件直接拷貝至其他目錄
                          控制文件需使用alter database backup controlfile
                              sql>alter database backup controlfile to 'd:\backup\demo.ctl';
                      @結(jié)束數(shù)據(jù)備份,為確保數(shù)據(jù)文件備份的同步性,還應(yīng)該歸檔當(dāng)前日志組
                              sql>alter database end backup;
                              sql>alter system archive log cuurent;

                3)備份表空間
                      *脫機(jī)備份(表空間處于OFFLINE狀態(tài),SYSTEM表空間和正在使用的UNDO空間不能脫機(jī))
                           @確定表空間包涵的數(shù)據(jù)文件,如空間USERS
                                sql>select file_name from dba_data_files
                                    where tablespace_name = 'users';
                           @設(shè)置表空間為脫機(jī)狀態(tài)
                                sql>alter tablespace users offline;
                           @復(fù)制數(shù)據(jù)文件
                           @設(shè)置表空間聯(lián)機(jī)
                                sql>alter tablespace users online;
                      *聯(lián)機(jī)備份
                           @確定對應(yīng)關(guān)系
                           @設(shè)置表空間為備份模式
                                sql>alter tablespace users begin backup ;
                           @復(fù)制數(shù)據(jù)文件
                           @設(shè)置表空間為正常模式
                                sql>alter tablespace users end backup ;
                      *處理聯(lián)機(jī)備份失敗
                          當(dāng)執(zhí)行聯(lián)機(jī)備份時(shí),若出現(xiàn)例程失敗,再次打開數(shù)據(jù)庫時(shí),如果數(shù)據(jù)文件仍處于備                份狀態(tài),則會出錯(cuò),此時(shí)需要結(jié)束備份
                           @裝載數(shù)據(jù)庫
                              sql>startup force mount
                           @查看處于聯(lián)機(jī)備份狀態(tài)的所有數(shù)據(jù)文件
                              sql>select file# from v$backup where status = 'active';
                           @結(jié)束聯(lián)機(jī)備份狀態(tài)
                              sql>alter tablespace end backup
                                或alter tablespace datafile backup;
                                或recover datafile 序號
                           @打開數(shù)據(jù)庫
                              sql>alter database open;
               2.還原
                   1)完全恢復(fù)
                       相關(guān)命令:
                          recover database :恢復(fù)數(shù)據(jù)庫的多個(gè)數(shù)據(jù)文件(在MOUNT狀態(tài)下運(yùn)行)
                          recover tadaspace :恢復(fù)多個(gè)表空間的所有數(shù)據(jù)文件(在open狀態(tài)下運(yùn)行)
                          recover datafile :恢復(fù)一個(gè)或多個(gè)數(shù)據(jù)文件(在mount,open狀態(tài)下都可)
                       *在mount狀態(tài)下恢復(fù)數(shù)據(jù)文件(用于SYSTEM表空間恢復(fù))
                           @裝載數(shù)據(jù)庫 mount狀態(tài)
                           @確定需要恢復(fù)的數(shù)據(jù)文件
                               sql>select file#,error from v$recover_file
                           @拷貝備份至原位置
                           @恢復(fù)數(shù)據(jù)庫 recover..上述命令
                           @打開數(shù)據(jù)庫 alter database open;
                       *在open狀態(tài)下恢復(fù)關(guān)閉后意外對視的數(shù)據(jù)文件,此時(shí)數(shù)據(jù)庫將無法打開
                           @裝載數(shù)據(jù)庫 mount
                           @確定需要恢復(fù)的數(shù)據(jù)文件
                           @使相應(yīng)的數(shù)據(jù)文件脫機(jī)
                               sql>alter database datafile 4 offline;
                           @打開數(shù)據(jù)庫
                               sql>alter database open
                           @復(fù)制數(shù)據(jù)備份文件
                           @恢復(fù)數(shù)據(jù)文件
                           @是數(shù)據(jù)文件聯(lián)機(jī) alter database datafile 4 online;
                       *在open狀態(tài)下恢復(fù)打開時(shí)意外丟失的數(shù)據(jù)文件
                           @確定要恢復(fù)的數(shù)據(jù)文件
                           @使數(shù)據(jù)文件脫機(jī)
                           @復(fù)制數(shù)據(jù)文件
                           @恢復(fù)相應(yīng)的表空間或數(shù)據(jù)文件
                           @使相應(yīng)的表空間或數(shù)據(jù)文件聯(lián)機(jī)
                       *在open狀態(tài)下恢復(fù)未備份的數(shù)據(jù)文件(要求在歸檔模式下,新建數(shù)據(jù)文件,其從簡歷              開始的所有歸檔日志必須全部存在)
                           @裝載數(shù)據(jù)庫
                           @確定要恢復(fù)的數(shù)據(jù)文件
                           @是數(shù)據(jù)文件脫機(jī)
                           @打開數(shù)據(jù)庫
                           @重新建立數(shù)據(jù)文件
                               sql>alter database create datafile 'd:\demo\users01.dbf';
                           @恢復(fù)數(shù)據(jù)文件
                           @使數(shù)據(jù)文件聯(lián)機(jī)
                 2)不完全恢復(fù)
                     使用已備份的數(shù)據(jù)文件,歸檔日志,和重做日志,將數(shù)據(jù)庫恢復(fù)到備份點(diǎn)與失敗點(diǎn)之間           某個(gè)時(shí)刻的狀態(tài)
                        *恢復(fù)分類
                            基于時(shí)間的恢復(fù):誤刪除表,誤截?cái)啾?提交了錯(cuò)誤的數(shù)據(jù)
                            基于取笑恢復(fù):恢復(fù)到某個(gè)日志序列號之前的狀態(tài)
                            基于SCN的恢復(fù):恢復(fù)到特定SCN值的狀態(tài)
                            基于備份控制文件恢復(fù):表空間被意外刪除,或所有控制文件全部損壞
                        *不完全恢復(fù)指令
                            recover database until time
                            recover database until change
                            recover database until cancel
                            recover database .. using backup controlfile
                        *基于時(shí)間的恢復(fù)
                            @關(guān)閉數(shù)據(jù)庫(為防止備份失敗,應(yīng)備份當(dāng)前數(shù)據(jù)庫所有文件
                            @裝載數(shù)據(jù)庫 startup mount
                            @復(fù)制所有備份數(shù)據(jù)文件,并確定備份文件的時(shí)間點(diǎn)
                                sql>select file#,to_char(time,'yyyy-mm-dd hh24:mi:ss)
                                    from v$recover_file;
                            @執(zhí)行recover database until time
                                sql>recover database until time "2006-09-26 16:40:33'
                            @以resetlogs方式打開數(shù)據(jù)庫,并檢查恢復(fù)結(jié)果
                                sql>alter database open resetlogs;
                            @備份數(shù)據(jù)庫所有數(shù)據(jù)文件和控制文件,因?yàn)楫?dāng)以resetlogs方式打開數(shù)據(jù)庫后                  會重新建立重做日志,清空原有重做日志的所有內(nèi)容,并將日志序列號復(fù)位為1
                                sql>select name from v$datafile;
                                sql>alter database begin backup;
                                拷貝數(shù)據(jù)文件
                                sql>alter database end backup;
                                sql>alter database backup controlfile
                                    to 'd:\backup\demo.ctl' reuse;
                                sql>alter system archive log cuurent;
                         *基于SCN恢復(fù)
                             @關(guān)閉數(shù)據(jù)庫
                             @裝載數(shù)據(jù)庫 mount
                             @復(fù)制所有備份數(shù)據(jù)文件,確定備份文件SCN值
                                 sql>select file#,change#, from v$recover_file;
                             @執(zhí)行recover database until change
                                 sql>recover database until change 675978;
                             @以resetlogs方式打開數(shù)據(jù)庫
                             @備份數(shù)據(jù)庫所有數(shù)據(jù)文件和控制文件
                         *基于取消恢復(fù)
                             @關(guān)閉數(shù)據(jù)庫
                             @裝載數(shù)據(jù)庫
                             @復(fù)制所有備份的數(shù)據(jù)文件,確定備份文件的SCN值
                                 sql>select file#,change# from v$recover_file;
                             @執(zhí)行recover database until cancle
                                 sql>recover database until cancel
                             @以resetlogs方式打開數(shù)據(jù)庫
                             @備份所有數(shù)據(jù)文件和控制文件
                         *基于備份控制文件的恢復(fù)
                             @通過查看alert文件,確定誤操作時(shí)間
                             @關(guān)閉數(shù)據(jù)庫
                             @復(fù)制所有備份的數(shù)據(jù)文件,控制文件
                             @裝載數(shù)據(jù)庫
                             @執(zhí)行recover database ..using backup controlfile
                                 sql>recover database until time '2004-09-26 19:51:06'
                                     using backup controlfile
                             @以resetlogs 方式打開數(shù)據(jù)庫
                             @備份所有數(shù)據(jù)文件和控制文件

           

          主站蜘蛛池模板: 通化市| 精河县| 永康市| 旅游| 阳曲县| 浑源县| 汉川市| 金山区| 东港市| 昭通市| 拉萨市| 莫力| 富裕县| 宜兴市| 华容县| 瑞丽市| 仁寿县| 周至县| 贵州省| 象州县| 邹城市| 鹤壁市| 抚顺市| 贞丰县| 大冶市| 鄂伦春自治旗| 阆中市| 铜陵市| 东莞市| 呼伦贝尔市| 呼图壁县| 资兴市| 景东| 弋阳县| 平塘县| 阳泉市| 平湖市| 宁海县| 苍山县| 新邵县| 定安县|