目录
DG环境介绍一、备库执行备份二、在新主机执行恢复操作 2.1、恢复spfile 2.2、恢复控制文件 2.3、注册备份信息 2.4、还原数据文件 2.5、恢复数据库 2.6、激活备库为主库,并启动数据库
在部署完ADG(Active Data Guard)后,不但可以将只读的查询交给备库执行,还可以把日常的数据库备份工作放在备库上执行,从而减轻主库的压力,充分的发挥服务器资源。 本文演示的是利用Oracle DG环境的备库执行备份,然后异机还原恢复成一个新的主库的过程。
DG环境介绍
项目 | primary db | physical standby db | 新库 |
---|---|---|---|
数据库类型(rac或单实例) | 单实例 |
||
数据库版本 | 11.2.0.3.0 |
||
platform_name | Linux x86 64-bit |
||
ORACLE_SID | oradg11g | oradgphy | LHRDB |
db_name/GLOBAL_DBNAME | oradg11g |
||
db_unique_name | oradg11g | oradgphy | LHRDB |
ORACLE_HOME | /u01/app/oracle/product/11.2.0/dbhome_1 |
一、备库执行备份
1cat > /rman/rman_backup_oradgphy_full.sh <<"EOF0"
2#!/bin/ksh
3
4export ORACLE_SID=oradgphy
5export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
6export PATH=$ORACLE_HOME/bin:$PATH
7export NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
8
9MYDATE=`date +'%Y%m%d%H%M%S'`
10
11rman target / log /rman/backup_oradgphy_full_$MYDATE.log append <<EOF
12run {
13sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
14sql 'alter session set NLS_LANGUAGE="AMERICAN"';
15allocate channel c1 type disk;
16allocate channel c2 type disk;
17allocate channel c3 type disk;
18allocate channel c4 type disk;
19allocate channel c5 type disk;
20allocate channel c6 type disk;
21allocate channel c7 type disk;
22allocate channel c8 type disk;
23allocate channel c9 type disk;
24allocate channel c10 type disk;
25allocate channel c11 type disk;
26allocate channel c12 type disk;
27
28backup as compressed backupset FILESPERSET 10 database format '/rman/FULL_%d_%U.full' section size 100G;
29
30backup as compressed backupset archivelog from time 'sysdate-1' format '/rman/ARC_%d_%U.arc' section size 100G;
31
32backup current controlfile format '/rman/standby_%U.ctl';
33
34backup spfile format '/rman/spfile_%d_%U.ora';
35
36release channel c1;
37release channel c2;
38release channel c3;
39release channel c4;
40release channel c5;
41release channel c6;
42release channel c7;
43release channel c8;
44release channel c9;
45release channel c10;
46release channel c11;
47release channel c12;
48 }
49EOF
50EOF0
赋予可执行权限:
1chmod +x /rman/rman_backup_oradgphy_full.sh
执行备份,或添加定时任务:
1nohup sh /rman/rman_backup_oradgphy_full.sh &
备份结果:数据库790G(非分配大小),压缩备份后140G,用时30分钟。
接下来就是把/rman备份目录下的内容都拷贝到新主机上,方法很多,例如scp、磁盘卸载重新挂载等。
二、在新主机执行恢复操作
2.1、恢复spfile
1export ORACLE_SID=LHRDB
2rman target /
3startup nomount;
4restore spfile to pfile '?/dbs/initLHRDB.ora' from '/rman/spfile_ORADG11G_3lv6cd9m_1_1.ora';
5
6-- 修改pfile,去除dg相关参数
7vi $ORACLE_HOME/dbs/initLHRDB.ora
-
需要根据情况对pfile做相关的修改,最终的参数文件内容:
1*.audit_file_dest='/u01/app/oracle/admin/LHRDB/adump'
2*.audit_trail='db'
3*.compatible='11.2.0.0.0'
4*.control_files='/u01/app/oracle/oradata/LHRDB/crontal01.ctl','/u01/app/oracle/oradata/LHRDB/control02.ctl'
5*.db_block_size=8192
6*.db_domain=''
7*.db_name='oradg11g'
8*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
9*.db_recovery_file_dest_size=4322230272
10*.db_unique_name='LHRDB'
11*.diagnostic_dest='/u01/app/oracle'
12*.dispatchers='(PROTOCOL=TCP) (SERVICE=oradg11gXDB)'
13*.log_archive_dest_1='LOCATION=USE_DB_RECOVERY_FILE_DEST'
14*.memory_max_target=209715200
15*.memory_target=209715200
16*.open_cursors=300
17*.processes=3000
18*.remote_login_passwordfile='EXCLUSIVE'
19*.undo_tablespace='UNDOTBS1' -
根据pfile文件内容创建相关目录
1mkdir -p /u01/app/oracle/admin/LHRDB/adump
2mkdir -p /u01/app/oracle/oradata/LHRDB/ -
根据pfile创建spfile,并启动到nomout
1create spfile from pfile;
2startup force nomount
2.2、恢复控制文件
1-- 因为要恢复为主库,所以需要加上primary关键字
2restore primary controlfile from '/rman/standby_3kv6cd9k_1_1.ctl';
2.3、注册备份信息
1-- 启动到mout阶段
2alter database mount;
3
4-- 清除之前的备份信息
5EXECUTE DBMS_BACKUP_RESTORE.RESETCFILESECTION(11); /** CLEAR V$ARCHIVED_LOG */
6EXECUTE DBMS_BACKUP_RESTORE.RESETCFILESECTION(12); /** CLEAR V$BACKUP_SET */
7EXECUTE DBMS_BACKUP_RESTORE.RESETCFILESECTION(13); /** CLEAR V$BACKUP_PIECE */
8
9--重新注册,注意路径最后一定需要加上/
10catalog start with '/rman/';
2.4、还原数据文件
1# 创建日志路径
2mkdir -p /home/oracle/rman_log/
3chown oracle.dba /home/oracle/rman_log/
4
5
6cat > /home/oracle/rman_restore_LHRDB.sh <<"EOF0"
7#!/bin/ksh
8
9export ORACLE_SID=LHRDB
10export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
11export PATH=$ORACLE_HOME/bin:$PATH
12
13MYDATE=`date +'%Y%m%d%H%M%S'`
14export NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
15
16rman target / log /home/oracle/rman_log/rman_restore_LHRDB_$MYDATE.log append <<EOF
17run {
18sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
19sql 'alter session set NLS_LANGUAGE="AMERICAN"';
20allocate channel c1 type disk;
21allocate channel c2 type disk;
22allocate channel c3 type disk;
23allocate channel c4 type disk;
24allocate channel c5 type disk;
25allocate channel c6 type disk;
26allocate channel c7 type disk;
27allocate channel c8 type disk;
28allocate channel c9 type disk;
29allocate channel c10 type disk;
30allocate channel c11 type disk;
31allocate channel c12 type disk;
32
33SET NEWNAME FOR DATABASE TO '/u01/app/oracle/oradata/LHRDB/%b';
34restore database;
35switch datafile all;
36
37release channel c1;
38release channel c2;
39release channel c3;
40release channel c4;
41release channel c5;
42release channel c6;
43release channel c7;
44release channel c8;
45release channel c9;
46release channel c10;
47release channel c11;
48release channel c12;
49}
50EOF
51EOF0
开始后台执行还原:
1chmod +x /home/oracle/rman_restore_LHRDB.sh
2nohup sh /home/oracle/rman_restore_LHRDB.sh &
还原时间大概2小时。
2.5、恢复数据库
首先使用如下命令找到最大的日志序列号:
1list backupset of archivelog from time "sysdate - 1";
准备恢复数据库的脚本:
1cat > /home/oracle/rman_recover_LHRDB.sh <<"EOF0"
2#!/bin/ksh
3
4export ORACLE_SID=LHRDB
5export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
6export PATH=$ORACLE_HOME/bin:$PATH
7
8MYDATE=`date +'%Y%m%d%H%M%S'`
9export NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
10
11rman target / log /home/oracle/rman_log/rman_recover_LHRDB_$MYDATE.log append <<EOF
12run{
13sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
14sql 'alter session set NLS_LANGUAGE="AMERICAN"';
15allocate channel c1 type disk;
16allocate channel c2 type disk;
17allocate channel c3 type disk;
18allocate channel c4 type disk;
19allocate channel c5 type disk;
20allocate channel c6 type disk;
21allocate channel c7 type disk;
22allocate channel c8 type disk;
23allocate channel c9 type disk;
24allocate channel c10 type disk;
25allocate channel c11 type disk;
26allocate channel c12 type disk;
27
28set until sequence 230 thread 1;
29recover database;
30
31release channel c1;
32release channel c2;
33release channel c3;
34release channel c4;
35release channel c5;
36release channel c6;
37release channel c7;
38release channel c8;
39release channel c9;
40release channel c10;
41release channel c11;
42release channel c12;
43}
44EOF
45EOF0
开始后台执行恢复:
1chmod +x /home/oracle/rman_recover_LHRDB.sh
2nohup sh /home/oracle/rman_recover_LHRDB.sh &
恢复时间大概30分钟。
2.6、激活备库为主库,并启动数据库
1-- 1、查询数据库状态
2select open_mode , database_role, flashback_on from v$database;
3
4-- 2、激活备库为主库,如果已经是主库了,那就跳过该步骤
5alter database activate standby database;
6-- ALTER DATABASE CLEAR LOGFILE GROUP 4;
7-- alter database drop logfile group 4;
8
9-- 3、启动数据库
10alter database open resetlogs;
结果:
1SYS@LHRDB > alter database open resetlogs;
2
3Database altered.
4
5SYS@LHRDB > archive log list;
6Database log mode Archive Mode
7Automatic archival Enabled
8Archive destination USE_DB_RECOVERY_FILE_DEST
9Oldest online log sequence 1
10Next log sequence to archive 1
11Current log sequence 1
12
13SYS@LHRDB > select open_mode , database_role, flashback_on from v$database;
14
15OPEN_MODE DATABASE_ROLE FLASHBACK_ON
16-------------------- ---------------- ------------------
17READ WRITE PRIMARY NO
本文结束。