Total Pageviews

Saturday, June 14, 2025

How to copy files from multiple servers to staging server and merge them into one consolidated file using shell script (Pull Method)

Below script can be used to scp all the csv files to staging server and merge them into single csv file.

 servers=("srv1" "srv2")

for srv in "${servers[@]}"
do
  scp oracle@"$srv":/tmp/OLAM_CP/CP_report/file.csv /tmp/OLAM_CP/CP_report/file_"$srv".csv
done

# Copy header from the first file
head -n 1 /tmp/OLAM_CP/CP_report/file_srv1.csv > /tmp/OLAM_CP/CP_report/final_report.csv

# Append data (skip header) from all files
for file in /tmp/OLAM_CP/CP_report/file_*.csv
do
  tail -n +2 "$file" >> /tmp/OLAM_CP/CP_report/final_report.csv
done


Proxy User and Connect Through

 Since Oracle 9i Release 2 it has been possible to create proxy users, allowing us to access a schema via a different username/password combination. 

This is done by using the GRANT CONNECT THROUGH clause on the destination user.


Why we need Proxy?

There are a two main reasons for using proxy users.


Some DBA tasks, like creating private database links or setting up jobs using the DBMS_JOB package, require the administrator to log in as a specific user. 

This can present a problem if the administrator doesn't know the password.

You have multiple developers working in a shared schema. Letting multiple people share the same credentials represents a security risk. Instead you create a separate proxy user for each individual, allowing them to connect to the schema owner with their own credentials. 

If a user leaves a project, you simply lock or drop their user, and they no longer have access to the shared schema.

We have Schema user "METALS"

Lets create proxy user:

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

         2 PDB$SEED                       READ ONLY  NO

         3 MYPDB1                         READ WRITE NO

         4 PDB3                           READ WRITE NO

SQL> alter session set container=pdb3;

Session altered.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED

---------- ------------------------------ ---------- ----------

         4 PDB3                           READ WRITE NO

SQL> create user metalprxy identified by metalprxy;

User created.

SQL> grant create session to metalprxy;

Grant succeeded.

SQL> alter user metals grant connect through metalprxy;

User altered.

E:\my_scripts>sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Thu May 15 05:50:52 2025

Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Connected to:

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

Version 19.3.0.0.0

SQL> conn metalprxy[metals]/metalprxy@//localhost:1521/pdb3

Connected.

SQL> show user

USER is "METALS"

The proxy authentication can be revoked using the following command.

alter user metals revoke connect through metalprxy;

How to merge two or multiple csv files into one consolidated file using powershell (Automation)

1. Below Powershell script can be used to merge the multiple files into one consolidated file.


# Define file paths

$file1 = "C:\Users\admin\Desktop\automation\file1.csv"

$file2 = "C:\Users\admin\Desktop\automation\file2.csv"

$outputFile = "C:\Users\admin\Desktop\automation\consolidated.csv"

# Get header from file1 and write to output file

Get-Content $file1 | Select-Object -First 1 | Out-File -FilePath $outputFile -Encoding utf8


# Append data (excluding header) from file1

Get-Content $file1 | Select-Object -Skip 1 | Out-File -FilePath $outputFile -Append -Encoding utf8


# Append data (excluding header) from file2

Get-Content $file2 | Select-Object -Skip 1 | Out-File -FilePath $outputFile -Append -Encoding utf8


Write-Output "Merge completed. Output file: $outputFile"

 


This can be executed as below:

PS C:\Users\admin> C:\Users\admin\Desktop\automation\merge-script.ps1

C:\Users\admin\Desktop\automation\merge-script.ps1 : File C:\Users\admin\Desktop\automation\merge-script.ps1 cannot be

loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at

https:/go.microsoft.com/fwlink/?LinkID=135170.

At line:1 char:1

+ C:\Users\admin\Desktop\automation\merge-script.ps1

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : SecurityError: (:) [], PSSecurityException

    + FullyQualifiedErrorId : UnauthorizedAccess


Note: If you see above error use below command to fix the same.

PS C:\Users\admin> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned


Execution Policy Change

The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose

you to the security risks described in the about_Execution_Policies help topic at

https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y


Then try again it will workfine.

PS C:\Users\admin> C:\Users\admin\Desktop\automation\merge-script.ps1

