保命法则:先备份再操作,磁盘空间紧张无法备份就让满足,给自己留退路。
场景说明:
1.本文档的环境为同平台、不同版本(操作系统版本可以不同,数据库小版本不同),源机器和目标机器部分控制文件和ORACLE_BASE目录一样。
2.目标机器只需要安装oracle只安装oracle数据库软件,不创建数据库(no netca dbca)。
3.第一次利用备份恢复测试环境,之后从源机器拷贝备份到目标机器并在控制文件中注册,再进行恢复测试。
主要过程:
将切换归档,执行rman全库备份以及密码文件拷贝到目标主机进行rman恢复、升级。
注意事项:
当使用rman nocatalog恢复时,数据库必须是处于“mount”状态的。而Oracle startup mount的前提条件是control必须存在。因此,你必须在恢复datafile之前先恢复controlfile。
特别说明:
源数据库和新数据库控制文件目录和ORACLE_BASE目录不同,其他目录相同。解决办法:更改参数文件中控制文件目录和ORACLE_BASE目录为新数据库位置。
源数据库和新数据库控制文件目录和ORACLE_BASE目录不同,其他目录不相同。解决办法:更改参数文件中控制文件目录和ORACLE_BASE目录为新数据库位置、更改控制文件中数据文件位置。
环境描述
源数据库环境
操作系统版本 : CentOS Linux release 7.9 内存:4G cpu:1核 磁盘:40G
数据库版本 : Oracle 11.2.0.3 x64
数据库名 : dxj
数据库SID : dxj
db_unique_name : dxj
instance_name : dxj
归档模式:已开启
IP : 192.168.40.53
目标数据库环境
操作系统版本 : CentOS Linux release 7.9 内存:4G cpu:1核 磁盘:40G
数据库版本 : Oracle 11.2.0.4 x64 (只安装oracle数据库软件,no netca dbca)
数据库名 : dxj
数据库SID : dxj
db_unique_name: dxj
instance_name : dxj
IP:192.168.40.52
迁移前准备
统计数据量
--查看数据文件总大小
select sum(bytes)/1024/1024/1024 as "size(G)" from dba_data_files;--查看总大小
select round(sum(bytes) / 1024 / 1024 / 1024, 2) || 'G'from dba_segments;
统计业务数据
--每个业务用户下的总对象数量校验
SELECT D.OWNER,COUNT(1)FROM dba_objects dWHERE d.OWNER in ('FUWA')AND NOT EXISTS (SELECT 1 FROM DBA_RECYCLEBIN B WHERE B.object_name=D.OBJECT_NAME AND D.OWNER=B.owner) GROUP BY D.OWNERORDER BY D.OWNER ;OWNER COUNT(1)
------------------------------ ----------
FUWA 1--每个业务用户下的各个对象类别的数量校验
SQL> select OWNER,OBJECT_TYPE,status,count(OBJECT_NAME) from dba_objects where owner in ('FUWA') group by OBJECT_TYPE,owner,status order by 1,3,2;OWNER OBJECT_TYPE STATUS COUNT(OBJECT_NAME)
------------------------------ ------------------- ------- ------------------
FUWA TABLE VALID 1
清理业务数据
需和开发 直属领导同意。对于占用空间大的表采用方案如下:
1、删除表数据。
2、备份时加参数排除,即不备份对业务来说不重要的表。
--查看表大小超过10G的大表
col owner for a40
col TABLE_NAME for a60
set linesize 999 pagesize 999
select * from (SELECT OWNER,TABLE_NAME,SUM(tabsize) table_size from (SELECT owner,SEGMENT_NAME as table_name,SUM(BYTES)/1024/1024/1024 as tabsize FROM DBA_SEGMENTS WHERE (OWNER,SEGMENT_NAME) IN (SELECT OWNER,TABLE_NAME FROM DBA_TABLES WHERE OWNER NOT IN ('SYS','SYSTEM','DBSNMP'))GROUP BY SEGMENT_NAME,ownerunion ALLselect lob.owner,lob.table_name,sum(seg.bytes)/1024/1024/1024 as tabsize from dba_lobs lob,dba_segments seg where lob.segment_name=seg.segment_name and lob.OWNER NOT IN ('SYS','SYSTEM','DBSNMP') group by lob.owner,lob.table_name) GROUP BY OWNER,TABLE_NAME ORDER BY 3 DESC)where table_size>10 ;--查询库中记录数大于2千万的所有表,当前用户拥有select any table权限
select table_name, num_rows from dba_tables t where t.owner = upper('hr') and num_rows > 20000000;
或
select table_name, num_rows from all_tables t where t.owner = upper('hr') and num_rows > 20000000;
停止监听
--停止监听
su - oracle
[oracle@dxj ~]$ lsnrctl status
[oracle@dxj ~]$ lsnrctl stop
[oracle@dxj ~]$ lsnrctl status
开启防火墙
--开启防火墙
systemctl start firewalld
systemctl status firewalld
firewall-cmd --list-ports
停止会话
查询会话,如果有业务会话给干掉
--查看不同用户的连接数
select username,count(username) from v$session where username is not null group by username;--查询用户会话
select username,serial#, sid ,status from v$session where username is not null;
select username,status,schemaname,osuser,process,machine,port,program,serial#, sid from v$session where username is not null;
--删除相关用户会话
alter system kill session 'sid,serial#';补充:
--源库杀用户连接 不建议 该步骤慎重 该文档未进行该操作 该方法迫不得已不用
ps -ef|grep LOCAL=NO|awk '{print $2}'|xargs kill -9
锁定业务用户
--查看用户 和现场负责人确定哪些是业务用户,哪些是需要迁的用户
set lin1000 pagesize 999
select username,default_tablespace,temporary_tablespace from dba_users where username not in ('SYS','SYSTEM','HR','OUTLN','MGMT_VIEW','FLOWS_FILES','MDSYS','ORDSYS','EXFSYS','DBSNMP','WMSYS','APPQOSSYS','APEX_030200','OWBSYS_AUDIT','ORDDATA','CTXSYS','ANONYMOUS','SYSMAN','XDB','ORDPLUGINS','OWBSYS','SI_INFORMTN_SCHEMA','OLAPSYS','SCOTT','ORACLE_OCM','XS$NULL','BI','PM','MDDATA','IX','SH','DIP','OE','APEX_PUBLIC_USER','SPATIAL_CSW_ADMIN_USR','SPATIAL_WFS_ADMIN_USR');USERNAME DEFAULT_TABLESPACE TEMPORARY_TABLESPACE
------------------------------ ------------------------------ ------------------------------
FUWA USERS TEMP--锁定业务用户
select username,account_status,lock_date from dba_users where username='FUWA';
alter user FUWA account lock;
或
select 'alter user '||username||' account lock;' from dba_users where username in ('FUWA') and ACCOUNT_STATU='OPEN';
执行以上输出结果
查看源库信息
查看数据库参数
--数据库的创建日期和归档方式
SELECT to_char(created,'yyyy-mm-dd'), log_mode, log_mode FROM V$database; --查数据库版本
SELECT version FROM product_component_version WHERE substr(product, 1, 6) = 'Oracle'; --查看实例名
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
topicisnew-- 查看数据库字符集
set linesize 999
col value for a60
select * from nls_database_parameters where PARAMETER in ('NLS_CHARACTERSET','NLS_NCHAR_CHARACTERSET');
PARAMETER VALUE
------------------------------ ----------------------------------------------------------------------------
NLS_CHARACTERSET AL32UTF8
NLS_NCHAR_CHARACTERSET UTF8select userenv('language') from dual;--查看当前数据库时区
select dbtimezone from dual;
补充:如果不是东八区 alter database set time_zone='+8:00';-- 查看数据库配置参数
set lin 1000 pagesize 999
col NAME for a30
col value for a40
col DISPLAY_VALUE for a40
SELECT name,value,display_value FROM v$parameter
WHERE name IN(
'processes',
'sessions',
'memory_target',
'memory_max_target',
'sga_max_size',
'sga_target',
'pga_aggregate_target'
);
NAME VALUE DISPLAY_VALUE
------------------------------ ---------------------------------------- -----------------------------------
processes 3000 3000
sessions 4528 4528
sga_max_size 34359738368 32G
sga_target 34359738368 32G
memory_target 0 0
memory_max_target 0 0
pga_aggregate_target 8589934592 8G
查看用户
--查看用户
set lin1000 pagesize 999
select username,default_tablespace,temporary_tablespace from dba_users where username not in ('SYS','SYSTEM','HR','OUTLN','MGMT_VIEW','FLOWS_FILES','MDSYS','ORDSYS','EXFSYS','DBSNMP','WMSYS','APPQOSSYS','APEX_030200','OWBSYS_AUDIT','ORDDATA','CTXSYS','ANONYMOUS','SYSMAN','XDB','ORDPLUGINS','OWBSYS','SI_INFORMTN_SCHEMA','OLAPSYS','SCOTT','ORACLE_OCM','XS$NULL','BI','PM','MDDATA','IX','SH','DIP','OE','APEX_PUBLIC_USER','SPATIAL_CSW_ADMIN_USR','SPATIAL_WFS_ADMIN_USR');USERNAME DEFAULT_TABLESPACE TEMPORARY_TABLESPACE
------------------------------ ------------------------------ ------------------------------
FUWA USERS TEMP--查看用户使用了哪些表空间
select distinct tablespace_name from dba_segments where owner in ('FUWA');
查看源数据库DBID、控制文件、数据文件、redo日志文件的存储位置和归档:
--查看源数据库的DBID:
sys@SYXK 2024-01-25 17:21:58> select dbid from v$database;DBID
----------
2712415422--查看控制文件:
sys@SYXK 2024-01-25 17:22:01> select name from v$controlfile;NAME
--------------------------------------------------------------------------------
/oradata/dxj/control01.ctl
/u01/app/oracle/fast_recovery_area/dxj/control02.ctl--查看数据文件:
sys@SYXK 2024-01-25 17:22:52> set linesize 999
sys@SYXK 2024-01-25 17:22:52> col name for a69
sys@SYXK 2024-01-25 17:22:19> select status,name from v$datafile;
STATUS NAME
------- --------------------------------------------------------------------------------
SYSTEM /oradata/dxj/system01.dbf
ONLINE /oradata/dxj/sysaux01.dbf
ONLINE /oradata/dxj/undotbs01.dbf
ONLINE /oradata/dxj/users01.dbf
ONLINE /oradata/dxj/fuwa01.dbf
ONLINE /oradata/dxj/top01.dbf6 rows selected.--查看日志文件:
sys@SYXK 2024-01-25 17:22:52> set linesize 999
sys@SYXK 2024-01-25 17:23:55> col member for a70
sys@SYXK 2024-01-25 17:24:05> select * from v$logfile;3 ONLINE /oradata/dxj/redo03.log NO2 ONLINE /oradata/dxj/redo02.log NO1 ONLINE /oradata/dxj/redo01.log NO--查看归档模式
sys@DXJ 2024-05-27 10:14:02> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oradata/archivelog
Oldest online log sequence 5
Next log sequence to archive 7
Current log sequence 7
源数据库服务器
rman备份源数据库
文件多的场景加section size
大文件切片 多开并行,优化速度,前提是存储牛逼,不然加了也没啥作用
评估:备份时间,传输时间,恢复时间,遇到问题
[oracle@dxj:/data/rman_bak]$ cat /data/rman_bak.sh
#!/bin/bash
source /home/oracle/.bash_profile
export NLS_DATE_FORMAT='YYYYMMDD hh24:mi:ss'
rq=`date +%Y%m%d`
bakdir=/data/rman_bak/${rq}
autobak=/data/rman_bak/autobackup
if [ ! -d ${bakdir} ];
then mkdir -p ${bakdir}
fi
if [ ! -d ${autobak} ];
then mkdir -p ${autobak}
fi
cd $ORACLE_HOME/bin
./rman log $bakdir/rman${rq}.log target / <<EOF
run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup incremental level 0 as compressed backupset section size 10g format='${autobak}/emrdb_full_%U_%d_%T_%s' database;
sql 'alter system switch logfile';
backup as compressed backupset section size 10g archivelog from time 'sysdate-1/12' format='${autobak}/emrdb_arc_%U_%d_%T_%s';
BACKUP CURRENT CONTROLFILE format '${autobak}/emrdb_ctl_bk_%U_%d_%T' ;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
exit;
EOFchmod +x /data/rman_bak.sh
nohup sh /data/rman_bak.sh &参数说明:sysdate-1/12 :2小时之前的归档。%U_%d_%T_%s
%U:默认是%u_%p_%c的简写形式,利用它可以为每一个备份片段(即磁盘文件)生成一个唯一名称,这是最常用的命名方式,执行不同备份操作时,生成的规则也不同,如下所示:生成备份片段时,%U=%u_%p_%c;
%u:是一个由备份集编号和建立时间压缩后组成的8字符名称。利用%u可以为每个备份集生成一个唯一的名称。
%p:备份集中备份片段的编号,从1开始。
%c:备份片段的复制数(从1开始编号,最大不超过256)。
%d:Oracle数据库名称。
%T:当前时间的年月日格式(YYYYMMDD)。
%s:备份集号。 rman备份有时间点,和scn 记录一下,恢复的时候按时间点和scn恢复就行了
通过rman进行一次全备:
vi rman_bak_L0.sh
#************************************************************************
#*** rman_bak_L0.sh ***
#************************************************************************
#!/bin/bash
source /home/oracle/.bash_profile
rq=`date +%Y%m%d`
bakdir=/topsoft/rmanbak/${rq}
autobak=/topsoft/rmanbak/autobackup
if [ ! -d ${bakdir} ];
then mkdir -p ${bakdir}
fi
if [ ! -d ${autobak} ];
then mkdir -p ${autobak}
fi
cd $ORACLE_HOME/bin
./rman target / log=$bakdir/rmanfull_${rq}.log <<EOF
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '${autobak}/%F';
run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
allocate channel c5 type disk;
allocate channel c6 type disk;
sql 'alter system archive log current';
backup as compressed backupset incremental level 0 database tag 'dbfull' format '${autobak}/backlv0_%d_%T_%t_%s_%p.bak';
sql 'alter system archive log current';
backup as compressed backupset archivelog all tag 'arch' format '${autobak}/arch_%d_%T_%t_%s_%p.bak';
backup current controlfile format '${autobak}/ctl_%d_%T_%t_%s_%p.bak';
release channel c1;
release channel c2;
release channel c3;
release channel c4;
release channel c5;
release channel c6;
}
report obsolete;
crosscheck backup;
crosscheck archivelog all;
delete noprompt obsolete;
delete noprompt expired backup;
list backup summary;
exit;
EOF【注意:控制文件一定要最后备份,由于没做归档目录数据库,rman备份信息都放在控制文件里面,要保证备份过程的信息内容都进入到控制文件,保证数据文件和控制文件是一致备份】分别列出参数文件备份,控制文件备份,数据文件备份,以及归档备份的名字:
a.参数文件备份如下:
RMAN> list backup of spfile;using target database control file instead of recovery catalogList of Backup Sets
===================BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
51 Full 9.36M DISK 00:00:00 25-JAN-24 BP Key: 51 Status: AVAILABLE Compressed: NO Tag: TAG20240125T180503Piece Name: /topsoft/rmanbak/autobackup/c-2492862311-20240125-06SPFILE Included: Modification time: 25-JAN-24SPFILE db_unique_name: SYXKb.控制文件备份如下:
RMAN> list backup of controlfile;List of Backup Sets
===================BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
51 Full 9.36M DISK 00:00:00 25-JAN-24 BP Key: 51 Status: AVAILABLE Compressed: NO Tag: TAG20240125T180503Piece Name: /topsoft/rmanbak/autobackup/c-2492862311-20240125-06Control File Included: Ckp SCN: 2895135 Ckp time: 25-JAN-24c.数据文件备份如下:
RMAN> list backup of database;
d.列出归档备份如下:
RMAN> list backup of archivelog all;
备份参数文件
create pfile='/home/oracle/initdxj20240528.ora' from spfile;
备份密码文件
密码文件位置:$ORACLE_HOME/dbs
cp /u01/app/oracle/product/11.2.0/db/dbs/orapwdxj /home/oracle/
将rman备份文件、参数文件和密码文件copy至目标机器
如果端口不是默认的22,需加-P port_value
--拷贝rman备份文件
scp -r /data/rman_bak/autobackup oracle@192.168.40.52:/home/oracle/--拷贝参数文件
scp /home/oracle/initdxj20240528.ora oracle@192.168.40.52:/home/oracle/--拷贝密码文件
scp /home/oracle/orapwdxj oracle@192.168.40.52:/home/oracle/
目标数据库服务器
如果目标服务器已经新创建了dxj实例,需使用以下命令进行删除,删除后再进行如下恢复操作。
--静默删除实例
dbca -silent -deleteDatabase -sourcedb dxj \
-sid dxj
修改参数文件
如果源服务器和目标服务器安装目录和数据目录都相同,该步骤可忽略。不然按如下步骤更改。
修改目录
参数文件中
dxj.__oracle_base为$ORACLE_BASE目录
.audit_file_dest为$ORACLE_BASE目录/admin/syxk/adump
.control_files中control02.ctl路径为$ORACLE_BASE目录/fast_recovery_area/dxj/control02.ctl
.db_recovery_file_dest为$ORACLE_BASE目录/fast_recovery_area
.diagnostic_dest为$ORACLE_BASE目录
--备份已备份的参数文件,避免改的有问题重新从源库服务器传输到目标库服务器
cd /home/oracle
cp initdxj20240528.ora initdxj20240528.ora_bak--更改参数文件中的oracle_base目录为目标库的oracle_base目录 控制文件路径等路径
vi /home/oracle/initdxj20240528.ora语法说明:
:%s#源目录#新目录#g
修改内存相关参数
如果源服务器和目标服务器配置都相同,该步骤可忽略。不然按如下步骤更改。
由于目标服务器32G和源服务器内存128G差异大,需要更改内存参数。
内存参数修改公式:
--sga和pga配置的最佳实践
对于OLTP系统来说,oracle建议是sga=系统内存*0.8*0.8 单位字节
pga=系统内存*0.8*0.2
对于DSS系统来说,oracle建议是sga=系统内存*0.8*0.5 单位字节
pga=系统内存*0.8*0.5
目标服务器上创建相应的目录
ORACLE_BASE目录不要创建,因为数据库软件已安装。源库和目标库ORACLE_BASE目录不同采用更改目标库参数文件中ORACLE_BASE目录的方法。
窗口1:查看参数文件
cat /home/oracle/initdxj20240528.ora
窗口2:创建参数文件中的目录
mkdir -p /u01/app/oracle/admin/dxj/adump
mkdir -p /u01/app/oracle/fast_recovery_area/dxj/
mkdir -p /oradata/dxj/
mkdir -p /oradata/archivelog
恢复参数文件,将数据库启动到nomount
export ORACLE_SID=dxj
[oracle@dxj dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Fri Dec 12 20:39:56 2014
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.SQL> startup nomount pfile='/home/oracle/initdxj20240528.ora';需要对拷贝过来的pfile进行修改【由于源控制路径和ORACLE_BASE目录不一样,要手工修改到目标数据库的相关路径,并建立相关数据库目录】,不然会提示找不到adump udump等目录路径文件的。
====================================================
[oracle@dxj:/home/oracle]$ cat initdxj20240528.ora
dxj.__db_cache_size=2147483648
dxj.__java_pool_size=16777216
dxj.__large_pool_size=16777216
dxj.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
dxj.__pga_aggregate_target=671088640
dxj.__sga_target=2650800128
dxj.__shared_io_pool_size=0
dxj.__shared_pool_size=452984832
dxj.__streams_pool_size=0
*._b_tree_bitmap_plans=FALSE
*._cleanup_rollback_entries=2000
*._datafile_write_errors_crash_instance=FALSE
*._index_partition_large_extents='FALSE'
*._memory_imm_mode_without_autosga=FALSE
*._optimizer_adaptive_cursor_sharing=FALSE
*._optimizer_extended_cursor_sharing='NONE'
*._optimizer_extended_cursor_sharing_rel='NONE'
*._optimizer_null_aware_antijoin=FALSE
*._optimizer_use_feedback=FALSE
*._partition_large_extents='FALSE'
*._PX_use_large_pool=TRUE
*._undo_autotune=FALSE
*._use_adaptive_log_file_sync='FALSE'
*.audit_file_dest='/u01/app/oracle/admin/dxj/adump'
*.audit_trail='NONE'
*.compatible='11.2.0.0.0'
*.control_file_record_keep_time=31
*.control_files='/oradata/dxj/control01.ctl','/u01/app/oracle/fast_recovery_area/dxj/control02.ctl'
*.db_block_size=8192
*.db_create_file_dest='/oradata'
*.db_domain=''
*.db_files=5000
*.db_name='dxj'
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4322230272
*.deferred_segment_creation=FALSE
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=dxjXDB)'
*.enable_ddl_logging=TRUE
*.event='28401 trace name context forever,level 1','10949 trace name context forever,level 1'
*.log_archive_dest_1='location=/oradata/archivelog'
*.log_archive_format='%t_%s_%r.dbf'
*.open_cursors=1000
*.parallel_max_servers=64
*.pga_aggregate_target=659554304
*.processes=2000
*.remote_login_passwordfile='EXCLUSIVE'
*.resource_limit=TRUE
*.resource_manager_plan='force:'
*.sec_case_sensitive_logon=FALSE
*.session_cached_cursors=300
*.sga_max_size=2638217216
*.sga_target=2638217216
*.undo_retention=10800
*.undo_tablespace='UNDOTBS1'
用pfile生成spfile文件(勿忘记)
SQL> create spfile from pfile='/home/oracle/initdxj20240528.ora';
SQL> shutdown immediate;
SQL> startup nomount; --启动到nomount状态
恢复控制文件
源库和目标库的控制文件路径虽然不一样,但是在更改参数文件的参数文件位置后,恢复控制文件过程中会把控制文件恢复到已更改的参数文件指定的控制文件路径。
[oracle@dxj:/home/oracle]$ export ORACLE_SID=dxj
[oracle@dxj:/home/oracle]$ rman target /Recovery Manager: Release 11.2.0.4.0 - Production on Tue May 28 19:00:17 2024Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database: DXJ (not mounted)RMAN> restore controlfile from '/home/oracle/autobackup/emrdb_ctl_bk_092rrir9_1_1_DXJ_20240527';Starting restore at 28-MAY-24
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISKchannel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/oradata/dxj/control01.ctl
output file name=/u01/app/oracle/fast_recovery_area/dxj/control02.ctl
Finished restore at 28-MAY-24--启库到mount状态
RMAN> alter database mount;database mounted
released channel: ORA_DISK_1
恢复数据文件
该文档源库和目标库数据文件路径相同,不涉及目录转换;如果源库和目标库数据文件路径不同,该文档步骤不适用,需将控制文件中记录的数据文件位置进行转换。
补充:catalog的原理是查文件头,里面 文件号 相对文件号 scn,目的就是把信息取出来,写到ctl的rman那一部分,就是建个档 不明白
[oracle@dxj:/home/oracle]$ export ORACLE_SID=dxj
[oracle@dxj:/home/oracle]$ rman target /Recovery Manager: Release 11.2.0.4.0 - Production on Tue May 28 19:18:03 2024Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.connected to target database: DXJ (DBID=2712415422, not open)
# 将备份集信息重新导入到当前控制文件中(一定要用“/”结尾,不然可能会找不到真实的路径)
RMAN> catalog start with '/home/oracle/autobackup/';
RMAN> restore database; --还原数据文件
RMAN> recover database; --还原归档日志 提示如下报错:
....
archived log file name=/oradata/archivelog/1_8_1170009408.dbf thread=1 sequence=8
unable to find archived log
archived log thread=1 sequence=9
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 05/28/2024 19:20:14
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 9 and starting SCN of 1079971--进行不完全恢复
RMAN> recover database until sequence 9;Starting recover at 28-MAY-24
using channel ORA_DISK_1starting media recovery
media recovery complete, elapsed time: 00:00:00Finished recover at 28-MAY-24
扩展:源库和目标库数据库文件路径不一致
如果源库和目标库数据文件路径不同,需将控制文件中记录的数据文件位置进行转换
参数文件修改
修改参数文件中数据目录
.control_files中有个1个control01.ctl指向的数据目录
.db_create_file_dest指向的数据目录
.log_archive_dest_1有可能指向的数据目录,如果归档目录和数据目录独立可不修改该参数
数据文件位置转换
查看文件位置
su - oracle
export ORACLE_SID=dxj
sqlplus / as sysdba--查看数据文件存放位置
col name for a60
select name from v$datafile;
或
col name for a60
select * from v$dbfile;--查看临时文件位置
col name for a50
select file#,name from v$tempfile;--查看控制文件位置
select name from v$controlfile;--查看日志文件位置
select member from v$logfile;
查看历史redo日志 select * from v$log_history;--查看归档文件位置
select name from v$archived_log;
还原
--加载备份目录下的备份集
catalog start with '/home/oracle/autobackup/';--更改控制文件中数据文件和临时文件的名称,还原数据库
run{
set newname for datafile '/u01/app/oracle/oradata/dhh/system01.dbf' to '/data/u01/app/oracle/oradata/dhh/system01.dbf';
set newname for datafile '/u01/app/oracle/oradata/dhh/sysaux01.dbf' to '/data/u01/app/oracle/oradata/dhh/sysaux01.dbf';
set newname for datafile '/u01/app/oracle/oradata/dhh/undotbs01.dbf' to '/data/u01/app/oracle/oradata/dhh/undotbs01.dbf';
set newname for datafile '/u01/app/oracle/oradata/dhh/users01.dbf' to '/data/u01/app/oracle/oradata/dhh/users01.dbf';
set newname for datafile '/u01/app/oracle/oradata/dhh/example01.dbf' to '/data/u01/app/oracle/oradata/dhh/example01.dbf';
set newname for datafile '/u01/app/oracle/oradata/dhh/fuwa01.dbf' to '/data/u01/app/oracle/oradata/dhh/fuwa01.dbf';
set newname for tempfile '/u01/app/oracle/oradata/dhh/temp01.dbf' to '/data/u01/app/oracle/oradata/dhh/temp01.dbf';
set newname for tempfile '/u01/app/oracle/oradata/dhh/fuwatmp01.dbf' to '/data/u01/app/oracle/oradata/dhh/fuwatmp01.dbf';
restore database;
switch datafile all;
switch tempfile all;
}--恢复归档至指定路径
run{
crosscheck archivelog all;
set archivelog destination to '/data/oradata/archivelog/dhh';
restore archivelog all;
}select name from v$archived_log;--在alter database open resetlogs之前,更改contorlfile中redo的路径
alter database rename file '/u01/app/oracle/oradata/dhh/redo01.log' to '/data/oradata/dhh/redo01.log';
alter database rename file '/u01/app/oracle/oradata/dhh/redo02.log' to '/data/oradata/dhh/redo02.log';
alter database rename file '/u01/app/oracle/oradata/dhh/redo03.log' to '/data/oradata/dhh/redo03.log';
select member from v$logfile;
恢复归档日志(可选)
此次恢复不涉及该步骤。
可能在恢复的过程中归档没有及时生成,可以在最后再备份一下归档日志,或者将新产生的归档拷贝到归档的相应目录下,然后进行如下的恢复操作步骤:
可能在恢复的过程中归档没有及时生成,可以在最后再备份一下归档日志,或者将新产生的归档拷贝到归档的相应目录下,然后进行如下的恢复操作步骤:
RMAN> alter database mount; --接上面
RMAN> catalog start with '/topsoft/oracle/oradata/archivelog/';
RMAN> recover database;
read only方式打开
恢复完成后,将库启动到read only模式,查询一下数据是否正常。会发现有如下报错,同时库也关闭了,那是因为还没操作升级步骤。
idle 2024-05-28 21:28:59> alter database open read only;
ERROR:
ORA-03114: not connected to ORACLEalter database open read only
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-00704: bootstrap process failure
ORA-39700: database must be opened with UPGRADE option
Process ID: 30977
Session ID: 22 Serial number: 41
升级
startup upgrade
--执行升级操作 启库到mount状态
idle 2024-05-28 19:28:44> startup upgrade;
ORACLE instance started.Total System Global Area 2638954496 bytes
Fixed Size 2256152 bytes
Variable Size 1275069160 bytes
Database Buffers 1358954496 bytes
Redo Buffers 2674688 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
open resetlogs库
使用resetlogs选项,会把当前的日志序号(log sequence number)重设为1,并抛弃所有日志信息。在以下条件时需要使用resetlogs选项:
在不完全恢复(介质恢复);
使用备份控制文件。
使用resetlogs打开数据库后,务必要完整地进行一次数据库备份。
会发现按照提示进行open resetlogs操作也报错,报错后实例也关闭了。
idle 28-MAY-24> alter database open resetlogs;
ERROR:
ORA-03114: not connected to ORACLEalter database open resetlogs
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-00704: bootstrap process failure
ORA-39700: database must be opened with UPGRADE option
Process ID: 26049
Session ID: 1 Serial number: 5
再次startup upgrade
再次执行startup upgrade open库成功,说明startup upgrade操作会进行关库 并启库操作。
[oracle@dxj:/u01/app/oracle/product/11.2.0/db/dbs]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Tue May 28 19:59:40 2024Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to an idle instance.idle 28-MAY-24> startup upgrade;
ORACLE instance started.Total System Global Area 2638954496 bytes
Fixed Size 2256152 bytes
Variable Size 1275069160 bytes
Database Buffers 1358954496 bytes
Redo Buffers 2674688 bytes
Database mounted.
Database opened.
idle 28-MAY-24>
执行脚本
不要多窗口并行执行脚本,会引发死锁,该过程比较耗时。
@?/rdbms/admin/catalog.sql --这个脚本执行很快就结束了
@?/rdbms/admin/catproc.sql --这个脚本执行大概3-5分钟
@?/rdbms/admin/catupgrd.sql --这个脚本执行时间比较久,脚本执行结束会shutdown immediate;
脚本说明:
主要用于创建数据字典视图。其中,脚本catalog.sql 和 catproc.sql 是创建数据库后必须要运行的两个脚本。
catalog.sql 创建系统常用的数据字典视图和同义词
catproc.sql 运行服务器端所需要的PL/SQL脚本
(1) catalog.sql
该脚本创建相对于系统基表的视图和系统动态性能视图以及他们的同义词。该脚本又同时运行创建其他对象的脚本,主要有:
基本PL/SQL环境,包括PL/SQL的声明:
- 数据类型
- 预定义例外
- 内建的过程和函数
- SQL操作等
- 审计
- 导入/导出
- SQL*Loader
- 安装选项
(2)catproc.sql
该脚本主要用于建立PL/SQL功能的使用环境。此外,还创建几个PL/SQL包用于扩展RDBMS功能。该脚本同时还为下面的一些RDBMS功能创建另外的一些包和视图:
-警告(Alerts)
-管道(Pipes)
-日志分析(LogMiner)
-大对象(Large objects)
-对象(Objects)
- 高级队列(Advanced queuing)
-复制选项( Replication option)
-其他的一些内建包和选项(Other built-ins and options)
(3) catupgrd.sql
随着Oracle版本的升级,某些对象的属性需要改变,而这些改变操作都将体现在升级脚本catupgrd.sql中
启库
idle 29-MAY-24> startup
ORACLE instance started.Total System Global Area 2638954496 bytes
Fixed Size 2256152 bytes
Variable Size 1476395752 bytes
Database Buffers 1157627904 bytes
Redo Buffers 2674688 bytes
Database mounted.
Database opened.
查看版本
idle 29-MAY-24> select * from v$version;BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
恢复密码文件
将拷贝过来的密码文件恢复至数据库对应目录下:
[oracle@dxj:/home/oracle/]$ cp /home/oracle/orapwdxj /u01/app/oracle/product/11.2.0/db/dbs/orapwdxj
或者手动创建密码文件:
orapwdfile='/u01/app/oracle/product/11.2.0/db/dbs/orapwdxj' password=oracle entries=30 force=y
数据量查看
--查看数据库
SQL> select sum(bytes)/1024/1024/1024 as "size(G)" from dba_data_files;size(G)
----------
1.70410156--每个业务用户下的总对象数量校验
SELECT D.OWNER,COUNT(1)FROM dba_objects dWHERE d.OWNER in ('FUWA')AND NOT EXISTS (SELECT 1 FROM DBA_RECYCLEBIN B WHERE B.object_name=D.OBJECT_NAME AND D.OWNER=B.owner) GROUP BY D.OWNERORDER BY D.OWNER ;OWNER COUNT(1)
------------------------------ ----------
FUWA 1--每个业务用户下的各个对象类别的数量校验
SQL> select OWNER,OBJECT_TYPE,status,count(OBJECT_NAME) from dba_objects where owner in ('FUWA') group by OBJECT_TYPE,owner,status order by 1,3,2;OWNER OBJECT_TYPE STATUS COUNT(OBJECT_NAME)
------------------------------ ------------------- ------- ------------------
FUWA TABLE VALID 1
问题处理
本实验文档未出现以下报错,如果备份恢复中遇到以下报错可参考以下解决办法。
ORA-27102: out of memory
--问题描述
SQL> startup nomount pfile='/topsoft/newbak/initsyxk20240125.ora';
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device
Additional information: -939524096
Additional information: 1--问题原因
由于目标服务器32G和源服务器内存128G差异大,需要更改内存参数。
参数文件中sga pga参数太大--解决办法
--sga和pga配置的最佳实践
对于OLTP系统来说,oracle建议是sga=系统内存*0.8*0.8 单位字节
pga=系统内存*0.8*0.2
对于DSS系统来说,oracle建议是sga=系统内存*0.8*0.5 单位字节
pga=系统内存*0.8*0.5更改参数文件中的关于内存的参数
recover database提示unable to find archived log
--问题描述
还原归档文件时间报错
RMAN> recover database;Starting recover at 25-JAN-24
using channel ORA_DISK_1starting media recoverychannel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=16
channel ORA_DISK_1: reading from backup piece /topsoft/newbak/autobackup/arch_SYXK_20240125_1159207499_60_1.bak
channel ORA_DISK_1: piece handle=/topsoft/newbak/autobackup/arch_SYXK_20240125_1159207499_60_1.bak tag=ARCH
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/topsoft/oracle/oradata/archivelog/1_16_1155666473.dbf thread=1 sequence=16
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=17
channel ORA_DISK_1: reading from backup piece /topsoft/newbak/autobackup/arch_SYXK_20240125_1159207499_61_1.bak
channel ORA_DISK_1: piece handle=/topsoft/newbak/autobackup/arch_SYXK_20240125_1159207499_61_1.bak tag=ARCH
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/topsoft/oracle/oradata/archivelog/1_17_1155666473.dbf thread=1 sequence=17
unable to find archived log
archived log thread=1 sequence=18
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 01/25/2024 19:54:41
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 18 and starting SCN of 2895113--解决办法
--进行不完全恢复
RMAN> recover database until sequence 18;Starting recover at 25-JAN-24
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=2271 device type=DISKstarting media recovery
media recovery complete, elapsed time: 00:00:00Finished recover at 25-JAN-24--恢复完成后,将库启动到read only模式,查询一下数据是否正常
SQL> alter database open read only;Database altered.--对比数据量无问题后
SQL> shutdown immediate;--resetlogs方式打开
SQL> alter database open resetlogs;
Database altered.--查看数据库状态
SQL> select status from v$instance;STATUS
------------
OPEN
最后小版本升级完成,且数据验证无问题。
概念补充
完全恢复:数据文件+归档+在线redo,就能做实例恢复 这一级别就是完全恢复。ctl scn 、 datafile scn 、datafile head scn一致
不完全恢复:数据文件+归档 只能到 归档的最后一条redo,不能实例恢复 就是不完全恢复
restore 解决 datafile
recover 解决实例恢复
catalog就是把三者注册到 ctl ,自动完成
如果你只注册前两个部分,那么第三个部分就要手工指定
recover database until cancel时 把当前redo的路径写上就行了
最后会显示下一个redo sequence号,这个号其实是不存在的,因为还没产生,oracle是机器,它只会让你找最全的redo,向前恢复
--实例恢复
老备份+归档+在线redo=内存中的oracle
关机了 归档+在线全都不需要
在线redo的存在,解决数据文件和内存不一致
归档,解决老备份和现有数据文件不一致
--文件恢复
recover
restore
rman备份恢复参考链接:
Oracle 11G RMAN备份恢复到异机数据库_oracle 11g 数据库备份并恢复到另一台机器-CSDN博客
https://www.cnblogs.com/yabingshi/p/3812920.html
https://www.cnblogs.com/plutozzl/p/13497908.html
小版本升级参考链接:
https://blog.51cto.com/yangbing/1696782
单机11.2.0.1恢复到RAC 11.2.0.4的案例-CSDN博客