Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Oracle.

Asked: November 09, 2015 - 5:00 pm UTC

Last updated: December 10, 2015 - 2:15 pm UTC

Version: 11.2.0.3

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I have two servers one is UAT and another one is Development. Both database have same user tablespaces but some tables and data miss match . now the development team requirement to copy the uat database all data with table and all to dev database. So, what is the simplest way to make it same.

Some users tablespaces and table are there with data but not same as it is.


Thanks

and Connor said...

The best (and typically fastest) way is with RMAN duplicate

http://docs.oracle.com/cd/E11882_01/backup.112/e10642/rcmdupdb.htm#i1008564

There's a little bit of setup, and then its just a one-liner

DUPLICATE TARGET DATABASE TO dupdb
FROM ACTIVE DATABASE

Hope this helps

Rating

  (8 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Full Database object and data copy

Oracle, November 10, 2015 - 1:31 pm UTC

Thanks for reply.

I have some tables, procedures and all with tables data are available so it is possible to do the same .

Doubts-
1. it will append or truncate existing data?

2.Or new objects will create which are not available ?

Thanks
Connor McDonald
November 11, 2015 - 12:59 am UTC

RMAN duplicate is an *entire* database copy. Everything gets *replaced* (users, tables, code, definitions, files...*everything*)

If you dont want that, then you probably need to use Datapump. See the Utilities guide for that.

Oracle, November 11, 2015 - 7:14 am UTC

Thanks for reply

I prefer for data pump because both server is different zone. So, no one talk with each other.

For datapump normal full export n import

Expdp user@orcl directory=dumdir dumpfile=fulldb.dmp logfile=fulldb.log full=y

Impdp user@orcl directory=dumpdir dumpfile=fulldb.dmp logfile=fulldb.log full=y

1.Am I right with script ?
2.It will also create and replace all database object no need to delete or truncate existing database object or we need to create user and tables and all or truncate if table existing there.

Thanks

Connor McDonald
November 11, 2015 - 10:16 am UTC

1. Yes

2. The table_exists_action parameter controls what happens with existing objects. The default is skip. You can specify truncate, append or replace instead:

http://docs.oracle.com/database/121/SUTIL/GUID-C9664F8C-19C5-4177-AC20-5682AEABA07F.htm#SUTIL936

A reader, November 16, 2015 - 1:18 pm UTC

Thanks for reply

As you suggested to go with expdp impdp but I have one more doubt both database name different and schema names are also different So can you pls guide me how to export and import.

Database names are different
Database names - dev and devdb
Scmemas - devdata and devdbdata

Thanks
Connor McDonald
November 17, 2015 - 1:06 am UTC

Database name does not matter. You just connect to the source for export, and the target for import. You can change the schema as you import.

Example:

C:\Users\comcdona>expdp schemas=SCOTT

Export: Release 12.1.0.2.0 - Production on Tue Nov 17 09:02:31 2015

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Username: sys/admin as sysdba

Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Starting "SYS"."SYS_EXPORT_SCHEMA_01":  sys/******** AS SYSDBA schemas=SCOTT
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
. . exported "SCOTT"."S"                                   0 KB       0 rows
Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:
  C:\ORACLE\ADMIN\NP12\DPDUMP\EXPDAT.DMP
Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at Tue Nov 17 09:02:57 2015 elapsed 0 00:00:23



C:\Users\comcdona>impdp schemas=SCOTT remap_schema=SCOTT:DEMO

Import: Release 12.1.0.2.0 - Production on Tue Nov 17 09:04:29 2015

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Username: sys/admin as sysdba

Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Master table "SYS"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_SCHEMA_01":  sys/******** AS SYSDBA schemas=SCOTT remap_schema=SCOTT:demo
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/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "DEMO"."S"                                      0 KB       0 rows
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type SCHEMA_EXPORT/STATISTICS/MARKER
Job "SYS"."SYS_IMPORT_SCHEMA_01" successfully completed at Tue Nov 17 09:04:48 2015 elapsed 0 00:00:14





A reader, November 19, 2015 - 4:01 am UTC

Thanks for reply,

1.As per you in rman duplicate no need to create any users, tablespaces, tables,procedures and all objects m I right ?

2. If I will go with datapump then also need not to create any users, tablespaces, tables,procedures and all objects.

3. As you suggested to export db and at the time of import mention remap schema; name but I am not doing export only schema I m exporting entire database with full=y option So how can I set ramap schema name.

Thanks
Connor McDonald
November 19, 2015 - 5:13 am UTC

1) yes
2) yes
3) from the docs

REMAP_SCHEMA

Default: There is no default

Purpose

Loads all objects from the source schema into a target schema.

Syntax and Description

REMAP_SCHEMA=source_schema:target_schema
Multiple REMAP_SCHEMA lines can be specified, but the source schema must be different for each one. However, different source schemas can map to the same target schema. Note that the mapping may not be 100 percent complete; see the Restrictions section below.

If the schema you are remapping to does not already exist, then the import operation creates it, provided that the dump file set contains the necessary CREATE USER metadata for the source schema, and provided that you are importing with enough privileges. For example, the following Export commands create dump file sets with the necessary metadata to create a schema, because the user SYSTEM has the necessary privileges:

> expdp system SCHEMAS=hr
Password: password

> expdp system FULL=YES
Password: password
If your dump file set does not contain the metadata necessary to create a schema, or if you do not have privileges, then the target schema must be created before the import operation is performed. This is because the unprivileged dump files do not contain the necessary information for the import to create the schema automatically.

Use datapump for objects that need to persists

Brian, November 19, 2015 - 8:13 pm UTC

You can use data pump to export only the objects that need to stay in the database after the rman clone, then clone the database, then import the objects that need to be imported with the replace option.

A reader, November 25, 2015 - 11:47 am UTC

Thanks for reply.

I have used expdp for that I have faced one issue what I do -

expdp user@db directory=DMPDIR dumpfile=schema.dmp logfile=schema.log schema=test;

impdp user@db directory=DMPDIR dumpfile=schema.dmp logfile=schema.log schema=test remap_schema=test:new table_exists_action=replace

1.Now it import all object with replace but it will not replace procedure function sequence and all. Exclude table.

2. I mentioned remap action from old schema to new schema so all table moved but at the time of create trigger it looks the old schema name with table name to create trigger on it.

How to resolve this error.

Thanks
Connor McDonald
November 27, 2015 - 2:36 am UTC

I created the following triggers in the SCOTT schema

SQL> create or replace
  2  trigger TRG1 before insert on scott.EMP
  3  for each row
  4  declare
  5    x int;
  6  begin
  7    select count(*) into x from scott.dept;
  8  end;
  9  /

Trigger created.

SQL>
SQL>
SQL> create or replace
  2  trigger TRG2 before insert on EMP
  3  for each row
  4  declare
  5    x int;
  6  begin
  7    select count(*) into x from scott.dept;
  8  end;
  9  /

Trigger created.

SQL>
SQL>
SQL> create or replace
  2  trigger TRG3 before insert on EMP
  3  for each row
  4  declare
  5    x int;
  6  begin
  7    select count(*) into x from dept;
  8  end;
  9  /

Trigger created.


Then I did:

expdp directory=TEMP dumpfile=schema.dmp logfile=schema.log schemas=scott
impdp directory=TEMP dumpfile=schema.dmp logfile=schemaimp.log schemas=scott remap_schema=scott:scott2 table_exists_action=replace

After import, you will see the following

TRG1 - in the SCOTT schema
TRG2 - in the SCOTT2 schema, trigger body still says 'scott.dept'
TRG3 - in the SCOTT2 schema, trigger body still says 'dept'

Thats how we do it because triggers do not HAVE to be in the same schema as an object. For example, I might have a schema called "SCOTT_TRIGGERS" where I define all of my triggers for tables owned by SCOTT. So we cant really go "hunting" for the word 'scott' on import and just change it wherever we want.

Hope this helps.

A reader, December 10, 2015 - 6:50 am UTC

Thanks of reply,

Now i have tried to copy full database one production to new database for that i have done-

export full-y database on production and tried to
import full=y on new created database but the problem is the production tablespaces datafiles mount point are different and on new created database mount are different so how can i perform this activity.

Production database datafile location

CREATE TABLESPACE "USERS" DATAFILE '/EBPPdata1/EBPP/datafiles/----.DBF'

Newly created database datafile location

datafile location on new created database is --> /RESTORE/EBPP/---.dbf

So, how can we change it at the time of full db import.
Thanks


Connor McDonald
December 10, 2015 - 2:15 pm UTC

You can either precreate the tablespaces, or use the REMAP_DATAFILE parameter on import.

A third way to copy prod-to-test.

Andrew, December 10, 2015 - 3:47 pm UTC

For a number of years now I have been using something like this routine to copy small to moderate size production to test. If the data source is too large it can impose quite a bit of load. Also, RAW data types won't work.
1. In the source database, create an account with appropriate privileges - something like EXP_FULL_DATABASE.
2. In destination database in the utility account that will be doing the import, create a database link to the account created in step 1.
3. In the destination database clean out the receiving schema using something like this.
BEGIN
    FOR x IN ( SELECT owner, object_type, object_name,
                      DECODE(OBJECT_TYPE, 'TABLE',' CASCADE CONSTRAINTS PURGE', ' ') as cascade_cxs
                 FROM dba_objects
                WHERE owner       IN ( 'MYSYS' )
                  AND object_name NOT LIKE 'BIN$%' and object_type NOT IN ( 'INDEX', 'DATABASE LINK',
                                                                            'PACKAGE BODY', 'TRIGGER', 'LOB')
               ORDER BY object_type )
    LOOP
      BEGIN
         EXECUTE IMMEDIATE 'DROP '||x.object_type||' ' ||x.owner||'."'||x.object_name|| '"'|| x.MYSYScade_cxs;
      EXCEPTION
        WHEN OTHERS THEN
          DBMS_OUTPUT.PUT_LINE( 'Command failed: '|| 'DROP '||x.object_type||' ' ||x.owner||'.'||x.object_name|| x.cascade_cxs );
      END;
    END LOOP;
END;

4. Execute the import via the database link using impdp par file that looks something like this.
userid     = /
DIRECTORY  = dumpdir
LOGFILE    = impdp_mysys.log
SCHEMAS    = ( MYSYS )
CONTENT    = ALL
NETWORK_LINK = <remote db link>
TRANSFORM  = OID:N
TRANSFORM  = STORAGE:N
TRANSFORM  = SEGMENT_CREATION:N
TRANSFORM  = LOB_STORAGE:SECUREFILE
FLASHBACK_TIME="TO_TIMESTAMP(to_char(sysdate,'dd-mm-yyyy hh24:mi:ss'), 'DD-MM-YYYY HH24:MI:SS')"

5. Compile schema and check for invalids.

More to Explore

Backup/Recovery

Check out the complete guide to all of the Backup & Recovery techniques in the Oracle Database.