Merge completed. Output file: C:\Data\consolidated.csv


2. Below script can be used if you have multiple files:


# Define source folder and output file

$sourceFolder = "C:\Users\admin\Desktop\automation"

$outputFile = "C:\Users\admin\Desktop\automation\consolidated_rpt.csv"


# Get list of CSV files in the folder

$csvFiles = Get-ChildItem -Path $sourceFolder -Filter "*.csv"


# Check if there are any files to merge

if ($csvFiles.Count -eq 0) {

    Write-Output "No CSV files found in $sourceFolder"

    exit

}


# Get header from the first file and write to output file

Get-Content $csvFiles[0].FullName | Select-Object -First 1 | Out-File -FilePath $outputFile -Encoding utf8


# Loop through each CSV file

foreach ($file in $csvFiles) {

    # Skip header and append data to output file

    Get-Content $file.FullName | Select-Object -Skip 1 | Out-File -FilePath $outputFile -Append -Encoding utf8

}


Write-Output "Merge completed. Output file: $outputFile"

 


Sunday, June 8, 2025

GV$PX_PROCESS

This contains generic process-related information about parallel processes, including status, session ID, process ID, and other information. SELECT pxp.inst_id INT, pxp.server_name, pxp.status, pid, spid, pxp.sid, pxp.serial#, username, service_name svcname FROM gv$px_process pxp, gv$session gvs WHERE gvs.inst_id = pxp.inst_id AND gvs.sid = pxp.sid AND gvs.serial# = pxp.serial# AND username NOT IN ('SYS', 'SYSTEM');


SELECT pxp.inst_id INT, pxp.server_name, pxp.status, pid, spid, pxp.sid, pxp.serial#, username, service_name svcname FROM gv$px_process pxp, gv$session gvs WHERE gvs.inst_id = pxp.inst_id AND gvs.sid = pxp.sid AND gvs.serial# = pxp.serial# AND username NOT IN ('SYS', 'SYSTEM');

The output from the query illustrates that there are two parallel jobs currently running with the QC on instance 8 and instance 5. The output has 24 parallel execution servers on instance 5 and 4 parallel execution servers on instance 8. The parallel servers are confined to single instance because in this specific example, the parameter PARALLEL_FORCE_LOCAL has been enabled. 


INT SERV STATUS PID SPID SID SERIAL# USERNAME SVCNAME 
---- ---- --------- ------ -------- ----- ---------- ---------- ----- 
8 P000 IN USE 49 8630 59 13633 MVALLATH SSSVC1 8 P002 IN USE 51 8632160 7979 MVALLATH SSSVC1 8 P003 IN USE 54 8634 315 3890 MVALLATH SSSVC1 8 P001 IN USE 56 30395 413 324 MVALLATH SSSVC1 5 P005 IN USE 64 26015 17 842 DWH SSSVC1 5 P006 IN USE 65 26017 53 4300 DWH SSSVC1 5 P007 IN USE 66 26019 108 711 DWH SSSVC1 5 P012 IN USE 34 24171 111 1139 DWH SSSVC1 5 P008 IN USE 67 26021 163 8261 DWH SSSVC1 5 P009 IN USE 68 26023 209 3222 DWH SSSVC1 5 P014 IN USE 52 24175 219 1002 DWH SSSVC1 5 P010 IN USE 69 26025 257 1442 DWH SSSVC1 5 P015 IN USE 53 24177 263 1118 DWH SSSVC1 5 P000 IN USE 54 26005 306 1045 DWH SSSVC1 5 P011 IN USE 70 26027 319 1063 DWH SSSVC1 5 P001 IN USE 55 26007 350 820 DWH SSSVC1 5 P018 IN USE 71 24183 356 3612 DWH SSSVC1 5 P019 IN USE 72 24185 400 2714 DWH SSSVC1 5 P020 IN USE 73 24187 454 3617 DWH SSSVC1 5 P021 IN USE 74 24189 502 1276 DWH SSSVC1 5 P016 IN USE 58 24179 504 1404 DWH SSSVC1  272 5 P022 IN USE 75 24191 549 739 DWH SSSVC1 5 P017 IN USE 59 24181 562 22027 DWH SSSVC1 5 P023 IN USE 76 24193 592 7108 DWH SSSVC1 5 P013 IN USE 44 24173 605 210 DWH SSSVC1 5 P002 IN USE 61 26009 648 4453 DWH SSSVC1 5 P003 IN USE 62 26011 701 358 DWH SSSVC1 5 P004 IN USE 63 26013 738 7416 DWH SSSVC1

