OCP考点实战演练01-备份恢复篇

时间:2024-04-05 08:03:51

本系列宗旨:真正掌握OCP考试中所考察的技能,坚决不做Paper OCP!

实验环境:RHEL 6.4 + Oracle 11.2.0.4

OCP考点实战演练01-备份恢复篇

1.数据库开启归档

```
#正常关闭数据库之后,启动数据库到mount状态,开启数据库归档
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
SQL> archive log list
```

2.配置数据库的可恢复性

## 2.1 闪回恢复区 ##
```
#查询闪回恢复区的整体使用情况
SQL> select * from v$recovery_file_dest;

查询闪回恢复区的详细使用情况

SQL> select * from v$flash_recovery_area_usage;

## 2.2 配置多个归档日志文件目标 ##
11g推荐使用log_archive_dest_n来配置多个归档日志文件目标:

设定两个本地归档文件目标

SQL> alter system set log_archive_dest_1 = 'LOCATION=/u01/oradb_arch1';

SQL> alter system set log_archive_dest_2 = 'LOCATION=/u01/oradb_arch2';

问题:定义了log_archive_dest_1 就不再使用闪回恢复区了?
实验证明:定义了log_archive_dest_1后,就不会再使用闪回恢复区存放归档日志。 问题:多个归档目标是否需要都成功?
实验证明:有一个定义归档最小成功路径数量,LOG_ARCHIVE_MIN_SUCCEED_DEST参数。需要注意,如果某个归档目标是MANDATORY的,那么该归档目标必须归档成功。

alter system set log_archive_min_succeed_dest=2;

## 2.3 保留策略 ##

保留策略设定为2份冗余

RMAN> CONFIGURE RETENTION POLICY TO REDUNDANCY 2;

保留策略设定为4天恢复窗口

RMAN> CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 4 DAYS;

<h1 id="3">3.RMAN恢复目录</h1>
参考:
- [RMAN Catalog创建、配置和管理](http://www.cnblogs.com/jyzhao/p/4864694.html) <h1 id="4">4.创建RMAN备份</h1>
完整备份,完全备份,增量备份[0级备份|1级差异(默认)|1级累积(cumulative)]
## 4.1 FORMAT子句中的替代变量 ##

format 'rman_%d_%T_%s_%p.backupset'

这样的备份集名称最易识别,生成文件名示例:rman_ORADB_20151030_27_1.backupset

如果多重备份,加一个%c

即: %d_%T_%s_%p_%c

## 4.2 完全备份 ##

备份users表空间(备份集)

RMAN> backup as backupset format '/u01/oradb_bak/rman_%d_%T_%s_%p.backupset' tablespace users;

不显示指定as backupset, 默认也是备份集形式备份

RMAN> backup format '/u01/oradb_bak/rman_%d_%T_%s_%p.backupset' tablespace users;

可以备份为压缩的备份集,节省空间,但同时备份和恢复时会消耗CPU资源压缩和解压。

RMAN> backup as compressed backupset tablespace system;

## 4.3 增量备份 ##

0级备份

RMAN> backup incremental level 0 tablespace users;

1级差异备份

RMAN> backup incremental level 1 tablespace users;

1级累积备份

RMAN> backup incremental level 1 cumulative tablespace users;

0级copy

RMAN> backup incremental level 0 as copy tablespace system;

**块更改跟踪文件**

开启块更改跟踪

SQL> alter database enable block change tracking;

查看块更改跟踪文件

SQL> select filename from v$block_change_tracking;

关闭块更改跟踪

SQL> alter database disable block change tracking;

## 4.4 多重备份 ##

备份为2份副本

RMAN> backup as compressed backupset copies 2 tablespace users format '/u01/oradb_bak/rman_bak01_%d_%T_%s_%p.backupset', '/u01/oradb_bak/rman_bak02_%d_%T_%s_%p.backupset';

RMAN> backup as compressed backupset tablespace users format '/u01/oradb_bak/rman_bak01_%d_%T_%s_%p_%c.backupset', '/u01/oradb_bak/rman_bak02_%d_%T_%s_%p_%c.backupset';

**配置磁盘通道的默认路径**

RMAN> configure channel device type disk format '/u01/oradb_bak/rman01_%U.backupset', '/u01/oradb_bak/rman02_%U.backupset';

## 4.5 备份集的备份 ##

RMAN> backup device type disk backupset all;

保存时间超过2周的备份集都会备份在磁带上,并且从磁盘上删除它们。

RMAN> backup device type sbt backupset completed before 'sysdate-14' delete input;

**只读表空间备份**
如果优化选项没有开启,默认是备份只读表空间的,但可以强制RMAN忽略只读表空间的备份,指定`SKIP READONLY`选项。
## 4.6 创建长期保留的归档备份 ##
11g版本的新特性:

保存1年的

RMAN> backup as compressed backupset database format '/u01/oradb_bak/archbak_%d_%T_%p_%s_%c' tag save1year keep until time 'sysdate+365';

备份输出信息可以清楚看到backup will be obsolete on date 2016-11-01 10:28:48类似这样的提示信息。也就是一年后才被标记为obsolete。

保存永久的

RMAN> backup as compressed backupset database format '/u01/oradb_bak/archbak_%d_%T_%p_%s_%c' tag saveforever keep forever;

同样,备份输出信息可以看到backup will never be obsolete这样的提示信息。也就是永远都不会被标记为obsolete。

修改归档备份的保留期限

RMAN> change backup tag 'SAVEFOREVER' nokeep;

RMAN> change backup tag 'SAVEFOREVER' keep forever;

change其他用法,例如归档日志路径变更

RMAN> HOST '/bin/mv $ORACLE_HOME/dbs/*.arc /disk2/archlog/';

RMAN> CHANGE ARCHIVELOG ALL UNCATALOG;

RMAN> CATALOG START WITH '/disk2/archlog' NOPROMPT;

特别注意:不可以使用闪回恢复区来存储归档备份。
## 4.7 创建多段、压缩和加密备份 ##
**11g新特性:多段备份**
适用于大文件表空间的备份

RMAN> backup tablespace users section size 1M;

注:同时可以分配多个磁盘和磁带通道,以并行方式备份每段数据文件。
验证备份也可以指定`section size`

RMAN> validate tablespace users;

RMAN> validate tablespace users section size 1M;

相关数据字典视图

目标数据库

v$backup_set

v$backup_datafile

恢复目录

rc_backup_set

rc_backup_datafile

**压缩备份**

CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default

**加密备份**
分为三种:透明加密、密码加密、双模式加密。
主要介绍下透明加密:
a) 配置sqlnet.ora文件

ENCRYPTION_WALLET_LOCATION = (SOURCE =

(METHOD = FILE)

(METHOD_DATA =

(DIRECTORY =

/etc/ORACLE/WALLETS/oracle)))

b) 创建wallet

mkdir -p /etc/ORACLE/WALLETS/oracle

chown -R oracle:oinstall /etc/ORACLE

--创建wallet

--第一种方法:orapki wallet create -wallet wallet_location [-pwd password]

--第二种方法:orapki wallet create -wallet wallet_location -auto_login [-pwd password]

$ orapki wallet create -wallet /etc/ORACLE/WALLETS/oracle -pwd MyWalletPWD1

c) 打开/关闭wallet,Set Master Key

--打开wallet

ALTER SYSTEM SET ENCRYPTION WALLET OPEN IDENTIFIED BY "MyWalletPWD1";

--关闭wallet

ALTER SYSTEM SET ENCRYPTION WALLET CLOSE IDENTIFIED BY "MyWalletPWD1";

--Master key

ALTER SYSTEM SET ENCRYPTION KEY IDENTIFIED BY "MyWalletPWD1";

d) RMAN透明加密备份

-- 打开加密功能

RMAN> CONFIGURE ENCRYPTION FOR DATABASE ON;

//RMAN> set encryption identified by "MyWalletPWD1";

-- 加密备份users表空间

RMAN> backup as compressed backupset tablespace users;

-- 关闭加密功能

RMAN> CONFIGURE ENCRYPTION FOR DATABASE OFF;

## 4.8 备份报告和维护 ##

-- list

list backup summary;

list backup;

list script names;

-- report

report need backup;

report schema;

-- delete

delete obsolete;

delete backup;

delete backupset XXXXXXX;

-- crosscheck & delete

crosscheck backup;

delete expired;

<h1 id="5">5.使用RMAN备份进行恢复</h1>
参考:
- [Oracle Recovery 01 - 常规恢复之完全恢复](http://www.cnblogs.com/jyzhao/p/4710227.html)
- [Oracle Recovery 02 - 常规恢复之不完全恢复](http://www.cnblogs.com/jyzhao/p/4723994.html) 其他脚本、相关视图、命令关键字:

-- run块脚本示例

run {

recover copy of database

with tag 'inc_upd';

backup incremental level 1

for recover of copy

with tag 'inc_upd'

database;

}

-- 恢复常用视图

v$datafile

v$datafile_header

v$recover_file

v$tablespace

-- 恢复相关命令关键字

offline

online

switch

restore

recover

resetlogs

<h1 id="6">6.用户管理的备份和恢复</h1>
## 6.1 丢失临时文件 ##
如果不停机,那么增加新的临时文件,删除旧的临时文件。

select file#, name from v$tempfile;

alter tablespace temp add tempfile '/u01/oradata/ORADB/datafile/temp01.dbf' size 30M;

alter tablespace temp drop tempfile '/u01/oradata/ORADB/datafile/o1_mf_temp_c2v10px4_.tmp';

select file#, name from v$tempfile;

如果停机,那么重启库时会自动创建临时文件(前提是存放临时文件的目录还可用且有读写权限)。
当然,丢失临时文件是没有必要停机恢复的,完全可以在线来做。
## 6.2 丢失重做日志组(重点) ##
参考:
- [Oracle丢失重做日志的几种场景恢复](http://www.cnblogs.com/jyzhao/p/4935592.html) ## 6.3 丢失口令文件 ##
orapwd命令重建口令文件

$ orapwd

Usage: orapwd file= entries= force=<y/n> ignorecase=<y/n> nosysdba=<y/n>

where

file - name of password file (required),

password - password for SYS will be prompted if not specified at command line,

entries - maximum number of distinct DBA (optional),

force - whether to overwrite existing file (optional),

ignorecase - passwords are case-insensitive (optional),

nosysdba - whether to shut out the SYSDBA logon (optional Database Vault only).

There must be no spaces around the equal-to (=) character.

## 6.4 查看恢复相关视图 ##
**6.4.1 需要恢复的文件**

set linesize 1000

select file#, d.name d_name, t.name t_name, status, error

from v$recover_file r

join v$datafile d using(file#)

join v$tablespace t using(ts#);

结果示例:
 FILE# D_NAME                                                             T_NAME                         STATUS  ERROR

     5 /u01/oradata/ORADB/datafile/o1_mf_tbs_test_c3mdft5o_.dbf           TBS_TEST                       ONLINE  FILE NOT FOUND
**6.4.2 查看恢复文件需要的归档日志**

SQL> select archive_name from v$recovery_log;

## 6.5 备份users表空间 ##

alter tablespace users begin backup;

!cp /xxxx/users01.dbf /xxxx/users01.dbf.bak

alter tablespace users end backup;

<h1 id="7">7.其他RMAN功能</h1>
## 7.1 使用RMAN创建复制数据库 ##
参考:
- [使用RMAN创建复制数据库](http://www.cnblogs.com/jyzhao/p/4939901.html) ## 7.2 表空间基于时间点的恢复(TSPITR) ##
参考:
- [表空间基于时间点的恢复(TSPITR)](http://www.cnblogs.com/jyzhao/p/4956220.html) <h1 id="8">8.监视和调整RMAN</h1>
## 8.1 监视RMAN会话和作业 ##

--v$session和v$process

select sid, spid, client_info

from v$process p join v$session s on (p.addr = s.paddr)

where client_info like '%rman%';

--run 块中可以指定command id ,便于区分。

run {

set command id to 'backup tbs users';

backup tablespace users;

}

--v$session_longops

select sid, serial#, opname, context, sofar, totalwork

from v$session_longops

where opname like 'RMAN%'

and sofar <> totalwork;

## 8.2 RMAN会话的调试 ##
示例:rman debug trace进入RMAN,只在备份表空间dbs_d_test时,开启debug,生成跟踪文件rman_debug.txt。

rman target / debug trace rman_debug.txt

run {

debug off;

backup tablespace dbs_d_jingyu;

debug on;

backup tablespace dbs_d_test;

}

执行过程输出:

$ rman target / debug trace rman_debug.txt

Recovery Manager: Release 11.2.0.4.0 - Production on Thu Nov 12 16:53:36 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.

RMAN-06005: connected to target database: ORADB (DBID=2647366668)

RMAN> run {

2> debug off;

3> backup tablespace dbs_d_jingyu;

4> debug on;

5> backup tablespace dbs_d_test;

6> }

Debugging turned off

Starting backup at 2015-11-12 16:54:33

using target database control file instead of recovery catalog

allocated channel: ORA_DISK_1

channel ORA_DISK_1: SID=47 device type=DISK

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00005 name=/u01/oradata/ORADB/datafile/o1_mf_dbs_d_ji_c45slpl1_.dbf

channel ORA_DISK_1: starting piece 1 at 2015-11-12 16:54:34

channel ORA_DISK_1: finished piece 1 at 2015-11-12 16:54:35

piece handle=/u01/oradb_bak/rman_ORADB_20151112_184_1_1.backupset tag=TAG20151112T165434 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 2015-11-12 16:54:36

Starting Control File and SPFILE Autobackup at 2015-11-12 16:54:36

piece handle=/u01/app/oracle/fast_recovery_area/ORADB/autobackup/2015_11_12/o1_mf_s_895596876_c48nyd6l_.bkp comment=NONE

Finished Control File and SPFILE Autobackup at 2015-11-12 16:54:37

RMAN-03036: Debugging set to level=9, types=ALL

RMAN-03090: Starting backup at 2015-11-12 16:54:37

RMAN-12016: using channel ORA_DISK_1

RMAN-08008: channel ORA_DISK_1: starting full datafile backup set

RMAN-08010: channel ORA_DISK_1: specifying datafile(s) in backup set

RMAN-08522: input datafile file number=00007 name=/u01/oradata/ORADB/datafile/o1_mf_dbs_d_te_c45ssxmd_.dbf

RMAN-08038: channel ORA_DISK_1: starting piece 1 at 2015-11-12 16:54:37

RMAN-08044: channel ORA_DISK_1: finished piece 1 at 2015-11-12 16:54:38

RMAN-08530: piece handle=/u01/oradb_bak/rman_ORADB_20151112_186_1_1.backupset tag=TAG20151112T165437 comment=NONE

RMAN-08540: channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

RMAN-03091: Finished backup at 2015-11-12 16:54:38

RMAN-03090: Starting Control File and SPFILE Autobackup at 2015-11-12 16:54:38

RMAN-08503: piece handle=/u01/app/oracle/fast_recovery_area/ORADB/autobackup/2015_1

RMAN> exit

Recovery Manager complete.

[oracle@JY-DB01 ~]$ pwd

/home/oracle

[oracle@JY-DB01 ~]$ ls -lh rman*

-rw-r--r--. 1 oracle oinstall 178K Nov 12 16:54 rman_debug.txt

## 8.3 调整RMAN ##
**确定备份和还原的步骤**
RMAN备份在通道中执行任务时,经历3个主要阶段:
- 读阶段:通道将数据块读入输入缓冲区。
- 复制阶段:通道将块从输入缓冲区复制到输出缓冲区,并根据需要执行其他处理(验证、压缩、加密都在此阶段)。
- 写阶段:通道将输出缓冲区中的块写入到磁盘或磁带。 每天或或每小时创建一次映像副本会增加备份时间,但却可以极大的减少恢复时间。
**并行执行备份集**
分配多个通道,分配的通道数量不得超过物理设备的数量。还可以给每个数据文件分配特定通道。

run {

allocate channel c1 device type disk;

allocate channel c2 device type disk;

allocate channel c3 device type disk;

backup incremental level 0

(datafile 1,2,9 channel c1)

(datafile 3,8,7 channel c2)

(datafile 4,5,6 channel c3)

as compressed backupset;

}

最多为每个RMAN会话分配225个通道,每个通道最多可以并行读取64个数据文件。
**RMAN多路复用操作**
主要通过FILESPERSET和MAXOPENFILES控制多路复用。
可用公式min(MAXOPENFILES,min(FILESPERSET,files_per_channel))
注:不能多路复用映像副本。
**调整RMAN通道**
使用MAXPIECESIZE,RATE和MAXOPENFILES参数调整RMAN通道。
**调整BACKUP命令**
使用MAXPIECESIZE,FILESPERSET,MAXOPENFILES和BACKUP DURATION参数调整BACKUP命令。
可以将BACKUP参数BACKUP DURATION设置为MINIMIZE TIME以便尽量加快备份速度,也可以将此参数设置为MINIMIZE LOAD以减少数据库的I/O请求。
**配置LARGE_POOL_SIZE**
可以配置初始化参数LARGE_POOL_SIZE降低RMAN备份对共享池的争用。
## 8.4 为异步I/O配置RMAN ##
**了解异步I/O和同步I/O**
同步I/O必须等到完成一个I/O请求后才能启动另一个I/O请求。异步备份操作无需等待。
将初始化参数BACKUP_TAPE_IO_SLAVES设置为TURE,以便为异步操作配置磁带备份。
初始化参数DBWR_IO_SLAVES设置时,会模拟异步I/O操作。
**监视异步I/O**
v$backup_async_io
LONG_WAITS与IO_COUNT的比率越低越好;
SHORT_WAIT_TIME_TOTAL或LONG_WAIT_TIME_TOTAL正常值应为0。
**监视同步I/O**
v$backup_sync_io
DISCRETE_BYTES_PER_SECOND列与磁带输出设备的最大比率做对比,以便确定调整时机。 <h1 id="9">9.配置和使用闪回</h1>
参考:
- [Oracle配置和使用闪回](http://www.cnblogs.com/jyzhao/p/4968804.html) <h1 id="10">Reference</h1>
- OCP 认证考试指南 (1Z0-053)[M]. 清华大学出版社, 2010.