之前涉及到Oracle定期备份的问题走的都是操作系统层面的任务计划,下面用oracle的task scheduler 定期执行oracle的备份,详细步骤见下:
1 备份语句/oracle/bak/fullbk.txt如下:
[oracle@dest ~]$ cat /oracle/bak/fullbk.txt
run{
delete noprompt obsolete;
crosscheck backup;
delete noprompt expired backup;
crosscheck archivelog all;
delete noprompt expired archivelog all;
backup incremental level=0 database format '/backup/crm/full-%T-%U.bak';
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';
}
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"
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
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
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
相关阅读:
Oracle datapump expdp/impdp 导入导出数据库时hang住