Tuesday, June 3, 2025

Playbook to merge two csv residng on different server

 ---

- name: Consolidate two CSV files from different servers without Python

  hosts: localhost

  gather_facts: no

  vars:

    server1: "srv1"

    server2: "srv2"

    file1: "/tmp/OLAM_CP/CP_report/file.csv"

    file2: "/tmp/OLAM_CP/CP_report/file.csv"

    local_tmp_dir: "/tmp/csv_consolidate"

    consolidated_file: "/tmp/OLAM_CP/CP_report/final_data.csv"


  tasks:

    - name: Ensure local temp directory exists

      ansible.builtin.file:

        path: "{{ local_tmp_dir }}"

        state: directory

        mode: '0755'


    - name: Fetch CSV from Server 1

      ansible.builtin.shell: |

        scp {{ server1 }}:{{ file1 }} {{ local_tmp_dir }}/file1.csv

      delegate_to: localhost


    - name: Fetch CSV from Server 2

      ansible.builtin.shell: |

        scp {{ server2 }}:{{ file2 }} {{ local_tmp_dir }}/file2.csv

      delegate_to: localhost


    - name: Consolidate the two CSV files (append using shell)

      ansible.builtin.shell: |

        head -n 1 {{ local_tmp_dir }}/file1.csv > {{ consolidated_file }}

        tail -n +2 {{ local_tmp_dir }}/file1.csv >> {{ consolidated_file }}

        tail -n +2 {{ local_tmp_dir }}/file2.csv >> {{ consolidated_file }}

      delegate_to: localhost

    - name: Clean up temp directory

      ansible.builtin.file:

        path: "{{ local_tmp_dir }}"

        state: absent

Sunday, June 1, 2025

How to create a user in Oracle who has access to all existing objects of a particular schema

 Step 1: Create the User

New user: ashu
Schema: Metals

create user ashu identified by ashu
default tablespace metals
temporary tablespace temp
quota unlimited on metals;

Grant create session to ashu;


Step 2 : Grant Access to All Existing Objects in a Particular Schema called METALS to new user ASHU.

BEGIN
  FOR t IN (SELECT table_name FROM all_tables WHERE owner = 'METALS') LOOP
    EXECUTE IMMEDIATE 'GRANT SELECT ON METALS.' || t.table_name || ' TO ASHU';
  END LOOP;
END;
/

Step 3: Grant EXECUTE on all procedures and packages in schema METALS

BEGIN
  FOR p IN (SELECT object_name FROM all_objects WHERE owner = 'METALS' AND object_type IN ('PROCEDURE', 'PACKAGE')) LOOP
    EXECUTE IMMEDIATE 'GRANT EXECUTE ON METALS.' || p.object_name || ' TO ASHU';
  END LOOP;
END;
/

Step 4: Grant SELECT on all views in schema METALS

  BEGIN
  FOR v IN (SELECT view_name FROM all_views WHERE owner = 'METALS') LOOP
    EXECUTE IMMEDIATE 'GRANT SELECT ON METALS.' || v.view_name || ' TO ASHU';
  END LOOP;
END;
/

or use below command to provide the access on Valid views only.

BEGIN
  FOR v IN (SELECT object_name 
            FROM all_objects 
            WHERE owner = 'METALS' 
              AND object_type = 'VIEW' 
              AND status = 'VALID') LOOP
    EXECUTE IMMEDIATE 'GRANT SELECT ON METALS.' || v.object_name || ' TO ASHU';
  END LOOP;
END;
/
Verify using below queries:

col GRANTEE form a12
col OWNER form a12
col TABLE_NAME form a30
col GRANTOR form a13
col PRIVILEGE form a13
SELECT * 
FROM DBA_TAB_PRIVS 
WHERE GRANTEE = 'ASHU';

SELECT * 
FROM DBA_ROLE_PRIVS 
WHERE GRANTEE = 'ASHU';

SELECT * 
FROM DBA_SYS_PRIVS 

WHERE GRANTEE = 'ASHU';