#1介绍 RMAN是Recovery Manager的缩写,为Oracle的恢复管理器,是Oracle数据库推荐提供的一种恢复和备份数据库的工具,也是数据库管理员管理数据库常用的工具之一。 它是物理备份,而EXP是逻辑备份 它可以实现一致性备份,非一致性备份,增量备份,而exp且做不到 很多第三方的工具也是调用rman的脚本来实现备份功能,如赛门铁克,NBU,TSM 它支持多种方式从备份集里恢复数据:基于时间的恢复,基于SCN 的恢复,基于日志序列的恢复,只读表空间的恢复,归档重做日志的还原。而exp只能还原你备份的那状态。 除了rman,Oracle还可以利用闪回区备份数据。 #2个人经验分享 rman可以备份数据库、表空间、数据文件、控制文件以及日志文件。 Oracle运行时有归档模式和非归档模式,用rman在备份数据库,如果你是一致性备份,你需要在mount状态下进行。如果你是非一致性备份数据库,你可以在open状态下备份,但是,前提是数据库需要以归档模式运行。但是在恢复数据库的时候一定要在mount状态下进行。如果你使用非归档模式,你是无法在oracle服务(open状态)下进行备份数据库的。 rman备份还原表空间的时候,可以在open状态下进行,只需要把当前表空间设置成离线,其它的表空间正常运行。 rman可以从备份集里面指定某一时间点来还原。 rman在做数据恢复的时候,它是要依赖数据文件,控制文件,归档日志的,所以这些文件也需要定时备份。 #3前期准备 ##3.1修改数据库为归档模式
sqlplus /nolog (启动sqlplus)
SQL> conn / as sysdba (以DBA身份连接数据库)
SQL> shutdown immediate; (立即关闭数据库)
SQL> startup mount (启动实例并加载数据库,但不打开)
SQL> alter database archivelog; (更改数据库为归档模式)
SQL> alter database open; (打开数据库)
SQL> alter system archive log start; (启用自动归档)
SQL> exit (退出)
如果linux找不到命令请执行:source /home/oracle/.bash_profile ##3.2修改时间格式 由于在后面恢复数据库操作时,看备份集的时间不好看,所以我设置一下时间格式
vi /etc/profile
#加入下面来句话
export NLS_LANG=AMERICAN ##如果这句不写,下面句不起作用
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
:wq!
source /etc/profile
##3.2创建恢复目录 在使用rman备份数据的时候,它是依赖一些控制文件等东西,所以你可以创建一个恢复目录,也可以不创建(使用控制文件),而这里的恢复目录其实是一个oracle的数据库,而且它也是有必要做备份的。为了数据安全起见,一般会把恢复目录的数据库创建在另外一个Oracle服务器上,而不是在同一个数据库中或者同一个服务器上,因为防止出现服务器宕机,导致数据文件一起丢失,而不能进行恢复,但是这里为了演示,就直接把恢复目录同时创建在本机上。 创建恢复目录的步骤分为创建恢复目录数据库并创建用户、给恢复目录用户赋角色和权限、创建恢复目录三个步骤。
--创建恢复目录专用的表空间rmtablespace
create tablespace rmtablespace datafile '/home/oracle/app/oracle/oradata/helowin/rmtablespace.dbf'
size 100m
autoextend on next 10m maxsize 500m
permanent
extent management local;
--创建用户bkp
create user bkp--用户名
identified by "123456"--密码
default tablespace rmtablespace--表空间名
temporary tablespace temp --临时表空间名
quota unlimited on rmtablespace--表空间无限制
profile DEFAULT --数据文件(默认数据文件)
account unlock-- 账户是否解锁(lock:锁定、unlock解锁)
-- 授权系统管理员权限
grant dba to bkp;
grant connect to bkp;
grant resource to bkp;
--授权拥有恢复目录权限
grant recovery_catalog_owner to bkp;
通过创建好的恢复目录用户进行登录连接到恢复目录数据库,执行命令如下:
[oracle@cd555c73f2d7 /]$ rman
Recovery Manager: Release 11.2.0.1.0 - Production on Wed Feb 20 19:24:03 2019
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
RMAN> connect catalog bkp/123456;
connected to recovery catalog database
RMAN> create catalog;
recovery catalog created
RMAN>
至此,RMAN的恢复目录已经创建完毕。 下面用RMAN连接恢复目录和注册
[oracle@cd555c73f2d7 /]$ rman target / catalog bkp/123456@helowin;
Recovery Manager: Release 11.2.0.1.0 - Production on Wed Feb 20 19:31:17 2019
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: HELOWIN (DBID=1384114315)
connected to recovery catalog database
RMAN> register database;
database registered in recovery catalog
starting full resync of recovery catalog
full resync complete
RMAN>
完成。 ##3.3创建一个用来做实验的用户
--创建恢复目录专用的表空间rmtablespace
create tablespace testsp
datafile '/home/oracle/app/oracle/oradata/testsp.dbf'
size 100m
autoextend on next 10m maxsize 500m
permanent
extent management local;
--创建用户rm
create user testdb--用户名
identified by "123456"--密码
default tablespace testsp--表空间名
temporary tablespace temp --临时表空间名
quota unlimited on testsp--表空间无限制
profile DEFAULT --数据文件(默认数据文件)
account unlock-- 账户是否解锁(lock:锁定、unlock解锁)
-- 授权系统管理员权限
grant dba to testdb;
grant connect to testdb;
grant resource to testdb;
登录进去testdb,插入一些测试数据
---一些用来后面测试的数据
CREATE TABLE A(name varchar(255));
--插入一条测试数据
insert into A values ('我是状态一');
#4简单的备份还原 ##4.1备份数据库 创建一个用来保存备份数据的目录
mkdir -p /home/oracle/oracle_bkp
执行备份语句 通过format来指定备份文件的路径
rman target / catalog bkp/123456@helowin;
backup as compressed backupset full database format '/home/oracle/oracle_bkp/db_%d_%s_%p_%t_%T.bkp' include current controlfile plus archivelog delete all input;
运行效果:
RMAN> backup as compressed backupset full database format '/home/oracle/oracle_bkp/db_%d_%s_%p_%t_%T.bkp' include current controlfile plus archivelog delete all input;
Starting backup at 2019-02-21 08:41:57
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=226 device type=DISK
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=1 RECID=1 STAMP=1000759626
input archived log thread=1 sequence=2 RECID=2 STAMP=1000759707
input archived log thread=1 sequence=3 RECID=3 STAMP=1000759727
input archived log thread=1 sequence=4 RECID=4 STAMP=1000768684
input archived log thread=1 sequence=5 RECID=5 STAMP=1000802517
channel ORA_DISK_1: starting piece 1 at 2019-02-21 08:41:58
channel ORA_DISK_1: finished piece 1 at 2019-02-21 08:42:01
piece handle=/home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084158_g6vwypn9_.bkp tag=TAG20190221T084158 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 2019-02-21 08:42:01
Starting backup at 2019-02-21 08:42:01
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/home/oracle/app/oracle/oradata/helowin/system01.dbf
input datafile file number=00002 name=/home/oracle/app/oracle/oradata/helowin/sysaux01.dbf
input datafile file number=00005 name=/home/oracle/app/oracle/oradata/helowin/example01.dbf
input datafile file number=00006 name=/home/oracle/app/oracle/oradata/helowin/rmtablespace.dbf
input datafile file number=00007 name=/home/oracle/app/oracle/oradata/testsp.dbf
input datafile file number=00003 name=/home/oracle/app/oracle/oradata/helowin/undotbs01.dbf
input datafile file number=00004 name=/home/oracle/app/oracle/oradata/helowin/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2019-02-21 08:42:01
channel ORA_DISK_1: finished piece 1 at 2019-02-21 08:42:16
piece handle=/home/oracle/oracle_bkp/db_HELOWIN_6_1_1000802521_20190221.bkp tag=TAG20190221T084201 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2019-02-21 08:42:17
channel ORA_DISK_1: finished piece 1 at 2019-02-21 08:42:18
piece handle=/home/oracle/oracle_bkp/db_HELOWIN_7_1_1000802536_20190221.bkp tag=TAG20190221T084201 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2019-02-21 08:42:18
Starting backup at 2019-02-21 08:42:18
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=6 RECID=6 STAMP=1000802539
channel ORA_DISK_1: starting piece 1 at 2019-02-21 08:42:19
channel ORA_DISK_1: finished piece 1 at 2019-02-21 08:42:20
piece handle=/home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084219_g6vwzcx2_.bkp tag=TAG20190221T084219 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2019-02-21 08:42:20
查看备份的结果:
list backup summary;#查看备份简略信息
list backup;#查看备份信息
list backup of database;#查看数据的备份信息
list backup of controlfile;#查看备份的控制文件
delete backup;#删除备份信息
RMAN> list backup summary;
List of Backups
===============
Key TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- ------------------- ------- ------- ---------- ---
5 B A A DISK 2019-02-21 08:41:59 1 1 NO TAG20190221T084158
6 B F A DISK 2019-02-21 08:42:15 1 1 NO TAG20190221T084201
7 B F A DISK 2019-02-21 08:42:18 1 1 NO TAG20190221T084201
8 B A A DISK 2019-02-21 08:42:19 1 1 NO TAG20190221T084219
RMAN> list backup;
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ -------------------
5 91.88M DISK 00:00:01 2019-02-21 08:41:59
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084158
Piece Name: /home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084158_g6vwypn9_.bkp
List of Archived Logs in backup set 5
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- ------------------- ---------- ---------
1 1 1087725 2016-01-04 01:28:54 1096319 2019-02-20 20:47:06
1 2 1096319 2019-02-20 20:47:06 1097139 2019-02-20 20:48:27
1 3 1097139 2019-02-20 20:48:27 1097176 2019-02-20 20:48:47
1 4 1097176 2019-02-20 20:48:47 1108472 2019-02-20 23:18:03
1 5 1108472 2019-02-20 23:18:03 1133379 2019-02-21 08:41:57
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
6 Full 1.03G DISK 00:00:14 2019-02-21 08:42:15
BP Key: 6 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084201
Piece Name: /home/oracle/oracle_bkp/db_HELOWIN_6_1_1000802521_20190221.bkp
List of Datafiles in backup set 6
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- ------------------- ----
1 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/system01.dbf
2 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/sysaux01.dbf
3 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/undotbs01.dbf
4 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/users01.dbf
5 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/example01.dbf
6 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/rmtablespace.dbf
7 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/testsp.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
7 Full 9.67M DISK 00:00:02 2019-02-21 08:42:18
BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084201
Piece Name: /home/oracle/oracle_bkp/db_HELOWIN_7_1_1000802536_20190221.bkp
SPFILE Included: Modification time: 2019-02-21 08:32:57
SPFILE db_unique_name: HELOWIN
Control File Included: Ckp SCN: 1133415 Ckp time: 2019-02-21 08:42:16
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ -------------------
8 31.50K DISK 00:00:00 2019-02-21 08:42:19
BP Key: 8 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084219
Piece Name: /home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084219_g6vwzcx2_.bkp
List of Archived Logs in backup set 8
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- ------------------- ---------- ---------
1 6 1133379 2019-02-21 08:41:57 1133420 2019-02-21 08:42:19
RMAN>
模拟误操作删除表数据或者数据文件
delete from A
执行数据库还原操作,数据还原无论是归档还是非归档都要求是mount状态下进行
sqlplus / as sysdba
shutdown immediate;
startup mount;
exit
rman target /
run {
restore database;
recover database;
alter database open;
}
可以发现数据库是恢复了,但是表的数据还是没有还原。 或许你应该进行非完整性恢复 先在测试库放点测试数据
insert into A values ('我是状态二');
--设置时间格式
alter session set nls_date_format="YYYY-MM-dd hh24:mi:ss"
--查看当前的时间
select sysdate from dual;
--返回2019/2/21 9:06:37
--再插入一条测试数据
insert into A values ('我是状态三');
--查看当前的时间
select sysdate from dual;
--返回的是2019/2/21 9:07:45
现在我要把数据库还原到2019/2/21 9:06:37,只有“我是状态二”一条数据
sqlplus / as sysdba
shutdown immediate;
startup mount;
exit
rman target /
run {
sql 'alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss"';
set until time = '2019/2/21 09:06:37';
restore database;
recover database;
alter database open resetlogs;
}
除了上面根据基于时间的恢复,还可以来一个基于SCN 的恢复
RMAN> list backup;
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ -------------------
5 91.88M DISK 00:00:01 2019-02-21 08:41:59
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084158
Piece Name: /home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084158_g6vwypn9_.bkp
List of Archived Logs in backup set 5
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- ------------------- ---------- ---------
1 1 1087725 2016-01-04 01:28:54 1096319 2019-02-20 20:47:06
1 2 1096319 2019-02-20 20:47:06 1097139 2019-02-20 20:48:27
1 3 1097139 2019-02-20 20:48:27 1097176 2019-02-20 20:48:47
1 4 1097176 2019-02-20 20:48:47 1108472 2019-02-20 23:18:03
1 5 1108472 2019-02-20 23:18:03 1133379 2019-02-21 08:41:57
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
6 Full 1.03G DISK 00:00:14 2019-02-21 08:42:15
BP Key: 6 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084201
Piece Name: /home/oracle/oracle_bkp/db_HELOWIN_6_1_1000802521_20190221.bkp
List of Datafiles in backup set 6
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- ------------------- ----
1 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/system01.dbf
2 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/sysaux01.dbf
3 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/undotbs01.dbf
4 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/users01.dbf
5 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/example01.dbf
6 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/helowin/rmtablespace.dbf
7 Full 1133410 2019-02-21 08:42:01 /home/oracle/app/oracle/oradata/testsp.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ -------------------
7 Full 9.67M DISK 00:00:02 2019-02-21 08:42:18
BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084201
Piece Name: /home/oracle/oracle_bkp/db_HELOWIN_7_1_1000802536_20190221.bkp
SPFILE Included: Modification time: 2019-02-21 08:32:57
SPFILE db_unique_name: HELOWIN
Control File Included: Ckp SCN: 1133415 Ckp time: 2019-02-21 08:42:16
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ -------------------
8 31.50K DISK 00:00:00 2019-02-21 08:42:19
BP Key: 8 Status: AVAILABLE Compressed: NO Tag: TAG20190221T084219
Piece Name: /home/oracle/app/oracle/flash_recovery_area/HELOWIN/backupset/2019_02_21/o1_mf_annnn_TAG20190221T084219_g6vwzcx2_.bkp
List of Archived Logs in backup set 8
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- ------------------- ---------- ---------
1 6 1133379 2019-02-21 08:41:57 1133420 2019-02-21 08:42:19
基于SCN恢复
run{
set until scn=1133410;
restore database from tag='TAG20190221T084201';
recover database;
sql 'alter database open resetlogs';
}
执行过程如下:
RMAN> run{
set until scn=1133410;
restore database from tag='TAG20190221T084201';
recover database;
sql 'alter database open resetlogs';
}2> 3> 4> 5> 6>
executing command: SET until clause
Starting restore at 2019-02-21 09:15:34
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00001 to /home/oracle/app/oracle/oradata/helowin/system01.dbf
channel ORA_DISK_1: restoring datafile 00002 to /home/oracle/app/oracle/oradata/helowin/sysaux01.dbf
channel ORA_DISK_1: restoring datafile 00003 to /home/oracle/app/oracle/oradata/helowin/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00004 to /home/oracle/app/oracle/oradata/helowin/users01.dbf
channel ORA_DISK_1: restoring datafile 00005 to /home/oracle/app/oracle/oradata/helowin/example01.dbf
channel ORA_DISK_1: restoring datafile 00006 to /home/oracle/app/oracle/oradata/helowin/rmtablespace.dbf
channel ORA_DISK_1: restoring datafile 00007 to /home/oracle/app/oracle/oradata/testsp.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/oracle_bkp/db_HELOWIN_6_1_1000802521_20190221.bkp
channel ORA_DISK_1: piece handle=/home/oracle/oracle_bkp/db_HELOWIN_6_1_1000802521_20190221.bkp tag=TAG20190221T084201
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:25
Finished restore at 2019-02-21 09:15:59
Starting recover at 2019-02-21 09:15:59
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 2019-02-21 09:15:59
sql statement: alter database open resetlogs
RMAN>
当你反复执行上面的还原操作的时候,可能会报错,找不到备份集,那时候你应该设置数据库的原型序列
RMAN> list incarnation;查看数据库的原型序列
List of Database Incarnations
DB Key Inc Key DB Name DB ID STATUS Reset SCN Reset Time
------- ------- -------- ---------------- --- ---------- ----------
1 1 HELOWIN 1384114315 PARENT 945184 2014-08-23 06:26:21
2 2 HELOWIN 1384114315 PARENT 1087725 2016-01-04 01:28:54
4 4 HELOWIN 1384114315 CURRENT 1133411 2019-02-21 09:15:59
3 3 HELOWIN 1384114315 ORPHAN 1134347 2019-02-21 09:00:43
RMAN> RESET DATABASE TO INCARNATION 4; 设置原型序列
restore 是还原,文件级的恢复,就是物理文件还原,所以只有当数据文件或其他文件丢失时,使用restore进行还原,还原的前提是数据库在mount状态下。 RMAN使用RECOVER命令可以恢复数据库,该命令是负责把归档日志文件用于重建的数据文件,来完成数据库的同步恢复。recover恢复是数据级的恢复,逻辑上恢复,即是一步一步利用归档日志恢复到出事的那个时间点,保持数据的一致性。但是,recover恢复的前提得是数据库开启了归档日志模式。 在实际应用中,restore还原和recover恢复是结合一起使用的,假如出现数据文件丢失导致数据库宕机时,我们首先是利用restore命令把之前定时备份好的数据文件还原到备份的时间点,然后再利用recover命令通过归档日志把还原点到出事点之间的数据进行恢复。最终完成数据库的完全恢复。
#4.2表空间备份还原
#备份语句
backup tablespace testsp format '/home/oracle/oracle_bkp/tablespaces_%d_%s_%p_%t_%T.bkp' plus archivelog;
#还原语句
run{
sql 'alter tablespace testsp offline immediate';
recover tablespace testsp until time "to_date('2019-02-20 17:52:06','yyyy-mm-dd hh24:mi:ss')" auxiliary destination '/home/oracle/prod';
sql 'alter tablespace testsp online';
}
auxiliary destination是存放辅助库文件,必须的。或许是因为当前表空间正在被使用。 表空间的备份还原不需要在mount状态下进行。