Oracle Data Pump Export Schema with expdp Command Guide

Oracle Data Pump Export Schema with expdp Command Guide

Purpose

Copying one user's tables, code and grants out of a database is a routine job: a refresh of a test system, a move to a new server, or a logical copy kept beside the physical backups. Oracle Data Pump Export (expdp) does this in schema mode. One short command writes everything the schema owns into a dump file that impdp can load later.

The command in this post exports the andy schema into a dump file in a directory object called my_dir. The sections below explain each parameter, what a schema export does and does not include, the privileges it needs, and the extra parameters Oracle recommends for a dependable export.

Code

1expdp system/****** schemas=andy dumpfile=andy.dp directory=my_dir

A more complete version adds a log file:

1expdp system/****** schemas=andy dumpfile=andy.dp directory=my_dir logfile=andy_export.log

Code Breakdown

ParameterValueMeaning
expdp—Starts the Oracle Data Pump Export client
system/******CredentialsConnects as the SYSTEM user; the password is masked here
schemas=andyandyRuns a schema-mode export of the andy schema
dumpfile=andy.dpandy.dpName of the dump file, written to the directory object
directory=my_dirmy_dirThe directory object that names the server folder for the dump file and the log file
logfile=andy_export.logandy_export.logName of the log file, in the same directory object

schemas=

Schema mode is the default export mode, and the SCHEMAS parameter selects it. A user who holds the DATAPUMP_EXP_FULL_DATABASE role can list several schemas. A user without that role can export only their own schema. The SYS schema cannot be used as a source schema for an export job.

dumpfile=

The file name may have any extension. If you give no extension, Export adds the default .dmp. So andy.dp works, but andy.dmp follows Oracle's own default and is easier for the next DBA to recognize. For large schemas, the %U substitution variable expands into a two-digit number, so Data Pump can write a set of files such as andy_01.dmp, andy_02.dmp.

directory=

DIRECTORY names a database directory object, not a file path. If you leave it out, Export uses the default directory object DATA_PUMP_DIR. Oracle can change the path of DATA_PUMP_DIR during upgrades and patches, so a named directory object such as my_dir is more predictable.

logfile=

If you do not name a log file, Export creates export.log in the directory given by DIRECTORY. Oracle states that all messages about work in progress, work completed and errors are written to the log file. NOLOGFILE=YES turns the log off. Oracle's own ESTIMATE_ONLY example uses it, but for a real export keep the log, because it is where errors are recorded.

Key Points

  • Data Pump is server-based. Dump files and log files are read and written on the database server, through directory objects. They are not written on the machine where you type expdp.
  • Cross-schema objects are left out. Oracle's example: a trigger on a table in andy that is owned by a schema you did not list is not exported. Add the other schema to SCHEMAS if you need it.
  • Do not run Data Pump as SYS or as SYSDBA. Oracle says to use SYSTEM, or a user granted the Data Pump full-privilege roles. SYSDBA is used internally and does not behave like an ordinary user.
  • SCHEMAS replaces OWNER. In Data Pump legacy mode, the original Export OWNER parameter is mapped to SCHEMAS.
  • A job needs a table of its own. Data Pump tracks the job in a parent job table, created in the schema of the user running it. That user needs CREATE TABLE and enough tablespace quota. The table has the same name as the job.

What Gets Exported

A schema export unloads the objects and data that the schema owns: tables and their rows, indexes, constraints, views, sequences, PL/SQL code, comments and object grants. Oracle's own sample schema-export log shows object paths such as SCHEMA_EXPORT/SEQUENCE/SEQUENCE, SCHEMA_EXPORT/TABLE/TABLE, SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT and SCHEMA_EXPORT/PROCEDURE/PROCEDURE.

To see every object type that schema mode can include on your release, query the SCHEMA_EXPORT_OBJECTS view:

1SELECT object_path, comments
2FROM   schema_export_objects
3WHERE  object_path NOT LIKE '%/%'
4ORDER  BY object_path;

The values in OBJECT_PATH are also the names you pass to INCLUDE and EXCLUDE to filter an export.

Insights and Best Practices

Set up the directory object first

A DBA creates the directory object and grants access to it before the export runs:

1CREATE OR REPLACE DIRECTORY my_dir AS '/u01/oracle/exports';
2GRANT READ, WRITE ON DIRECTORY my_dir TO system;

Check that it exists and where it points:

1SELECT directory_name, directory_path
2FROM   dba_directories
3WHERE  directory_name = 'MY_DIR';

The operating system folder must also exist on the server and be writable by the Oracle software owner. Oracle permissions do not override the file system.

Keep the password off the command line

A password typed after the user name can be kept in your shell history and, on many systems, seen by other users in the process list. This is general operating-system behaviour, not an Oracle rule. Leave it out and Export prompts for it:

1expdp system@orclpdb schemas=andy directory=my_dir dumpfile=andy.dmp logfile=andy_export.log

Here orclpdb is a connect identifier (a Net service name). Export prompts Password: and then runs the job against that database.

Put the parameters in a parameter file

Oracle's Data Pump best-practices paper recommends a parameter file ("parfile"). It makes a job easy to repeat and avoids errors in long commands, mainly when values need quotation marks. A schema export parfile might read:

1SCHEMAS=andy
2DIRECTORY=my_dir
3DUMPFILE=andy_%U.dmp
4LOGFILE=andy_export.log
5FLASHBACK_TIME=SYSTIMESTAMP
6EXCLUDE=STATISTICS
7LOGTIME=ALL
8METRICS=Y

Run it with expdp system parfile=andy_export.par.

Make the export consistent

Without FLASHBACK_TIME or FLASHBACK_SCN, tables are unloaded at different moments while users keep changing data. FLASHBACK_TIME=SYSTIMESTAMP makes the whole export consistent as of the time the job starts. The two parameters cannot be used together.

Leave statistics out

Oracle recommends that you do not export optimizer statistics. EXCLUDE=STATISTICS makes both the export and the import faster. Gather fresh statistics on the target after the import.

Turn on timestamps

LOGTIME=ALL (Oracle Database 12.1 and later) puts a timestamp on every log line, and METRICS=Y records the number of objects and the elapsed time. Together they show which step of a slow export took the time.

Estimate the size before you run it

ESTIMATE_ONLY=YES reports how much space the export would use and then stops without writing a dump file:

1expdp system schemas=andy estimate_only=yes nologfile=yes

When to Run This

  • Before a schema is changed by an application release, as a logical copy you can restore one table from.
  • To copy a schema from production to a test or development database.
  • To move a schema to a new server or a new Oracle release, with impdp on the target.
  • To keep a portable copy of a small schema beside the RMAN backups.

Troubleshooting Common Issues

ORA-39002 and ORA-39070 (unable to open the log file). The directory object is missing, the user has no READ, WRITE grant on it, or the server folder does not exist or is not writable. Check DBA_DIRECTORIES and the folder on the server.

The job seems to hang. Watch it from another session. DBA_DATAPUMP_JOBS lists each job and its state, and V$SESSION_LONGOPS shows how far the table-data transfer has got:

1SELECT owner_name, job_name, operation, job_mode, state
2FROM   dba_datapump_jobs;
3
4SELECT sid, serial#, sofar, totalwork
5FROM   v$session_longops
6WHERE  opname = 'SYS_EXPORT_SCHEMA_01'
7AND    sofar != totalwork;

Use your own job name in the second query. The JOB_NAME parameter sets it; Oracle's examples show a schema export named SYS_EXPORT_SCHEMA_01 when none is given.

ORA-31634 (job already exists). A job name cannot match an existing table, and a failed job can leave its parent job table behind. Find the job in DBA_DATAPUMP_JOBS, attach with expdp system attach=<job_name> and run KILL_JOB, or give the new run a different JOB_NAME.

Objects missing on import. Cross-schema references are not exported. List every schema the objects depend on in SCHEMAS.


References

Posts in this series