2017-05-10 100 views
1

我想每天自動導出完整的Oracle數據庫。 我知道爲了出口的話,我們使用COMMANDE波紋管:導出數據庫Oracle PL/SQL

exp userid=user/pass full=yes file='expdat.dmp' log='file.log' 

,但我想知道如何自動去做。 有沒有辦法用pl/sql做到這一點?

[編輯] 我創建了一個包含以下命令的.bat文件。 和我創建DBMS_SCHEDULER工作如下:

BEGIN 
dbms_scheduler.create_credential(
     credential_name => 'c', 
     username   => 'user', 
     password   => 'password'); 

    SYS.DBMS_SCHEDULER.CREATE_JOB(job_name => 'ex_job_1', 
     job_type => 'EXECUTABLE', 
     job_action => 'C:\WINDOWS\system32\cmd.exe', 
     job_class => 'DEFAULT_JOB_CLASS', 
     comments => 'Job to call batch script on Windows', 
     auto_drop => FALSE, 
     number_of_arguments => 3, 
     enabled => FALSE, 
     credential_name=>'c' 
     ); 

    SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(job_name => 'ex_job_1', argument_position => 1, argument_value => '/q'); 
    SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(job_name => 'ex_job_1', argument_position => 2, argument_value => '/c'); 
    SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(job_name => 'ex_job_1', argument_position => 3, argument_value => '"C:\Backup_folder\export_database.bat"'); 
    SYS.DBMS_SCHEDULER.ENABLE('ex_job_1'); 

DBMS_SCHEDULER.RUN_JOB('ex_job_1'); 


    END; 

,它給了我下面的錯誤:

Error Report - 
ORA-06550: Line 2, column 17: 
PLS-00302: The 'CREATE_CREDENTIAL' component must be declared 
ORA-06550: Line 2, column 2: 
PL/SQL: Statement ignored 
ORA-06550: Line 7, column 5: 
PLS-00306: number or wrong argument types in call to 'CREATE_JOB' 
ORA-06550: Line 7, column 5: 
PL/SQL: Statement ignored 
06550. 00000 - "line% s, column% s: \ n% s" 
* Cause: Usually a PL/SQL compilation error. 
*Action: 

我卡住了,我應該怎麼辦?

PS:模式導出可能足以滿足我的需求。

+0

DBMS_SCHEDULER安排作業。您可以調用主機/ OS作業。如果您調用腳本來執行exp,您的問題將隱藏用戶名/密碼。 – TenG

+0

爲什麼你想每天導出整個數據庫? – APC

+0

使用['RMAN'](https://docs.oracle.com/cd/E11882_01/backup.112/e10642/rcmquick.htm#BRADV89346)進行完整備份。 – MT0

回答

0

我設法用做日常導出整個數據庫的dbms_scheduler和dbms_datapump用下面的代碼:

begin 
    dbms_scheduler.create_schedule(
     schedule_name => 'daily_back', 
     repeat_interval => 'FREQ=DAILY;BYDAY=MON,TUE,WED,THU,FRI,SAT,SUN;BYHOUR=8;BYMINUTE=0;BYSECOND=0', 
     start_date => SYSTIMESTAMP 
); 

    dbms_scheduler.create_program 
    ( program_name => 'backup_database', 
     program_type => 'PLSQL_BLOCK', 
     program_action => 'DECLARE 
          handle NUMBER; 
          nom_job varchar(25) := to_char(SYSDATE,''DD-MM-YYYY_HH24-MI-SS''); 
          nom_dump varchar(35) := ''EXPORT_''|| nom_job || ''.DMP''; 
          nom_log varchar(35) := ''EXPORT_''|| nom_job || ''.LOG''; 
          BEGIN 
          execute immediate ''create or replace directory '' || ''BACKUP_FOLDER'' || 
              '' as '''''' || ''C:\BACKUP_FOLDER\BACKUP'' || ''''''''; 
          handle := SYS.DBMS_DATAPUMP.OPEN(operation => ''EXPORT'',job_mode => ''FULL'',remote_link => NULL,job_name => nom_job,version=> ''10.0.0''); 
          SYS.DBMS_DATAPUMP.ADD_FILE(handle=> handle,filename => nom_dump,directory => ''BACKUP_FOLDER'',filetype => 1); 
          SYS.DBMS_DATAPUMP.ADD_FILE(handle=> handle,filename=> nom_log,directory => ''BACKUP_FOLDER'',filetype => 3); 
          SYS.DBMS_DATAPUMP.START_JOB(handle => handle,skip_current => 0,abort_step => 0); 
          SYS.DBMS_DATAPUMP.DETACH(handle=> handle); END;', 
     enabled  => TRUE 
    ); 

    dbms_scheduler.create_job (
    job_name=>'daily_backup', 
    program_name =>'backup_database', 
    schedule_name=> 'daily_back', 
    enabled  => true 
); 

end; 

我希望這將有助於未來的人。