This post is also available in: Português
In this article, we will deal a very common situation where the DBA is tasked to rename or clone a user into the same database. In the following lines, I will explain how to make this copy without higher complexities and with no need to generate intermediate dump files, using DataPump import utility with a loopback dblink.
This task can be done in 3 easy steps:
- Create a temporary loopback dblink.
- Run the Import DataPump to clone the user.
- Remove the temporary dblink created in item 1.
Let's start:
1) Create a temporary loopback dblink.
In this example, we will clone the whole user SCOTT with its objects and data to a new, SCOTT_BKP. We will do this task with the user "DBADMIN", which is a DBA this database.
The first step is to create a temporary loopback dblink. Assuming that the SERVICE_NAME DB is "ORCL" and he is running on port "1521":
create database link TMP_DBLINK using '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP) (HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)))';
You could also just use the TNS entry name, if there is one that already locally point to this DB. Assuming, for example, that there one TNS Names "ORCL":
create database link TMP_DBLINK using 'ORCL';
Note that on both cases I do omit the "connect to" clause to force connection to the database as the locally connected user (DBADMIN, in this example). Once you create it, test it by running:
SQL> select * from dual@TMP_DBLINK; D - X
2) Run the Import DataPump to clone the user.
The next step is to clone the user. If you are using this tutorial to rename a user, ensure that there is no transaction, DDL or DML in its objects.
Run the PL/SQL block:
DECLARE H1 NUMBER; HSTATUS VARCHAR2(200); FROMUSER VARCHAR2(30); TOUSER VARCHAR2(30); BEGIN FROMUSER := 'SCOTT'; TOUSER := 'SCOTT_BKP'; H1 := DBMS_DATAPUMP.OPEN(OPERATION => 'IMPORT', JOB_MODE => 'SCHEMA', REMOTE_LINK => 'TMP_DBLINK'); DBMS_DATAPUMP.METADATA_FILTER(HANDLE => H1, NAME => 'SCHEMA_LIST', VALUE => '''' || FROMUSER || ''''); DBMS_DATAPUMP.METADATA_REMAP(HANDLE => H1, NAME => 'REMAP_SCHEMA', OLD_VALUE => FROMUSER, VALUE => TOUSER); DBMS_DATAPUMP.START_JOB(HANDLE => H1); DBMS_DATAPUMP.WAIT_FOR_JOB(HANDLE => H1, JOB_STATE => HSTATUS); DBMS_OUTPUT.PUT_LINE('STATUS = ' || HSTATUS); END; /
The runtime of the script varies depending on the amount of objects and user data. Check whether the output was "STATUS = COMPLETED". If not, follow the steps in the last part of this article.
3) Remove the temporary dblink created in item 1.
Finally, check that all objects were duplicated with the SQL below:
select owner,object_type,status,count(*) from dba_objects where owner in ('SCOTT','SCOTT_BKP') group by owner,object_type,status order by 2,1,3;
If everything is similar, remove the temporary dblink created:
drop database link TMP_DBLINK;
Okay, now you have an identical copy of your schema in your database! If you are using this tutorial to rename a user, you can now remove the old user or keep it as a backup.
Checking in case of Failures
If you have problems during the checking of objects, it is possible to enable logging of import DataPump to identify the cause of failure. To do so, you must create a temporary directory for the log:
create directory TMP_DIR as '/tmp';
Then add the following statement below the line of PL/SQL with DBMS_DATAPUMP.OPEN command:
DBMS_DATAPUMP.ADD_FILE( HANDLE => H1, FILENAME => 'imp.log', DIRECTORY => 'TMP_DIR', FILETYPE => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE );
Finally, after correcting the error and run the command correctly, do not forget to remove the temporary directory:
drop directory TMP_DIR;Have you enjoyed? Please leave a comment or give a 👍!
8 comments
Skip to comment form
Hi Rodrigo,
I hope you remember me.
You are the best. You post really unique articles.
Last time you created the article for sys audit reporting. Which helped a lot for my project. I used that script in all my 30 production databases and its working perfectly.
I have posted one question i MOSC.
https://community.oracle.com/thread/3684004?sr=inbox
Regards
Rajkishore
Thanks so much your article above. It was a great help.
Usefull article RJ !!
Beware that tables with LONG-columns cannot be imported over a database link,
they have to be exported the "normal" way (expdp-impdp) ....
select distinct a.owner||'.'||a.table_name
from dba_tables a, dba_tab_columns b
where b.data_type like 'LONG%'
and a.owner=b.owner
and a.table_name=b.table_name
and b.owner in
('SCOTT')
-- and a.NUM_ROWS > 0
/
Cheers René
It worked perfectly . thank you so much.
Thank you very much Rodrigo,
Your site is very helpful. Your articles are a gold.
hello,
I have installed oraclexe 11g 64 bits under windows 10 64 bits. : ok no problème!
I also installed apex514 on oraclexe 11g: ok no issues
I created with 'expdp' command a file for the shema 'MFO': ok
I imported the file with 'import' command on another computer that is configured with the same oraclexe11g db and apex514: ok
--------------------------------------------------------------------------------------------------------------------------------------------------------
ISSUE: the import was successful but the name of the shema when i loaded the MY OWN apex applications THE NAME OF THE CHANGED on 'FMO'???
--------------------------------------------------------------------------------------------------------------------------------------------------------
STRANGE! DO YOU HAVE A RESPONSE!
WHY MFO SCHEMA IS CHANGED ON FMO SCHEMA?
Author
Hey MFO, sorry for late response. I have no idea.. maybe you should open a SR as I have no access to the commands you've executed.
Peace,
RJ
Hello,
Are-you Alive?
mfo