# [OCI] Clone PDB

*If you prefer to read in Spanish* [***Spanish version***](https://dbaenlasombra.com/oci-clone-pdb)*.*

Today, let's talk about the steps to **clone** a PDB using **RMAN Active Duplicate** or **DDL** commands betweent differents **CDB$Root**.

![](https://cdn.hashnode.com/uploads/covers/65605419d28f19cc44df7ef1/e3df546b-cfa0-4f91-b716-7abcbdfd1e3f.png align="center")

### DDL

Let's use a script in order to do it.

This script should run under **oracle** user on the target machine.

Here the scripts:

[https://github.com/dbaenlasombra/OCI/blob/main/remote\_clonning.sh](https://github.com/dbaenlasombra/OCI/blob/main/remote_clonning.sh)

what do the script do?

*   Define a **common user** in the source with the necessary grants. The name is absolutly ramdom, not static, generated in unix using "*tr -dc 'A-Za-z' </dev/urandom | head -c 5 | tr '\[:lower:\]' '\[:upper:\]'*" . Regarding the password, it use the same procedure, following Oracle's minimum security requirements.
    
*   Define a **DB Link** in the target. The name is random like the previous steps.
    
*   Define **PDB** in the target.
    

After creating our PDB, the script will drop the **common user** and **DB Link**.

Regarding the names are generated ramdon in order to avoid blocks with another task into the machine.

As the previous steps before running the script, we need to do as follows:

*   Let's set up an **rsa key** for the **oracle** user on the target machine with name **id\_rsa** to allow communication between machines.
    
*   Let's upgrade the tnsnames in order to include the local naming "SOURCE" on the target machine.
    
*   Let's set the **one\_step\_plugin\_for\_pdb\_with\_tde** parameter to true on the tharget machine.
    

Lastly, the parameters that our script are waiting, it's as follows:

*   **IP** address of the **source machine**. The reason is because the scripts connect to the source machine below oracle user in order to create common user and the necessary grants.
    
*   Name of the PDB.
    
*   Password of the TDE.
    

Let's run the sh in **nohup** mode:

```oracle-sql
[oracle@~]$  
nohup ./remote_clonning.sh $1 $2 $3 > /tmp/remote_clonning.log 2>&1 &
```

Output of the sh:

![](https://cdn.hashnode.com/uploads/covers/65605419d28f19cc44df7ef1/b580430b-7834-499d-bc34-af3b0d6a995d.png align="center")

In case of an error, we can see it in the output:

![](https://cdn.hashnode.com/uploads/covers/65605419d28f19cc44df7ef1/141279de-d7d2-4b49-997a-e94efb7b061b.png align="center")

### RMAN

In this laboratory, we also have a small script in order to do this.

Here the scripts:

[https://github.com/dbaenlasombra/OCI/blob/main/rman\_clone\_pdb.sh](https://github.com/dbaenlasombra/OCI/blob/main/rman_clone_pdb.sh)

As the previous steps before running the script, we need to do as follows:

*   Let's upgrade the tnsnames in both machine to include local naming.
    
*   Let's export the **wallet** on the source machine in order to import it on the target machine.
    
*   Let's set the **remote\_recovery\_file\_dest parameter** on the target machine:
    

```sql
SQL> alter system set remote_recovery_file_dest = '+RECO' scope = both sid = '*';
```

Let's run the sh in nohup mode:

```oracle-sql
[oracle@ ~]$ nohup ./rman_clone_pdb.sh "user$/$pass@$local_name" "user$/$pass@$local_name" $pdb $db > /tmp/rman_clone_pdb.log 2>&1 &
```

Output of the sh:

![](https://cdn.hashnode.com/uploads/covers/65605419d28f19cc44df7ef1/d2d91bac-2e49-46c3-bcea-e6fe7763e223.png align="center")

Looking forward to seeing you in the next article :)
