情景描述:
有朋友问,MSSQLSERVER将数据库备份还原到其它机器很方便,基本就是傻瓜式操作,oracle有控制文件、参数文件一堆东西,
觉得还原很复杂;其实不然,我抽出了点时间,简单演示下案例,朋友们,参考下!为了简单快速,我采用RMAN备份和还原
--说明:
(1).RMAN备份到异机恢复的时候,db_name需要相同。
如果说要想改成其他的实例名,可以在恢复成功后,用nid 命令修改。 实例名的信息会
记录到控制文件里,所以如果在恢复的时候,如果实例名不一致,恢复的时候会报错。
(2).恢复的路径和源库不一致时,就需要在restore命令中使用set 命令指定新位置,
并且使用switch datafile all将变更信息更新的到控制文件中。
测试环境:
源数据库服务器A,安装在E,备份目录已在E盘;
源数据库服务器B,安装在F盘.
------------------------------------
一、源数据库准备工作
------------------------------------
--1. 查询DBID
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
ORCL 1320546556
--2. 备份源数据DB
run {
configure retention policy to recovery window of 14 days;
configure controlfile autobackup on; --自动开启控制文件备份
configure controlfile autobackup format for device type disk to 'E:\backup\controlfile\bak_%F';
allocate channel c1 device type disk format 'E:\backup\data\bak_%u';
allocate channel c2 device type disk format 'E:\backup\data\bak_%u';
sql 'alter system archive log current';
backup incremental level=0 database skip inaccessible
plus archivelog filesperset 20
delete all input;
release channel c1;
release channel c2;
}
allocate channel for maintenance device type disk;
crosscheck backupset;
delete noprompt obsolete;
--或
run {
configure retention policy to recovery window of 14 days;
allocate channel c1 device type disk format 'E:\backup\data\bak_%u';
allocate channel c2 device type disk format 'E:\backup\data\bak_%u';
sql 'alter system archive log current';
backup incremental level=0 database skip inaccessible
plus archivelog filesperset 20
delete all input;
--手动直接指明备份文件名和路径
backup current controlfile tag='bak_ctrollfile' format='E:\backup\controlfile\bak_ctl_file_%U_%T';
backup spfile tag='bak_spfile' format='E:\backup\controlfile\bak_spfile_%U_%T';
release channel c1;
release channel c2;
}
allocate channel for maintenance device type disk;
crosscheck backupset;
delete noprompt obsolete;
--3.手动备份spfile
create pfile='E:\backup\inittest.ora' from spfile;
------------------------------------
二、目标库准备工作:
------------------------------------
---步骤1. 创建口令文件
--如果有就不需要新建.
orapwd file=F:\app\Administrator\product\11.2.0\dbhome_1\database\PWDorcl.ora password=password
--步骤2.恢复参数文件
--方法1.源数据库spfile并拷贝到B数据库服务器
\\192.168.2.25\e$\backup\inittest.ORA
复制到
E:\bk\inittest.ORA
--步骤3.编辑参数文件中的文件路径
如路径和原来一样,则不需修改
eg:
- *_DUMP_DEST
- LOG_ARCHIVE_DEST*
- CONTROL_FILES
---实例:
orcl.__db_cache_size=939524096
orcl.__java_pool_size=16777216
orcl.__large_pool_size=16777216
orcl.__oracle_base='F:\app\Administrator'#修改路径
orcl.__pga_aggregate_target=855638016
orcl.__sga_target=2550136832
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=1526726656
orcl.__streams_pool_size=16777216
*.audit_file_dest='F:\app\Administrator\admin\orcl\adump' #修改路径
*.audit_trail='db'
*.compatible='11.2.0.0.0'
#修改路径
*.control_files='F:\app\Administrator\oradata\orcl\control01.ctl','F:\app\Administrator\oradata\orcl\control02.ctl','F:\app\Administrator\oradata\orcl\control03.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='F:\arch' #修改路径
*.db_recovery_file_dest_size=6442450944
*.diagnostic_dest='F:\app\Administrator' #修改路径
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.log_archive_dest_1='location=f:\arch' #修改路径
*.nls_date_format='yyyy-mm-dd hh:mi:ss'
*.open_cursors=300
*.optimizer_capture_sql_plan_baselines=TRUE
*.pga_aggregate_target=845152256
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=2536505344
*.skip_unusable_indexes=TRUE
*.undo_tablespace='UNDOTBS1'
--步骤4.重启实例,使用已编辑好的参数文件
将用pfile将B数据库服务器启动到nomout 状态
rman target /
startup nomount pfile='e:\bk\inittest.ora'
--步骤5.恢复控制文件,装载数据库
rman target /
set dbid 1320546556
restore controlfile from 'e:\bk\controlfile\BAK_CTL_FILE_6LO6SON4_1_1_20130412';
启动 restore 于 2013-04-11 13:58:58
使用目标数据库控制文件替代恢复目录
分配的通道: ORA_DISK_1
通道 ORA_DISK_1: SID=129 设备类型=DISK
通道 ORA_DISK_1: 正在还原控制文件
通道 ORA_DISK_1: 还原完成, 用时: 00:00:01
输出文件名=F:\APP\ADMINISTRATOR\ORADATA\ORCL\CONTROL01.CTL
输出文件名=F:\APP\ADMINISTRATOR\ORADATA\ORCL\CONTROL02.CTL
输出文件名=F:\APP\ADMINISTRATOR\ORADATA\ORCL\CONTROL03.CTL
完成 restore 于 2013-04-11 13:59:00
--步骤6.将DB启动到mout状态
alter database mount;
数据库已装载
释放的通道: ORA_DISK_1
--加载备份目录
CATALOG START WITH 'E:\bk';
搜索与样式 E:\bk 匹配的所有文件
数据库未知文件的列表
====================================
文件名: E:\bk\controlfile\BAK_C-1320546556-20130411-03
文件名: E:\bk\controlfile\BAK_CTL_FILE_65O6QILO_1_1_20130411
文件名: E:\bk\controlfile\BAK_SPFILE_66O6QILQ_1_1_20130411
文件名: E:\bk\data\BAK_60O6QIGV
文件名: E:\bk\data\BAK_61O6QIGV
文件名: E:\bk\data\BAK_62O6QIH1
文件名: E:\bk\data\BAK_63O6QIH1
文件名: E:\bk\data\BAK_64O6QILM
是否确实要将上述文件列入目录 (输入 YES 或 NO)? yes
正在编制文件目录...
目录编制完毕
已列入目录的文件的列表
======================
文件名: E:\bk\controlfile\BAK_C-1320546556-20130411-03
文件名: E:\bk\controlfile\BAK_CTL_FILE_65O6QILO_1_1_20130411
文件名: E:\bk\controlfile\BAK_SPFILE_66O6QILQ_1_1_20130411
文件名: E:\bk\data\BAK_60O6QIGV
文件名: E:\bk\data\BAK_61O6QIGV
文件名: E:\bk\data\BAK_62O6QIH1
文件名: E:\bk\data\BAK_63O6QIH1
文件名: E:\bk\data\BAK_64O6QILM
--步骤7.restore 数据库
7.1 恢复目录不同的情况:
到Target 库查询一下:
SQL> select trim(file_id),trim(file_name) from dba_data_files ORDER BY FILE_ID ASC;
1 E:\APP\ADMINISTRATOR\ORADATA\ORCL\SYSTEM01.DBF
2 E:\APP\ADMINISTRATOR\ORADATA\ORCL\SYSAUX01.DBF
3 E:\APP\ADMINISTRATOR\ORADATA\ORCL\UNDOTBS01.DBF
4 E:\APP\ADMINISTRATOR\ORADATA\ORCL\USERS01.DBF
6 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY02.DBF
7 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY03.DBF
8 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY04.DBF
9 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY05.DBF
11 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY06.DBF
12 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY07.DBF
13 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY08.DBF
14 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY09.DBF
15 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY10.DBF
16 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY01.DBF
17 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY11.DBF
18 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY12.DBF
19 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY13.DBF
20 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY14.DBF
21 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY15.DBF
22 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY16.DBF
23 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY17.DBF
24 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY18.DBF
25 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY19.DBF
26 E:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY20.DBF
SQL>select trim(file_id),trim(file_name) from dba_temp_files;
1 E:\APP\ADMINISTRATOR\ORADATA\ORCL\TEMP01.DBF
select 'SET NEWNAME FOR DATAFILE'|| ' '||file_id||' '||'TO'|| ' '||''''||trim(file_name)||'''' ||';'
from dba_data_files ORDER BY FILE_ID ASC;
---如下实例:
RUN
{
# rename the datafiles
SET NEWNAME FOR DATAFILE 1 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\SYSTEM01.DBF';
SET NEWNAME FOR DATAFILE 2 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\SYSAUX01.DBF';
SET NEWNAME FOR DATAFILE 3 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\UNDOTBS01.DBF';
SET NEWNAME FOR DATAFILE 4 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\USERS01.DBF';
SET NEWNAME FOR DATAFILE 6 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY02.DBF';
SET NEWNAME FOR DATAFILE 7 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY03.DBF';
SET NEWNAME FOR DATAFILE 8 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY04.DBF';
SET NEWNAME FOR DATAFILE 9 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY05.DBF';
SET NEWNAME FOR DATAFILE 11 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY06.DBF';
SET NEWNAME FOR DATAFILE 12 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY07.DBF';
SET NEWNAME FOR DATAFILE 13 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY08.DBF';
SET NEWNAME FOR DATAFILE 14 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY09.DBF';
SET NEWNAME FOR DATAFILE 15 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY10.DBF';
SET NEWNAME FOR DATAFILE 16 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY01.DBF';
SET NEWNAME FOR DATAFILE 17 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY11.DBF';
SET NEWNAME FOR DATAFILE 18 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY12.DBF';
SET NEWNAME FOR DATAFILE 19 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY13.DBF';
SET NEWNAME FOR DATAFILE 20 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY14.DBF';
SET NEWNAME FOR DATAFILE 21 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY15.DBF';
SET NEWNAME FOR DATAFILE 22 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY16.DBF';
SET NEWNAME FOR DATAFILE 23 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY17.DBF';
SET NEWNAME FOR DATAFILE 24 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY18.DBF';
SET NEWNAME FOR DATAFILE 25 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY19.DBF';
SET NEWNAME FOR DATAFILE 26 TO 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\VPAY20.DBF';
SQL "ALTER DATABASE RENAME FILE ''E:\app\Administrator\oradata\orcl\REDO01.LOG''
TO ''F:\app\Administrator\oradata\orcl\REDO01.LOG'' ";
SQL "ALTER DATABASE RENAME FILE ''E:\app\Administrator\oradata\orcl\REDO02.LOG''
TO ''F:\app\Administrator\oradata\orcl\REDO02.LOG'' ";
SQL "ALTER DATABASE RENAME FILE ''E:\app\Administrator\oradata\orcl\REDO03.LOG''
TO ''F:\app\Administrator\oradata\orcl\REDO03.LOG'' ";
RESTORE DATABASE;
SWITCH DATAFILE ALL;
}
对switch datafile all的说明:
--对于nocatalog 模式下,rman备份的信息是保存在控制文件里的,包括文件的路径信息。 这里的switch datafile all的作用,就是更新控制文件里的信息。
--步骤8.recover 数据库
RECOVER DATABASE;
/***常见错误:
归档日志线程=1 序列=1696
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: recover 命令 (在 04/11/2013 15:18:07 上) 失败
RMAN-06054: 介质恢复正在请求未知的线程 1 序列 1696 的归档日志以及起始 SCN 240793
29
---解决方法:
RMAN> recover database until scn 24159677;
启动 recover 于 2013-04-11 15:29:05
使用通道 ORA_DISK_1
正在开始介质的恢复
介质恢复完成, 用时: 00:00:02
完成 recover 于 2013-04-11 15:29:11
***/
----步骤9.open...resetlogs打开数据库
alter database open resetlogs;
/*******常见错误:
RMAN> alter database open resetlogs;
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: alter db 命令 (在 04/11/2013 15:38:25 上) 失败
ORA-00392: 日志 1 (用于线程 1) 正被清除, 不允许操作
ORA-00312: 联机日志 1 线程 1: 'F:\APP\ADMINISTRATOR\ORADATA\ORCL\REDO01.LOG'
----解决办法:
SQL> select group#,bytes/1024/1024||'M',status from v$log;
GROUP# BYTES/1024/1024||'M' STATUS
---------- ----------------------------------------- ----------------
1 50M CLEARING_CURRENT
3 50M CLEARING
2 50M CLEARING
SQL> alter database clear logfile group 1; --清空日志文件
SQL> alter database clear logfile group 2;
SQL> alter database clear logfile group 3;
SQL> select group#,bytes/1024/1024||'M',status from v$log;
GROUP# BYTES/1024/1024||'M' STATUS
--------- ----------------------------------------- ----------------
1 50M CURRENT
3 50M UNUSED
2 50M UNUSED
*******/
---步骤10:处理temp 临时表空间
sqlplus / as sysdba
SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
E:\app\Administrator\oradata\orcl\TEMP01.DBF --控制文件中路径还在E盘
--A数据库表空间offline
SQL> alter database tempfile 'E:\app\Administrator\oradata\orcl\TEMP01.DBF' offline;
Database altered.
--在OS 级别移动temp的数据文件到B数据库服务器
SQL> mv E:\app\Administrator\oradata\orcl\TEMP01.DBF F:\app\Administrator\oradata\orcl\TEMP01.DBF
--修改控制文件中temp文件的信息
SQL> alter database rename file 'E:\app\Administrator\oradata\orcl\TEMP01.DBF' to 'F:\app\Administrator\oradata\orcl\TEMP01.DBF';
--temp 表空间online
SQL> alter database tempfile 'F:\app\Administrator\oradata\orcl\TEMP01.DBF' online;
Database altered.
--验证
SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
F:\app\Administrator\oradata\orcl\TEMP01.DBF