您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎樣深入解析RMAN 備份與恢復,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
RMAN 備份與恢復深入解析
一 數據庫版本
SQL> select *from v$version;
BANNER
----------------------------------------------------------------
OracleDatabase 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux:Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 -Production
二 工具
1.Rman:Oracle 8i開始就在使用的oracle專業備份恢復工具,這也是廣大DBA同志接觸最多的備份工具,不僅可以備份單實例還可以在RAC模式下備份,俗話說RMAN在手煩惱沒有,下面我們開始深入淺出來講講Rman的使用方法和備份策略。
Rman能夠干什么:(1)全庫備份,這是必然要進行的操作,因為它是一切恢復的源泉
(2)增量備份,目的減少備份的數據量,分為差異和累計
(3)細粒度備份,表空間,數據文件,控制文件,參數文件,歸檔日志
(4)數據庫克隆,使用備份來遷移數據庫
(5)設計備份策略
(6)管理備份集
(7)自定義Rman腳本
(8)生成Rman報告
如何學習Rman:很多人見了Rman不知道按照什么規則備份,因為Rman里面有很多參數需要設置,搞不好還會備份錯誤,備份策略如何設計等等一系列問題。在這里我要說,遇到一個工具最快的掌握方法就是先用起來,用的好用的壞那是另一回事。其實Rman原理并不復雜,就是復制數據庫一系列文件打個包放在磁盤or磁帶上,需要恢復的時候在拷貝回來。Rman實踐要比原理重要的多,因此“實操族”是Rman的最愛。
注意:通過RMAN來備份和恢復數據庫時,都必須先啟動實例并加載數據庫才行,這是給剛入門的朋友一點提示,如果你是大俠請自動忽視吧。
2.登陸RMAN和退出RMAN
RMAN連接本地數據庫
[oracle@linuxdbbase]$ rman target / 一氣呵成法
RecoveryManager: Release 10.2.0.4.0 - Production on Tue May 7 20:57:49 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connected to target database: BASE (DBID=1845289414) 這個dbid要記好了,很多時候我們需要它
RMAN>
還可以先登陸rman,進入后在連接數據庫
[oracle@linuxdbbase]$ rman
RecoveryManager: Release 10.2.0.4.0 - Production on Tue May 7 21:00:31 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
RMAN>connect target /
connectedto target database: BASE (DBID=1845289414) 每個數據庫都有一個唯一dbid
RMAN>
RMAN連接遠程數據庫
[oracle@linuxdbbase]$ rman target sys/oracle@base179 后綴連接串即可
RecoveryManager: Release 10.2.0.4.0 - Production on Tue May 7 21:10:48 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connectedto target database: BASE (DBID=1843237732)
RMAN>
退出RMAN
RMAN>exit
RMAN>quit
這兩個命令都可以退出,這是給初學者一點點引航,大俠請肅靜and回避
三 備份恢復前的一些準備工作
有備無患是DBA們的口頭禪,如果你想做一個賞心悅目的DBA那就要把功課做充分,從下面入手
1. control_file_record_keep_time
control_file_record_keep_time初始化參數:rman元數據在控制文件中保留的最小有效天數,默認為7天。
官方文檔中描述它是控制文件保留rman元數據有效的最小天數,如果新增加一條記錄到控制文件可重用區reusable,這時最老的記錄尚沒有超出最小保留天數,那么記錄將控制文件的這一部分擴展。如果將該參數設置為0,那么控制文件可重用區將永遠不會擴展。
注意:這個參數只應用于控制文件中可循環利用的部分,如歸檔日志文件,各種備份記錄。不應用于諸如數據文件,表空間,重做日志等,這些內容只有當其從對應的表空間中刪除后才能重用。
最小天數的理解:假設我們設置為7天,那如果我有10天前(甚至更早)的datafile backup和controlfile backup+至今的全部日志,就不能完全恢復了嘛?這是否定的,因為rman元數據保留在控制文件可重用區,如果7天內的備份記錄沒有把可重用區裝滿,就算過了7天你的備份記錄還是存在的,可以正常完全恢復。如果7天內可重用區被裝滿了,控制文件會拿最老的備份記錄來覆蓋(但會保證最小天數內的記錄是有效的)。
我們根據以往經驗,把這個值設置為30天
語法:alter system set control_file_record_keep_time=30;
初始化參數control_file_record_keep_time是動態參數可以直接修改,不用重啟數據庫
SQL>show parameter control
NAME TYPE VALUE
----------------------------------------------- -------------------------------------------
control_file_record_keep_time integer 30
2.啟動歸檔模式
SYS@base>archive log list
Databaselog mode No Archive Mode
Automaticarchival Disabled
Archivedestination USE_DB_RECOVERY_FILE_DEST
Oldestonline log sequence 10
Currentlog sequence 12
首先我們先要創建一個存放archive log的目錄
Oracle10g&11g默認歸檔、閃回、備份都存放在flash_recovery_area中,默認大小2G,一般在生產環境中為了更方便管理這些重要文件,為其專門創建目錄存放。
[oracle@linuxdboracle]$ mkdir archdata 創建一個archive log目錄
路徑:/opt/oracle/archdata
登陸sqlplus,設置歸檔路徑
[oracle@linuxdbarchdata]$ sqlplus / as sysdba
SYS@base> alter system setlog_archive_dest_1='location=/opt/oracle/archdata' scope=both; 已經生效
System altered.
SQL> selectdest_name,destination,status,error from v$archive_dest wheredest_name='LOG_ARCHIVE_DEST_1';
DEST_NAME DESTINATION STATUS ERROR
---------------------------------------------------------------
LOG_ARCHIVE_DEST_1 /opt/oracle/archdata VALID
已經生效
重啟數據庫mount狀態,開啟歸檔
SYS@base> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@base> startup mount
ORACLE instance started.
Total System Global Area1610612736 bytes
Fixed Size 2084296 bytes
Variable Size 385876536 bytes
Database Buffers 1207959552 bytes
Redo Buffers 14692352 bytes
Database mounted.
SYS@base> alter databasearchivelog; 開啟歸檔模式
Database altered.
SYS@base> alter databaseopen; 打開數據庫
Database altered.
凡是alter database操作都是修改“控制文件”內容,走到那說到那嘿
[oracle@linuxdbarchdata]$ ll 剛剛開啟歸檔還沒有生成日志,我們手工切換一下
總用量 0
SQL> alter systemswitch logfile; 手動切換不會觸發checkpoint,自動切換會觸發checkpoint
System altered
[oracle@linuxdbarchdata]$ ll 這時已經有歸檔日志生成了
總用量 22208
-rw-r-----1 oracle oinstall 22736384 5月 8 15:35 1_13_814444678.dbf
-rw-r-----1 oracle oinstall 1024 5月 8 15:35 1_14_814444678.dbf
SQL> selectsequence#,name,archived,applied from v$archived_log; 數據庫層面查看
SEQUENCE# NAME ARCHIVED APPLIED
----------------------------------------------------------- -------- -------
13 /opt/oracle/archdata/1_13_814444678.dbf YES NO
14 /opt/oracle/archdata/1_14_814444678.dbf YES NO
SYS@base> archivelog list;
Database logmode Archive Mode 歸檔模式
Automaticarchival Enabled 自動歸檔啟動
Archivedestination /opt/oracle/archdata 歸檔日志目錄
Oldest online logsequence 13 舊在線日志序號,已經歸檔完的
Next log sequence toarchive 15 下一個將要歸檔的日志序號
Current logsequence 15 當前在線日志序號
寫的很清楚,從這里我們就可以判斷歸檔日志的情況了,有多少歸檔,現在是幾號日志,已經完成歸檔是幾號等等。
3.安裝rlwrap-0.37-1.el5.x86_64.rpm包
大家有沒有在sqlplus中不能使用鍵盤的上下左右鍵,打錯了連刪除字符都不行,沒天理啊,作為一名“鍵盤族”這是忍受不了的,熟可忍熟不可忍。那有沒有什么法寶可以解決這個看似小問題其實大問題的問題(周鴻祎說過任何理由都應該與用戶體驗為準繩)
rlwrap-0.37-1.el5.x86_64.rpm包就可以解決這個問題,但在安裝這個包之前需要先安裝2個依賴包
one:readline-devel-6.0-4.el6.x86_64.rpm
two:ncurses-devel-5.7-3.20090208.el6.x86_64.rpm
oracle用戶環境變量中添加
[oracle@linuxdb~]$ vim .bash_profile
alias sqlplus="rlwrap sqlplus" 添加一個別名
[oracle@linuxdb~]$ . .bash_profile 環境變量生效
[oracle@linuxdb~]$ sqlplus / as sysdba 登陸sqlplus見證奇跡的時刻來臨
上下左右 backspace 全都可以使用了對吧是不是很爽啊~nice go~
4.RMAN環境變量
為什么要設置RMAN環境變量
答:這是一個好問題,做什么事之前問一個“為什么”可以有助于你在別人面前提升自己多動腦的光輝形象:)
進入正題,RMAN環境變量和OS環境變量有同工異曲之意,設置好后,對全局生效,不用每次都指定目錄指定名稱,這是懶人的福音~阿門!不設置可不可以呢,oracle是個開放的軟件,當然可以,這就需要在命令中寫好參數一次性完成,下次再做繼續再寫,比較適合我這樣喜歡狂敲鍵盤的人。下面我們來配置配置吧~咚咚波
因RMAN配置信息都是放在數據庫控制文件中的,因此我們先要連接到目標庫才能顯示環境變量
[oracle@linuxdb~]$ rman target /
RecoveryManager: Release 10.2.0.4.0 - Production on Wed May 8 16:48:50 2013
Copyright(c) 1982, 2007, Oracle. All rightsreserved.
connected to target database: BASE (DBID=1845289414) 有dbid證明已連接到目標庫
RMAN>show all;
using target database control file instead of recovery catalog 使用控制文件來代替恢復目錄數據庫存放rman信息
RMANconfiguration parameters are:
CONFIGURERETENTION POLICY TO REDUNDANCY 1; # default
CONFIGUREBACKUP OPTIMIZATION OFF; # default
CONFIGUREDEFAULT DEVICE TYPE TO DISK; # default
CONFIGURECONTROLFILE AUTOBACKUP OFF; # default
CONFIGURECONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGUREDEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGUREDATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREMAXSETSIZE TO UNLIMITED; # default
CONFIGUREENCRYPTION FOR DATABASE OFF; # default
CONFIGUREENCRYPTION ALGORITHM 'AES128'; # default
CONFIGUREARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURESNAPSHOT CONTROLFILE NAME TO'/opt/oracle/product/10.2.0/db_1/dbs/snapcf_base.f'; # default
這只是一部分,如想多多學習請參考【參】Books-> Backup and Recovery Reference -> CONFIGURE
(1)配置RMAN默認備份介質保存目錄 /opt/oracle/backup
[oracle@linuxdboracle]$ mkdir backup 創建保存目錄
RMAN> configure channel device type disk format'/opt/oracle/backup/DB_%U';
usingtarget database control file instead of recovery catalog
使用目標庫“控制文件”代替“恢復目錄數據庫”存放rman信息
old RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters are successfully stored
新RMAN配置參數生效
(2)配置控制文件自動備份并保存到 /opt/oracle/backup/control
注:當控制文件內容有變化時會自動觸發備份
[oracle@linuxdbbackup]$ mkdir control 創建保存目錄
RMAN> configure controlfile autobackup on; 啟動控制文件自動備份
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP ON;
new RMANconfiguration parameters are successfully stored
RMAN> configure controlfile autobackup format for device type diskto '/opt/oracle/backup/control/cf_%F'; 配置控制文件自動備份保存目錄和格式
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
new RMANconfiguration parameters are successfully stored
(3)配置備份介質保留期為7天
RMAN> configure retention policy to recovery window of 7 days;
new RMANconfiguration parameters:
CONFIGURERETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
new RMANconfiguration parameters are successfully stored
設置好后我們再來看一下rman環境變量
RMAN>show all;
RMANconfiguration parameters are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS; 恢復窗口7天
CONFIGUREBACKUP OPTIMIZATION OFF; # default
CONFIGUREDEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON; 啟動控制文件自動備份,目錄和格式
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
CONFIGUREDEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGUREDATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U'; 備份介質保存目錄
CONFIGUREMAXSETSIZE TO UNLIMITED; # default
CONFIGUREENCRYPTION FOR DATABASE OFF; # default
CONFIGUREENCRYPTION ALGORITHM 'AES128'; # default
CONFIGUREARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURESNAPSHOT CONTROLFILE NAME TO '/opt/oracle/product/10.2.0/db_1/dbs/snapcf_base.f';# default
我們目前配置這4個變量就可以了,其它的什么時候用什么時候做或者直接在命令行中指定。
四 備份與恢復實例
1. 用RMAN分別作數據庫,表空間和數據文件的備份和數據庫,表空間和數據文件損壞后的恢復實例
數據庫級備份與恢復
全庫壓縮備份與全庫非壓縮備份應用場景:
如果你的系統有專用“備份磁陣”或者有足夠的磁盤空間來讓您隨心所欲的用,那說明你很幸運,遇到一個不差錢的boss,往往事與愿違,我們可以自由支配的磁盤空間非常有限,這也驗證了國人勤儉節約的優良傳統。在有限的空間里如何裝下更多的備份呢,這里就用上了壓縮屬性,把原來很大的文件盡可能壓縮,提高空間利用率,當然備份和恢復的時間窗口會長一些,這就是時間換空間的精髓。
Come on 我們先來備個全庫吧
全庫壓縮備份腳本
backup as compressed backupset full database format 命令行中直接指定壓縮選項即可
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelogformat '/opt/oracle/backup/arch_bk1_%u%p%s.rmn' delete all input;
全庫非壓縮備份腳本
backupfull database format
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
全庫使用默認通道默認配置備份腳本,同時刪除備份過的歸檔日志
backup as compressed backupset full databaseinclude current controlfile plus archivelog delete all input;
上面有三種不同情況的備份腳本,我們用第二個,這個我想應該是大眾最常用的。
備份之前檢查archive log ,有三個歸檔日志
[oracle@linuxdb archdata]$ ll
總用量 64644
-rw-r----- 1 oracle oinstall 22736384 5月 8 15:35 1_13_814444678.dbf
-rw-r----- 1 oracle oinstall 1024 5月 8 15:35 1_14_814444678.dbf
-rw-r----- 1 oracle oinstall 43454464 5月 9 12:36 1_15_814444678.dbf
[oracle@linuxdb ~]$ rman target / 進入rman
connected to target database: BASE(DBID=1845289414) 一定要連接到數據庫才行哦
RMAN> backup full database format
2>'/opt/oracle/backup/full_bk1_%u%p%s.rmn' include current controlfile
3> plus
4> archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
Starting backup at 09-MAY-13 備份開始時間
current log archived 一般都從歸檔日志備份
using target database control file instead ofrecovery catalog
allocated channel: ORA_DISK_1 分配默認通道
channel ORA_DISK_1: sid=145 devtype=DISK
channel ORA_DISK_1: starting archive logbackupset
channel ORA_DISK_1: specifying archive log(s)in backup set 歸檔日志列表 13~16
input archive log thread=1 sequence=13recid=1 stamp=814894510
input archive log thread=1sequence=14 recid=2 stamp=814894549
input archive log thread=1sequence=15 recid=3 stamp=814970205
input archive log thread=1sequence=16 recid=4 stamp=814982236
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 備份片名稱arch_bk1_01o9792t11.rmn
piecehandle=/opt/oracle/backup/arch_bk1_01o9792t11.rmn tag=TAG20130509T155717comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用時2秒
channel ORA_DISK_1: deleting archive log(s) 刪除備份過的歸檔日志13~16
archive logfilename=/opt/oracle/archdata/1_13_814444678.dbf recid=1 stamp=814894510
archive logfilename=/opt/oracle/archdata/1_14_814444678.dbf recid=2 stamp=814894549
archive logfilename=/opt/oracle/archdata/1_15_814444678.dbf recid=3 stamp=814970205
archive logfilename=/opt/oracle/archdata/1_16_814444678.dbf recid=4 stamp=814982236
Finished backup at 09-MAY-13
Starting backup at 09-MAY-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafilebackupset 備份數據文件
channel ORA_DISK_1: specifying datafile(s) inbackupset 數據文件列表 1~6
input datafile fno=00005name=/opt/oracle/oradata/base/sinojfs_01.dbf
input datafile fno=00006name=/opt/oracle/oradata/base/sinojfs2_01.dbf
input datafile fno=00001 name=/opt/oracle/oradata/base/system01.dbf
input datafile fno=00002name=/opt/oracle/oradata/base/undotbs01.dbf
input datafile fno=00003name=/opt/oracle/oradata/base/sysaux01.dbf
input datafile fno=00004name=/opt/oracle/oradata/base/users01.dbf
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 備份片名稱full_bk1_02o9793012.rmn
piecehandle=/opt/oracle/backup/full_bk1_02o9793012.rmn tag=TAG20130509T155720comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:03 用時3秒
channel ORA_DISK_1: starting full datafilebackupset
channel ORA_DISK_1: specifying datafile(s) inbackupset
including current control file inbackupset 同時隨便把控制文件和參數文件也備份了
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 備份片名稱full_bk1_03o9793313.rmn
piecehandle=/opt/oracle/backup/full_bk1_03o9793313.rmn tag=TAG20130509T155720comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用時2秒
Finished backup at 09-MAY-13
Starting backup at 09-MAY-13
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archive logbackupset
channel ORA_DISK_1: specifying archive log(s)in backup set 最后收尾在做一次歸檔日志備份
input archive log thread=1sequence=17 recid=5 stamp=814982245 這里面存放著數據庫最后動作的信息
channel ORA_DISK_1: starting piece 1 at09-MAY-13
channel ORA_DISK_1: finished piece 1 at09-MAY-13 備份片名稱arch_bk1_04o9793514.rmn
piecehandle=/opt/oracle/backup/arch_bk1_04o9793514.rmn tag=TAG20130509T155725comment=NONE
channel ORA_DISK_1: backup set complete,elapsed time: 00:00:02 用時2秒
channel ORA_DISK_1: deleting archive log(s) 刪除17號歸檔
archive logfilename=/opt/oracle/archdata/1_17_814444678.dbf recid=5 stamp=814982245
Finished backup at 09-MAY-13
當數據庫結構有變化時,自動觸發控制文件和參數文件備份
Starting Control File and SPFILEAutobackup at 09-MAY-13
piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-00comment=NONE
Finished Control File and SPFILE Autobackupat 09-MAY-13
操作系統層面看看有沒有這些文件,刪除沒刪除“備份過的舊歸檔日志”
[oracle@linuxdbbackup]$ ll 四個備份集全都有
總用量 382264
-rw-r-----1 oracle oinstall 72907264 5月 9 15:57arch_bk1_01o9792t11.rmn
-rw-r-----1 oracle oinstall 3584 5月 9 15:57arch_bk1_04o9793514.rmn
drwxr-xr-x2 oracle oinstall 4096 5月 9 15:57 control
-rw-r-----1 oracle oinstall 242302976 5月 9 15:57 full_bk1_02o9793012.rmn
-rw-r-----1 oracle oinstall 76218368 5月 9 15:57full_bk1_03o9793313.rmn
[oracle@linuxdbcontrol]$ ll 控制文件自動備份也有了
總用量 74464
-rw-r-----1 oracle oinstall 76251136 5月 9 15:57 cf_c-1845289414-20130509-00
[oracle@linuxdbarchdata]$ ll 歸檔日志全刪了
總用量 0
SYS@base>archive log list 新歸檔日志從18號開始,17號之前都已經備份并刪除
Databaselog mode Archive Mode
Automaticarchival Enabled
Archivedestination /opt/oracle/archdata
Oldestonline log sequence 16
Next logsequence to archive 18
Currentlog sequence 18
一切準備就緒后,我們開始搞破壞吧:)全部改名
[oracle@linuxdbbase]$ mv system01.dbf system01.dbf.bak
[oracle@linuxdbbase]$ mv sinojfs_01.dbf sinojfs_01.dbf.bak
[oracle@linuxdbbase]$ mv sinojfs2_01.dbf sinojfs2_01.dbf.bak
[oracle@linuxdbbase]$ mv sysaux01.dbf sysaux01.dbf.bak
SYS@base>shutdown abort 強制關庫
ORACLEinstance shut down.
SYS@base>startup 啟動
ORACLEinstance started.
TotalSystem Global Area 1610612736 bytes
FixedSize 2084296 bytes
VariableSize 385876536 bytes
DatabaseBuffers 1207959552 bytes
RedoBuffers 14692352 bytes
Databasemounted.
ORA-01157:cannot identify/lock data file 1 - see DBWR trace file
ORA-01110:data file 1: '/opt/oracle/oradata/base/system01.dbf
提示找不到1號文件,oracle都是先從1號文件開始訪問
Good 破壞完畢,趕集恢復,我這是用試運行項目數據庫做測試的,而且備份僅此一份,這要是出了岔子就不好玩了,如有雷同純屬巧合。
SYS@base>select status from v$instance; 啟動數據庫到mount狀態
STATUS
------------
MOUNTED
進入RMAN看一看備份集,這些信息都是從control file讀出的。
RMAN>list backupset;
usingtarget database control file instead of recovery catalog
List of Backup Sets
第一個備份片 大小 保存設備 用時 備份日期
BSKey Size Device Type Elapsed Time Completion Time
----------------- ----------- ------------ ---------------
1 69.53M DISK 00:00:02 09-MAY-13
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155717 如果壓縮這會是YES
Piece Name: /opt/oracle/backup/arch_bk1_01o9792t11.rmn 備份片路徑和名稱
List of Archived Logs in backup set 1 包含的文件
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- -------------------
1 13 335077 07-MAY-13 362020 08-MAY-13
1 14 362020 08-MAY-13 362034 08-MAY-13
1 15 362034 08-MAY-13 398238 09-MAY-13
1 16 398238 09-MAY-13 403789 09-MAY-13
第二個備份片信息,由于保存的是數據文件,明顯容量很大
BSKey Type LV Size Device Type Elapsed Time Completion Time
----------- -- ---------- ----------- ------------ ---------------
2 Full 231.07M DISK 00:00:03 09-MAY-13
BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155720
Piece Name:/opt/oracle/backup/full_bk1_02o9793012.rmn 備份片路徑和名稱
List of Datafiles in backup set 2
File LV Type Ckp SCN Ckp Time Name 文件列表
---- -- ---- ---------- --------- ----
1 Full 403795 09-MAY-13/opt/oracle/oradata/base/system01.dbf
2 Full 403795 09-MAY-13/opt/oracle/oradata/base/undotbs01.dbf
3 Full 403795 09-MAY-13/opt/oracle/oradata/base/sysaux01.dbf
4 Full 403795 09-MAY-13/opt/oracle/oradata/base/users01.dbf
5 Full 403795 09-MAY-13/opt/oracle/oradata/base/sinojfs_01.dbf
6 Full 403795 09-MAY-13/opt/oracle/oradata/base/sinojfs2_01.dbf
第三個備份片信息,保存的是控制文件
BSKey Type LV Size Device Type Elapsed Time Completion Time
----------- -- ---------- ----------- ------------ ---------------
3 Full 72.67M DISK 00:00:01 09-MAY-13
BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155720
Piece Name:/opt/oracle/backup/full_bk1_03o9793313.rmn
Control File Included: Ckp SCN: 403796 Ckp time: 09-MAY-13
第四個備份片信息,保存的是收尾的歸檔日志
BSKey Size Device Type Elapsed Time Completion Time
----------------- ----------- ------------ ---------------
4 3.00K DISK 00:00:01 09-MAY-13
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155725
Piece Name:/opt/oracle/backup/arch_bk1_04o9793514.rmn
List of Archived Logs in backup set 4
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- -------------------
1 17 403789 09-MAY-13 403800 09-MAY-13
第五個備份片信息,保存的是控制文件和參數文件自動備份
BSKey Type LV Size Device Type Elapsed Time Completion Time
----------- -- ---------- ----------- ------------ ---------------
5 Full 72.70M DISK 00:00:01 09-MAY-13
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20130509T155728
Piece Name:/opt/oracle/backup/control/cf_c-1845289414-20130509-00
Control File Included: Ckp SCN: 403806 Ckp time: 09-MAY-13
SPFILE Included: Modification time: 08-MAY-13
萬事俱備只欠東風,我們來恢復吧!請關注備份集的大小,這是影響恢復快慢的重要指標
RMAN>restore database;
Startingrestore at 09-MAY-13
allocatedchannel: ORA_DISK_1
channelORA_DISK_1: sid=155 devtype=DISK
channelORA_DISK_1: starting datafile backupset restore 利用備份已經復制回來數據文件了
channelORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /opt/oracle/oradata/base/system01.dbf
restoring datafile 00002 to /opt/oracle/oradata/base/undotbs01.dbf
restoring datafile 00003 to /opt/oracle/oradata/base/sysaux01.dbf
restoring datafile 00004 to /opt/oracle/oradata/base/users01.dbf
restoring datafile 00005 to /opt/oracle/oradata/base/sinojfs_01.dbf
restoring datafile 00006 to /opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: reading from backup piece /opt/oracle/backup/full_bk1_02o9793012.rmn
channelORA_DISK_1: restored backup piece 1
piecehandle=/opt/oracle/backup/full_bk1_02o9793012.rmn tag=TAG20130509T155720
channelORA_DISK_1: restore complete, elapsed time: 00:01:46
Finishedrestore at 09-MAY-13
###########################################################################################
[oracle@linuxdbbase]$ ll
-rw-r----- 1 oracle oinstall 10737426432 5月 9 18:11 sinojfs_01.dbf
-rw-r-----1 oracle oinstall 10737426432 5月 9 17:28 sinojfs_01.dbf.bak
-rw-r----- 1 oracle oinstall 10737426432 5月 9 18:10 sinojfs2_01.dbf
-rw-r-----1 oracle oinstall 10737426432 5月 9 15:57 sinojfs2_01.dbf.bak
-rw-r----- 1 oracle oinstall 125837312 5月 9 18:11 sysaux01.dbf
-rw-r-----1 oracle oinstall 125837312 5月 9 17:27 sysaux01.dbf.bak
-rw-r----- 1 oracle oinstall 314580992 5月 9 18:11 system01.dbf
-rw-r-----1 oracle oinstall 314580992 5月 9 17:28 system01.dbf.bak
操作系統層面都是可以看到的對不對
###########################################################################################
RMAN>recover database; 不僅要restore還需要應用archive和redo log進行介質恢復
Startingrecover at 09-MAY-13
usingchannel ORA_DISK_1
starting media recovery
mediarecovery complete, elapsed time: 00:00:07
Finishedrecover at 09-MAY-13
RMAN>alter database open; 把數據庫恢復到最新狀態才能順利open
databaseopened
SYS@base>select status from v$instance;
STATUS
------------
OPEN
SYS@base>archive log list 因為我們應用到最后一個日志,因此日志會接著前面繼續順延
Databaselog mode Archive Mode
Automaticarchival Enabled
Archivedestination /opt/oracle/archdata
Oldestonline log sequence 17
Next logsequence to archive 19
Currentlog sequence 19
###########################################################################################
表空間級別備份和恢復
使用默認通道默認備份介質保存目錄
RMAN>backup tablespace sinojfs2;
Startingbackup at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting full datafile backupset
channelORA_DISK_1: specifying datafile(s) in backupset
inputdatafile fno=00006 name=/opt/oracle/oradata/base/sinojfs2_01.dbf 這個表空間就包含一個數據文件
channelORA_DISK_1: starting piece 1 at 09-MAY-13
channelORA_DISK_1: finished piece 1 at 09-MAY-13 備份片路徑和名稱
piecehandle=/opt/oracle/backup/DB_06o97i9d_1_1 tag=TAG20130509T183421 comment=NONE
channelORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finishedbackup at 09-MAY-13
看又自動備份控制文件和參數文件了
StartingControl File and SPFILE Autobackup at 09-MAY-13
piecehandle= /opt/oracle/backup/control/cf_c-1845289414-20130509-01 comment=NONE
FinishedControl File and SPFILE Autobackup at 09-MAY-13
操作系統層面查看
[oracle@linuxdbbackup]$ ll
總用量 383112
-rw-r-----1 oracle oinstall 72907264 5月 9 15:57arch_bk1_01o9792t11.rmn
-rw-r-----1 oracle oinstall 3584 5月 9 15:57arch_bk1_04o9793514.rmn
drwxr-xr-x2 oracle oinstall 4096 5月 9 18:34 control
-rw-r----- 1 oracle oinstall 868352 5月 9 18:34 DB_06o97i9d_1_1 表空間的備份集
-rw-r-----1 oracle oinstall 242302976 5月 9 15:57 full_bk1_02o9793012.rmn
-rw-r-----1 oracle oinstall 76218368 5月 9 15:57full_bk1_03o9793313.rmn
[oracle@linuxdbcontrol]$ ll
總用量 148928
-rw-r-----1 oracle oinstall 76251136 5月 9 15:57 cf_c-1845289414-20130509-00
-rw-r----- 1 oracle oinstall 76251136 5月 9 18:34 cf_c-1845289414-20130509-01 這是第二次自動備份集
如果沒有指定保存目錄,使用RMAN參數中默認的備份介質保存目錄/opt/oracle/backup
###########################################################################################
進入sqlplus刪除sinojfs2表空間
SYS@base>drop tablespace sinojfs2 including contents and datafiles; 刪除sinojfs2表空間
Tablespacedropped.
SYS@base>select * from v$tablespace; 已經刪除了吧
TS# NAME INC BIG FLA ENC
---------------------------------------- --- --- --- ---
0 SYSTEM YES NO YES
1 UNDOTBS1 YES NO YES
2 SYSAUX YES NO YES
3 TEMP NO NO YES
4 USERS YES NO YES
5 SINOJFS YES NO YES
6 rowsselected.
利用表空間備份進行恢復,進入RMAN
RMAN>restore tablespace sinojfs2;
Startingrestore at 09-MAY-13
usingtarget database control file instead of recovery catalog
allocatedchannel: ORA_DISK_1
channelORA_DISK_1: sid=155 devtype=DISK
RMAN-00571:===========================================================
RMAN-00569:=============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571:===========================================================
RMAN-03002: failure of restore command at 05/09/2013 19:14:39
RMAN-20202: tablespace not found in the recovery catalog
RMAN-06019: could not translate tablespace name "SINOJFS2"
大家知道為什么找不到sinojfs2表空間嗎?數據庫結構是不是存放在控制文件中的,剛才我們是怎么破壞的表空間的?使用了drop tablespace sinojfs2 includingcontents and datafiles; 這條語句,它做的動作是刪除表空間同時把控制文件中的表空間信息也一并刪除,因此后來在使用控制文件恢復表空間時報找不到信息。怎么辦,這個不行那就換一種方法。
首先重新恢復數據庫到原來狀態,過程省略,剛才已經講過了,再來一遍估計就要拋磚啦!
重新創建一個新sinojfs2表空間
SQL> createtablespace sinojfs2 datafile '/opt/oracle/oradata/base/sinojfs2_01.dbf' size10G autoextend off;
Tablespace created
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
-------------------------------------------------------------------------------------------------
6/opt/oracle/oradata/base/sinojfs2_01.dbf ONLINE
在備份一次表空間
RMAN>backup tablespace sinojfs2;
Startingbackup at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting full datafile backupset
channelORA_DISK_1: specifying datafile(s) in backupset
inputdatafile fno=00006 name=/opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: starting piece 1 at 09-MAY-13
channelORA_DISK_1: finished piece 1 at 09-MAY-13
piecehandle=/opt/oracle/backup/DB_0fo97o6p_1_1 tag=TAG20130509T201521comment=NONE
channelORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finishedbackup at 09-MAY-13
StartingControl File and SPFILE Autobackup at 09-MAY-13
piecehandle=/opt/oracle/backup/control/cf_c-1845289414-20130509-03 comment=NONE
FinishedControl File and SPFILE Autobackup at 09-MAY-13
我們這回直接刪除表空間對應的數據文件
[oracle@linuxdbbase]$ rm -rf sinojfs2_01.dbf
SYS@base>alter tablespace sinojfs2 offline; 讓表空間offline
altertablespace sinojfs2 offline
*
ERROR atline 1:
ORA-01116:error in opening database file 6
ORA-01110:data file 6: '/opt/oracle/oradata/base/sinojfs2_01.dbf' 找不到對應數據文件
ORA-27041:unable to open file
Linux-x86_64Error: 2: No such file or directory
Additionalinformation: 3
SYS@base>alter database datafile 6 offline; 先脫機數據文件
Databasealtered.
此時可以在數據庫open狀態下恢復sinojfs2表空間
RMAN>restore tablespace sinojfs2; 復制文件
Startingrestore at 09-MAY-13
usingchannel ORA_DISK_1
channelORA_DISK_1: starting datafile backupset restore
channelORA_DISK_1: specifying datafile(s) to restore from backup set
restoringdatafile 00006 to /opt/oracle/oradata/base/sinojfs2_01.dbf
channelORA_DISK_1: reading from backup piece /opt/oracle/backup/DB_0fo97o6p_1_1
channelORA_DISK_1: restored backup piece 1
piecehandle=/opt/oracle/backup/DB_0fo97o6p_1_1 tag=TAG20130509T201521
channelORA_DISK_1: restore complete, elapsed time: 00:00:55
Finishedrestore at 09-MAY-13
RMAN>recover tablespace sinojfs2; 介質恢復
Startingrecover at 09-MAY-13
usingchannel ORA_DISK_1
startingmedia recovery
mediarecovery complete, elapsed time: 00:00:01
Finishedrecover at 09-MAY-13
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
-------------------------------------------------------------------------------------------------
6/opt/oracle/oradata/base/sinojfs2_01.dbf OFFLINE
SYS@base> alter databasedatafile 6 online; 把offline啟動成online
Database altered.
SQL> selectfile#,name,status from v$datafile where file#=6;
FILE# NAME STATUS
-------------------------------------------------------------------------------------------------
6/opt/oracle/oradata/base/sinojfs2_01.dbf ONLINE
到此表空間sinojfs2完整恢復回來,真是驚心動魄啊!數據文件恢復也是如此,命令如下這里就不演示了
restore datafile 6;
recover datafile 6;
來一點小提示,我們備份了這么多備份集,如何檢查rman元數據和操作系統上的備份集對應關系呢
RMAN> crosscheck backupset; 交叉校驗,看看對象數是否一致,我們這里一共9個備份集,再看看操作系統上即可
using channel ORA_DISK_1
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/arch_bk1_09o97nhr19.rmn recid=7stamp=814997052
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/full_bk1_0ao97nhu110.rmn recid=8stamp=814997055
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/full_bk1_0bo97ni5111.rmn recid=9stamp=814997061
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/arch_bk1_0co97ni7112.rmn recid=10stamp=814997064
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-01recid=11 stamp=814997066
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-02recid=12 stamp=814997575
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/DB_0fo97o6p_1_1 recid=13stamp=814997721
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-03recid=14 stamp=814997723
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/opt/oracle/backup/control/cf_c-1845289414-20130509-04recid=15 stamp=814998546
Crosschecked 9 objects
delete backup 刪除所有備份
delete expired backup 刪除所有過期備份
小結:我們成功進行了數據庫級別、表空間、數據文件級別的備份與恢復,當你數據庫處于穩定狀態時一定要記住做一個全備“以備后患”。Good 今天就講到這里。
RMAN backupset catalog 全庫備份 差異增量備份 累積增量備份
看完上述內容,你們對怎樣深入解析RMAN 備份與恢復有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。