Oracle的RMAN备份与恢复脚本
一、备份方案:
采用多级备份是为了减少了恢复所需要的时间和减少每天备份所需要的时间,而又保证系统有很好的恢复性。
以下是一种建议的方案:
每半年做一个数据库的全备份(包含只读表空间)
每个月做一次零级备份(不包含只读表空间)
每个星期做一次一级备份
每天做一个二级备份
任何表空间改成只读状态后做一个该表空间的备份。
当需要时(如四个小时归档文件系统就要接近满了)备份归档文件
1、数据库全备份脚本 :
run {
allocate channel c1 type disk;
backup full tag 'dbfull' format '/opt/backup/full_%d_%u' (database include current controlfile);
sql 'alter system archive log current' ;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
2、零级备份 :
run {
allocate channel c1 type disk;
backup incremental level 0 filesperset 5 tag 'dbL0' format '/opt/backup/ora_0_%d_%u' (database include current controlfile);
sql 'alter system archive log current' ;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
3、一级备份 :
run {
allocate channel c1 type disk;
backup incremental level 1 filesperset 5 tag 'dbL1' format '/opt/backup/ora_0_%d_%u' (database include current controlfile);
sql 'alter system archive log current' ;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
4、二级备份 :
run {
allocate channel c1 type disk;
backup incremental level 2 filesperset 5 tag 'dbL2' format '/opt/backup/ora_0_%d_%u' (database include current controlfile);
sql 'alter system archive log current' ;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
5、表空间备份 脚本(以users表空间为例):
run {
allocate channel c1 type disk;
backup tag 'tsusers' format '/opt/backup/ora_0_%d_%u' tablespace users;
sql 'alter system archive log current' ;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
6、归档文件备份 脚本:
run {
allocate channel c1 type disk;
backup format '/opt/backup/archivelog_%d_%u' (archivelog all);
release channel c1;
}
二、RMAN恢复案例
1 、丢失全部数据文件,控制文件、日志文件必须存在
模拟介质损坏:删除所有dbf文件
启动数据库:startup mount
恢复语句:
run{
allocate channel c1 type disk;
restore database;
recover database;
sql 'alter database open';
release channel c1;
}
注意:Oracle永远不会备份使用临时文件的TEMPORARY表空间,因为临时表空间包含的只是暂时
性的数据,根本不必还原。不过在还原后数据字典中还是有临时表空间的相关信息,你只要
简单的重建一个临时表空间,设置其为缺省临时表空间,再把以前的临时表空间删除即可。
SQL> create temporary tablespace temp2 tempfile 'd:oracleoradataoradbtemp02.dbf' size 100M;
SQL> alter database default temporary tablespace temp2;
SQL> drop tablespace temp including contents and datafiles;
2、丢失非系统数据文件恢复
模拟介质损坏:删除users01.dbf文件
启动数据库:startup mount
恢复语句:
run {
allocate channel c1 type disk;
sql 'alter tablespace users offline';
restore tablespace users;
recover tablespace users;
sql 'alter tablespace users online';
release channel c1;
}
3、 丢失全部数据文件、控制文件、日志文件恢复
注意:在备份时配置了configure controlfile autobackup on;
[oracle@Linux1 rman_backup]$ rman target /
RMAN> set DBID=285819149
RMAN> restore controlfile from autobackup(也可以:restore controlfile from ‘file name’ );
启动数据库:startup mount
恢复语句:
run{
allocate channel c1 type disk;
restore database;
recover database;
sql 'alter database open resetlogs';
release channel c1;
}
4、不完全恢复
A、基于时间点的不完全恢复:
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
SET UNTIL TIME = '2002-12-09:11:44:00';
RESTORE DATABASE;
RECOVER DATABASE;
sql'ALTER DATABASE OPEN RESESTLOGS';
RELEASE CHANNEL 1;
}
B、基于日志序列的不完全恢复:
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
SET UNTIL SEQUENCE 120 THREAD 1;
RESTORE DATABASE;
RECOVER DATABASE; # recovers through log 119
sql'ALTER DATABASE OPEN RESESTLOGS';
RELEASE CHANNEL 1;
}
C、基于SCN的不完全恢复:
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
SET UNTIL SCN=100145;
RESTORE DATABASE;
RECOVER DATABASE;
sql'ALTER DATABASE OPEN RESESTLOGS';
RELEASE CHANNEL 1;
}
D、基于cancel的不完全恢复:
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
RESTORE DATABASE;
RECOVER DATABASE UNTIL CANCEL;
SQL'ALTER DATABASE OPEN RESESTLOGS';
RELEASE CHANNEL 1;
}
三、维护RMAN RMAN的维护主要分为几个方面
1、 查看RMAN的信息
检查现有备份
RMAN>list backup
列出过期备份
RMAN>report obsolete
删除过期的备份
RMAN>allocate channel for maintenance type disk;
RMAN>change backupset id delete;
RMAN>release channel;
删除所有过期的备份
RMAN>delete obsolete;
2、 同步或重置RMAN
如果目标数据库物理对象发生了变化,如添加了一个数据文件,需要用如下命令同步:
RMAN>resync catalog;
如果目标数据库reset了数据库,需要用如下命令同步
RMAN>reset database;
当手工删除了数据库的归档文件后,要执行以下脚本同步
RMAN>allocate channel for maintenance type disk;
RMAN> change archivelog all crosscheck;
RMAN>release channel;
当手工删除了数据库的RMAN备份后,要执行以下脚本来同步
RMAN>allocate channel for maintenance type disk;
RMAN>crosscheck backup;
RMAN>delete expired backup;
RMAN>release channel;
如果觉得《oracle rman备份 Oracle的RMAN备份与恢复脚本》对你有帮助,请点赞、收藏,并留下你的观点哦!