Cloning a Pluggable database

The container database can have number for PDB’s so we can either create new PDB’s or clone an existing PDB.
Now let’s try to clone our Pluggable database into a new pluggable database.
We had few custom objects in our Pluggable database PDB121. So we will validate if after cloning we are able to get the same objects in cloned pluggable database.

Source Pluggable database details:
Pluggable Database Name: PDB121
Schema Name: Himanshu
Object name: Test_reccovery2
Global user: C##Himanshu

Let’s give the new pluggable database name as : PDBCLONE

Note:
******************************************************************************
The disadvantage in cloning a current pluggable database is that it must be in Read Only Mode.
******************************************************************************


 Steps for performing cloning:
1. Connect to our container database and check the current PDBs.
SQL> select name from v$database;

NAME
--------------------
CDB122

SQL> select con_id,name,open_mode,restricted from v$pdbs;

    CON_ID NAME                 OPEN_MODE                      RESTRICTED
---------- -------------------- ------------------------------ ---------
         2 PDB$SEED             READ ONLY                      NO
         3 PDB122               READ WRITE                     NO
         4 PDB121               READ WRITE                     NO
        
2. Bring PDB121 in read only mode.
SQL> alter pluggable database PDB121 close;
Pluggable database altered.

SQL>  select CON_ID, NAME, OPEN_MODE,restricted from V$PDBS;
    CON_ID NAME                 OPEN_MODE                      RESTRICTED
---------- -------------------- ------------------------------ ---------
         2 PDB$SEED             READ ONLY                      NO
         3 PDB122               READ WRITE                     NO
         4 PDB121               MOUNTED
SQL> alter pluggable database PDB121 open read only;
Pluggable database altered.
SQL>  select CON_ID, NAME, OPEN_MODE,restricted from V$PDBS;
    CON_ID NAME                 OPEN_MODE                      RESTRICTED
---------- -------------------- ------------------------------ ---------
         2 PDB$SEED             READ ONLY                      NO
         3 PDB122               READ WRITE                     NO
         4 PDB121               READ ONLY                      NO

3. Create new pluggable database using PDB121.
20:11:19 SQL> create pluggable database PDBCLONE from PDB121 FILE_NAME_CONVERT= ('/ub817/oradataCDB122/CDB122/PDB121', '/ub817/oradata/ CDB122/CDB122/PDBCLONE');
Pluggable database created.
Elapsed: 00:00:27.36

4. Verify new pluggable database. It should be in mounted state and we need to open it.
20:11:58 SQL> select CON_ID, NAME, OPEN_MODE, restricted from V$PDBS;

    CON_ID NAME                 OPEN_MODE                      RESTRICTED
---------- -------------------- ------------------------------ ---------
         2 PDB$SEED             READ ONLY                      NO
         3 PDB122               READ WRITE                     NO
         4 PDB121               READ ONLY                      NO
         5 PDBCLONE             MOUNTED

20:12:39 SQL> alter pluggable database PDBCLONE OPEN;

Pluggable database altered.

Elapsed: 00:00:20.04
20:14:24 SQL> select CON_ID, NAME, OPEN_MODE, restricted from V$PDBS;

    CON_ID NAME                 OPEN_MODE                      RESTRICTED
---------- -------------------- ------------------------------ ---------
         2 PDB$SEED             READ ONLY                      NO
         3 PDB122               READ WRITE                     NO
         4 PDB121               READ ONLY                      NO
         5 PDBCLONE             READ WRITE                     NO

Elapsed: 00:00:00.05

5. Our clone pluggable database is ready. Lets check for the custom objects.

20:17:13 SQL> select username,CREATED from dba_users where upper(username) like '%HIMANSHU%';

USERNAME         CREATED
------------   ---------------
HIMANSHU         14-SEP-16
c##Himanshu      19-SEP-16
Elapsed: 00:00:00.18
20:17:25 SQL> select count(*) from  himanshu.test_reccovery2;
  COUNT(*)
----------
     91843

Elapsed: 00:00:01.94

Wooow that was easy, Looks good.
But when cloning using this method it actually copies the files with same name as in source.

So we should move/rename as a good practice and of course it’s simple in 12c.

CON_NAME
-----------
PDBCLONE
20:22:06 SQL> select file_name from dba_data_files;

FILE_NAME
-----------------------------------------------------------------------------/ub817/oradata/CDB122/CDB122/PDBCLONE/system01.dbf
/ub817/oradata/CDB122/CDB122/PDBCLONE/sysaux01.dbf
/ub817/oradata/CDB122/CDB122/PDBCLONE/PDB121_users01.dbf
Elapsed: 00:00:00.31
Rename Datafile:
20:22:08 SQL> alter database move datafile '/ub817/oradata/CDB122/CDB122/PDBCLONE/PDB121_users01.dbf' to '/ub817/oradata/CDB122/CDB122/PDBCLONE/PDBCLONE_users01.dbf';
Database altered.
Elapsed: 00:00:13.08
20:24:11 SQL> select file_name from dba_data_files;
FILE_NAME
----------------------------------------------------------------------------
/ub817/oradata/CDB122/CDB122/PDBCLONE/system01.dbf
/ub817/oradata/CDB122/CDB122/PDBCLONE/sysaux01.dbf

/ub817/oradata/CDB122/CDB122/PDBCLONE/PDBCLONE_users01.dbf