Senthil Murugan's Blog

Home » Oracle » Expdp and Impdp

Expdp and Impdp

How to Take schema backup using Expdp

Export, Import are the most commonly used backup and restoration technique in Oracle Database by Database Administrator. This tool is improved in every version of Oracle Database.  Before Oracle 10g exp and imp were used, which is not upgraded to expdp and impdp.

Here, I am using Expdp to take schema backup, because exp is almost obsolete now a days. In both Impdp and Expdp I will use system database user for taking and restoring backups. System user will take backup with user creation and grant scripts. So that, on the destination server it will automatically create and provide same grants.

The backup command after successful execution will make a .dmp file at defined directory on the Database Server, To restore it DBA has to move this file from source to destination and execute Impdp command.

Perquisites for Export and Import Backups:
     Database must be in open mode.
     A directory in database should have created.
Steps to take Schema Backup using Expdp:
Below are the four steps to take schema backup using expdp.
1. Set Database Environment variable
Export the sid of database using export command
[oracle@test ~]$ export ORACLE_SID=test01
[oracle@test ~]$ echo $ORACLE_SID
test01
2. Backup Command Syntax:
Support you want to take backup of schema remote_dba, Then Use this command to take backup
Syntax:
[oracle@test ~]$ expdp {user_name}/{Password} dumpfile={Dump file name} directory={Name of directory } logfile={Log file Name} schemas={Backup schema Name}
Example:
[oracle@test ~]$ expdp system/sys dumpfile=remote_dba.dmp directory=backup logfile=remote_dba_imp.log  schemas=remote_dba
Detail of parameters used in expdp command:
There are various parameters used with expdp command, We must have a knowledge about these to have a successful backup.

User Name/Password: For a full schema backup use system user name. It will include all commands to make a user and assign grants to the user at time of schema restoration at destination database.

Dumpfile: Name of the backup file created. Give any appropriate name with extension (.dmp).
Directory:  This is the path at which backups will be placed. We can use default directory made in oracle “data_pump_dir”. Data_pump_dir is physically located at $ORACLE_HOME/rdbms/log.
If you want to place Database Expdp backup at some other place then DBA has to create a new database directory. Use following to Create new database directory
a. Before creating the directory check

The physical location of directory must exist. like /opt/oracle .
Ownership of the directory to Oracle user. Use this command
[oracle@test opt]$ ls -ltrh
total 12K
drwxrwxrwx  15 oracle oinstall  12K Aug  4  2010 oracle
Here Oracle is the owner of /op/oracle folder.
b. Create Database Directory
SQL> create directory backup  as ‘/opt/oracle’;
Directory created.
SQL> grant read, write on directory backup  to remote_dba;
Grant succeeded.
Here, I have created a new database directory for backup, Now i can use this “backup” name in the directory parameter of expdp command and my backups will store at “/opt/oracle”
Schemas:  Name of the schema for which backup is taken.  More than one name is also allowed by command separator. Only one backup file will generate for all schemas.
Logfile:  The name of log file to keep log of the backups. The path for log file will be same as given in directory path. It’s recommended to give logfile name for future references of backup.
Above are minimum parameters to take a schema backup.

3. Execute backup command: Once you will execute backup command you will see this type of output on your screen.

[oracle@test ~]$ expdp system/sys dumpfile=remote_dba.dmp directory=backup logfile=remote_dba.log  schemas=remote_dba

Export: Release 10.2.0.4.0 - Production on Thursday, 19 May, 2011 11:45:48
Copyright (c) 2003, 2007, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 -Production 
With the Partitioning, OLAP, Data Mining and Real Application Testing option Starting       Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 38 MB
------------------------------------------------
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
exported "REMOTE_DBA"."TEST1"           11.16 MB  123342 rows
exported "REMOTE_DBA"."TEST2"                     4.671 MB   25545 rows
exported "REMOTE_DBA"."TEST3"           2.380 MB   20221 rows  Master table "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_SCHEMA_01 is:  /opt/oracle/remote_dba.dmp
Job "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully completed at 11:46:16

Here database back is done, Path of backup file is /opt/oracle/remote_dba.dmp.

How to Restore schema using Impdp

Export, Import are the most commonly used backup and restoration technique available for DBA in Oracle. Since you have already taken backup of your database schema using Expdp. Now, its time to restore backup of the schema

Perquisites for Import:
Destination Database must be in open mode.
A directory in database should have created. 
Four Steps to Restore Schema Backup using Impdp:
Database Administrator has to move .dmp backup of source schema from source database to destination database directory. DBA can find existing database directories using:
sql> select * from dba_directories;
1. Set Database Environment variable
Export the sid of database using export command
[oracle@test ~]$ export ORACLE_SID=test01
[oracle@test ~]$ echo $ORACLE_SID
test01
2. Dump restoration scenarios:
In case of dump restoration, there could by n number of scenarios for DBA to consider. Here, I am considering most commonly used two scenarios.
a. Restoring dump on same database Instance.
b. Restoring dump on different database Instance.
Let’s discuss both one by one.

