之前涉及到oracle定期备份的问题走的都是操作系统层面的任务计划,下面用oracle的task scheduler 定期执行oracle的备份,详细步骤见下:
1 备份语句/oracle/bak/fullbk.txt如下:
[oracle@dest ~]$ cat /oracle/bak/fullbk.txt
run{
delete noprompt obsolete; # 以不提示的方式删除状态为obsolete的备份(注意备份是否obsolete和备份保留策略有关)
crosscheck backup; # 校验rman资料库中记录的备份是否存在于磁盘或物理设备上,如果不存在,则更改rman资料库中该备份记录为expired状态
delete noprompt expired backup; # 以不提示的方式删除rman资料库中所有标记为expired状态的记录
crosscheck archivelog all; # 校验rman资料库中所有归档记录,对在物理设备上不存在的归档,在rman资料库中更新其标记为expired
delete noprompt expired archivelog all; #以不提示的方式删除rman资料库中所有标记为expired状态的归档记录
backup incremental level=0 database format '/backup/crm/full-%T-%U.bak'; #做一个0级别的数据库全备份
backup archivelog all format '/backup/crm/arch-%T-%U.bak'; #备份所有归档
backup current controlfile format '/backup/crm/ctl-%T-%U.bak'; #备份当前控制文件
backup spfile format '/backup/crm/spf-%T-%U.bak'; # 备份参数文件
delete noprompt archivelog all completed before 'SYSDATE - 7'; # 以不提示的方式删除7天之前的所有归档
}
注意 以上脚本仅仅是为测试,该脚本对于从上一次全备份到数据库当前状态丢失了一部分归档,或者归档损坏的情况,是无法进行完全恢复的。
2 备份脚本/oracle/bak/rman.sh如下:
[oracle@dest ~]$ cat /oracle/bak/rman.sh
#!/bin/bash
export today=`date +%Y%m%d%H%M`
export ORACLE_HOME=/oracle/app/db1
export ORACLE_SID=CRM
$ORACLE_HOME/bin/rman target sys/DHHZDHHZ log=/oracle/bak/crm.log cmdfile=/oracle/bak/fullbk.txt
mv /oracle/bak/crm.log "/oracle/bak/crm${today}.log"
此脚本让rman调用/oracle/bak/fullbk.txt文本中的备份语句,记录当前备份过程于日志文件,并更改生成的日志文件名为当前日期。
3 在oracle中创建program
begin
dbms_scheduler.create_program (
program_name =>'oracle_bk_program',
program_action =>'/oracle/bak/rman.sh',
program_type =>'EXECUTABLE',
enabled =>true,
comments =>'oracle.sh');
end;
/
查询我们刚刚创建的program如下
select program_name,program_type,program_action,number_of_arguments,enabled from user_scheduler_programs where program_name='ORACLE_BK_PROGRAM';
PROGRAM_NAME PROGRAM_TYPE PROGRAM_ACTION NUMBER_OF_ARGUMENTS ENABL
------------------- ---------------- ---------------------- ------------------- -----
ORACLE_BK_PROGRAM EXECUTABLE /oracle/bak/rman.sh 0 TRUE
注意如果要修改程序,job或者shceduler则可用如下语句
BEGIN
DBMS_SCHEDULER.SET_ATTRIBUTE (
name => '',
attribute => '',
value => '');
END;
/
如下:
BEGIN
DBMS_SCHEDULER.SET_ATTRIBUTE (
name => 'oracle_bk_schedule',
attribute => 'repeat_interval',
value => 'FREQ=DAILY;BYHOUR=15,16,17');
END;
/
4 创建一个scheduler
begin
dbms_scheduler.create_schedule(
schedule_name =>'oracle_bk_schedule',
repeat_interval =>'FREQ=DAILY;BYHOUR=15,16,17',
comments =>'backup start 3pm');
end;
/
查选我们刚刚创建的scheduler如下:
SQL> select schedule_name,repeat_interval from user_scheduler_schedules where schedule_name='ORACLE_BK_SCHEDULE';
SCHEDULE_NAME REPEAT_INTERVAL
------------------------------ ------------------------------------------------------------
ORACLE_BK_SCHEDULE FREQ=DAILY;BYHOUR=17
5 创建一个job,在job中引用创建的程序和scheduler
注意 After you create a job and enable it, the Scheduler automatically runs the job according to its schedule or when the specified event is detected
begin
dbms_scheduler.create_job(
job_name =>'oracle_bk',
program_name =>'oracle_bk_program',
schedule_name =>'oracle_bk_schedule',
enabled =>true);
end;
/
查询我们刚刚创建的job如下:
SQL> select job_name,job_type,job_action, REPEAT_INTERVAL,enabled,state from user_scheduler_jobs where job_name='ORACLE_BK';
JOB_NAME JOB_TYPE JOB_ACTION REPEAT_INTERVAL ENABL STATE
------------ ------------- ----------------- ----------------- ----- ---------------
ORACLE_BK TRUE SCHEDULED
SQL>
6 查询创建job的运行状况如下:
select log_id,log_date,status,additional_info from user_scheduler_job_run_details where job_name='ORACLE_BK';
LOG_ID LOG_DATE STATUS ADDITIONAL_INFO
---------- ----------------------------------- ---------- ------------------------------
511 22-JAN-14 03.54.43.650305 PM +08:00 SUCCEEDED
514 22-JAN-14 03.58.51.953108 PM +08:00 SUCCEEDED
519 22-JAN-14 04.58.10.062466 PM +08:00 SUCCEEDED
522 22-JAN-14 05.57.50.288474 PM +08:00 SUCCEEDED
7 查询生成的备份日志文件记录如下:
[oracle@dest bak]$ ls -lt
total 304
-rw-r--r-- 1 oracle oinstall 9680 Jan 22 17:57 crm201401221756.log
-rw-r--r-- 1 oracle oinstall 10595 Jan 22 16:58 crm201401221656.log
-rw-r--r-- 1 oracle oinstall 13155 Jan 22 15:58 crm201401221556.log
-rw-r--r-- 1 oracle oinstall 12681 Jan 22 15:54 crm201401221551.log
注意 1 删除program和scheduler前应先删除调用program和schedule的job
2 之前rman.sh脚本缺少#!/bin/bash 以及rman未输入全路径导致任务计划不能执行提示脚本不能执行
3 关于oracle scheduler的详细介绍请参考官方文档
本文出自 “myblog” 博客,请务必保留此出处http://jiujian.blog.51cto.com/444665/1354083