Refresh a Schema with Oracle Data Pump: expdp and impdp Explained Step by Step
Updated: Oct 24
Refreshing a test or development schema from production is a routine DBA task, and Oracle Data Pump (expdp and impdp) is the standard tool for it. Done carelessly, it produces inconsistent data, fills a file system, or overwrites the wrong schema. Done properly, it is fast and repeatable.
This post walks through a full schema refresh, from production export to a verified import on the test database. Every step explains what the options do and why they are there.
The Scenario
Source: production database PROD, schema SALES.
Target: test database TEST, schema SALES_TEST in tablespace TEST_DATA.
Goal: a consistent copy of SALES as of one moment in time.
Step 1: Create a Directory Object on the Source
Data Pump runs inside the database server, not in your client session, so it can only write to directories the database knows about. A directory object maps a name to an OS path. The oracle OS user must be able to write to that path.
SQL |
-- As a DBA on PROD CREATE OR REPLACE DIRECTORY dp_dir AS '/u04/datapump'; GRANT READ, WRITE ON DIRECTORY dp_dir TO system;
SELECT directory_name, directory_path FROM dba_directories WHERE directory_name = 'DP_DIR'; |
Shell |
# As the oracle OS user mkdir -p /u04/datapump df -h /u04 |
What this does: The directory object DP_DIR points at /u04/datapump. On RAC, the path must be on shared storage (for example ACFS or NFS) if you run with PARALLEL and CLUSTER=Y, because workers can run on any node. Otherwise add CLUSTER=N to the export.
Step 2: Estimate the Export Size
Never start a large export without knowing whether it fits. ESTIMATE_ONLY calculates the size without writing a dump file.
Shell |
expdp system schemas=SALES directory=DP_DIR estimate_only=YES estimate=STATISTICS |
What to expect: A line such as "Total estimation using STATISTICS method: 182.4 GB". The estimate is for uncompressed table data only. Make sure the directory has at least this much free space, or use compression.
Tip: Compression of data in the dump file (COMPRESSION=ALL or DATA_ONLY) requires the Advanced Compression option. COMPRESSION=METADATA_ONLY is the default and needs no extra licence.
Step 3: Write an Export Parameter File
A parameter file avoids shell quoting problems, documents exactly what you ran, and makes the refresh repeatable.
Shell |
cat > /u04/datapump/exp_sales.par <<'EOF' schemas=SALES directory=DP_DIR dumpfile=sales_%U.dmp logfile=exp_sales.log parallel=8 filesize=20G flashback_time=systimestamp exclude=statistics metrics=yes logtime=all EOF |
Here is what each parameter does:
schemas=SALES exports all objects owned by SALES.
dumpfile=sales_%U.dmp with %U creates numbered files (sales_01.dmp, sales_02.dmp and so on), which lets parallel workers write at the same time.
parallel=8 uses up to eight worker processes. A good starting point is two per CPU core available for the job, but not more than the number of dump files.
filesize=20G caps each dump file, which makes copying easier.
flashback_time=systimestamp makes the whole export consistent as of the moment it starts. Without it, each table is consistent only with itself, and parent and child tables can disagree.
exclude=statistics skips optimizer statistics. It is usually faster and cleaner to gather fresh statistics on the target.
metrics=yes and logtime=all add timing information to the log, which helps you tune future runs.
Step 4: Run the Export
Start the export with the parameter file. Use nohup or a terminal multiplexer so that a dropped SSH session does not matter. The job keeps running in the database even if your client disconnects, but it is easier to follow when the client stays attached.
Shell |
cd /u04/datapump nohup expdp system parfile=exp_sales.par > exp_sales.out 2>&1 & tail -f exp_sales.log |
What to expect: The log lists each object type as it is processed and ends with "Job SYS_EXPORT_SCHEMA_01 successfully completed". Any line containing ORA- needs to be read. Some warnings, for example about objects that no longer compile, may be acceptable.
Step 5: Monitor the Job While It Runs
Data Pump jobs can be watched from the database and controlled interactively. This is useful for long jobs or when you need to change the parallel degree on the fly.
SQL |
SELECT owner_name, job_name, operation, job_mode, state, degree, attached_sessions FROM dba_datapump_jobs;
-- Progress of the long-running work SELECT sid, opname, sofar, totalwork, ROUND(sofar/NULLIF(totalwork,0)*100,1) AS pct, time_remaining FROM v$session_longops WHERE opname LIKE 'SYS_EXPORT%' AND sofar <> totalwork; |
Shell |
# Attach to the running job from another terminal expdp system attach=SYS_EXPORT_SCHEMA_01
Export> STATUS Export> PARALLEL=12 Export> CONTINUE_CLIENT |
What this does: DBA_DATAPUMP_JOBS shows the job state (EXECUTING, IDLING, NOT RUNNING). After attaching, STATUS prints per-worker progress, PARALLEL changes the number of workers, and CONTINUE_CLIENT returns to logging mode. STOP_JOB pauses a job so it can be restarted later with START_JOB.
Step 6: Copy the Dump Files to the Target Server
Move the dump files to a directory on the test server. Check the files arrived intact before you import, especially over slow or unreliable links.
Shell |
# On the source cd /u04/datapump && sha256sum sales_*.dmp > sales.sha256
scp sales_*.dmp sales.sha256 oracle@testdb01:/u04/datapump/
# On the target cd /u04/datapump && sha256sum -c sales.sha256 |
What to expect: Every file reports OK. A corrupted dump file will fail halfway through the import, which wastes far more time than this check.
Step 7: Prepare the Target Database
On the target, create the directory object and decide what happens to the existing schema. The cleanest refresh drops and recreates the target schema, so no old objects are left behind. Double-check the database name before you drop anything.
SQL |
-- On TEST. Make sure you are on the right database! SELECT name, db_unique_name FROM v$database;
CREATE OR REPLACE DIRECTORY dp_dir AS '/u04/datapump';
-- Disconnect application sessions using the schema SELECT sid, serial#, username, machine FROM v$session WHERE username = 'SALES_TEST';
DROP USER sales_test CASCADE; |
What this does: The first query is a safety check. DROP USER ... CASCADE removes the old schema and all its objects. Data Pump will create the user again from the export, under the new name.
Step 8: Preview the DDL with SQLFILE (Optional but Recommended)
SQLFILE writes the DDL that the import would run into a text file, without importing anything. It is the best way to check remaps, tablespaces and grants before the real run.
Shell |
impdp system directory=DP_DIR dumpfile=sales_%U.dmp \ remap_schema=SALES:SALES_TEST remap_tablespace=SALES_DATA:TEST_DATA \ sqlfile=preview_sales.sql
grep -c "CREATE TABLE" /u04/datapump/preview_sales.sql grep -i "tablespace" /u04/datapump/preview_sales.sql | sort | uniq -c | head |
What to expect: All TABLESPACE clauses refer to TEST_DATA, and all objects are created in SALES_TEST. If you see other tablespaces, add more REMAP_TABLESPACE entries.
Step 9: Run the Import
Now import with remapping. The extra options make the import faster and safer.
Shell |
cat > /u04/datapump/imp_sales.par <<'EOF' directory=DP_DIR dumpfile=sales_%U.dmp logfile=imp_sales.log remap_schema=SALES:SALES_TEST remap_tablespace=SALES_DATA:TEST_DATA remap_tablespace=SALES_IDX:TEST_DATA parallel=8 transform=disable_archive_logging:y transform=oid:n exclude=db_link metrics=yes logtime=all EOF
nohup impdp system parfile=imp_sales.par > imp_sales.out 2>&1 & tail -f /u04/datapump/imp_sales.log |
Here is what the import options do:
remap_schema creates the objects in SALES_TEST instead of SALES.
remap_tablespace moves segments from production tablespaces into TEST_DATA.
transform=disable_archive_logging:y loads tables and indexes with minimal redo, which can be much faster. It is ignored if the database is in FORCE LOGGING mode.
transform=oid:n gives object types new OIDs, avoiding clashes if the same types exist elsewhere.
exclude=db_link stops production database links from being created in test, so test code cannot accidentally write to production.
Step 10: Check the Import Result
Compare the object counts between source and target, and look for invalid objects. Then recompile.
SQL |
-- On PROD SELECT object_type, COUNT(*) FROM dba_objects WHERE owner = 'SALES' GROUP BY object_type ORDER BY 1;
-- On TEST SELECT object_type, COUNT(*) FROM dba_objects WHERE owner = 'SALES_TEST' GROUP BY object_type ORDER BY 1;
EXEC UTL_RECOMP.RECOMP_SERIAL('SALES_TEST');
SELECT object_name, object_type FROM dba_objects WHERE owner = 'SALES_TEST' AND status = 'INVALID'; |
What to expect: The counts match except for excluded objects (database links in our case). Invalid objects that remain usually reference other schemas or database links that do not exist in test.
Step 11: Gather Statistics and Reset Passwords
Because we excluded statistics, gather fresh ones so the optimizer has good information. Also make sure the imported user does not keep the production password.
SQL |
EXEC DBMS_STATS.GATHER_SCHEMA_STATS(ownname => 'SALES_TEST', degree => 8);
ALTER USER sales_test IDENTIFIED BY "New#Test#Password1" ACCOUNT UNLOCK; |
Step 12: Clean Up
Old dump files are a security risk, because they contain a full copy of production data. Remove them once the refresh is accepted, and check that no failed jobs are left behind.
Shell |
rm -f /u04/datapump/sales_*.dmp |
SQL |
-- Orphaned jobs show as NOT RUNNING. Their master tables can be dropped. SELECT owner_name, job_name, state FROM dba_datapump_jobs; -- DROP TABLE system.SYS_EXPORT_SCHEMA_01 PURGE; |
Summary
A reliable schema refresh comes down to a few habits: estimate before exporting, export with FLASHBACK_TIME for consistency, use parallel workers with %U dump files, verify the copy, preview with SQLFILE, remap carefully, and exclude anything that should never reach a test system, such as database links. Save both parameter files, and the next refresh is a matter of minutes of your time.
Comments