==================
-- Create DBMS_SCHEDULER jobs for each schema
DECLARE
v_schema_names VARCHAR2(4000) := '&1';
v_bucket_name VARCHAR2(100) := '&2';
v_parallel_degree NUMBER := &3;
v_use_multiple_files VARCHAR2(1) := '&4';
v_schema_list DBMS_UTILITY.UNCL_ARRAY;
v_count NUMBER;
v_schema_name VARCHAR2(30);
BEGIN
DBMS_UTILITY.COMMA_TO_TABLE(v_schema_names, v_count, v_schema_list);
FOR i IN 1..v_count LOOP
v_schema_name := TRIM(v_schema_list(i));
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'EXP_' || v_schema_name || '_' || TO_CHAR(SYSDATE, 'HH24MISS'),
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN export_schemas_to_dir(p_schema_names => ''' || v_schema_name || ''', p_bucket_name => ''' || v_bucket_name || ''', p_parallel_degree => ' || v_parallel_degree || ', p_use_multiple_files => ''' || v_use_multiple_files || '''); END;',
start_date => SYSTIMESTAMP,
repeat_interval => NULL,
enabled => TRUE,
auto_drop => FALSE,
comments => 'Scheduled job to export schema ' || v_schema_name || ' metadata to DATA_PUMP_DIR'
);
DBMS_OUTPUT.PUT_LINE('Scheduled job created for schema: ' || v_schema_name);
END LOOP;
END;
/
1. Reading Contents of Log Files in DATA_PUMP_DIR
Log files in DATA_PUMP_DIR (e.g., hr_export.log, finance_export.log) are generated by the Data Pump export job (as specified in the script with DBMS_DATAPUMP.ADD_FILE for filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE). These files contain detailed information about the export process (e.g., start/completion times, errors, objects exported). You can read these files using the rdsadmin.rds_file_util.read_text_file procedure, which is similar to how S3 task logs are read in the BDUMP directory (as shown in the original document).
Script to Read Contents of a Specific Log File in DATA_PUMP_DIR:
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
-- Read contents of a specific log file
DECLARE
v_log_file VARCHAR2(100) := 'hr_export.log'; -- Replace with your log file name (e.g., 'finance_export.log')
BEGIN
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('DATA_PUMP_DIR', v_log_file))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error reading ' || v_log_file || ': ' || SQLERRM);
END;
/- Usage:
- Replace hr_export.log with the name of the log file (from SELECT * FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))).
- Run in Toad’s SQL Editor (Ctrl+Enter or F5).
- Output in the DBMS Output tab shows the log file contents (e.g., Data Pump job details, errors like "materialized views not exported").
- Example output:text
Starting "HR"."HR_EXPORT_20250909131700": Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA Completed 25 TABLE objects
Script to List and Read All Log Files in DATA_PUMP_DIR: This lists all log files matching your export pattern and reads their contents.
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
-- List and read all log files in DATA_PUMP_DIR
BEGIN
-- List log files
DBMS_OUTPUT.PUT_LINE('Log Files in DATA_PUMP_DIR:');
FOR file_rec IN (SELECT filename, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%.log'
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('File: ' || file_rec.filename || ' | Modified: ' || file_rec.mtime);
END LOOP;
-- Read contents of each log file
DBMS_OUTPUT.PUT_LINE('--- Log Contents ---');
FOR file_rec IN (SELECT filename
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%.log') LOOP
DBMS_OUTPUT.PUT_LINE('Reading: ' || file_rec.filename);
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('DATA_PUMP_DIR', file_rec.filename))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--- End of ' || file_rec.filename || ' ---');
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/- Usage:
- Run in SQL Editor (F5).
- Lists log files (e.g., hr_export.log, finance_export.log) and prints their contents.
- Adjust the LIKE '%_export%.log' pattern if your log files have a different naming convention.
Notes:
- Requires EXECUTE on rdsadmin.rds_file_util. If you get a privilege error, grant it as the master user:sql
GRANT EXECUTE ON rdsadmin.rds_file_util TO YOUR_USER; - Log files may be large; consider limiting output or filtering specific lines (e.g., for errors) if needed.
2. Fetching File Sizes in GBs in DATA_PUMP_DIR
The rdsadmin.rds_file_util.listdir function provides file sizes in bytes. You can convert these to GBs (1 GB = 1024^3 bytes) for easier monitoring, especially for large dump files created with multiple files (%u pattern).
Script to List File Sizes in GBs:
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
-- List all files in DATA_PUMP_DIR with sizes in GBs
BEGIN
DBMS_OUTPUT.PUT_LINE('Files in DATA_PUMP_DIR:');
FOR file_rec IN (SELECT filename, type, bytes, ROUND(bytes / 1024 / 1024 / 1024, 4) AS size_gb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('File: ' || RPAD(file_rec.filename, 30) || ' | Type: ' || RPAD(file_rec.type, 10) ||
' | Size (GB): ' || TO_CHAR(file_rec.size_gb, 'FM9990.0000') || ' | Modified: ' || file_rec.mtime);
END LOOP;
-- Total size in GBs
SELECT ROUND(SUM(bytes) / 1024 / 1024 / 1024, 4) INTO :total_size_gb
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'));
DBMS_OUTPUT.PUT_LINE('Total Size (GB): ' || TO_CHAR(:total_size_gb, 'FM9990.0000'));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/- Usage:
- Run in SQL Editor (F5).
- Output shows each file’s size in GBs and the total size (e.g., hr_export_u01.dmp | DUMP | Size (GB): 1.2345).
- Example output:text
File: hr_export_u01.dmp | Type: DUMP | Size (GB): 1.2345 | Modified: 09-SEP-25 13:17:00 File: hr_export.log | Type: LOG | Size (GB): 0.0001 | Modified: 09-SEP-25 13:17:00 Total Size (GB): 1.2346
Script to List Sizes for Specific Export Files: Filters for files matching your export pattern (e.g., hr_export*).
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
-- List sizes for specific export files
BEGIN
DBMS_OUTPUT.PUT_LINE('Export Files in DATA_PUMP_DIR:');
FOR file_rec IN (SELECT filename, type, bytes, ROUND(bytes / 1024 / 1024 / 1024, 4) AS size_gb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%' -- Adjust pattern (e.g., '%_export_u%.dmp' for multiple files)
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('File: ' || RPAD(file_rec.filename, 30) || ' | Type: ' || RPAD(file_rec.type, 10) ||
' | Size (GB): ' || TO_CHAR(file_rec.size_gb, 'FM9990.0000') || ' | Modified: ' || file_rec.mtime);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/- Usage:
- Adjust the LIKE clause (e.g., '%_export_u%.dmp' for multiple dump files, '%_export%.log' for logs).
- Run in SQL Editor (F5).
3. Comprehensive Monitoring Scripts
These combine monitoring for scheduler jobs, Data Pump jobs, S3 upload tasks, and DATA_PUMP_DIR files (including log contents and sizes in GBs).
Script to Monitor All Aspects:
-- Set formatting
SET LINESIZE 180;
SET PAGESIZE 100;
SET SERVEROUTPUT ON;
BEGIN
-- Scheduler Jobs
DBMS_OUTPUT.PUT_LINE('--- Scheduler Jobs ---');
FOR rec IN (SELECT job_name, state, last_start_date, last_run_duration
FROM dba_scheduler_jobs
WHERE job_name LIKE 'EXPORT_%_JOB_%'
ORDER BY last_start_date DESC) LOOP
DBMS_OUTPUT.PUT_LINE('Job: ' || RPAD(rec.job_name, 30) || ' | State: ' || RPAD(rec.state, 10) ||
' | Last Start: ' || rec.last_start_date || ' | Duration: ' || rec.last_run_duration);
END LOOP;
-- Scheduler Job Run Details
DBMS_OUTPUT.PUT_LINE('--- Scheduler Job Run Details ---');
FOR rec IN (SELECT job_name, status, error#, additional_info
FROM dba_scheduler_job_run_details
WHERE job_name LIKE 'EXPORT_%_JOB_%'
ORDER BY log_date DESC FETCH FIRST 10 ROWS ONLY) LOOP
DBMS_OUTPUT.PUT_LINE('Job: ' || RPAD(rec.job_name, 30) || ' | Status: ' || RPAD(rec.status, 10) ||
' | Error#: ' || rec.error# || ' | Info: ' || SUBSTR(rec.additional_info, 1, 100));
END LOOP;
-- Data Pump Jobs
DBMS_OUTPUT.PUT_LINE('--- Data Pump Jobs ---');
FOR rec IN (SELECT owner_name, job_name, state, operation, job_mode, degree
FROM dba_datapump_jobs
ORDER BY job_name DESC) LOOP
DBMS_OUTPUT.PUT_LINE('Job: ' || RPAD(rec.job_name, 30) || ' | Owner: ' || RPAD(rec.owner_name, 15) ||
' | State: ' || rec.state || ' | Parallelism: ' || rec.degree);
END LOOP;
-- Files in DATA_PUMP_DIR with Sizes in GBs
DBMS_OUTPUT.PUT_LINE('--- Files in DATA_PUMP_DIR ---');
FOR file_rec IN (SELECT filename, type, ROUND(bytes / 1024 / 1024 / 1024, 4) AS size_gb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%'
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('File: ' || RPAD(file_rec.filename, 30) || ' | Type: ' || RPAD(file_rec.type, 10) ||
' | Size (GB): ' || TO_CHAR(file_rec.size_gb, 'FM9990.0000') || ' | Modified: ' || file_rec.mtime);
END LOOP;
-- Total Size in GBs
DECLARE
v_total_size_gb NUMBER;
BEGIN
SELECT ROUND(SUM(bytes) / 1024 / 1024 / 1024, 4) INTO v_total_size_gb
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'));
DBMS_OUTPUT.PUT_LINE('Total Size (GB): ' || TO_CHAR(v_total_size_gb, 'FM9990.0000'));
END;
-- Read Log Files in DATA_PUMP_DIR
DBMS_OUTPUT.PUT_LINE('--- Log File Contents ---');
FOR file_rec IN (SELECT filename
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%.log'
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('Reading: ' || file_rec.filename);
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('DATA_PUMP_DIR', file_rec.filename))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--- End of ' || file_rec.filename || ' ---');
END LOOP;
-- Recent S3 Task Logs in BDUMP
DBMS_OUTPUT.PUT_LINE('--- Recent S3 Task Logs in BDUMP ---');
FOR rec IN (SELECT filename, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('BDUMP'))
WHERE filename LIKE 'dbtask-%.log'
ORDER BY mtime DESC FETCH FIRST 10 ROWS ONLY) LOOP
DBMS_OUTPUT.PUT_LINE('Task Log: ' || rec.filename || ' | Modified: ' || rec.mtime);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/- Usage:
- Run in SQL Editor (F5).
- Outputs scheduler job status, Data Pump job status, file sizes in GBs, log file contents, and recent S3 task logs.
4. S3 Upload Task Monitoring (After Manual Upload)
Since you’re using Option 1 and manually uploading to S3 with rdsadmin.rdsadmin_s3_tasks.upload_to_s3, here’s a script to initiate and monitor uploads in parallel.
Script to Initiate and Monitor S3 Uploads:
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
-- Initiate S3 uploads for all schemas and monitor
DECLARE
v_schema_names VARCHAR2(4000) := 'HR,FINANCE'; -- Same schemas as export
v_bucket_name VARCHAR2(100) := 'mys3bucket';
v_use_multiple_files VARCHAR2(1) := 'Y'; -- Match export setting
v_schema_list DBMS_UTILITY.UNCL_ARRAY;
v_count NUMBER;
v_schema_name VARCHAR2(30);
v_prefix VARCHAR2(100);
v_task_id VARCHAR2(100);
TYPE t_task_ids IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER;
v_task_ids t_task_ids;
v_status VARCHAR2(4000);
BEGIN
-- Submit uploads
DBMS_UTILITY.COMMA_TO_TABLE(v_schema_names, v_count, v_schema_list);
FOR i IN 1..v_count LOOP
v_schema_name := TRIM(v_schema_list(i));
IF v_use_multiple_files = 'Y' THEN
v_prefix := LOWER(v_schema_name) || '_export_u';
ELSE
v_prefix := LOWER(v_schema_name) || '_export';
END IF;
v_task_id := rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => v_bucket_name,
p_prefix => v_prefix,
p_s3_prefix => '',
p_directory_name => 'DATA_PUMP_DIR'
);
v_task_ids(i) := v_task_id;
DBMS_OUTPUT.PUT_LINE('S3 Upload Task ID for ' || v_schema_name || ': ' || v_task_id);
END LOOP;
-- Monitor upload tasks (optional polling)
FOR i IN 1..v_count LOOP
v_schema_name := TRIM(v_schema_list(i));
DBMS_OUTPUT.PUT_LINE('Checking status for Task ID: ' || v_task_ids(i));
BEGIN
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('BDUMP', 'dbtask-' || v_task_ids(i) || '.log'))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error reading task log for ' || v_task_ids(i) || ': ' || SQLERRM);
END;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/- Usage:
- Run after export jobs complete.
- Submits uploads in parallel (each upload_to_s3 call returns immediately).
- Outputs task_id for each schema and reads task logs immediately (logs may not be complete if uploads are still running).
- Re-run the log-reading part later for updated status:sql
SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('BDUMP', 'dbtask-1663788182757-49.log'));
Execution Workflow for Option 1
- Run Export Jobs:
- Load schedule_export_multiple_schemas_no_s3.sql.
- Hardcode parameters:sql
v_schema_names VARCHAR2(4000) := 'HR,FINANCE'; v_bucket_name VARCHAR2(100) := 'mys3bucket'; v_parallel_degree NUMBER := 4; v_use_multiple_files VARCHAR2(1) := 'Y'; - Execute with F5.
- Monitor Exports:
- Use the comprehensive monitoring script above.
- Upload to S3:
- Run the S3 upload script to submit uploads in parallel.
- Cleanup:
- After verifying S3 uploads:sql
DECLARE CURSOR c_files IS SELECT filename FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR')) WHERE filename LIKE '%_export%'; BEGIN FOR rec IN c_files LOOP UTL_FILE.FREMOVE('DATA_PUMP_DIR', rec.filename); DBMS_OUTPUT.PUT_LINE('Removed: ' || rec.filename); END LOOP; END; /
- After verifying S3 uploads:
Troubleshooting
- Log File Access:
- If rdsadmin.rds_file_util.read_text_file fails, grant:sql
GRANT EXECUTE ON rdsadmin.rds_file_util TO YOUR_USER;
- If rdsadmin.rds_file_util.read_text_file fails, grant:
- Large Log Files:
- Filter specific lines (e.g., errors) by modifying the read script:sql
WHERE text LIKE '%ERROR%';
- Filter specific lines (e.g., errors) by modifying the read script:
- S3 Upload Failures:
- Check IAM role and S3_INTEGRATION:sql
SELECT * FROM v$option WHERE parameter = 'S3_INTEGRATION';
- Check IAM role and S3_INTEGRATION:
If you need automation (e.g., polling S3 tasks until completion) or additional monitoring features, let me know!
scheduler:
Monitoring Scripts for Oracle RDS Data Pump Export Job, S3 Upload, and Related Operations
Below, I'll provide SQL scripts for monitoring all aspects of the process based on the original document and our discussion. These include:
- Fetching and monitoring S3 upload task IDs (including how to fetch if not captured initially).
- Data Pump job monitoring.
- File monitoring in DATA_PUMP_DIR (list files, sizes, and sizes of each file created).
- Scheduler job monitoring (state, details, logs).
- Cleanup scripts for DATA_PUMP_DIR.
- Additional comprehensive queries for overall job monitoring (e.g., combining scheduler, Data Pump, and files).
All scripts are designed to be run in Toad for Oracle (or SQL*Plus) as the master user or a privileged user. Enable DBMS_OUTPUT in Toad (View > DBMS Output, click the green Play button) for output. Use SET LINESIZE 180 and SET PAGESIZE 100 for better formatting.
1. S3 Upload Task ID Monitoring
The rdsadmin.rdsadmin_s3_tasks.upload_to_s3 procedure returns a task_id immediately, and the upload runs in the background. If you didn't capture the task_id during execution, there's no direct system view to list all historical task IDs. However, you can fetch recent task logs from the BDUMP directory (where RDS stores task logs as dbtask-task_id.log), list recent files, and read them to identify upload tasks.
Script to Fetch Recent S3 Upload Task IDs (if not captured): This lists recent dbtask-*.log files in BDUMP (sorted by mtime), extracts potential task IDs from filenames, and reads the log content to confirm if it's an S3 upload task.
-- Set formatting
SET LINESIZE 180;
SET PAGESIZE 100;
-- List recent task logs in BDUMP (sorted by most recent)
SELECT filename, type, bytes, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('BDUMP'))
WHERE filename LIKE 'dbtask-%.log'
ORDER BY mtime DESC;
-- To fetch and read a specific task log (replace 'task_id' with one from above, e.g., from filename 'dbtask-1663788182757-49.log')
DECLARE
v_task_id VARCHAR2(100) := '1663788182757-49'; -- Replace with actual task ID from filename
BEGIN
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('BDUMP', 'dbtask-' || v_task_id || '.log'))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
END;
/- Usage: Run the first query to list recent logs (e.g., look for files created around the time of your upload). Extract the task ID from the filename (e.g., 1663788182757-49 from dbtask-1663788182757-49.log). Then run the second block to read the log and confirm it's an S3 upload (look for lines like "Upload to S3 started" or "Completed").
- Effective Way if Not Captured: Always capture task_id in future runs by assigning it to a variable and outputting it (as in the upload script). If lost, use the above to scan recent logs (BDUMP retains logs for a period, but old ones may be purged).
Script to Monitor S3 Upload Task Status (if task_id known):
-- Replace with your task_id
DECLARE
v_task_id VARCHAR2(100) := '1663788182757-49';
BEGIN
FOR log_line IN (SELECT text FROM TABLE(rdsadmin.rds_file_util.read_text_file('BDUMP', 'dbtask-' || v_task_id || '.log'))) LOOP
DBMS_OUTPUT.PUT_LINE(log_line.text);
END LOOP;
END;
/- Output: Shows the full log, including status (e.g., "Starting upload", "Completed successfully", or errors).
2. Data Pump Job Monitoring
Monitor running or completed Data Pump jobs, including state and owner.
Script to Monitor Data Pump Jobs:
-- Set formatting
SET LINESIZE 180;
-- Check executing Data Pump jobs
SELECT owner_name, job_name, state, operation, job_mode, degree
FROM dba_datapump_jobs
WHERE state = 'EXECUTING'
ORDER BY job_name;
-- Check all Data Pump jobs (including completed or failed)
SELECT owner_name, job_name, state, operation, job_mode, degree
FROM dba_datapump_jobs
ORDER BY job_name DESC;
-- Detailed view if v$datapump_job is accessible (may require additional privileges)
SELECT * FROM v$datapump_job;- Usage: Run periodically to see if jobs are EXECUTING, COMPLETED, or NOT RUNNING. The degreecolumn shows parallelism.
3. File Size Monitoring in DATA_PUMP_DIR
Monitor files in DATA_PUMP_DIR, including sizes (in bytes) and total size.
Script to List Files and Sizes in DATA_PUMP_DIR:
-- Set formatting
SET LINESIZE 180;
-- List all files in DATA_PUMP_DIR with sizes (bytes), type, and mtime
SELECT filename, type, bytes AS size_bytes, ROUND(bytes / 1024 / 1024, 2) AS size_mb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
ORDER BY mtime DESC;
-- Total size of all files in DATA_PUMP_DIR
SELECT SUM(bytes) AS total_size_bytes, ROUND(SUM(bytes) / 1024 / 1024, 2) AS total_size_mb
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'));- Usage: Run to see individual file sizes (size_mb) and total directory size. Filter for specific files, e.g., WHERE filename LIKE 'hr_export%'.
Script to Monitor Size of Each File Created: This filters for files matching your export prefix (e.g., hr_export*).
-- Replace 'prefix' with your file prefix (e.g., 'hr_export')
SELECT filename, type, bytes AS size_bytes, ROUND(bytes / 1024 / 1024, 2) AS size_mb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%' -- Adjust for your dump file pattern (e.g., '%export_u%')
ORDER BY mtime DESC;- Usage: Adjust the LIKE clause based on your dump file names (e.g., '%_export.dmp' for single files or '%_export_u%.dmp' for multiple).
4. Scheduler Job Monitoring
Monitor the DBMS_SCHEDULER jobs, including state, run details, and logs.
Script to Monitor Scheduler Jobs:
-- Set formatting
SET LINESIZE 180;
-- Check state of scheduler jobs
SELECT job_name, state, enabled, run_count, last_start_date, last_run_duration, next_run_date
FROM dba_scheduler_jobs
WHERE job_name LIKE 'EXPORT_%_JOB_%'
ORDER BY last_start_date DESC;
-- Detailed run history and logs for scheduler jobs
SELECT job_name, status, error#, additional_info
FROM dba_scheduler_job_run_details
WHERE job_name LIKE 'EXPORT_%_JOB_%'
ORDER BY log_date DESC;- Usage: Run to see if jobs are RUNNING, SUCCEEDED, or FAILED. The additional_info column shows any errors or output.
5. Cleanup Scripts
Clean up DATA_PUMP_DIR after verifying S3 uploads.
Script for Cleanup:
-- List files before cleanup
SELECT * FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR')) ORDER BY mtime;
-- Remove a specific file (repeat for each file)
EXEC UTL_FILE.FREMOVE('DATA_PUMP_DIR', 'hr_export.dmp');
-- Bulk cleanup (remove all export files; run carefully!)
DECLARE
CURSOR c_files IS
SELECT filename
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
WHERE filename LIKE '%_export%';
BEGIN
FOR rec IN c_files LOOP
UTL_FILE.FREMOVE('DATA_PUMP_DIR', rec.filename);
DBMS_OUTPUT.PUT_LINE('Removed: ' || rec.filename);
END LOOP;
END;
/- Usage: Run the list query first to confirm files. Use FREMOVE for individual files or the bulk block for all matching files.
6. Comprehensive Job Monitoring Script (All in One)
This combines all monitoring into a single script that outputs key details.
Script for Overall Monitoring:
-- Set formatting
SET LINESIZE 180;
SET SERVEROUTPUT ON;
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Scheduler Jobs ---');
FOR rec IN (SELECT job_name, state, last_start_date, last_run_duration
FROM dba_scheduler_jobs
WHERE job_name LIKE 'EXPORT_%_JOB_%'
ORDER BY last_start_date DESC) LOOP
DBMS_OUTPUT.PUT_LINE('Job: ' || rec.job_name || ' | State: ' || rec.state || ' | Last Start: ' || rec.last_start_date || ' | Duration: ' || rec.last_run_duration);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--- Data Pump Jobs ---');
FOR rec IN (SELECT owner_name, job_name, state
FROM dba_datapump_jobs
ORDER BY job_name DESC) LOOP
DBMS_OUTPUT.PUT_LINE('Job: ' || rec.job_name || ' | Owner: ' || rec.owner_name || ' | State: ' || rec.state);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--- Files in DATA_PUMP_DIR ---');
FOR rec IN (SELECT filename, type, ROUND(bytes / 1024 / 1024, 2) AS size_mb, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('DATA_PUMP_DIR'))
ORDER BY mtime DESC) LOOP
DBMS_OUTPUT.PUT_LINE('File: ' || rec.filename || ' | Type: ' || rec.type || ' | Size (MB): ' || rec.size_mb || ' | MTime: ' || rec.mtime);
END LOOP;
DBMS_OUTPUT.PUT_LINE('--- Recent S3 Task Logs in BDUMP ---');
FOR rec IN (SELECT filename, mtime
FROM TABLE(rdsadmin.rds_file_util.listdir('BDUMP'))
WHERE filename LIKE 'dbtask-%.log'
ORDER BY mtime DESC FETCH FIRST 10 ROWS ONLY) LOOP
DBMS_OUTPUT.PUT_LINE('Task Log: ' || rec.filename || ' | MTime: ' || rec.mtime);
END LOOP;
END;
/- Usage: Run this script (F5) for a snapshot of all monitoring aspects. It outputs scheduler state, Data Pump state, file sizes, and recent task logs.
If you need modifications (e.g., alerts or automation), let me know!
Below, I’ll provide:
- A generic script to export a single schema, which you can reuse for each of SCHEMA_01, SCHEMA_02, SCHEMA_03, and SCHEMA_07.
- Individual scripts for each schema, with unique dump file names and S3 upload commands.
- Steps to run and monitor these jobs, considering your context of tablespace consolidation (40 old to 8 new) and Toad session disconnects.
Key Modifications
- Individual Dump Files: Each schema gets its own dump file (e.g., schema_01.dmp) instead of a combined small_schemas.dmp.
- Metadata-Only: Retains CONTENT => 'METADATA' and excludes INDEX, STATISTICS as per your original expdp example.
- No Parallelism: Since these are small schemas, parallelism is set to 1 (default), unlike the large schemas (SCHEMA_04, SCHEMA_05, SCHEMA_06 with parallel=24).
- S3 Integration: Each dump file is uploaded to a unique S3 path for clarity.
- Background Execution: Scripts include DBMS_DATAPUMP.DETACH to ensure jobs run in the background if Toad disconnects due to a time limit.
- Robustness: Error handling, job handle output for monitoring, and compatibility with RDS constraints.
Step 1: Generic Script for Single Schema Export
sql
DECLARE
hdl NUMBER; -- Job handle
job_state VARCHAR2(30); -- Job status
schema_name VARCHAR2(30) := '&1'; -- Replace with schema name (e.g., SCHEMA_01)
dump_file VARCHAR2(100) := LOWER(schema_name) || '.dmp';
log_file VARCHAR2(100) := LOWER(schema_name) || '_export.log';
BEGIN
-- Open export job for single schema
hdl := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => schema_name || '_EXPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
-- Add dump and log files
DBMS_DATAPUMP.ADD_FILE(
handle => hdl,
filename => dump_file,
directory => 'DATA_PUMP_DIR',
filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE
);
DBMS_DATAPUMP.ADD_FILE(
handle => hdl,
filename => log_file,
directory => 'DATA_PUMP_DIR',
filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE
);
-- Specify single schema
DBMS_DATAPUMP.METADATA_FILTER(
handle => hdl,
name => 'SCHEMA_LIST',
value => '''' || schema_name || ''''
);
-- Metadata only
DBMS_DATAPUMP.SET_PARAMETER(
handle => hdl,
name => 'CONTENT',
value => 'METADATA'
);
-- Exclude INDEX and STATISTICS
DBMS_DATAPUMP.METADATA_FILTER(
handle => hdl,
name => 'EXCLUDE_PATH_EXPR',
value => 'IN (''INDEX'', ''STATISTICS'')'
);
-- Start the job
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Schema: ' || schema_name || ' | Dump File: ' || dump_file);
-- Detach to run in background
DBMS_DATAPUMP.DETACH(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job started for ' || schema_name || '. Detaching...');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for ' || schema_name || ': ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/Usage:
- Run in SQL*Plus or Toad as rdsadm/frusi@db_name.
- Replace &1 with the schema name (e.g., SCHEMA_01) or hardcode it.
- Example: SQL> @export_single_schema.sql SCHEMA_01
- Output: Job handle and dump file name (e.g., schema_01.dmp).
S3 Upload (run after job completes):
BEGIN
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_prefix => 'dmp_files/small/' || LOWER('&1') || '/',
p_directory_name => 'DATA_PUMP_DIR'
);
END;
/Step 2: Individual Scripts for Each Schema
For convenience, here are specific scripts for SCHEMA_01, SCHEMA_02, SCHEMA_03, and SCHEMA_07, with unique dump files and S3 paths.
Export SCHEMA_01
DECLARE
hdl NUMBER;
job_state VARCHAR2(30);
BEGIN
hdl := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => 'SCHEMA_01_EXPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_01.dmp', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_01_export.log', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'SCHEMA_LIST', value => '''SCHEMA_01''');
DBMS_DATAPUMP.SET_PARAMETER(handle => hdl, name => 'CONTENT', value => 'METADATA');
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'EXCLUDE_PATH_EXPR', value => 'IN (''INDEX'', ''STATISTICS'')');
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Dump File: schema_01.dmp');
DBMS_DATAPUMP.DETACH(handle => hdl);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for SCHEMA_01: ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/
-- Upload to S3
BEGIN
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_prefix => 'dmp_files/small/schema_01/',
p_directory_name => 'DATA_PUMP_DIR'
);
END;
/Export SCHEMA_02
DECLARE
hdl NUMBER;
job_state VARCHAR2(30);
BEGIN
hdl := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => 'SCHEMA_02_EXPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_02.dmp', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_02_export.log', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'SCHEMA_LIST', value => '''SCHEMA_02''');
DBMS_DATAPUMP.SET_PARAMETER(handle => hdl, name => 'CONTENT', value => 'METADATA');
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'EXCLUDE_PATH_EXPR', value => 'IN (''INDEX'', ''STATISTICS'')');
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Dump File: schema_02.dmp');
DBMS_DATAPUMP.DETACH(handle => hdl);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for SCHEMA_02: ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/
-- Upload to S3
BEGIN
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_prefix => 'dmp_files/small/schema_02/',
p_directory_name => 'DATA_PUMP_DIR'
);
END;
/Export SCHEMA_03
DECLARE
hdl NUMBER;
job_state VARCHAR2(30);
BEGIN
hdl := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => 'SCHEMA_03_EXPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_03.dmp', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_03_export.log', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'SCHEMA_LIST', value => '''SCHEMA_03''');
DBMS_DATAPUMP.SET_PARAMETER(handle => hdl, name => 'CONTENT', value => 'METADATA');
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'EXCLUDE_PATH_EXPR', value => 'IN (''INDEX'', ''STATISTICS'')');
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Dump File: schema_03.dmp');
DBMS_DATAPUMP.DETACH(handle => hdl);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for SCHEMA_03: ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/
-- Upload to S3
BEGIN
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_prefix => 'dmp_files/small/schema_03/',
p_directory_name => 'DATA_PUMP_DIR'
);
END;
/Export SCHEMA_07
DECLARE
hdl NUMBER;
job_state VARCHAR2(30);
BEGIN
hdl := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => 'SCHEMA_07_EXPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_07.dmp', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_07_export.log', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'SCHEMA_LIST', value => '''SCHEMA_07''');
DBMS_DATAPUMP.SET_PARAMETER(handle => hdl, name => 'CONTENT', value => 'METADATA');
DBMS_DATAPUMP.METADATA_FILTER(handle => hdl, name => 'EXCLUDE_PATH_EXPR', value => 'IN (''INDEX'', ''STATISTICS'')');
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Dump File: schema_07.dmp');
DBMS_DATAPUMP.DETACH(handle => hdl);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for SCHEMA_07: ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/
-- Upload to S3
BEGIN
rdsadmin.rdsadmin_s3_tasks.upload_to_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_prefix => 'dmp_files/small/schema_07/',
p_directory_name => 'DATA_PUMP_DIR'
);
END;
/Step 3: Running and Monitoring the Jobs
- Run Exports:
- Execute each script in Toad or SQL*Plus as rdsadm/frusi@db_name.
- Run sequentially or in parallel sessions (since these are small schemas, sequential is fine unless time is critical).
- Note the job handle for each job (e.g., Job Handle: 1234 | Dump File: schema_01.dmp).
- Jobs run in the background after DETACH, even if Toad disconnects due to a time limit.
- Monitor Jobs:
- Use the monitoring scripts from previous responses to track progress:
- Real-Time (Job Handle): Use monitor_datapump_job.sql with the job handle (replace :job_handle).
- All Jobs (No Handle): Use monitor_datapump_views.sql to check dba_datapump_jobs:sql
SELECT job_name, state, attached_sessions FROM dba_datapump_jobs WHERE state NOT IN ('NOT RUNNING') ORDER BY job_name; - Logged History: Use monitor_datapump_log.sql to log to datapump_monitor_log:sql
SELECT job_name, state, percent_done, error_message FROM datapump_monitor_log WHERE log_time > SYSDATE - 1/24 ORDER BY log_time DESC;
- Use the monitoring scripts from previous responses to track progress:
- Upload to S3:
- Run the S3 upload block after each job completes (or monitor logs to confirm completion).
- Example: After SCHEMA_01 export, run its S3 upload script.
- Verify Dump Files:sql
SELECT filename FROM dba_directories WHERE directory_name = 'DATA_PUMP_DIR';
Step 4: Import Considerations
For importing these individual dump files, you’ll need corresponding import scripts with REMAP_TABLESPACE. Here’s a generic import script for a single schema (e.g., SCHEMA_01):
DECLARE
hdl NUMBER;
job_state VARCHAR2(30);
BEGIN
rdsadmin.rdsadmin_s3_tasks.download_from_s3(
p_bucket_name => 'your-rds-s3-bucket',
p_s3_prefix => 'dmp_files/small/schema_01/',
p_directory_name => 'DATA_PUMP_DIR'
);
hdl := DBMS_DATAPUMP.OPEN(
operation => 'IMPORT',
job_mode => 'SCHEMA',
job_name => 'SCHEMA_01_IMPORT_' || TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')
);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_01.dmp', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE);
DBMS_DATAPUMP.ADD_FILE(handle => hdl, filename => 'schema_01_import.log', directory => 'DATA_PUMP_DIR', filetype => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
FOR rec IN (SELECT old_tablespace, new_tablespace FROM ts_mappings) LOOP
DBMS_DATAPUMP.METADATA_REMAP(handle => hdl, name => 'REMAP_TABLESPACE', old_value => rec.old_tablespace, value => rec.new_tablespace);
END LOOP;
DBMS_DATAPUMP.METADATA_TRANSFORM(handle => hdl, name => 'LOB_STORAGE', value => 'SECUREFILE');
DBMS_DATAPUMP.START_JOB(handle => hdl);
DBMS_OUTPUT.PUT_LINE('Job Handle: ' || hdl || ' | Dump File: schema_01.dmp');
DBMS_DATAPUMP.DETACH(handle => hdl);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error for SCHEMA_01: ' || SQLERRM);
DBMS_DATAPUMP.DETACH(handle => hdl);
RAISE;
END;
/Repeat for Other Schemas:
- Create similar scripts for SCHEMA_02, SCHEMA_03, SCHEMA_07, updating the dump file and S3 prefix (e.g., schema_02.dmp, dmp_files/small/schema_02/).
Additional Notes
- Why Individual Exports?:
- Modularity: Easier to manage, troubleshoot, or re-run specific schemas.
- Flexibility: Import schemas independently if needed.
- Smaller files: Metadata-only exports are typically small, so splitting doesn’t impact storage significantly.
- RDS Considerations:
- Ensure new tablespaces (e.g., TRU_DATA_01) exist on the target before import.
- Verify S3 bucket permissions and IAM roles.
- Monitor CloudWatch for FreeStorageSpace and CPUUtilization.
- Toad Disconnect:
- Jobs run in the background after DETACH, even if Toad times out.
- Use monitoring scripts (from previous responses) to check status post-disconnect.
- Validation:
- After export, check DATA_PUMP_DIR for schema_01.dmp, etc.
- After import, validate object placement:sql
SELECT object_type, tablespace_name, COUNT(*) FROM dba_objects WHERE owner IN ('SCHEMA_01', 'SCHEMA_02', 'SCHEMA_03', 'SCHEMA_07') GROUP BY object_type, tablespace_name;
If you provide specific schema names or ts_mappings details, I can further customize the scripts. Let me know if you need import scripts for all schemas or additional monitoring steps!
No comments:
Post a Comment