We can make backup and recovery on the Oracle database using physical or logical methods.RMAN backup and recovery is one of the physical backup & recovery methods. We can also make logical backups and recoveries using the EXPORT & IMPORT utilities of the Oracle database.
Starting from Oracle 10g, a new feature of export/ import utilities that supports data pump technology, expdp & impdp, are introduced. The old version or the standard export & import utilities are also called exp and imp respectively. The data pump comprises the features of the standard import/export utilities and has new features that enable us to efficiently backup/restore and move data among different environments.
We can import and export specific tables, schema, tablespace, and the entire database using data pump technology, expdp, and impdp.
On this topic, we will see some practical examples of how to export and import a schema, drop a schema, and then re-create it using data pump technology. We will also do the same on a table and tablespace.
Exporting Schema
[oracle@srv1 ~]$ sqlplus /nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 17:49:25 2024
Copyright (c) 1982, 2014, Oracle. All rights reserved.
SQL>
SQL> conn sys/orcl@orcl as sysdba Connected.
SQL> create directory data_pump as '/u01/app/oracle/DATAPUMP_BACKUP';
Directory created.
SQL>
SQL> grant write, read on directory data_pump to SH;
Grant succeeded.
Finally, run the expdp utility to export the schema.
[oracle@srv1 ~]$ sqlplus /nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 17:49:25 2024
Copyright (c) 1982, 2014, Oracle. All rights reserved.
SQL>
SQL> conn sys/orcl@orcl as sysdba
Connected.
SQL> exit
$expdp system/orcl directory=data_pump schemas=SH dumpfile=SH.dmp logfile=SH.log
Export: Release 12.1.0.2.0 Production on Fri Feb 23 17:49:25 2024
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Starting "SYSTEM"."SYS_EXPORT_SCHEMA_01": system/******** directory=data_pump schemas=SH dumpfile=SH.dmp logfile=SH.log
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/FUNCTIONAL_INDEX/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/IDENTITY_COLUMN
Processing object type SCHEMA_EXPORT/VIEW/VIEW
Processing object type SCHEMA_EXPORT/TABLE/INDEX/FUNCTIONAL_INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
. . exported "SH"."TBL_CUSTOMER" 6.859 KB 3 rows
. . exported "SH"."EMP_SAL" 5.914 KB 1 rows
. . exported "SH"."SH" 5.492 KB 1 rows
. . exported "SH"."STTMS_CUST_ACCOUNT" 0 KB 0 rows
Master table "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_SCHEMA_01 is:
/u01/app/oracle/DATAPUMP_BACKUP/SH.DMP
Job "SYSTEM"."SYS_EXPORT_SCHEMA_01" successfully completed at Fri Feb 23 17:49:25 2024 elapsed 0 00:01:10
Exporting Table
[oracle@srv1 ~]$sqlplus/nolog
SQL*Plus: 12.1.0.2.0 Production on Fri Feb 23 17:59:25 2024
Copyright (c) 1982, 2016, Oracle. All rights reserved.
SQL> conn sys/orcl@orcl as sysdba
Connected.
SQL> $expdp system/orcl directory=data_pump tables=SH.tbl_customer dumpfile=tbl_customer.dmp logfile=tbl_customer.log;
Export: Release 12.1.0.2.0 Production on Fri Feb 23 18:00:25 2024
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Starting "SYSTEM"."SYS_EXPORT_TABLE_01": system/******** directory=data_pump tables=SH.tbl_customer dumpfile=tbl_customer.dmp logfile=tbl_customer.log
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/IDENTITY_COLUMN
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
. . exported "SH"."TBL_CUSTOMER" 6.859 KB 3 rows
Master table "SYSTEM"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_TABLE_01 is:
/u01/app/oracle/DATAPUMP_BACKUP/TBL_CUSTOMER.DMP
Job "SYSTEM"."SYS_EXPORT_TABLE_01" successfully completed at Fri Feb 23 18:00:25 2024 elapsed 0 00:00:32
Export Tablespace
$ expdp SH/SH directory=data_pump dumpfile=users.dmp tablespaces=users
Export: Release 12.1.0.2.0 Production on Fri Feb 23 18:10:25 2024
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Starting "SYSTEM"."SYS_EXPORT_TABLESPACE_01": SYSTEM/******** directory=data_pump dumpfile=users.dmp tablespaces=users
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/FUNCTIONAL_INDEX/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/COMMENT
Processing object type TABLE_EXPORT/TABLE/IDENTITY_COLUMN
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/INDEX/FUNCTIONAL_INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
. . exported "SH"."TBL_CUSTOMER" 6.859 KB 3 rows
. . exported "SH"."EMP_SAL" 5.914 KB 1 rows
. . exported "SH"."SH" 5.492 KB 1 rows
. . exported "AMAN"."AMAN" 0 KB 0 rows
. . exported "SH"."STTMS_CUST_ACCOUNT" 0 KB 0 rows
Master table "SH"."SYS_EXPORT_TABLESPACE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SH.SYS_EXPORT_TABLESPACE_01 is:
/u01/app/oracle/DATAPUMP_BACKUP/USERS.DMP
Job "SYSTEM"."SYS_EXPORT_TABLESPACE_01" successfully completed at Fri Feb 23 18:10:25 2024 elapsed 0 00:00:37
Import Schema
--Dropping a schema named SH
[oracle@srv1 ~]$sqlplus/nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 19:10:25 2024
Copyright (c) 1982, 2016, Oracle. All rights reserved.
SQL> conn sys/orcl@orcl as sysdba
Connected.
SQL> drop user SH;
drop user SH
*
ERROR at line 1:
ORA-01922: CASCADE must be specified to drop 'SH'
SQL> drop user SH cascade;
User dropped.
SQL>
---Re-creating schema SH from dumpfile SH.dmp create previously.
[oracle@srv1 ~]$sqlplus/nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 19:15:25 2024
Copyright (c) 1982, 2016, Oracle. All rights reserved.
SQL> conn sys/orcl@orcl as sysdba
Connected.
SQL> exit
$impdp system/orcl directory=data_pump schemas=SH dumpfile=SH.dmp logfile=SH.log
Import: Release 12.1.0.2.0 - Production on Fri Feb 23 19:18:25 2024
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=data_pump schemas=SH dumpfile=SH.dmp logfile=SH.log
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SH"."TBL_CUSTOMER" 6.859 KB 3 rows
. . imported "SH"."EMP_SAL" 5.914 KB 1 rows
. . imported "SH"."SH" 5.492 KB 1 rows
. . imported "SH"."STTMS_CUST_ACCOUNT" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/VIEW/VIEW
Processing object type SCHEMA_EXPORT/TABLE/INDEX/FUNCTIONAL_INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/FUNCTIONAL_INDEX/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully completed at Fri Feb 23 19:20:25 2024 elapsed 0 00:00:36
SQL>
Now, to confirm whether the schema is recreated or not let's run the following SQL statement.
SQL> conn SH/SH@orcl
Connected.
SQL>
[oracle@srv1 ~]$sqlplus/nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 19:20:25 2024
Copyright (c) 1982, 2016, Oracle. All rights reserved.
SQL> conn SH/SH@orcl
Connected.
---Drop table tbl_customer
SQL> drop table tbl_customer;
Table dropped.
SQL>
---Import table tbl_customer
[oracle@srv1 ~]$sqlplus/nolog
SQL*Plus: Release 12.1.0.2.0 Production on Fri Feb 23 19:21:25 2024
Copyright (c) 1982, 2016, Oracle. All rights reserved.
SQL> conn sys/orcl@orcl as sysdba
Connected.
SQL>exit
$impdp system/orcl directory=data_pump tables=SH.tbl_customer dumpfile=tbl_customer.dmp
Import: Release 12.1.0.2.0 - Production on Fri Feb 23 19:22:25 2024
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
Master table "SYSTEM"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TABLE_01": system/******** directory=data_pump tables=SH.tbl_customer dumpfile=tbl_customer.dmp
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SH"."TBL_CUSTOMER" 6.859 KB 3 rows
Processing object type TABLE_EXPORT/TABLE/IDENTITY_COLUMN
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
Job "SYSTEM"."SYS_IMPORT_TABLE_01" successfully completed at Fri Feb 23 19:30:25 2024 elapsed 0 00:00:10
And again to confirm whether the table is re-created or not and let's run the following SQL statement.
SQL> select * from SH.tbl_customer;
CUST_NO CUSTOMER_T CUST_NAME SWIFT_CODE COUNTRY
---------- ---------- ---------- ----------- ------------
########## I Messi John Dave USA
########## C Mereba Technology UK
########## B Swiss Bank Switzerland
SQL>