使用oracle dbms_scheduler代替crontab

wisdomone1發表於2009-03-01
oracle10g裡用DBMS_SCHEDULER替代linux下的crontab
2009-02-24 11:00
比如完成周日0級全備、週三1級備份、其它星期為2級備份,每天都是4:30開始執行,crontab裡是如下設定:

$ crontab -l
30 4 * * 0 /orahome/backup/rman_level.sh 0
30 4 * * 3 /orahome/backup/rman_level.sh 1
30 4 * * 1,2,4,5,6 /orahome/backup/rman_level.sh 2

現在可以在oracle裡透過dbms_scheduler包來設定,完成以上功能的排程設定如下:

SQL> conn /as sysdba

SQL> BEGIN
   DBMS_SCHEDULER.drop_job (job_name => 'rman_level0');
END;
/
SQL> BEGIN
   DBMS_SCHEDULER.drop_job (job_name => 'rman_level1');
END;
/
SQL> BEGIN
   DBMS_SCHEDULER.drop_job (job_name => 'rman_level2');
END;
/

SQL> BEGIN
   DBMS_SCHEDULER.create_job (
      job_name              => 'rman_level0',
      job_type              => 'EXECUTABLE',
      job_action            => '/orahome/backup/rman_level.sh',
      number_of_arguments   => 1,
      repeat_interval       => 'freq=daily; BYDAY=SUN; byhour=4; byminute=30',
      enabled               => FALSE,
      auto_drop             => FALSE,
      comments              => 'rman incremental level 0 backup database'
   );
   DBMS_SCHEDULER.set_job_argument_value ('rman_level0', 1, '0');
   DBMS_SCHEDULER.enable ('rman_level0');
END;
/

SQL> BEGIN
   DBMS_SCHEDULER.create_job (
      job_name              => 'rman_level1',
      job_type              => 'EXECUTABLE',
      job_action            => '/orahome/backup/rman_level.sh',
      number_of_arguments   => 1,
      repeat_interval       => 'freq=daily; BYDAY=WED; byhour=4; byminute=30',
      enabled               => FALSE,
      auto_drop             => FALSE,
      comments              => 'rman incremental level 1 backup database'
   );
   DBMS_SCHEDULER.set_job_argument_value ('rman_level1', 1, '1');
   DBMS_SCHEDULER.enable ('rman_level1');
END;
/

SQL> BEGIN
   DBMS_SCHEDULER.create_job (
      job_name              => 'rman_level2',
      job_type              => 'EXECUTABLE',
      job_action            => '/orahome/backup/rman_level.sh',
      number_of_arguments   => 1,
      repeat_interval       => 'freq=daily; BYDAY=MON,TUE,THU,FRI,SAT; byhour=4; byminute=30',
      enabled               => FALSE,
      auto_drop             => FALSE,
      comments              => 'rman incremental level 2 backup database'
   );
   DBMS_SCHEDULER.set_job_argument_value ('rman_level2', 1, '2');
   DBMS_SCHEDULER.enable ('rman_level2');
END;
/

使用dbms_sheduler包來設定定時任務的主要優勢,除了安全原因外(也就是不想讓能登陸linux的使用者能看到資料庫的備份策略),還有就是可以透過檢視錶dba_scheduler_job_run_details獲得每次排程的詳細資訊:

SQL> SELECT   log_date,
           status,
           req_start_date,
           actual_start_date,
           run_duration,
           additional_info
    FROM   dba_scheduler_job_run_details
   WHERE   job_name LIKE 'RMAN%'
ORDER BY   1 DESC;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-558233/,如需轉載,請註明出處,否則將追究法律責任。

相關文章