目录
Windows—Linux—RMAN实现Windows平台迁移到Linux平台(方法2:重新载入备份位置方法)—不完全恢复,无法通过归档追到最新数据,全库关闭方式迁移恢复
一、环境准备
1、源库环境
操作系统:Windows 2012 R2 字 符 集:AL32UTF8 数据库版本:11.2.0.4.0
2、目标库环境
操作系统:Redhat/Oracle/CentOS Linux 7.9 字 符 集:AL32UTF8 数据库版本:11.2.0.4.0
3、迁移方法
源库进行RMAN备份,新库进行RMAN还原
4、注意事项:
源库和目标库的字符集一定要一致,否则会出现乱码。目标库在dbca的时候,要根据源库的字符集设置。
二、Windows源库备份
1、源库备份——方式1(归档开启模式下的备份)
(1)数据库关闭,监听关闭
为了切断所有连接,不让数据库进行更新操作。
SQL>shutdown immediate; C:\Users\Administrator>lsnrctl stop
(2)数据库开启
开启数据库,但不开启监听,禁止所有外部写入动作。
SQL>startup
(3)切换归档日志和检查点,让所有在redolog的数据落盘
因为windows的路径和linux的路径不一致,后续需要通过重建控制文件进行更改,需要使用不完全恢复,那么redo log的数据则无法使用,需要提前将里面的数据落到数据文件中。
SQL>alter system switch logfile; SQL>alter system switch logfile; SQL>alter system switch logfile; SQL>alter system switch logfile; SQL>alter system switch logfile; SQL>alter system switch logfile; SQL>alter system checkpoint;
(4)rman备份数据库
这个过程会自动备份数据文件,控制文件和参数文件。
C:\Users\Administrator>rman target / RMAN> backup database; 启动 backup 于 02-9月-22 使用通道 ORA_DISK_1 通道 ORA_DISK_1: 正在启动全部数据文件备份集 通道 ORA_DISK_1: 正在指定备份集内的数据文件 输入数据文件: 文件号=00001 名称=D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSTEM01.DBF 输入数据文件: 文件号=00002 名称=D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSAUX01.DBF 输入数据文件: 文件号=00003 名称=D:\APP\ADMINISTRATOR\ORADATA\MESORCL\UNDOTBS01.DBF 输入数据文件: 文件号=00005 名称=D:\MES800\TEST01.DBF 输入数据文件: 文件号=00004 名称=D:\APP\ADMINISTRATOR\ORADATA\MESORCL\USERS01.DBF 通道 ORA_DISK_1: 正在启动段 1 于 02-9月 -22 通道 ORA_DISK_1: 已完成段 1 于 02-9月 -22 段句柄=D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP 标记=TAG20220902T151435 注释=NONE 通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:07 通道 ORA_DISK_1: 正在启动全部数据文件备份集 通道 ORA_DISK_1: 正在指定备份集内的数据文件 备份集内包括当前控制文件 备份集内包括当前的 SPFILE 通道 ORA_DISK_1: 正在启动段 1 于 02-9月 -22 通道 ORA_DISK_1: 已完成段 1 于 02-9月 -22 段句柄=:D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NCSNF_TAG20220902T163446_KK3J1G6V_.BKP 标记=TAG20220902T151435 注释=NONE 通道 ORA_DISK_1: 备份集已完成, 经过时间:00:00:01 完成 backup 于 02-9月-22
2、源库备份——方式2(归档未开启的备份方式)
(1)源库为非归档模式
(2)将源库开启到mount状态
(3)rman进行备份
run { allocate channel d1 type disk maxpiecesize=10G; allocate channel d2 type disk maxpiecesize=10G; allocate channel d3 type disk maxpiecesize=10G; allocate channel d4 type disk maxpiecesize=10G; backup as compressed backupset format 'E:\backup\db_%n_%U.dbf'(database); release channel d1; release channel d2; release channel d3; release channel d4; ALLOCATE CHANNEL d5 TYPE disk; backup format 'E:\backup\spfile_%s_%p_%t_%c.spf' (spfile); release channel d5; ALLOCATE CHANNEL d6 TYPE DISK; backup format 'E:\backup\cntl_%s_%p_%t_%c.ctl' (current controlfile); release channel d6; }
3、备份传输
将上述备份的文件拷贝到linux中。
D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T093859_KK2QOMJS_.BKP D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NCSNF_TAG20220902T093859_KK2QOVMF_.BKP
三、Linux目标数据库还原
1、安装新的数据库,并且数据库名称要和源库的一致
我们此处使用mesorcl。
2、启动数据库到nomount状态
SQL> startup nomount;
3、对当前库的参数文件做备份(备份为pfile)
SQL> create pfile='/backup/rman/pfile.ora' from spfile;
4、rman还原控制文件
RMAN> restore controlfile from '/backup/rman/backup/O1_MF_NCSNF_TAG20220902T163446_KK3J1G6V_.BKP';
SQL> alter database mount;
5、查看当前控制文件记录的内容
可以看到,当前控制文件记录的数据文件都是windows的目录信息;
SQL> set line 300 SQL> col name for a60 SQL> select file#,name,status from v$datafile; FILE# NAME STATUS ---------- ------------------------------------------------------------ ------- 1 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSTEM01.DBF SYSTEM 2 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSAUX01.DBF ONLINE 3 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\UNDOTBS01.DBF ONLINE 4 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\USERS01.DBF ONLINE 5 D:\MES800\TEST01.DBF ONLINE
6、查看当前控制文件加载的备份片信息
可以看到备份集的备份片在这里“ D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T093859_KK2QOMJS_.BKP”;
所以后面我们使用restore database的时候,默认是找这个路径:
“ D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T093859_KK2QOMJS_.BKP”;
但这个路径在linux中是不存在的,也无法指定的,所以我们可以重新catalog加载备份文件。
RMAN> list backup;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
7 Full 1018.28M DISK 00:00:05 2022-09-02 16:34:51
BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20220902T163446
Piece Name: D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP
List of Datafiles in backup set 7
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- ------------------- ----
1 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSTEM01.DBF
2 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSAUX01.DBF
3 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\UNDOTBS01.DBF
4 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\USERS01.DBF
5 Full 948228 2022-09-02 16:34:46 D:\MES800\TEST01.DBF
7、核对备份文件
CROSSCHECK 命令用于核对备份文件,以确保 RMAN 资料库与备份文件保持同步。
当执行该命令时,如果 RMAN 资料库记录的状态与实际备份文件不匹配,则会更新资料库里的状态使其与实际备份文件状态一致;若备份文件处于 expired 状态,那么说明该备份已失效,可以删除。
RMAN> crosscheck backup; using channel ORA_DISK_1 crosschecked backup piece: found to be 'EXPIRED' backup piece handle=D:\ARCHIVELOG\MESORCL\BACKUPSET\2022_09_02\O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP RECID=7 STAMP=1114360486 Crosschecked 1 objects
8、删除失效的备份文件
RMAN> delete expired backup;
9、更新备份文件(指向新的备份片)
--catalog start with '/backup/rman/backup/'; -- 注意此处的目录 RMAN> catalog start with '/backup/rman/backup/'; searching for all files that match the pattern /backup/rman/backup/ List of Files Unknown to the Database ===================================== File Name: /backup/rman/backup/O1_MF_1_8_KK2W0VTY_.ARC File Name: /backup/rman/backup/O1_MF_1_9_KK3HZBLW_.ARC File Name: /backup/rman/backup/O1_MF_1_10_KK3HZG5Q_.ARC File Name: /backup/rman/backup/O1_MF_1_11_KK3HZGTQ_.ARC File Name: /backup/rman/backup/O1_MF_NCSNF_TAG20220902T163446_KK3J1G6V_.BKP File Name: /backup/rman/backup/O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP
Do you really want to catalog the above files (enter YES or NO)? yes
cataloging files...
cataloging done
List of Cataloged Files
=======================
File Name: /backup/rman/backup/O1_MF_1_8_KK2W0VTY_.ARC
File Name: /backup/rman/backup/O1_MF_1_9_KK3HZBLW_.ARC
File Name: /backup/rman/backup/O1_MF_1_10_KK3HZG5Q_.ARC
File Name: /backup/rman/backup/O1_MF_1_11_KK3HZGTQ_.ARC
File Name: /backup/rman/backup/O1_MF_NCSNF_TAG20220902T163446_KK3J1G6V_.BKP
File Name: /backup/rman/backup/O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP
10、查看重新加载的备份片信息
RMAN> list backup;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
8 Full 9.36M DISK 00:00:00 2022-09-02 16:34:53
BP Key: 8 Status: AVAILABLE Compressed: NO Tag: TAG20220902T163446
Piece Name: /backup/rman/backup/O1_MF_NCSNF_TAG20220902T163446_KK3J1G6V_.BKP
SPFILE Included: Modification time: 2022-09-02 09:29:06
SPFILE db_unique_name: MESORCL
Control File Included: Ckp SCN: 948230 Ckp time: 2022-09-02 16:34:53
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
9 Full 1018.28M DISK 00:00:00 2022-09-02 16:34:46
BP Key: 9 Status: AVAILABLE Compressed: NO Tag: TAG20220902T163446
Piece Name: /backup/rman/backup/O1_MF_NNNDF_TAG20220902T163446_KK3J1645_.BKP
List of Datafiles in backup set 9
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- ------------------- ----
1 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSTEM01.DBF
2 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\SYSAUX01.DBF
3 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\UNDOTBS01.DBF
4 Full 948228 2022-09-02 16:34:46 D:\APP\ADMINISTRATOR\ORADATA\MESORCL\USERS01.DBF
5 Full 948228 2022-09-02 16:34:46 D:\MES800\TEST01.DBF
11、还原数据库
RMAN> run{ allocate channel d1 type disk; allocate channel d2 type disk; set newname for datafile 1 to '/oracle/app/oracle/oradata/mesorcl/system01.dbf'; set newname for datafile 2 to '/oracle/app/oracle/oradata/mesorcl/sysaux01.dbf'; set newname for datafile 3 to '/oracle/app/oracle/oradata/mesorcl/undotbs01.dbf'; set newname for datafile 4 to '/oracle/app/oracle/oradata/mesorcl/users01.dbf'; set newname for datafile 5 to '/oracle/app/oracle/oradata/mesorcl/test01.dbf'; restore database; switch datafile all; release channel d1; release channel d2; } 注意:有可能会报错:
RMAN-03002: failure of restore command at 09/03/2022 15:39:56 RMAN-06026: some targets not found - aborting restore RMAN-06023: no backup or copy of datafile 4 found to restore RMAN-06023: no backup or copy of datafile 3 found to restore RMAN-06023: no backup or copy of datafile 2 found to restore RMAN-06023: no backup or copy of datafile 1 found to restore
--分析:这是incarnation的问题,重置后即可
RMAN> list incarnation; List of Database Incarnations DB Key Inc Key DB Name DB ID STATUS Reset SCN Reset Time ------- ------- -------- ---------------- ------- -------------- ---------------------- 1 1 MESORCL 3048150118 PARENT 1 2013-10-09 18:23:38 2 2 MESORCL 3048150118 PARENT 897124 2022-09-02 09:20:40 3 3 MESORCL 3048150118 CURRENT 948244 2022-09-03 10:20:17 RMAN> reset database to incarnation 2; database reset to incarnation 2
12、恢复数据库
SQL> recover database using backup controlfile until cancel; ORA-00279: change 948228 generated at 09/02/2022 16:34:46 needed for thread 1 ORA-00289: suggestion : /oracle/archivelog/MESORCL/archivelog/2022_09_03/o1_mf_1_12_%u_.arc ORA-00280: change 948228 for thread 1 is in sequence #12 Specify log: {<RET>=suggested | filename | AUTO | CANCEL} cancel Media recovery cancelled.
注意:
windows的归档日志无法应用到linux上,会提示报错,所有无法通过归档的方式,将源库新增的数据追击上。要想windows所有的数据最全,需要在无外部连接的情况进行备份,然后移至Linux还原。
或者可以通过增量备份和恢复方式,恢复增量备份数据,缩短停机时间。
见下一节:https://www.topunix.com/post-9748.html
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
/backup/rman/backup/2022_09_03/O1_MF_1_15_KK61MP0F_.ARC
ORA-10562: Error occurred while applying redo to data block (file# 2, block# 64022)
ORA-10564: tablespace SYSAUX
ORA-01110: data file 2: '/oracle/app/oracle/oradata/mesorcl/sysaux01.dbf'
ORA-10561: block type 'TRANSACTION MANAGED DATA BLOCK', data object# 268
ORA-00600: internal error code, arguments: [ktbrcl:CDLC not in CR], [106], [],[], [], [], [], [], [], [], [], []
13、打开数据库(resetlogs方式)
SQL> alter database open resetlogs;
四、数据库日志文件调整(如果需要调整)
1、查看日志文件信息
SQL> col member for a100
SQL> select a.group#,a.thread#,a.bytes,a.status,b.member from v$log a,v$logfile b where a.group#=b.group#; GROUP# THREAD# BYTES STATUS MEMBER ---------- ---------- ---------- ---------------- ---------------------------------------------------------------------------------------------------- 3 1 52428800 UNUSED /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO03.LOG 2 1 52428800 UNUSED /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO02.LOG 1 1 52428800 CURRENT /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO01.LOG
2、添加日志组和文件
alter database add logfile group 5 '/oracle/app/oracle/oradata/mesorcl/redo05.log' size 50m; alter database add logfile group 6 '/oracle/app/oracle/oradata/mesorcl/redo06.log' size 50m; alter database add logfile group 7 '/oracle/app/oracle/oradata/mesorcl/redo07.log' size 50m;
3、查看当前的状态
SQL> select a.group#,a.thread#,a.bytes,a.status,b.member from v$log a,v$logfile b where a.group#=b.group#; GROUP# THREAD# BYTES STATUS MEMBER ---------- ---------- ---------- ---------------- ---------------------------------------------------------------------------------------------------- 3 1 52428800 UNUSED /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO03.LOG 2 1 52428800 UNUSED /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO02.LOG 1 1 52428800 CURRENT /oracle/app/oracle/product/11.2.0/db_1/dbs/D:APPADMINISTRATORORADATAMESORCLREDO01.LOG 5 1 52428800 UNUSED /oracle/app/oracle/oradata/mesorcl/redo05.log 6 1 52428800 UNUSED /oracle/app/oracle/oradata/mesorcl/redo06.log 7 1 52428800 UNUSED /oracle/app/oracle/oradata/mesorcl/redo07.log
4、删除组1,2,3,再创建1,2,3组
--利用语句切换删除
SQL> alter system switch logfile; SQL> alter system checkpoint; SQL> alter database drop logfile group 2; SQL> alter database drop logfile group 3; SQL> alter system switch logfile; SQL> alter system checkpoint; SQL> alter database drop logfile group 1;
--物理删除redo01.log,redo02.log,redo03.log
alter database add logfile group 1 '/oracle/app/oracle/oradata/mesorcl/redo01.log' size 50m; alter database add logfile group 2 '/oracle/app/oracle/oradata/mesorcl/redo02.log' size 50m; alter database add logfile group 3 '/oracle/app/oracle/oradata/mesorcl/redo03.log' size 50m;
五、创建临时文件(如果不存在临时文件)
1、查看当前临时文件
SQL> select * from v$tempfile; SQL> select * from dba_temp_files; SQL> select file_name,file_id,tablespace_name,bytes,status from dba_temp_files;
2、创建其他临时表空间,并设置为默认
SQL> create temporary tablespace temp2 tempfile '/oracle/app/oracle/oradata/mesorcl/temp02.dbf' size 20m autoextend on; SQL> alter database default temporary tablespace temp2;
3、删除原来的临时表空间temp
SQL> drop tablespace temp including contents and datafiles; [oracle@qdmes219:/oracle/app/oracle/oradata/mesorcl]$rm -rf temp01.dbf
4、创建新的临时表空间temp,并设为默认
SQL> create temporary tablespace temp tempfile '/oracle/app/oracle/oradata/mesorcl/temp01.dbf' size 20m autoextend on; SQL> alter database default temporary tablespace temp;
5、删除temp2临时表空间
SQL> drop tablespace temp2 including contents and datafiles;
如果此时卡住,则退出SQL,重新执行删除操作即可,因为当前SQL可能在使用这个临时表空间。
六、升级数据字典
1、通过 startup upgrade 启动实例
--停库
SQL> shutdown immediate
--启动到upgrage模式
SQL> startup upgrade;
2、升级数据字典
大约5-10分钟
SQL> @$ORACLE_HOME/rdbms/admin/catupgrd.sql ...
Final Actions . 00:00:00 Total Upgrade Time: 00:04:26 PL/SQL procedure successfully completed.
3、重启实例
--停库
SQL> shutdown immediate
--启动到正常模式
SQL> startup;
七、测试数据库
查询表数据、进行日志切换等操作。