在前一个项目中因项目经理的要求,需要提供一个自动备份功能。刚开始是想写个脚本将整个数据库exp出来进行保存,后来无意中看到了rman备份,且是官方推荐方式,所以在网上搜索了资料进行观看,感觉挺有收获的,以下是我在网上搜寻到的资料,各位有兴趣的话可以看看:
http://oracle.chinaitlab.com/backup/523597.html
http://oracle.chinaitlab.com/backup/23304.html
http://www.oracle.com/technology/global/cn/pub/articles/havewala-rman-grid.html (不错)
http://microjava.javaeye.com/blog/510023
http://www.91linux.com/html/article/database/oracle/20100909/20502.html
http://blog.csdn.net/tianlesoftware/archive/2009/10/20/4699320.aspx (不错)
http://blog.csdn.net/tianlesoftware/archive/2009/12/13/4976998.aspx(不错)
在了解了相关知识之后,最后提供的备份思路是这样的:
(1)提供一个模板文件,此文件包括rman参数设置命令,备份命令,归档日志清除命令等,参考连接:http://www.oracle.com/technology/global/cn/pub/articles/havewala-rman-grid.html
(2)通过批处理脚本创建相关目录、备份脚本,将目录分为:data、log 、script。data目录用于存放数据库备份数据;log目录用于存储执行日志;script目录保存rman备份批处理脚本
(3)在系统中增加备份任务,在每日01:30进行数据库备份。
以下是模板文件:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 1 DAYS ;
CONFIGURE CONTROLFILE AUTOBACKUP ON ;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ' $ BACKUPDIR \ cf_%F' ;
CONFIGURE BACKUP OPTIMIZATION ON ;
CONFIGURE DEVICE TYPE disk PARALLELISM 3 ;
CONFIGURE DEFAULT DEVICE TYPE TO disk ;
CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT ' $ BACKUPDIR \ b_%U' ;
CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT ' $ BACKUPDIR \ b_%U' ;
CONFIGURE CHANNEL 3 DEVICE TYPE DISK FORMAT ' $ BACKUPDIR \ b_%U' ;
CONFIGURE SNAPSHOT CONTROLFILE NAME TO ' $ BACKUPDIR \ snapcf_ $ DBNAME . f' ;
# Perform backup of database and archivelogs , deleting backed up archivelogs
BACKUP DATABASE PLUS ARCHIVELOG DELETE INPUT ;
# Maintainance commands for crosschecks and deleting expired backups
ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK ;
CROSSCHECK BACKUP ;
DELETE NOPROMPT EXPIRED BACKUP ;
DELETE NOPROMPT OBSOLETE DEVICE TYPE DISK ;
CROSSCHECK ARCHIVELOG ALL ;
DELETE NOPROMPT EXPIRED ARCHIVELOG ALL ;
# End of RMAN command script
以下是批处理脚本,已经对一些步骤进行了注释:
set / p user = 请输入系统用户
set / p pwd = 请输入系统密码
:: 将ora_dba用户组权限赋予系统执行用户
net localgroup ora_dba %user% / add
set / p ORACLE_SID = 请输入备份数据库名称
set / p backDir = 请输入备份路径
if exist " %backDir% " (
rd / s / q " %backDir% "
)
md " %backDir% "
md " %backDir%\data "
md " %backDir%\log "
md " %backDir%\script "
:: 生成rman备份脚本
cd . > " %backDir%\script\rman_%ORACLE_SID%.cmd "
:: 这句话很关键,启用延缓环境变量扩展名
setlocal enabledelayedexpansion
:: 遍历模板文件
for / f " delims=: tokens=1* " %%i in ( 'findstr / n . * " %~dp0model_file.txt " ' ) do (
set current = %%j
:: 对参数使用环境变量进行替换
set current = !current: $ BACKUPDIR = %backDir%!
set current = !current: $ DBNAME = %ORACLE_SID%!
echo !current! >> " %backDir%\script\rman_%ORACLE_SID%.cmd "
)
:: 生成计划执行脚本
echo @echo off > " %backDir%\script\task_%ORACLE_SID%.bat "
echo set ORACLE_SID = %ORACLE_SID% >> " %backDir%\script\task_%ORACLE_SID%.bat "
echo rman target / msglog = " %backDir%\log\bak_%date:~0,10%.log " cmdfile = " %backDir%\script\rman_%ORACLE_SID%.cmd " >> " %backDir%\script\task_%ORACLE_SID%.bat "
schtasks / delete / tn " %ORACLE_SID%_backup " / f
schtasks / create / RU %user% / RP %pwd% / sc daily / st 01 : 30 : 00 / tn " %ORACLE_SID%_backup " / tr " %backDir%\script\task_%ORACLE_SID%.bat "
pause
以上就是所有内容了,自己记录下