启动归档模式
SYS@base>archive log list
(1)配置RMAN默认备份介质保存目录 /opt/oracle/backup
[oracle@linuxdboracle]$ mkdir backup 创建保存目录
RMAN> configure channel device type disk format'/opt/oracle/backup/DB_%U';
usingtarget database control file instead of recovery catalog
使用目标库“控制文件”代替“恢复目录数据库”存放rman信息
old RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters:
CONFIGURECHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';
new RMANconfiguration parameters are successfully stored
新RMAN配置参数生效
(2)配置控制文件自动备份并保存到/opt/oracle/backup/control
注:当控制文件内容有变化时会自动触发备份
[oracle@linuxdbbackup]$ mkdir control 创建保存目录
RMAN>configure controlfile autobackup on;启动控制文件自动备份
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP ON;
new RMANconfiguration parameters are successfully stored
RMAN> configure controlfile autobackup format for device type diskto '/opt/oracle/backup/control/cf_%F';配置控制文件自动备份保存目录和格式
new RMANconfiguration parameters:
CONFIGURECONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
new RMANconfiguration parameters are successfully stored
(3)配置备份介质保留期为7天
RMAN> configure retention policy to recovery window of 7 days;
new RMANconfiguration parameters:
CONFIGURERETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
new RMANconfiguration parameters are successfully stored
设置好后我们再来看一下rman环境变量
RMAN>show all;
RMANconfiguration parameters are:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;恢复窗口7天
CONFIGUREBACKUP OPTIMIZATION OFF; # default
CONFIGUREDEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON;启动控制文件自动备份,目录和格式
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO'/opt/oracle/backup/control/cf_%F';
CONFIGUREDEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGUREDATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGUREARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/opt/oracle/backup/DB_%U';备份介质保存目录
CONFIGUREMAXSETSIZE TO UNLIMITED; # default
CONFIGUREENCRYPTION FOR DATABASE OFF; # default
CONFIGUREENCRYPTION ALGORITHM 'AES128'; # default
CONFIGUREARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURESNAPSHOT CONTROLFILE NAME TO '/opt/oracle/product/10.2.0/db_1/dbs/snapcf_base.f';# default
我们目前配置这4个变量就可以了,其它的什么时候用什么时候做或者直接在命令行中指定。
四备份与恢复实例
1. 用RMAN分别作数据库,表空间和数据文件的备份和数据库,表空间和数据文件损坏后的恢复实例
数据库级备份与恢复
全库压缩备份与全库非压缩备份应用场景:
如果你的系统有专用“备份磁阵”或者有足够的磁盘空间来让您随心所欲的用,那说明你很幸运,遇到一个不差钱的boss,往往事与愿违,我们可以自由支配的磁盘空间非常有限,这也验证了国人勤俭节约的优良传统。在有限的空间里如何装下更多的备份呢,这里就用上了压缩属性,把原来很大的文件尽可能压缩,提高空间利用率,当然备份和恢复的时间窗口会长一些,这就是时间换空间的精髓。
Come on 我们先来备个全库吧
全库压缩备份脚本
backup as compressed backupset full database format 命令行中直接指定压缩选项即可
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelogformat '/opt/oracle/backup/arch_bk1_%u%p%s.rmn' delete all input;
全库非压缩备份脚本
backupfull database format
'/opt/oracle/backup/full_bk1_%u%p%s.rmn'include current controlfile
plus
archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
全库使用默认通道默认配置备份脚本,同时删除备份过的归档日志
backup as compressed backupset full databaseinclude current controlfile plus archivelog delete all input;
上面有三种不同情况的备份脚本,我们用第二个,这个我想应该是大众最常用的。
备份之前检查archive log ,有三个归档日志
[oracle@linuxdb archdata]$ ll
总用量 64644
-rw-r----- 1 oracle oinstall 22736384 5月 8 15:35 1_13_814444678.dbf
-rw-r----- 1 oracle oinstall 1024 5月 8 15:35 1_14_814444678.dbf
-rw-r----- 1 oracle oinstall 43454464 5月 9 12:36 1_15_814444678.dbf
[oracle@linuxdb ~]$ rman target / 进入rman
connected to target database: BASE(DBID=1845289414) 一定要连接到数据库才行哦
RMAN> backup full database format
2>'/opt/oracle/backup/full_bk1_%u%p%s.rmn' include current controlfile
3> plus
4> archivelog format '/opt/oracle/backup/arch_bk1_%u%p%s.rmn'delete all input;
恢复:
RMAN>list backupset; 查看备份集情况
RMAN>restore database;
RMAN>recover database;
RMAN>alter database open;
SPFILE文件损坏
模拟操作:
oracle> mv spfileora10g.ora spora10g.ora
oracle>rman target /;
rman> shutdown immediate;
rman> startup nomount;
startup failed: ORA-01078: failure in processing system parameters
LRM-00109: could not open parameter file '/home/oracle/product/10.20/dbs/initora10g.ora'
rman>set dbid 3988862108;
rman>restore spfile from autobackup;
执行该命令,如果没有找到的话,那可能是文件的路径发生错误.可以通过直接赋予它的文件
rman>restore spfile from '/u01/oracle/flash_recovery_area/ORA10G/autobackup/2008_12_09/o1_mf_s_673025706_4mw7xc79_.bkp
在dbs/目录下产生spfileora10g.ora文件。证明spfile 已经恢复好
rman> shutdown immediate;
rman> startup ;(如果该命令不能够启动数据库,那么需要set dbid 3988862108)
controlfile 丢失:
startup nomount;
restore controlfile from autobackup;
alter database mount;
recover database;
alter database open resetlogs;
注意:在做了alter database open resetlogs;会把online redelog file清空,数据文件丢失.所以这个时候要做一个全备份。
oracle>rm *.ctl
oracle>rman target / ;//不能够连接到rman ,因为controlfile丢失
oracle>sqlplus /nolog;
SQL>shutdown immediate; //因为controlfile丢失,不能够正常shutdown
SQL>shutdown abort;
oracle>rman target /;
rman>startup nomount;
rman>restore controlfile from autobackup;
rman>alter database mount;
rman>alter database open resetlogs;
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 12/09/2008 16:21:13
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/home/oracle/oradata/ora10g/system01.dbf
//出错, redo log的scn记录在controlfile里面的,因为我们有新的controlfile,所以需要resetlogs;
/*
resetlogs命令表示一个数据库逻辑生存期的结束和另一个数据库逻辑生存期的开始,每次使用resetlogs命令的时候,SCN不会被重置,不过oracle会重置日志序列号,而且会重置