Tạo Job Schedule Backup Trong Oracle
--- Bài mới hơn ---
DBMS_SCHEDULER là 1 package của Oracle (có từ version 10) cung cấp các procedure giúp tạo job.
Tạo Oracle Job gồm 3 bước:
- Tạo time schedule – dbms_scheduler.create_schedule
- Tạo program declaration – dbms_scheduler.create program
- Tạo job (conflation) – dbms_scheduler.create_job
Lưu ý
: login vào với vai trò sys và gán quyền cho user cần tạo job:
GRANT create any directory TO fhr;
GRANT create procedure TO fhr;
GRANT create table TO fhr;
GRANT create job TO fhr;
GRANT manage scheduler TO fhr;
begin -- daily from Monday to Sunday at 22:00 (10:00 p.m.) dbms_scheduler.create_schedule -- run every hour, every day dbms_scheduler.create_schedule( -- run every 5 minute, every day dbms_scheduler.create_schedule( -- run every minute, every day dbms_scheduler.create_schedule( -- run every Sunday at 18:00 (06:00 p.m.) dbms_scheduler.create_schedule end;
begin -- Call a procedure of a database package dbms_scheduler.create_program ); end;
begin -- Connect both dbms_scheduler parts by creating the final job dbms_scheduler.create_job end;
begin -- change start time DBMS_SCHEDULER.SET_ATTRIBUTE( ); -- change repeat interval DBMS_SCHEDULER.SET_ATTRIBUTE( ); end;
begin dbms_scheduler.run_job('JOB_COLLECT_SESS_DATA',TRUE); end;
begin dbms_scheduler.disable('JOB_COLLECT_INST_INFO'); dbms_scheduler.enable('JOB_COLLECT_INST_INFO'); end;
-- All jobs select * from user_scheduler_jobs; -- Get information to job select * from user_scheduler_job_log order by log_date desc; -- Show details on job run select * from user_scheduler_job_run_details;
-- View all running jobs, Run the following query from SQL*Plus SELECT chúng tôi c.serial#, chúng tôi a.failures, to_char(a.this_date, 'mm/dd/yyyy hh:mi pm') startdatetime, b.what FROM dba_jobs_running a, dba_jobs b, v$session c WHERE chúng tôi = chúng tôi AND chúng tôi = chúng tôi order by a.this_date; -- View all jobs, Run the following query from SQL*Plus SELECT job, to_char(last_date, 'mm/dd/yyyy hh:mi pm') lastdate, to_char(next_date, 'mm/dd/yyyy hh:mi pm') nextdate, failures, broken, what FROM dba_jobs ORDER BY next_date;
Ý hay là khi muốn kill 1 job mà không muốn nó tự restart lại là hãy đánh dấu broken. Chạy lệnh sau từ SQL*Plus:
-- Võìi job_id lâìy týÌ câu truy vâìn “View all running jobs”. execute dbms_job.broken(job_id, true);
Sau đó, kill session bằng 1 trong 2 cách:
#1: (the best / fast way to kill a session)
-- Find the thread you want to kill SELECT sid, spid as thread, osuser, s.program FROM sys.v_$process p, sys.v_$session s WHERE chúng tôi = s.paddr; -- Run the following for Oracle on Linux, sid is the name given to the Oracle Instance -- (the name given in the chúng tôi fle) orakill sid thread
#2:
-- Run the following query from SQL*Plus -- sid and serial# came from the "View all running jobs" query. -- The problem with this option is it can sometimes take a long time to kill the session alter system kill session 'sid, serial#'
Để remove hoàn toàn job, chạy lệnh:
-- job_id is obtained from the "View all running jobs" query. execute dbms_job.remove(job_id)
Nếu chỉ muốn stop job để fix chứ không muốn remove hoàn toàn, hãy fix job và restart bằng lệnh:
-- job_id is obtained from the "View all running jobs" query execute dbms_job.broken(job_id, false);
Ghi chú: Khi nào job trở thành ‘broken’?
- Oracle thực hiện job thành công sau 16 lần cố gắng thực hiện (có lỗi khi thực hiện job). hoặc
- Người dùng đánh dấu job là ‘broken’, dùng proc DBMS_JOB.BROKEN.
- Khi 1 job được đánh dấu là ‘broken’. Oracle sẽ không cố gắng thực hiện job nữa đến khi job được đánh dấu không còn ‘broken’ hoặc bị ép thực thi bằng lời gọi DBMS_JOB.RUN.
10. Tạo job mà không cần time scheduler và program declaration
begin dbms_scheduler.create_job ( end;
--- Bài cũ hơn ---