一、迁移环境
以下为WIN平台下RMAN异机迁移恢复实例。
--源库
操作系统:WIN SVR 2008 R2
主机名:orcldb1
IP地址:172.16.1.120
数据库版本:11.2.0.4
存储方式:文件系统
ORACLE_HOME:D:\app\Administrator\product\11.2.0\dbhome_1
ORACLE_SID:orcl
数据库字符集:SIMPLIFIED CHINESE_CHINA.ZHS16GBK
--目标库:
操作系统:WIN SVR 2008 R2
主机名:orcldb2
IP地址:172.20.100.41
数据库版本:11.2.0.4
存储方式:文件系统
ORACLE_HOME:D:\app\Administrator\product\11.2.0\dbhome_1
ORACLE_SID:orcl
数据库字符集:SIMPLIFIED CHINESE_CHINA.ZHS16GBK
二、迁移准备
1、在迁移前需要在源数据库做好RMAN全备,以及归档日志备份。
2、在迁移前目标库需要安装好数据库,不创建实例。
三、迁移步骤
RMAN恢复步骤:
•RMAN备份源库(包括参数文件、数据文件、控制文件、归档日志文件等);
•初始化数据库,安装相同环境;
•恢复参数文件;
•恢复控制文件;
•启动数据库到MOUNT状态,利用控制文件进行数据恢复;
•恢复归档日志备份;
•restore –> recover —> alter database open ressetlogs;
•验证
源库备份操作:
1、相关备份配置及脚本如下:
RMAN> show all;
db_unique_name 为 orcl 的数据库的 RMAN 配置参数为:
CONFIGURE RETENTION POLICY TO RECOVERYWINDOW OF 14 DAYS; #保留备份为14天
CONFIGURE BACKUP OPTIMIZATION OFF; #default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; #default
CONFIGURE CONTROLFILE AUTOBACKUP ON; #需要打开自动备份
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FORDEVICE TYPE DISK TO 'e:\data_backup\ctl_%F.bak'; #指定备份控制文件及参数文件备份路径格式
CONFIGURE DEVICE TYPE DISK PARALLEorclM 1BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICETYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FORDEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; #default
CONFIGURE ENCRYPTION FOR DATABASE OFF; #default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; #default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' ASOF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TONONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO'D:\APP\ADMINISTRATOR\PRODUCT\11.2.0\DBHOME_1\DATABASE\SNCForcl.ORA'; # default
备份脚本如下:
full_backup.sql
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup as compressed backupset fulldatabase format 'e:\data_backup\full_%d_%U.bak';
sql 'alter system archive log current';
backup archivelog all format'e:\data_backup\arch_%d_%U.bak' delete all input;
backup current controlfile format 'e:\data_backup\ctl_%d_%U.bak';
backup spfile tag='spfile' format='e:\data_backup\spfile_%d_%U.bak';
release channel c1;
release channel c2;
report obsolete;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
orclt backup summary;
}
full_backup.bat
set oracle_sid=orcl
set d=%date:~,4%%date:~5,2%%date:~8,2%
echo=>D:\backup\log\full_backup_%d%.log
2、执行备份:
rman target /cmdfile=D:\backup\script\full_backup.sql msglog=e:\rman_script\log\full_backup_%d%.log
可单独备份参数文件:
SQL> create pfile='e:\data_backup\initorcl.ora' from spfile;
3、拷贝备份文件至目标数据库服务器
目标库操作:
1、初始化目标数据库,安装相同环境,拷贝备份
a.目标库只安装ORACLE软件,不创建实例,并安装与源库相同路径
b.然后把相关备份文件拷贝到目标数据库备份路径,为避免不必要的麻烦,建议设置与源库一致。
2、建立密码文件、数据库服务、监听器,否则无法连接DB
C:\Users\Administrator>D:
D:\>cd D:\app\Administrator\product\11.2.0\dbhome_1\BIN
a.创建密码文件:
D:\>orapwd file=D:\app\Administrator\product\11.2.0\dbhome_1\database\pwdorcl.ora password=oracle entries=5;
b.创建数据库服务:
D:\> oradim -new -sid orcl -startmode m
实例已创建
c.创建监听:
D:\> netca
d.连接数据库:
D:\>set ORACLE_SID=orcl
D:\>sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on 星期五 4月 21 14:19:482017
Copyright (c) 1982, 2010, Oracle. All rights reserved.
已连接到空闲例程。
此时可以连接到DB了。
出现 ORA-12560: TNS: 协议适配器错误
最后按照以下方法设置:
修改系统环境变量path中的Oracle和客户端的顺序即可。
D:\app\Administrator\product\11.2.0\dbhome_1\bin
3、恢复SPfile参数文件
a.通过备份的pfile文件进行(编辑修改,根据实际情况调整内存大小)恢复
# 以下为修改后的initorcl.ora文件内容:
orcl.__java_pool_size=33554432
orcl.__large_pool_size=33554432
orcl.__oracle_base='d:\app\Administrator' #ORACLE_BASE set from environment
orcl.__pga_aggregate_target=536870912
orcl.__sga_target=536870912
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=536870912
orcl.__streams_pool_size=16777216
*.audit_file_dest='d:\app\Administrator\admin\orcl\adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='d:\app\Administrator\oradata\orcl\control01.ctl','d:\app\Administrator\flash_recovery_area\orcl\control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='d:\app\Administrator\flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.diagnostic_dest='d:\app\Administrator'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.log_archive_format='ARC%S_%R.%T'
*.memory_target=1073741824
*.nls_language='SIMPLIFIED CHINESE'
*.nls_territory='CHINA'
*.open_cursors=100
*.processes=500
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
b.找到源spfile备份相关文件进行恢复:
RMAN> restore spfile from 'e:\data_backup\spfile-20180116-00.BAK';
也可以手动建立一个pfile文件(目的是使用数据库启动到nomount状态),放到D:\app\Administrator\product\11.2.0\dbhome_1\database目录下,initorcl.ora内容如下:
db_name=orcl
pga_aggregate_target=8589934592
sga_target=34359738368
java_pool_size=4194304
large_pool_size=4194304
shared_pool_size=96468992
以此pfile启动数据库到nomount状态:
SQL> startup nomount pfile='D:\app\Administrator\product\11.2.0\dbhome_1\database\initorcl.ora';
ORACLE 例程已经启动。
Total System Global Area 162873344 bytes
Fixed Size 2173800 bytes
Variable Size 104858776 bytes
Database Buffers 50331648 bytes
Redo Buffers 5509120 bytes
SQL> create spfile from pfile;
SQL> shutdown immediate
SQL> startup nomount
4、切换到RMAN下,并设置SET DBID=2897709788(源库DBID):
查看源库DBID:
SQL> select dbid from v$database;
DBID
----------
2897709788
目标库设置相同的DBID:
rman target /
set DBID=2897709788 ;
5、创建必要的目录:
创建flash_recovery_area、ORADATA等目录,否则恢复数据时会报错ORA-01263: Name given for file destination directory is invalid 。
建立相关目录:admin、oradata、flash_recovery_area
6、此时可以进行恢复控制文件,找到源控制文件备份相关文件进行恢复:
RMAN> restore controlfile from 'd:\backup\rman\ctl-2897709788-20191112-01';
控制文件恢复完成后,接下来把数据库修改为MOUNT状态:
RMAN> alter database mount;
7、查看归档日志备份的状态,查看能最大能恢复到哪个sequence
SQL> select max(sequence#) from v$archived_log;
RMAN> list backup of archivelog all;
把拷贝到新机器的备份文件注册到(刚恢复的)控制文件中(redolog不能被注册,所以最后有报错,没有关系):
RMAN> catalog start with "d:\backup\rman\";
8、进行数据文件恢复:
# 直接进行数据文件恢复(最后有报错可以忽略)
RMAN> restore database;
# 或者根据最大SCN进行恢复
RMAN> run {
set until sequence 343305144;
restore database;
}
恢复数据库:
RMAN> recover database;
9、将源库新增的归档日志拷贝到目标端进行恢复:
D:\> sqlplus / as sysdba
SQL> alter system switch logfile;
System altered.
创建一张测试表,等恢复完后进行验证。
SQL> create table t1 (id number,name varchar2(10));
Table created.
SQL> insert into t1 values (1,'sysdate');
1 row created.
SQL> commit;
Commit complete.
SQL> alter system switch logfile; --切换日志,把日志写进硬盘
System altered.
关闭源数据库:
SQL> shutdown immediate;
拷贝新增的归档日志到目标端进行恢复
重新注册归档文件:
RMAN> catalog archivelog 'd:\archivelog\arch_27.266.941652809';
RMAN> catalog archivelog 'd:\archivelog\arch_28.256.941652811';
......
# 或者可以直接注册日志目录
RMAN> catalog start with 'd:\backup\rman\';
恢复数据库:
RMAN> recover database;
10、恢复密码文件(步骤2中已创建可以忽略此步):
将拷贝过来的密码文件恢复至数据库对应目录下:
D:\app\Administrator\product\11.2.0\dbhome_1\dbs\orapworcl
或者手动创建密码文件:
orapwdfile='D:\app\Administrator\product\11.2.0\dbhome_1\dbs\orapworcl' password=oracle entries=30 force=y
11、resetlogs打开数据库:
先read only方式打开数据库,验证数据是否恢复完整后再打开数据库:
SQL> alter database open read only;
Database altered.
查询之前插入的数据是否恢复:
SQL> select * from t1;
关闭数据库,重新启动到mount状态:
SQL> shutdown immediate
SQL> startup mount
RESETLOGS打开数据库:
SQL> alter database open resetlogs;
database opened
12、在注册表添加SID:
# 修改注册表中SID
HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraDb11g_home1
"ORACLE_SID"="orcl"
13、重启数据库,是否以SPFILE启动:
SQL> shutdown immediate
SQL> startup
SQL> select decode(count(*),1,'spfile','pfile') from v$spparameter where rownum=1 and isspecified = 'TRUE';
14、修改listener.ora,tnsnames.ora
--listener.ora
# orcltener.ora Network Configuration File:D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ORATEST)(PORT = 1521))
)
ADR_BASE_LISTENER =D:\app\Administrator\product\11.2.0\dbhome_1\log
--tnsnames.ora
# tnsnames.ora Network Configuration File:D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.
orcl =
(DESCRIPTION =
(ADDRESS_orclT =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.18.25)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
查看监听状态:lsnrctl status
验证数据,OK!