Oracle数据库备份与恢复 - RMAN备份
如果要说DBA工作中最重要的职责(没有之一),那无疑就是保证客户数据的安全和完整,可以看到几乎任何一本Oracle DBA的技术书籍一定都会把大篇幅来介绍数据库的备份与恢复,从中也可以看到备份和恢复的重要程度。eygle曾经总结了DBA生存之四大守则的第一条就是:备份重于一切
我们必需知道,系统总是要崩溃的,没有有效的备份只是等哪一天死!我经常开玩笑的说,唯一会使DBA在梦中惊醒的就是,没有有效的备份.
RMAN概览
RMAN是Oracle数据库软件自带的备份/恢复工具。RMAN只能用于9i或更高的版本中。它能够备份整个数据库或数据库部件,如表空间、数据文件、控制文件、归档文件以及Spfile参数文件。通过RMAN的方式无论是要备份还是要恢复,都必须先启动实例并加载数据库。
RMAN之备份
连接数据库
很简单,进入到命令提示符界面:
也可以先启动RMAN,然后再通过CONNECT命令来连接目标数据库:
使用RMAN来做各种类型的备份
- 整库备份
整库备份的命令很简单,就是backup database1234567891011121314151617181920212223242526272829303132RMAN> backup database;Starting backup at 28-FEB-17using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=1717 device type=DISKchannel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00001 name=/u01/data/orcl/system01.dbfinput datafile file number=00002 name=/u01/data/orcl/sysaux01.dbfinput datafile file number=00003 name=/u01/data/orcl/undotbs01.dbfinput datafile file number=00004 name=/u01/data/orcl/users01.dbfinput datafile file number=00007 name=/u01/data/orcl/dev_paydb.dbfinput datafile file number=00008 name=/u01/data/orcl/dev_payboxdb.dbfinput datafile file number=00009 name=/u01/data/orcl/ticket.dbfinput datafile file number=00005 name=/u01/data/orcl/PAYBOX_FG_PRO.dbfinput datafile file number=00006 name=/u01/data/orcl/PAYBOX_TEST2SP.dbfchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/01rtn1md_1_1 tag=TAG20170228T105613 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:40:26channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/02rtn428_1_1 tag=TAG20170228T105613 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 28-FEB-17RMAN>
整库备份集生成了两个备份片段:一个存储数据文件,另一个存储控制文件和SPFILE(服务器端初始化参数文件),都被保存到Oracle软件的安装目录下,这是因为没有为备份集指定存储路径,默认情况下就会存储到Oracle软件的安装目录中
真正的备份操作,你肯定希望能够指定备份集的存储位置,没问题,最简单的方式是在执行BACKUP命令时,指定FORMAT参数来自定义备份片段的路径和命令规则
查看创建的全库备份
- 表空间的备份
rman还可以针对表空间备份1234567891011121314RMAN> BACKUP TABLESPACE USERS;Starting backup at 28-FEB-17using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00004 name=/u01/data/orcl/users01.dbfchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/03rtn5li_1_1 tag=TAG20170228T120402 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:01:05Finished backup at 28-FEB-17RMAN>
查看表空间的备份
这里发现USER表空间存在2份备份(因为之前做过一次全量备份)
删除指定的BACKUPSET
- 数据文件的备份1234567891011121314151617SQL> select file#,name from v$datafile;FILE# NAME---------- --------------------------------------------------1 /u01/data/orcl/system01.dbf2 /u01/data/orcl/sysaux01.dbf3 /u01/data/orcl/undotbs01.dbf4 /u01/data/orcl/users01.dbf5 /u01/data/orcl/PAYBOX_FG_PRO.dbf6 /u01/data/orcl/PAYBOX_TEST2SP.dbf7 /u01/data/orcl/dev_paydb.dbf8 /u01/data/orcl/dev_payboxdb.dbf9 /u01/data/orcl/ticket.dbf9 rows selected.SQL>
|
|
- 控制文件备份1234567891011121314RMAN> BACKUP CURRENT CONTROLFILE;Starting backup at 28-FEB-17using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/05rtn873_1_1 tag=TAG20170228T124731 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 28-FEB-17RMAN>
查看控制文件备份
- 归档文件的备份
归档日志对于数据库介质恢复相当关键,只要拥有相应的归档日志文件,就能确保我们将数据库恢复到备份之后的任意时刻。在RMAN中备份归档日志有以下两种方式:
- 利用BACKUP ARCHIVELOG命令备份12345678910111213141516171819202122232425RMAN> BACKUP ARCHIVELOG ALL;Starting backup at 28-FEB-17current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=868 RECID=1 STAMP=937050974input archived log thread=1 sequence=871 RECID=2 STAMP=937050978input archived log thread=1 sequence=872 RECID=3 STAMP=937050981input archived log thread=1 sequence=873 RECID=4 STAMP=937052764input archived log thread=1 sequence=874 RECID=5 STAMP=937052771input archived log thread=1 sequence=875 RECID=6 STAMP=937073029input archived log thread=1 sequence=876 RECID=7 STAMP=937079244input archived log thread=1 sequence=877 RECID=8 STAMP=937087288input archived log thread=1 sequence=878 RECID=9 STAMP=937108830input archived log thread=1 sequence=879 RECID=10 STAMP=937124801input archived log thread=1 sequence=880 RECID=11 STAMP=937140632channel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/06rtn8cp_1_1 tag=TAG20170228T125032 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:15Finished backup at 28-FEB-17RMAN>
BACKUP ARCHIVELOG命令比较灵活,ALL是指备份当前所有可访问到的归档文件,你还可以通过UNTIL、SCN、TIME、SEQUENCE等参数灵活指定要备份的归档区间。
- 执行BACKUP命令时指定PLUS ARCHIVELOG子句
在备份控制文件之前首先对所有归档文件进行备份,BACKUP…PLUS ARCHIVELOG命令在备份过程中会依次执行下列步骤:
- 运行ALTER SYSTEM ARCHIVE LOG CURRENT语句对当前 Redolog 进行归档
- 执行BACKUP ARCHIVELOG ALL命令备份所有已归档日志
- 执行BACKUP命令对指定项进行备份
- 再次运行ALTER SYSTEM ARCHIVE LOG CURRENT对当前 Redolog 归档
- 对新生成的尚未备份的归档文件进行备份12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849RMAN> BACKUP CURRENT CONTROLFILE PLUS ARCHIVELOG;Starting backup at 28-FEB-17current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=868 RECID=1 STAMP=937050974input archived log thread=1 sequence=871 RECID=2 STAMP=937050978input archived log thread=1 sequence=872 RECID=3 STAMP=937050981input archived log thread=1 sequence=873 RECID=4 STAMP=937052764input archived log thread=1 sequence=874 RECID=5 STAMP=937052771input archived log thread=1 sequence=875 RECID=6 STAMP=937073029input archived log thread=1 sequence=876 RECID=7 STAMP=937079244input archived log thread=1 sequence=877 RECID=8 STAMP=937087288input archived log thread=1 sequence=878 RECID=9 STAMP=937108830input archived log thread=1 sequence=879 RECID=10 STAMP=937124801input archived log thread=1 sequence=880 RECID=11 STAMP=937140632input archived log thread=1 sequence=881 RECID=12 STAMP=937140870channel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/07rtn8k6_1_1 tag=TAG20170228T125430 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:03Finished backup at 28-FEB-17Starting backup at 28-FEB-17using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current control file in backup setchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/08rtn8ka_1_1 tag=TAG20170228T125434 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 28-FEB-17Starting backup at 28-FEB-17current log archivedusing channel ORA_DISK_1channel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=882 RECID=13 STAMP=937140878channel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/09rtn8ke_1_1 tag=TAG20170228T125438 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 28-FEB-17RMAN>
完成备份之后,可以通过下列命令查看已备份的归档日志片段
初始化参数文件的备份
1234567891011121314RMAN> BACKUP SPFILE;Starting backup at 28-FEB-17using channel ORA_DISK_1channel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setincluding current SPFILE in backup setchannel ORA_DISK_1: starting piece 1 at 28-FEB-17channel ORA_DISK_1: finished piece 1 at 28-FEB-17piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/0artn91l_1_1 tag=TAG20170228T130141 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:01Finished backup at 28-FEB-17RMAN>对备份集备份
备份所有备份集
1RMAN> BACKUP BACKUPSET ALL;备份指定的备份集
12#n=备份集ID,可以同时指定多个,相互间以逗号分隔即可RMAN> BACKUP BACKUPSET n;
参考:http://blog.csdn.net/pan_tian/article/details/46766985
本文出自”Jack Wang Blog”:http://www.yfshare.vip/2017/02/28/Oracle数据库备份与恢复-RMAN备份/