Restoring Schema Dump on Same Database Instance: Usually this type of restoration is done, when Database Administrator wants to make replica of a existing schema on same instance. Since, you are restoring schema on same database, you have to create new database user for dump restoration. Impdp command executed by system user will automatically create new database user with different name given by you but with same privillages. DBA has to just add “remap_schema” parameter with impdp command.

Syntax:
[oracle@test ~]$ impdp {user_name}/{Password} dumpfile={Dump file name} directory={Name of directory } logfile={Log file Name} schemas={Backup schema Name} remap_schema={exising_schema_name:new_schema_name}
Example:
[oracle@test ~]$ impdp system/sys dumpfile=remote_dba.dmp directory=backup logfile=test_schema_imp.log  schemas=remote_dba remap_schema=remote_dba:remote_dba1

Restoring Schema Dump on Different Database Instance: When Database Administrator moves a schema from one database to another database using then he use this approach. Suppose DBA is moveing user “Remote_dba” from server1 to server2. There are two possibilities for database user existence on destination database. Same way there are two possibilities in case of Tablespaces, either all tablespaces as on source database exists on destination database or doesn’t exists. Let’s see them all in detail.

A. Database User not Exists: If database user (e.g. remote_dba) is not on destination instance proceed with dump restoration, user will be created by dump restoration automatically.
B. Database User Exists: If database user (e.g. remote_dba) is already on destination instance. We need to drop the user and then restore dump. Following are steps to drop a user:
    Connect to the database using sys user and execute following.
    SQL> drop user remote_dba cascade;
    User dropped.
    If you got this message then proceed with restoration, otherwise you may encounter error like
    SQL> drop user remote_dba cascade;
    drop user remote_dba cascade
    *

    ERROR at line 1:

    ORA-01940: cannot drop a user that is currently connected.

    To resolve this connect by sys user and execute following.
    SQL> select sid,SERIAL# from v$session where username like ‘remote_dba%’;
    SID    SERIAL#
    ———- ———-
    110        654
    Now kill all the session connected using
    SQL> ALTER SYSTEM KILL SESSION ‘110,654’ immediate;
    System altered.
    Now again execute the drop user command
    SQL> drop user remote_dba cascade;
    User dropped.
    Now you can proceed with dump restoration.

C. Tablesapces exists: If tablespaces with same name as on source exists on destination then no issue with restoration, this is because at time of restoring by default restoration process looks for same name of tablespaces at destination schema.

D. Tablesapces Not exists: If tablespaces with same name as on source does not exists on destination then restoration will return error “ORA-00959: tablespace ‘test_Tablesapce’ does not exist”
To avoid this error ues “remap_tablesape” parameter with impdp command. More than one tablespaces can also be remap by comma separator. like
remap_tablespace={source_tablespace_name: dest_tablespace_name}, {source_tablespace_name1: dest_tablespace_name1}
Syntex:

[oracle@test ~]$ impdp {user_name}/{Password} dumpfile={Dump file name} directory={Name of directory } logfile={Log file Name} schemas={Backup schema Name} remap_tablespace={source_tablespace_name: dest_tablespace_name}

Example:

[oracle@test ~]$ impdp system/sys dumpfile=remote_dba.dmp directory=backup logfile=test_schema_imp.log  schemas=remote_dba  remap_tablespace= tablespace_name1: tablespace_name2

3. Execute Impdp Command: This output is for a simple case, when DBA is restoring schema on different database instance where all tablespace exists which were available on source database. Destination schema has not any user as “remote_dba“, So this impdp will create a new remote_dba user.

[oracle@test opt]$ impdp system/sys dumpfile=remote_dba.dmp directory=backup logfile= test01_imp.log schemas=remote_dba

Import: Release 10.2.0.4.0 - Production on Friday, 20 May, 2011 11:25:06
Copyright (c) 2003, 2007, Oracle.  All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01":  system/******** dumpfile=remote_dba.dmp directory=backup logfile= remote_dba_imp.log schemas=remote_dba
------------------------------------------------------------------------------------
/FUNCTIONAL_AND_BITMAP/INDEX
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/FUNCTIONAL_AND_BITMAP/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_FULL_01" completed with 1 error(s) at 11:31:56

Check restoration log in the directory folder by remote_dba_imp.log.log name.
As explained, this impdp will make a new database user “remote_dba” into this database and this use will be locked by default. So, DBA has to unlock and provide password for this user.
sql> alter user remote_dba identified by {password};
sql> alter user remote_dba account unlock;
4. Some Known Issues:
A. DB links are not created properly: Please reconfigure it.
B. Jobs are not restored: Get job creation script from using and paste it in destination schema.
C. Invalid Objects: Compile all invalid objects.
DBA can also take and Restore whole database backup instead of one schema backup using What Everybody Should Know about Export Database and Import Database In Oracle ?
Are you able to restore your dump with help of this article. Please share !!

http://www.dbas-oracle.com/2011/05/how-to-take-schema-backup-using-expdp.html

http://www.dbas-oracle.com/2012/11/how-to-restore-schema-using-impdp.html

Thanks to Umesh Sharma (founder of DBA-Oracles.com)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: