RAMN概述
恢复管理器,可以跨越不同的操作系统进行数据库的备份与恢复
RMAN的优势:
Rman操作简单 自动化功能强
Rman可以忽略备份后未发生改变的block
使用rman可以轻松的创建备份或stabdby数据库
自动执行并行的备份和还原操作
可以不实际执行的情况下检查文件是否被正确的备份或还原
第一种连接方式
[oracle@localhost ~]$ rman
RMAN> connect target /
第二种连接方式
[oracle@localhost ~]$ rman target /
======================================
关闭数据库
SQL> shutdown immediate;
启动实例并加载数据库
SQL> startup mount
更改数据库为归档模式
SQL> alter database archivelog;
打开数据库
SQL> alter database open;
启用自动归档
SQL> alter system archive log start;
查看归档状态
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 1
Next log sequence to archive 3
Current log sequence 3
全库恢复要在mount状态
表恢复可以在open状态
=========================================
[oracle@localhost ~]$ rman target /
RMAN> list backup; (查看备份列表)
using target database control file instead of recovery catalog
(备份表空间)
RMAN> backup tablespace users;
Starting backup at 13-DEC-12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=142 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00004 name=/oracle/app/oradata/TEST/users01.dbf
channel ORA_DISK_1: starting piece 1 at 13-DEC-12
channel ORA_DISK_1: finished piece 1 at 13-DEC-12
piece handle=/oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_nnndf_TAG20121213T221721_8dmrvk8w_.bkp tag=TAG20121213T221721 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 13-DEC-12
RMAN> sql "alter system flush buffer_cache";(rman用sql,清空缓存)
[oracle@localhost ~]$ rm -rf users01.dbf(删除数据文件)
SQL> startup force
ORA-01110: data file 4: '/oracle/app/oradata/TEST/users01.dbf'
(报错4号文件 没有找到)
SQL> !
[oracle@localhost ~]$ rman target /
RMAN> restore datafile 4;(恢复文件)
RMAN> sql "alter database datafile 4 online";(数据文件在线)
SQL> startup force(正常运行)
RAMAN能找到恢复的路径,是因为RAMN在备份数据文件的同时也备份了控制文件,通过控制文件的路径来恢复数据文件。当数据文件有更新要重新备份。
备份位置
[oracle@localhost ~]$ cd /oracle/app/flash_recovery_area/TEST/backupset/
(Rman自动备份控制文件和参数文件,如果不配置,备份普通表空间的时候就不自动备份控制文件和参数文件)
=======================================
全库备份
1.
RMAN> backup database;
Starting backup at 13-DEC-12
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/oracle/app/oradata/TEST/system01.dbf
input datafile fno=00003 name=/oracle/app/oradata/TEST/sysaux01.dbf
input datafile fno=00002 name=/oracle/app/oradata/TEST/undotbs01.dbf
input datafile fno=00004 name=/oracle/app/oradata/TEST/users01.dbf
channel ORA_DISK_1: starting piece 1 at 13-DEC-12
channel ORA_DISK_1: finished piece 1 at 13-DEC-12
piece handle=/oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_nnndf_TAG20121213T221801_8dmrwsmc_.bkp tag=TAG20121213T221801 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:18
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 13-DEC-12
channel ORA_DISK_1: finished piece 1 at 13-DEC-12
piece handle=/oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_ncsnf_TAG20121213T221801_8dms15k1_.bkp tag=TAG20121213T221801 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 13-DEC-12
RMAN> list backup;
2.
[oracle@localhost ~]$ cd /oracle/app/oradata/TEST/
[oracle@localhost TEST]$ rm -rf *
3.SQL> startup force (找不到控制文件)
ORA-00205: error in identifying control file, check alert log for more info
4.[oracle@localhost TEST]$ cd /oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/
(找到o1_mf_ncsnf_TAG20121213T112651_8dllsp0q_.bkp)
5.恢复控制文件
Rman自动备份控制文件和参数文件
[oracle@localhost ~]$ rman target /
Recovery Manager: Release 10.2.0.1.0 - Production on Thu Dec 13 11:42:37 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: TEST (not mounted)
RMAN> restore controlfile from '/oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_ncsnf_TAG20121213T112651_8dllsp0q_.bkp';
Starting restore at 13-DEC-12
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
output filename=/oracle/app/oradata/TEST/control01.ctl
output filename=/oracle/app/oradata/TEST/control02.ctl
output filename=/oracle/app/oradata/TEST/control03.ctl
Finished restore at 13-DEC-12
RMAN> alter database mount;(数据库可以mount)
database mounted
released channel: ORA_DISK_1
6.[oracle@localhost TEST]$ ls (控制文件恢复回来了)
control01.ctl control02.ctl control03.ctl
7.恢复数据文件
RMAN> restore database;
Starting restore at 13-DEC-12
Starting implicit crosscheck backup at 13-DEC-12
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=155 devtype=DISK
Crosschecked 2 objects
Finished implicit crosscheck backup at 13-DEC-12
Starting implicit crosscheck copy at 13-DEC-12
using channel ORA_DISK_1
Finished implicit crosscheck copy at 13-DEC-12
searching for all files in the recovery area
cataloging files...
cataloging done
List of Cataloged Files
=======================
File Name: /oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_ncsnf_TAG20121213T112651_8dllsp0q_.bkp
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /oracle/app/oradata/TEST/system01.dbf
restoring datafile 00002 to /oracle/app/oradata/TEST/undotbs01.dbf
restoring datafile 00003 to /oracle/app/oradata/TEST/sysaux01.dbf
restoring datafile 00004 to /oracle/app/oradata/TEST/users01.dbf
channel ORA_DISK_1: reading from backup piece /oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_nnndf_TAG20121213T112651_8dllqwb4_.bkp
channel ORA_DISK_1: restored backup piece 1
piece handle=/oracle/app/flash_recovery_area/TEST/backupset/2012_12_13/o1_mf_nnndf_TAG20121213T112651_8dllqwb4_.bkp tag=TAG20121213T112651
channel ORA_DISK_1: restore complete, elapsed time: 00:01:08
Finished restore at 13-DEC-12
[oracle@localhost TEST]$ ls(数据文件恢复回来了)
control01.ctl control03.ctl system01.dbf users01.dbf
control02.ctl sysaux01.dbf undotbs01.dbf
8.RMAN> alter database open resetlogs;(报错)
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 12/13/2012 11:49:45
ORA-01152: file 1 was not restored from a sufficiently old backup
ORA-01110: data file 1: '/oracle/app/oradata/TEST/system01.dbf'
RMAN> recover database;(报错)
Starting recover at 13-DEC-12
using channel ORA_DISK_1
starting media recovery
unable to find archive log
archive log thread=1 sequence=8
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 12/13/2012 11:50:30
RMAN-06054: media recovery requesting unknown log: thread 1 seq 8 lowscn 634948
RMAN> alter database open resetlogs; (可以打开数据库)
(restore 是还原,文件级的恢复。就是物理文件还原
recover 是恢复,数据级的恢复。逻辑上恢复)
database opened
SQL> startup force(已经能够重启 恢复成功)
[oracle@localhost TEST]$ ls (日志文件也有了)
control01.ctl redo01.log sysaux01.dbf undotbs01.dbf
control02.ctl redo02.log system01.dbf users01.dbf
control03.ctl redo03.log temp01.dbf
============================================================
多通道全库备份(提高效率)
mkdir backup{1,2}
rman target /
RMAN>run{
allocate channel c1 type disk format '/home/oracle/backup1/%u';
allocate channel c2 type disk format '/home/oracle/backup2/%u';
backup database ;
}
piece handle=/home/oracle/backup1/04nsp7ou tag=TAG20121213T165350 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:56
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
including current control file in backupset
channel c1: starting piece 1 at 13-DEC-12
channel c2: finished piece 1 at 13-DEC-12
piece handle=/home/oracle/backup2/05nsp7ov tag=TAG20121213T165350 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:56
channel c2: starting full datafile backupset
channel c2: specifying datafile(s) in backupset
including current SPFILE in backupset
channel c2: starting piece 1 at 13-DEC-12
channel c1: finished piece 1 at 13-DEC-12
piece handle=/home/oracle/backup1/06nsp7qm tag=TAG20121213T165350 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:04
channel c2: finished piece 1 at 13-DEC-12
piece handle=/home/oracle/backup2/07nsp7qn tag=TAG20121213T165350 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:03
Finished backup at 13-DEC-12
released channel: c1
released channel: c2
((
%c copy ID
%p backup piece ID
%s backup set ID
%e log sequence
%h log thread ID
%d database name
%n database name(x填充到8个字符)
%I DBID
%f file ID
%F DBID, day, month, year, and sequencer的复合
%N tablespace name
%t timestamp
%M mh mm格式
%Y year yyyy格式
))