第五课 数据备份恢复实验
一 模拟Control File 丢失故障处理(JCH1实例)
数据库中必要文件:数据文件、控制文件、redo、??文件?、参数文件某些情况可以不用
删除控制文件
启动报错!
shutdown immediate 停掉数据库
使用完好的控制文件恢复丢失的控制文件
cp control02.ctl ../disk1/control01.ctl
重启数据库
startup
二 创建CATALOG库保存RMAN备份信息
1.catalog库统一管理RMAN备份信息即元数据信息,如果没有catalog库,就把RMAN备份信息写入控制文件
2.目标数据库必须注册到catalog库中才能使用
官方文档:Backup and Recovery Reference -> CREATE CATALOG和REGISTER
Backup and Recovery Advanced User’s Guide -> 10 Managing the Recovery Catalog -> Creating a Recovery Catalog
注:JCH1是目标数据库
JCH2是CATALOG数据库
1.创建表空间 这个表空间存放catalog库内容
sqlplus sys/oracle@LEO2 as sysdba
create tablespace catalog_tbs datafile '/u01/app/oracle/oradata/JCH2/catalog_tbs01.dbf' size 100m autoextend on;
查看是否创建成功:
2.创建catalog库管理用户catalog_admin默认表空间catalog_tbs
create user catalog_admin identified by catalog_admin default tablespace catalog_tbs;
SYS@JCH2> create user catalog_admin identified by catalog_admin default tablespace catalog_tbs;
User created.
3.将recovery_catalog角色授予给catalog_admin用户才能备份和恢复
注:有的同学会问,我直接授予DBA角色不就可以了吗。其实DBA角色不是万能的,它不包括上述角色因此我们需要单独设置
select * from dba_roles where role like '%RECOVER%'; 有没有这个角色
select * from role_sys_privs where role='RECOVERY_CATALOG_OWNER'; 这个角色包含哪些权限
SYS@JCH2> select * from dba_roles where role like '%RECOVER%';
ROLE PASSWORD
------------------------------ --------
RECOVERY_CATALOG_OWNER NO
SYS@JCH2> select * from role_sys_privs where role='RECOVERY_CATALOG_OWNER';
ROLE PRIVILEGE ADM
------------------------------ ---------------------------------------- ---
RECOVERY_CATALOG_OWNER CREATE SYNONYM NO
RECOVERY_CATALOG_OWNER CREATE CLUSTER NO
RECOVERY_CATALOG_OWNER ALTER SESSION NO
RECOVERY_CATALOG_OWNER CREATE DATABASE LINK NO
RECOVERY_CATALOG_OWNER CREATE SESSION NO
RECOVERY_CATALOG_OWNER CREATE TABLE NO
RECOVERY_CATALOG_OWNER CREATE SEQUENCE NO
RECOVERY_CATALOG_OWNER CREATE PROCEDURE NO
RECOVERY_CATALOG_OWNER CREATE VIEW NO
RECOVERY_CATALOG_OWNER CREATE TYPE NO
RECOVERY_CATALOG_OWNER CREATE TRIGGER NO
11 rows selected.
SYS@JCH2> grant connect,resource,recovery_catalog_owner to catalog_admin;
Grant succeeded.
grant connect,resource,recovery_catalog_owner to catalog_admin; 只需这三个角色即可
SYS@JCH2> grant connect,resource,recovery_catalog_owner to catalog_admin;
Grant succeeded.
4.登录RMAN创建CATALOG库
rman catalog catalog_admin/catalog_admin@JCH2
create catalog tablespace catalog_tbs; 把catalog_tbs表空间指定为catalog库 。时间长。要创建很多对象,来保存库的信息。
[oracle@ocm2 JCH2]$ rman catalog catalog_admin/catalog_admin@JCH2
Recovery Manager: Release 10.2.0.1.0 - Production on Wed Jan 15 07:07:36 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to recovery catalog database
RMAN> create catalog tablespace catalog_tbs;
recovery catalog created
RMAN>
exit
5.注册目标数据库,下面把环境修改了一下,tnsnames为JCH111,JCH112后面的数字是IP也是机器的顺序。
注册JCH1
必须要管理员身份登录数据库才可注册普通用户不可以 .想要管理哪个,就把哪个库注册进来。
rman target sys/oracle@JCH111 catalog catalog_admin/catalog_admin@JCH112
出了错:
重建密码文件
再次连接成功:
register database;
[oracle@ocm2 admin]$ rman target sys/oracle@JCH111 catalog catalog_admin/catalog_admin@JCH112
Recovery Manager: Release 10.2.0.1.0 - Production on Wed Jan 15 08:25:04 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: JCH1 (DBID=1969774514)
connected to recovery catalog database
RMAN> register database;
database registered in recovery catalog
starting full resync of recovery catalog
full resync complete
RMAN>
注册JCH2
rman target sys/oracle catalog catalog_admin/catalog_admin
register database;
【unregister database; 】取消注册命令
6.在数据库中确认注册成功,看看哪些数据库注册到catalog库了
sqlplus catalog_admin/catalog_admin@JCH112
select * from rc_database;
7.JCH1开启归档
alter system set log_archive_dest_1='location=/oracle/oracle/rman_backup' scope=spfile;
shutdown immediate
startup mount
alter database archivelog;
alter database open;
select log_mode from v$database;
SQL> alter system set log_archive_dest_1='location=/oracle/oracle/arch_backup' scope=spfile;
System altered.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 536870912 bytes
Fixed Size 1220460 bytes
Variable Size 159383700 bytes
Database Buffers 373293056 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> alter database archivelog;
Database altered.
SQL> alter database open;
Database altered.
SQL> select log_mode from v$database;
LOG_MODE
------------
ARCHIVELOG
SQL>
三 设置RMAN环境变量
官方文档:Backup and Recovery Reference -> CONFIGURE
我们在使用RMAN工具进行备份恢复时,第一 可以在命令行模式下指定备份恢复选项
第二 可以提前设置好RMAN环境变量,当达到触发条件时自动触发(目的)
必须连接到目标数据库后才可以查看设置好的变量值
rman target sys/oracle@JCH111 catalog catalog_admin/catalog_admin@JCH112
show all;
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default 冗余配置保留政策:冗余数是1
CONFIGURE BACKUP OPTIMIZATION OFF; # default 开启增量备份:关
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default 默认备份设备是磁盘
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default 控制文件自动备份:关
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default 控制文件自动备份目录和格式:%F 【备份设备:Disk】
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default 备份的并行度:1,备份类型为备份集【几个并行度就分配几个通道】
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default 数据文件采用复制方式备份
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default 归档日志采用复制方式备份
CONFIGURE MAXSETSIZE TO UNLIMITED; # default 最大值:无限制
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default 加密数据库:关
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default 加密算法采用AES128 加密表空间时默认AES192算法
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default 归档日志删除策略:空
CONFIGURE SNAPSHOT CONTROLFILE NAME TO 快照控制文件名
RMAN默认备份介质保存目录/oracle/oracle/rman_backup
configure channel device type disk format '/oracle/oracle/rman_backup/JCH1_%U';
RMAN> configure channel device type disk format '/oracle/oracle/rman_backup/JCH1_%U';
old RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/oracle/oracle/rman_backup/JCH1_%U';
new RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/oracle/oracle/rman_backup/JCH1_%U';
new RMAN configuration parameters are successfully stored
starting full resync of recovery catalog
full resync complete
控制文件自动备份
configure controlfile autobackup on;
configure controlfile autobackup format for device type disk to '/oracle/oracle/control_backup/controlfile_%F';
备份并行度2
CONFIGURE DEVICE TYPE DISK PARALLELISM 2;
增量备份
configure backup optimization on;
备份介质保留期为30天
configure retention policy to recovery window of 30 days;
显示我们设置完的参数配置
show all;
四 RMAN备份和闪回数据库
官方文档:Backup and Recovery Reference -> BACKUP
Backup and Recovery Basics -> 4 Backing Up Databases Using RMAN -> 4.2.5 Using Compressed Backupsets for RMAN Backup
1.压缩全备
backup as compressed backupset database plus archivelog; 使用默认的备份介质目录和格式,使用控制文件和参数文件自动备份
在JCH1机器查看备份文件:
2.RMAN模块版压缩备份数据库,采用压缩备份方式全库备份
run{
backup as compressed backupset full database
format '/oracle/oracle/rman_backup/full_JCH1_%u%p%s.rmn'
include current controlfile;
backup as compressed backupset archivelog all
format '/oracle/oracle/rman_backup /arch_leo1_%u%p%s.rmn'
delete all input;
}
3.使用默认备份介质目录和格式,同时删除备份过的归档日志
backup as compressed backupset full database include current controlfile plus archivelog delete all input;
4.“一行RMAN命令”压缩备份数据库
backup as compressed backupset full database format
'/oracle/oracle/rman_backup/full_JCH1_%u%p%s.rmn' include current controlfile
plus
archivelog format '/oracle/oracle/rman_backup/full_JCH1_%u%p%s.rmn' delete all input;
5.检查所有备份集
list backupset summary;
6.配置flashback数据库
闪回数据库:使用闪回日志恢复数据库到之前的一个时间点。
启动闪回前提条件是开启归档:归档日志可以辅助闪回日志完成闪回工作,往回闪不用归档,往前闪会用到归档。
1.检查数据库状态
select open_mode,log_mode,flashback_on from v$database; 是否在mount和archive状态
SQL> select open_mode,log_mode,flashback_on from v$database;
OPEN_MODE LOG_MODE FLASHBACK_ON
---------- ------------ ------------------
READ WRITE ARCHIVELOG NO
SQL>
archive log list;
先设置快速恢复区大小再设置目录
alter system set db_recovery_file_dest_size=2g;
mkdir -p /oracle/oracle/flash
alter system set db_recovery_file_dest='/oracle/oracle/flash'; (保存闪回日志)
设置闪回保留期24小时(1天)单位是分钟
alter system set db_flashback_retention_target=1440 scope=spfile;
2.重启使参数生效
shutdown immediate
startup mount
show parameter db_recovery
启动闪回功能
alter database flashback on;
3.打开数据库
alter database open;
select open_mode,log_mode,flashback_on from v$database;
闪回种类
闪回查询 undo
闪回表 recycbin
闪回版本 undo
闪回事物 undo
闪回数据库 闪回日志
闪回归档 表空间(永久保存11g新特性)
扩展知识 各种闪回实验
《Oracle Flashback 知行合一》 详细阐述了flashback特性的原理与应用场景
http://blog.itpub.net/26686207/viewspace-761416/
《RMAN 备份与恢复深入解析(一) 》
http://blog.itpub.net/26686207/viewspace-760869
《RMAN 备份与恢复深入解析(二) 》
http://blog.itpub.net/26686207/viewspace-760871
大家如果想深入的学习RMAN与FLASHBACK知识可以阅读这三篇文章