转:RMAN 备份与恢复 实例
轉載自:http://blog.csdn.net/tianlesoftware/article/details/4699320
1. 檢查數(shù)據(jù)庫模式:
?? sqlplus /nolog?
?? conn /as sysdba
?? archive log list (查看數(shù)據(jù)庫是否處于歸檔模式中)
?? 若為非歸檔,則修改數(shù)據(jù)庫歸檔模式。
?? startup mount
?? alter database archivelog
?? alter database open
2.連接到target數(shù)據(jù)庫
命令: connect target? / (connect target?system/oracle@ora10g,如果數(shù)據(jù)庫沒有起來,也可要直接在rman命令下用startup進行啟動數(shù)據(jù)庫)?
可以連接到target database.(rman 一邊連接到target數(shù)據(jù)庫,另外一邊連接到control file(nocatalog mode),control file 中存儲rman 的備份信息)
3.用list backupset 命令查看有沒有備份的東西
4. 常用備份命令:
?備份全庫:
RMAN> backup database plus archivelog delete input;???? (備份全庫及控制文件、服務器參數(shù)文件與所有歸檔的重做日志,并刪除舊的歸檔日志)
備份表空間:
RMAN> backup tablespace system plus archivelog delete input;???? (備份指定表空間及歸檔的重做日志,并刪除舊的歸檔日志)_
備份歸檔日志:
RMAN> backup archivelog all delete input;
======================對整個數(shù)據(jù)庫進行備份==================
1.對整個數(shù)據(jù)庫進行全備份(full backup)。
??? 只要輸入命令: backup database;
2.list backupset 查看備份的具體信息
List of Backup Sets
===================
BS Key? Type LV Size?????? Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1?????? Full??? 6.80M????? DISK??????? 00:00:02???? 06-DEC-08??????
??????? BP Key: 1?? Status: AVAILABLE? Compressed: NO? Tag: TAG20081206T201041
??????? Piece Name: /home/oracle/flash_recovery_area/ORA10G/backupset/2008_12_06/o1_mf_ncsnf_TAG20081206T201041_4mntz78s_.bkp
? Control File Included: Ckp SCN: 782019?????? Ckp time: 06-DEC-08
? SPFILE Included: Modification time: 06-DEC-08
BS(backupset), piece是一個文件,一個BS包含多個piece.
3.rman中缺省的參數(shù),可以通過 show all ;
來進行查看(RMAN configuration parameters),我們在使用backup database命令中,可以把這些default value 用固定的值來進行替代.
4.我們可以把備份的文件才備份的目錄中拷貝到磁帶上,然后刪除備份目錄下面的備份文件,如果下次需要恢復的話,只要把文件重新拷回到用來的備份目錄就可以了
5.查看control file 文件中的備份信息(因為我們做的備份是在nocatalog模式下),control file 在/u01/oracle/oradata/ora10g目錄下,由于control file 是個二進制文件,要查看control file 文件中的內(nèi)容,用strings control03.ctl,發(fā)現(xiàn)control03.ctl中有rman備份的信息了
?
====================0級增量備份===============
概念:全備份和0級增量備份。全備份和0級增量備份幾乎是一樣的。唯一的區(qū)別,0級增量備份能作為增量備份的基礎,而全備份不能作為增量備份的基礎。其它方面完全一致
1.backup incremental level=0(leve 0) database;(增量為0的備份)
2.backup incremental level 1(level=1) database;(增量為1的備份)
?
在上面的備份中,我們備份了datafile,controlfile和parameter file.沒有備份的文件有歸檔日志,重做日志和口令文件沒有備份.口令文件不需要備份,我們用orapw來創(chuàng)建一個
新的口令文件.rman 在nocatalog模式下,不能夠對redo log file 進行備份
===================備份archivelog 在nocatalog模式下=================
命令:backup database plus archivelog delete input(delete input的意思在備份完成后,刪除 archivelog文件,這個選項可要可不要,這個命令也可以用 backup incremental level=0(1,2...)來進行備份)
=======================備份表空間====================
backup tablespace tablespacename
如果我們不知道tablespace的名字,在rman中,可要通過report schema命令,來查看表空間的名字
MAN> report schema;
Report of database schema
List of Permanent Datafiles
===========================
File Size(MB) Tablespace?????????? RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1??? 480????? SYSTEM?????????????? ***???? /home/oracle/oradata/ora10g/system01.dbf
2??? 25?????? UNDOTBS1???????????? ***???? /home/oracle/oradata/ora10g/undotbs01.dbf
3??? 250????? SYSAUX?????????????? ***???? /home/oracle/oradata/ora10g/sysaux01.dbf
4??? 5??????? USERS??????????????? ***???? /home/oracle/oradata/ora10g/users01.dbf
5??? 200????? PERFSTAT???????????? ***???? /home/oracle/oradata/ora10g/perfstat.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace?????????? Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1??? 20?????? TEMP???????????????? 32767?????? /home/oracle/oradata/ora10g/temp01.dbf
?
========================備份控制文件====================
backup current controlfile
backup database include current controlfile
?
========================備份鏡像========================
在rman的備份中有兩種方式:備份集(backupset)和備份鏡像(image copies).鏡像備份主要是文件的拷貝:copy datafile ... to ...
我們在rman>report schema;
Report of database schema
List of Permanent Datafiles
===========================
File Size(MB) Tablespace?????????? RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1??? 480????? SYSTEM?????????????? ***???? /home/oracle/oradata/ora10g/system01.dbf
2??? 25?????? UNDOTBS1???????????? ***???? /home/oracle/oradata/ora10g/undotbs01.dbf
3??? 250????? SYSAUX?????????????? ***???? /home/oracle/oradata/ora10g/sysaux01.dbf
4??? 5??????? USERS??????????????? ***???? /home/oracle/oradata/ora10g/users01.dbf
5??? 200????? PERFSTAT???????????? ***???? /home/oracle/oradata/ora10g/perfstat.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace?????????? Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1??? 20?????? TEMP???????????????? 32767?????? /home/oracle/oradata/ora10g/temp01.dbf
?
rman>copy datafile 5 to '/u01/rmanbak/tbso1bak.dbf';(copy 5 對應的schme:perfstat.dbf)
?
它會把tbs作為一個拷貝。我們用list backupset來看,不能夠查看我們剛備份的 tbs01bak.dbf',因為它不是backupset. 我們用list copy 就能夠查看我們剛才剛剛備份的文件
?
=======================單命令與批命令=================
單命令: backup database;
批命令:
rman> run{
?2> allocate channel cha1 type disk;
?3> backup
?4> format '/u01/rmanbak/full_%t'
?5> tag full-backup //標簽可以順便起,沒關系
?6> database;
?7> release channel cha1;
?8>}
這個run中有3條命令,分別用分號來進行分割.
format:
%c:備份片的拷貝數(shù)(從1開始編號);
%d:數(shù)據(jù)庫名稱;
%D:位于該月中的天數(shù)(DD);
%M:位于該年中的月份(MM);
%F:一個基于DBID唯一的名稱,這個格式的形式為c-xxx-YYYYMMDD-QQ,其中xxx位該數(shù)據(jù)庫的DBID,YYYYMMDD為日期,QQ是一個1-256的序列;
%n:數(shù)據(jù)庫名稱,并且會在右側用x字符進行填充,使其保持長度為8;
%u:是一個由備份集編號和建立時間壓縮后組成的8字符名稱。利用%u可以為每個備份集產(chǎn)生一個唯一的名稱;
%p:表示備份集中的備份片的編號,從1開始編號;
%U:是%u_%p_%c的簡寫形式,利用它可以為每一個備份片段(既磁盤文件)生成一個唯一的名稱,這是最常用的命名方式;
%t:備份集時間戳;
%T:年月日格式(YYYYMMDD);
channel的概念:一個channel是rman于目標數(shù)據(jù)庫之間的一個連接,"allocate channel"命令在目標數(shù)據(jù)庫啟動一個服務器進程,同時必須定義服務器進程執(zhí)行備份和恢復操作使
用的I/O類型
通道控制命令可以用來:
????? 控制rman使用的OS資源
????? 影響并行度
????? 指定I/O帶寬的限制值(設置 limit read rate 參數(shù))
????? 指定備份片大小的限制(設置 limit kbytes)
????? 指定當前打開文件的限制值(設置 limit maxopenfiles)
?
=================================RMAN一周典型備份方案============================
1.星期天晚上????? -level 0 backup performed(全備份)
2.星期一晚上????? -level 2 backup performed
3.星期二晚上????? -level 2 backup performed
4.星期三晚上????? -level 1 backup performed
5.星期四晚上????? -level 2 backup performed
6.星期五晚上????? -level 2 backup performed
7.星期六晚上????? -level 2 backup performed
?
如果星期二需要恢復的話,只需要1+2,
如果星期四需要恢復的話,只需要1+4,
如果星期五需要恢復的話,只需要1+4+5,
如果星期六需要恢復的話,只需要1+4+5+6.
?
自動備份:備份腳本+crontab
?bakl0
?bakl1
?bakl2
執(zhí)行腳本:
rman target / msglog=bakl0.log cmdfile=bakl0 (/表示需要連接的目標數(shù)據(jù)庫,msglog表示日志文件,cmdfile表示的是腳本文件)
rman target / msglog=bakl1.log cmdfile=bakl1
rman target / msglog=bakl2.log cmdfile=bakl2
實例:rman target?system/oracle@ora10g(/) msglog=/u01/rmanbak/bakl1.log cmdfile=/u01/rmanbak/bakl0
完整的命令:/u01/oracle/product/10.2.0/bin/rman target?system/oracle@ora10g(/) msglog=/u01/rmanbak/bakl1.log cmdfile=/u01/rmanbak/bakl0
?
把備份腳本放到/u01/rmanbak/script目錄下面,vi bakl0,bakl0的內(nèi)容為:
run{
??? allocate channel cha1 type disk;
??? backup
??? incremental level? 0
??? format '/u01/rmanbak/inc0_%u_%T'(u表示唯一的ID,大T是日期,小t是時間)
??? tag monday_inc0 //標簽可以順便起,沒關系
??? database;
??? release channel cha1;
??? }
,類似就可以寫出bakl1,bakl2相應的腳本.
?
自動備份
crontab
crontab -e -u oracle(改命令的意思是編輯oracle用戶的定時執(zhí)行(-e,edit -u oracle,oracle用戶))
分? 時? 日 月 星期(0代表星期天)
45 23? *? *??? 0??? rman target / msglog=bakl0.log cmdfile=bakl0(星期天的23:45會以oracle用戶的身份來執(zhí)行命令)
45 23? *? *??? 1??? rman target / msglog=bakl2.log cmdfile=bakl2
45 23? *? *??? 2??? rman target / msglog=bakl2.log cmdfile=bakl2
45 23? *? *??? 3??? rman target / msglog=bakl1.log cmdfile=bakl1
45 23? *? *??? 4??? rman target / msglog=bakl2.log cmdfile=bakl2
45 23? *? *??? 5??? rman target / msglog=bakl2.log cmdfile=bakl2
45 23? *? *??? 6??? rman target / msglog=bakl2.log cmdfile=bakl2
?
然后啟動crontab ,啟動crontab的命令:
root> service crond restart
??
=======================RMAN恢復================
在非catalog模式下,備份的信息存儲在controlfile文件中,如果controlfile文件發(fā)生毀壞,那么就不能能夠進行恢復,
使用在備份的時候需要把controlfile也進行自動備份?
?
RMAN>show all;
using target database control file instead of recovery catalog
RMAN configuration parameters are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/home/oracle/product/10.20/dbs/snapcf_ora10g.f'; # default
其中CONFIGURE CONTROLFILE AUTOBACKUP OFF; 沒有對controlfile進行 autobackup,使用我們需要運行下面命令來對controlfile進行自動備份
RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
RMAN> show all;
手動備份控制文件:
backup current controlfile
Dbid表示database的一個ID,將來用于恢復spfile和controlfile時候要用到.
RMAN> connect target /
connected to target database: ORA10G (DBID=3988862108)
這個Dbid=3988862108
?
RMAN> list backup;查看以前備份的信息
RMAN>delete backupset 24;//24代表backupset 的編號
RMAN>backup format '/u01/rmanbak/full_%T_%U.bak' database plus archivelog;(進行一次全備份)
驗證備份:
RMAN> validate backupset 3;? //3代表backupset的編號
?
口令文件丟失(不屬于rman備份的范疇),我們只需要用一個命令來重建這個文件就可以了:
orapw file=orapwsid password=pass entries=5;? //口令文件的路徑:/u01/oracle/product/10.20/db_1/dbs目錄下
oracle> cd /u01/oracle/product/10.20/db_1/dbs
oracle> rm orapwora10g;(文件刪除,模擬丟失)
oracle> orapwd file=orapwora10g password=oracle entries=5;(重新建立一個文件),entries的意思(DBA的用戶最多有5個)
?
?
SPFILE丟失:
startup nomount;
set dbid 3988862108;
restore spfile from autobackup;
shutdown immediate;
set dbid 3988862108;
startup;
?
模擬操作:
oracle> mv spfileora10g.ora spora10g.ora
oracle>rman target /;
rman> shutdown immediate;
rman> startup nomount;
startup failed: ORA-01078: failure in processing system parameters
LRM-00109: could not open parameter file '/home/oracle/product/10.20/dbs/initora10g.ora'
rman>set dbid 3988862108;
rman>restore spfile from autobackup;
執(zhí)行該命令,如果沒有找到的話,那可能是文件的路徑發(fā)生錯誤.可以通過直接賦予它的文件
rman>restore spfile from '/u01/oracle/flash_recovery_area/ORA10G/autobackup/2008_12_09/o1_mf_s_673025706_4mw7xc79_.bkp
在dbs/目錄下產(chǎn)生spfileora10g.ora文件。證明spfile 已經(jīng)恢復好
rman> shutdown immediate;
rman> startup ;(如果該命令不能夠啟動數(shù)據(jù)庫,那么需要set dbid 3988862108)
controlfile 丟失:
startup nomount;
restore controlfile from autobackup;
alter database mount;
recover database;
alter database open resetlogs;
注意:在做了alter database open resetlogs;會把online redelog file清空,數(shù)據(jù)文件丟失.所以這個時候要做一個全備份。
oracle>rm *.ctl
oracle>rman target / ;//不能夠連接到rman ,因為controlfile丟失
oracle>sqlplus /nolog;
SQL>shutdown immediate; //因為controlfile丟失,不能夠正常shutdown
SQL>shutdown abort;
oracle>rman target /;
rman>startup nomount;
rman>restore controlfile from autobackup;
rman>alter database mount;
rman>alter database open resetlogs;
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 12/09/2008 16:21:13
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/home/oracle/oradata/ora10g/system01.dbf
//出錯, redo log的scn記錄在controlfile里面的,因為我們有新的controlfile,所以需要resetlogs;
?
/*
resetlogs命令表示一個數(shù)據(jù)庫邏輯生存期的結束和另一個數(shù)據(jù)庫邏輯生存期的開始,每次使用resetlogs命令的時候,SCN不會被重置,不過oracle會重置日志序列號,而且會重置
聯(lián)機重做日志內(nèi)容.
這樣做是為了防止不完全恢復后日志序列會發(fā)生沖突(因為現(xiàn)有日志和數(shù)據(jù)文件間有了時間差)。
*/
rman>recover database;
rman>alter database open resetlogs;
Redolog file丟失:(下面的這些語句一定要在sqlplus中執(zhí)行,不是在rman中執(zhí)行)
?(sqlplus/nolog)
1.shutdown immediate;
2.startup mount;
3.recover database until cancel;(media recovery)
4.alter database resetlogs;
?
數(shù)據(jù)文件丟失(在rman中執(zhí)行sql語句,在sql后面用雙引號括起來):
1. sql "alter database datafile 3 offline";
2. restore datafile 3
3. recover datafile 3
4. sql "alter database datafile 3 online";
?
表空間丟失:
1. sql "alter tablespace users offline";//如果文件不存在,則用 sql "alter tablespace users offline immeidate";
2. restore tablespace users;
3. recover tablespace users; //與online redolog file 信息一致
4. sql "alter tablespace users online";
?
非catalog方式完全恢復
數(shù)據(jù)庫出現(xiàn)問題:
1.startup nomount;
2.restore controlfile from autobackup;
3.alter database mount;
4.restore database;
5.recover database;
6.alter database open resetlogs;
?
模擬操作:
oracle ora10g> rm *;
oracle ora10g> ls;
oracle ora10g>? //數(shù)據(jù)文件,控制文件全部刪除
oracle ora10g> rman target /; //因為controlfile 丟失,不能夠連接到rman
oracle ora10g> sqlplus /nolog;
oracle ora10g> connect / as sysdba;
oracle ora10g> shutdown abort;
oracle ora10g> rman target /
?
rman> startup nomount;
rman> restore controlfile from autabackup;
rman> alter database mount;
rman> restore database;
rman> recover database; //online redolog 不存在
SQL>recover database until cancel; //當redo log丟失,數(shù)據(jù)庫在缺省的方式下,是不容許進行recover操作的,那么如何在這種情況下操作呢
SQL>create pfile from spfile;
vi /u01/product/10.20/dbs/initora10g.ora,在這個文件的最后一行添加
*.allow_resetlogs_corruption='TRUE'; //容許resetlog corruption
?
SQL>shutdown immediate;
SQL>startup pfile='/u01/product/10.20/dbs/initora10g.ora' mount;
SQL>alter database open resetlogs;
?
?基于時間點的恢復:
?run{
? set until time "to_date(07/01/02 15:00:00','mm/dd/yy hh24:mi:ss')";
? restore database;
? recover database;
? alter database open resetlogs;
}
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS';
1.startup mount;
2.restore database until time "to_date('2009-7-19 13:19:00','YYYY-MM-DD HH24:MI:SS')";
3.recover database until time "to_date('2009-7-19 13:19:00','YYYY-MM-DD HH24:MI:SS')";
4.alter database open resetlogs;
?
如果有open resetlogs,都是不完整恢復.
?
基于 SCN的恢復:
1.startup mount;
2.restore database until scn 10000;
3.recover database until scn 10000;
4.alter database open resetlogs;
基于日志序列的恢復:
1.startup mount;
2.restore database until SEQUENCE 100 thread 1; //100是日志序列
3.recover database until SEQUENCE 100 thread 1;
4.alter database open resetlogs;
日志序列查看命令: SQL>select * from v$log;其中有一個sequence字段.resetlogs就會把sequence 置為1
=================================RMAN catalog模式下的備份與恢復=====================
1.創(chuàng)建Catalog所需要的表空間
SQL>create tablespace rman_ts size datafile '/u01/oracle/oradata/ora10g/rmants.dbf' 20M;
?
2.創(chuàng)建RMAN用戶并授權
SQL>create user rman identified by rman default tablespace rman_ts quota unlimited on rman_ts;
SQL>grant recovery_catalog_owner to rman;(grant connect to rman)
?
查看角色所擁有的權限: select * from dba_sys_privs where grantee='RECOVERY_CATALOG_OWNER';
(RECOVER_CATALOG_OWNER,CONNECT,RESOURCE)
3.創(chuàng)建恢復目錄
oracle>rman catalog rman/rman
RMAN>create catalog tablespace rman_ts;
RMAN>register database;(database是target database)
database registered in recovery catalog
starting full resync of recovery catalog
full resync complete
RMAN> connect target /;
以后要使用備份和恢復,需要連接到兩個數(shù)據(jù)庫中,命令:
oracle>rman target / catalog rman/rman (第一斜杠表示target數(shù)據(jù)庫,catalog表示catalog目錄 rman/rman表示catalog用戶名和密碼)
命令執(zhí)行后顯示:
Recovery Manager: Release 10.2.0.1.0 - Production on Wed Dec 10 15:00:42 2008
Copyright (c) 1982, 2005, Oracle.? All rights reserved.
connected to target database: ORA10G (DBID=3988862108)
connected to recovery catalog database
命令解釋:
Report schema????????????? Report shema是指在數(shù)據(jù)庫中需找schema
List backup?????????????????? 從control讀取信息
Crosscheck backup?????? 看一下backup的文件,檢查controlfile中的目錄或文件是否真正在磁盤上
Delete backupset 24??? 24代表backupset 的編號, 既delete目錄,也delete你的文件
?
注意:在做了alter database open resetlogs;會把online redelog file清空,數(shù)據(jù)文件丟失.所以這個時候要做一個全備份。
resetlogs命令表示一個數(shù)據(jù)庫邏輯生存期的結束和另一個數(shù)據(jù)庫邏輯生存期的開始,每次使用resetlogs命令的時候,SCN不會被重置,不過oracle會重置日志序列號,而且會重置
聯(lián)機重做日志內(nèi)容.這樣做是為了防止不完全恢復后日志序列會發(fā)生沖突(因為現(xiàn)有日志和數(shù)據(jù)文件間有了時間差)。
?
Rman 歸檔文件丟失導致不能備份的,在備份前先執(zhí)行以下兩條命令
crosscheck archivelog all;
delete expired archivelog all;
總結
以上是生活随笔為你收集整理的转:RMAN 备份与恢复 实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webstorm使用技巧
- 下一篇: Servlet页面跳转实现方法的区别