Wednesday 10 May 2017

Shell script to backup DB2 database even if applications are connected with group owner ID and schedule at specific time

In this document we will use two scripts.
First script backup the DB2 database and second script deletes backup older than 90 days.



1. backup.sh

# This script backup the DB2 database named mydb at the path /home/db2inst1/backups


#!/bin/sh

/home/db2inst1/sqllib/db2profile

/home/db2inst1/sqllib/bin/db2 force applications all
/home/db2inst1/sqllib/bin/db2 deactivate database moit
/home/db2inst1/sqllib/bin/db2 force applications all
/home/db2inst1/sqllib/bin/db2 backup database testdb to "/home/db2inst1/backups"


#db2set DB2COMM=TCPIP


# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 backup.sh

 2. housekppping.sh

# This script deletes backup files older than the 90 days 

#!/bin/sh

logfile= /home/db2inst1/backups/backup.log

rm -f $logfile

for file in `find  /home/db2inst1/backups  -mtime +90 -type f -name '*.backup' `
do
  echo "deleting: " $file >> $logfile
  rm $file
done

exit 0



# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 housekppping.sh


Execute following command to schedule your script to run at 1 pm and 10 pm daily 

[root]# crontab -e
0 13 * * * /home/db2inst1/backups/backup.sh
0 22 * * *  /home/db2inst1/backups/backup.sh
0 22 * * *  /home/db2inst1/backups/housekeeping.sh






Monday 10 April 2017

Shell script to backup postgres database and schedule at specific time

In this document we will use two scripts.
First script backup the Postgresql database and second script deletes backup older than 90 days.



1. backup.sh

# This script backup the postgresql db mydb at the path /home/postgres/9.5/backups


#!/bin/sh

pg_dump  -U postgres   -p 5432 -F c -b -v -d mydb> /home/postgres/9.5/backups/mydb_`date +%d-%m-%Y"_"%H_%M_%S`.backup



# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 backup.sh

 2. housekppping.sh

# This script deletes backup files older than the 90 days 

#!/bin/sh

logfile=/home/postgres/9.5/backups/backup.log

rm -f $logfile

for file in `find /home/postgres/9.5/backups  -mtime +90 -type f -name '*.backup' `
do
  echo "deleting: " $file >> $logfile
  rm $file
done

exit 0



# after creating backup.sh file execute following command to make the file executable.

# chmod -R 775 housekppping.sh


Execute following command to schedule your script to run at 1 pm and 10 pm daily 

[root]# crontab -e
0 13 * * * /home/postgres/9.5/backups/backup.sh
0 22 * * * /home/postgres/9.5/backups/backup.sh
0 22 * * * /home/postgres/9.5/backups/housekeeping.sh





Use following query to restore the backup.


pg_restore -U postgres -F c  -v  -d mydb  < mydb_21-02-2017_13_00_04.backup


Sunday 9 April 2017

Batch Script to Create postgres backup and copy backup files from local drive to network also send email alert to user

Following document help to copy latest files from local drive and  paste on the network drive.
this document also delete files older than 10 days from the network drive where we are copying the backup files.


to copy backup files we will create following files on the  c:\users\sarzaidi\documents\ path .

we would also need to create a folder named log under  c:\users\sarzaidi\documents\ 

1. backup.bat.
this file will copy backup from local drive to mapped network drive.
drive will be mapped at runtime.

2. sendemail.bat
this file will use powershell and emailcommand.ps1 file to send email .

3. emailcommand.ps1
this file contains command to send email.
in this file we can specify sender ,receiver ,email subject and body we can also use this file to attach file to email.

4.main.bat

this file calls backup.bat and sendemail.bat to perform backup and send email.
we can execute this file from command prompt or we can schedule this file to execute the task whenever we need. 

 1. backup.bat

set backuplog=backup_db_%date:~10%%date:~4,2%%date:~7,2%.log

echo set batchscriptdir="c:\program files\postgresql\backups\"
echo set backupsourcedir="c:\program files\postgresql\backups\"
echo set backupdestinationdir=\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev

pg_dump  -u postgres   -p 5432 -f c -b -v -d eor4 > "c:\program files\postgresql\backups"\%backupname%

net use x: \\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev
x:

echo " xcopy  /y /f /i /c:  %backupsourcedir%*.* x:\"
echo backup started from local:%backupsourcedir% to azure . >> %batchscriptdir%logs\%backuplog%
xcopy  /y /f /i /d:%date:~4,2%-%date:~7,2%-%date:~10% %backupsourcedir%*.* x: >> %batchscriptdir%logs\%backuplog%
echo backup completed at \\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev >> %batchscriptdir%logs\%backuplog%
x:
echo delete older than 10 day  started. >> %batchscriptdir%logs\%backuplog%
forfiles /m *.bak /d -10 /c "cmd /c echo @path "
echo delete older than 10 day completed >> %batchscriptdir%logs\%backuplog%
c:
net use x: /delete /y
echo done 



2. sendemail.bat

set powershellscriptpath=%batchscriptdir%emailcommand.ps1
powershell -noprofile -executionpolicy bypass -command "& '%powershellscriptpath%'";

3. emailcommand.ps1


$from_ = "sarzaidi@mydomain.com"
$to_ = "sarzaidi@mydomain.com","arzaidi@lmkr.com"
$subject_ = "doa dev 168 backup"
$body_ = "backup completed logs at avaliable at \\infoportal-dev\logs "
$smtpserver_  =  "outlook.mydomain.com"

send-mailmessage -from $from_ -to $to_ -subject $subject_ -body $body_ –smtpserver $smtpserver_


4.main.bat

set batchscriptdir="c:\program files\postgresql\backups\"
set logdir=eor_backup_%date:~10%%date:~4,2%%date:~7,2%
set backuplog=backup_%date:~10%%date:~4,2%%date:~7,2%.log
set emaillog=sendemail_%date:~10%%date:~4,2%%date:~7,2%.log
set backupsourcedir="c:\program files\postgresql\backups\"
set backupdestinationdir=\\ccps-sql-01.ad.ccps.net\ccps_backups\db_backups\kp_dev
set backupname=vo8_%date:~10%%date:~4,2%%date:~7,2%.backup
c:
cd %batchscriptdir%logs
md %logdir%
move /y *.log %logdir%
 c:

cd %batchscriptdir%
echo backup.bat started. >> %batchscriptdir%logs\%backuplog%
call backup.bat >> %batchscriptdir%logs\%backuplog%
echo backup.bat ended. >> %batchscriptdir%logs\%backuplog%

c:
cd %batchscriptdir%
echo sendemail started. >> %batchscriptdir%logs\%emaillog%
call sendemail.bat >> %batchscriptdir%logs\%emaillog%
echo sendemail ended. >> %batchscriptdir%logs\%emaillog%
rem cd %batchscriptdir%logs
rem md unittestlogs_%date:~10%%date:~4,2%%date:~7,2%
rem mv %batchscriptdir%logs\*.log unittestlogs_%date:~10%%date:~4,2%%date:~7,2%


forfiles /p "%batchscriptdir%logs" /s /d -6 /c "cmd /c if @isdir == true rd /s /q @path"
cd %batchscriptdir%
echo done 




Thursday 30 March 2017

Recover Database when system.dbf is corrupted


SQL*Plus: Release 11.2.0.2.0 Production on Thu Mar 30 17:31:08 2017

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

SQL> conn / as sysdba
Connected.

SQL> startup mount;
ORACLE instance started.

Total System Global Area 1071333376 bytes
Fixed Size                  1388352 bytes
Variable Size             620757184 bytes
Database Buffers          444596224 bytes
Redo Buffers                4591616 bytes
Database mounted.

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open


SQL> alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: 'D:\ORACLE\APP\ORACLE\ORADATA\XE\SYSTEM.DBF'

SQL> recover database using backup controlfile until cancel;
ORA-00279: change 4987181 generated at 03/19/2017 12:47:52 needed for thread 1
ORA-00289: suggestion :
D:\ORACLE\APP\ORACLE\FAST_RECOVERY_AREA\XE\ARCHIVELOG\2017_03_30\O1_MF_1_173_%U_.ARC
ORA-00280: change 4987181 for thread 1 is in sequence #173


Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
cancel
ORA-10879: error signaled in parallel recovery slave
ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: 'D:\ORACLE\APP\ORACLE\ORADATA\XE\SYSTEM.DBF'



SQL> create pfile='D:\oracle\app\oracle\product\11.2.0\server\database\initXE.ora' from spfile;

File created.


Open pfile created at last setp at the follwoing path D:\oracle\app\oracle\product\11.2.0\server\database\initXE.ora

and at end of file add follwoing parameter.

_allow_resetlogs_corruption=true

and save the file.

Now shutdown database and start with this file as below.

SQL> shutdown immediate;
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='D:\oracle\app\oracle\product\11.2.0\server\database\initXE.ora';
ORACLE instance started.

Total System Global Area 1071333376 bytes
Fixed Size                  1388352 bytes
Variable Size             620757184 bytes
Database Buffers          444596224 bytes
Redo Buffers                4591616 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open


SQL> alter database open resetlogs;

Database altered.

SQL> create spfile from pfile;

File created.

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup;
ORACLE instance started.

Total System Global Area 1071333376 bytes
Fixed Size                  1388352 bytes
Variable Size             620757184 bytes
Database Buffers          444596224 bytes
Redo Buffers                4591616 bytes
Database mounted.
Database opened.
SQL>

Monday 20 March 2017

Use Sql Developer to load formatted excel report data into Oracle

I this post we will learn to load formatted report data from excel or csv files into ORACLE database.

sample data in excel format.

Jan 2002- Dec 2002
St.Name N E Jan_2002 Feb_2002 Mar_2002 Apr_2002 May_2002 June_2002 July_2002 Aug_2002 Sep_2002 Oct_2002 Nov_2002 Dec_2002
Bhakkar 31.61667 71.06667 0 0 0 0 0 0 0 0 0 0 0 0
Bahawal-Nagar 29.88099 73.35250 48 44 30 19 16 28 34 40 40 29 40 48
Bahawal-Pur 30.53333 71.78333 45 41 33 18 20 29 36 40 48 41 44 46
CHAKWAL 32.91667 72.85000 0 0 0 0 0 0 0 0 0 0 0 0
Jan 2003- Dec 2003
St.Name N E Jan_2003 Feb_2003 Mar_2003 Apr_2003 May_2003 June_2003 July_2003 Aug_2003 Sep_2003 Oct_2003 Nov_2003 Dec_2003
Bhakkar 31.61667 71.06667 0 0 0 0 0 0 0 0 0 0 0 0
Bahawal-Nagar 29.88099 73.35250 58 51 37 20 14 31 55 53 49 28 29 46
Bahawal-Pur 30.53333 71.78333 53 52 35 19 17 31 53 55 51 35 44 45
CHAKWAL 32.91667 72.85000 0 0 0 0 0 0 0 0 0 0 0 0
D-G-KHAN 30.05000 70.63333 55 44 38 22 15 25 53 54 53 33 30 40
Jan 2004- Dec 2004
St.Name N E Jan_2004 Feb_2004 Mar_2004 Apr_2004 May_2004 June_2004 July_2004 Aug_2004 Sep_2004 Oct_2004 Nov_2004 Dec_2004
Bhakkar 31.61667 71.06667 0 0 0 0 0 0 0 0 0 0 0 0
Bahawal-Nagar 29.88099 73.35250 63 40 26 19 19 32 36 49 39 37 35 45
Bahawal-Pur 30.53333 71.78333 59 46 32 21 22 30 38 56 48 45 43 48
CHAKWAL 32.91667 72.85000 0 0 0 0 0 0 0 0 0 0 0 0
D-G-KHAN 30.05000 70.63333 55 44 35 21 21 34 46 54 47 40 41 42


To load above data execute following scripts on your database with appropriate user and follow the procedure.

I have used scott user for this demo.


Following scrip will create table in which data would be loaded.


create table climate_data
(
city_Name varchar2(100) ,
North varchar2(100) ,
East varchar2(100) ,
Jan varchar2(100) ,
Feb varchar2(100) ,
Mar varchar2(100) ,
Apr varchar2(100) ,
May varchar2(100) ,
Jun varchar2(100) ,
Jul varchar2(100) ,
Aug varchar2(100) ,
Sep varchar2(100) ,
Oct varchar2(100) ,
Nov varchar2(100) ,
Dec varchar2(100),
year_name varchar2(100) )
/


This package will help to hold data temporarily.


CREATE OR REPLACE PACKAGE uldemo7_climate_data AS
    year_name  varchar2(100);
END uldemo7_climate_data;
/


This trigger will convert the formatted data in to our required column in my case I want to populate year_name column.


CREATE OR REPLACE TRIGGER uldemo7_climate_data_insert
  BEFORE INSERT ON climate_data
  FOR EACH ROW

  BEGIN
  IF substr(:new.Dec,1,3) ='Dec' THEN
     uldemo7_climate_data.year_name := substr(:new.Dec,5,4);   -- save value for later use
  ELSE
     :new.year_name := uldemo7_climate_data.year_name;   -- use last valid value
  END IF;

  END;
/


Following triggers is used to  delete extra/garbage  data form the table.


create or replace TRIGGER uldemo7_climate_data_aft_ins
  after INSERT ON climate_data

  BEGIN
 delete from climate_data where year_name is null or city_name like 'Jan%' or north is null;

  END;
  /


After executing above scripts we are ready to used sql developer or sqlloader to load data in our required format in the climate_data table.


To load data using sql developer start the sql developer with the your user in my case it is scott.

open the table tree in the object explorer and right click the climate_data table and click import.

at first screen click browse and provide the excel or csv file.

header checkbox must be unchecked click next

select import method to insert and click next
again click next on the choose column page
in the column definition page select match by position option and click next.
on finish page click finish.

A dialog box will appear to show the status if import .

click ok


now you can query climate_data table to view the loaded data .




Load formatted report in to Oracle using sqlloader

I this post we will learn to load formatted report data from excel or csv files into ORACLE database.

sample data in excel format.

Jan 2002- Dec 2002
St.NameNEJan_2002Feb_2002Mar_2002Apr_2002May_2002June_2002July_2002Aug_2002Sep_2002Oct_2002Nov_2002Dec_2002
Bhakkar31.6166771.06667000000000000
Bahawal-Nagar29.8809973.35250484430191628344040294048
Bahawal-Pur30.5333371.78333454133182029364048414446
CHAKWAL32.9166772.85000000000000000
Jan 2003- Dec 2003
St.NameNEJan_2003Feb_2003Mar_2003Apr_2003May_2003June_2003July_2003Aug_2003Sep_2003Oct_2003Nov_2003Dec_2003
Bhakkar31.6166771.06667000000000000
Bahawal-Nagar29.8809973.35250585137201431555349282946
Bahawal-Pur30.5333371.78333535235191731535551354445
CHAKWAL32.9166772.85000000000000000
D-G-KHAN30.0500070.63333554438221525535453333040
Jan 2004- Dec 2004
St.NameNEJan_2004Feb_2004Mar_2004Apr_2004May_2004June_2004July_2004Aug_2004Sep_2004Oct_2004Nov_2004Dec_2004
Bhakkar31.6166771.06667000000000000
Bahawal-Nagar29.8809973.35250634026191932364939373545
Bahawal-Pur30.5333371.78333594632212230385648454348
CHAKWAL32.9166772.85000000000000000
D-G-KHAN30.0500070.63333554435212134465447404142


To load above data execute following scripts on your database with appropriate user.

I have used scott user for this demo.


Following scrip will create table in which data would be loaded.


create table climate_data
(
city_Name varchar2(100) ,
North varchar2(100) ,
East varchar2(100) ,
Jan varchar2(100) ,
Feb varchar2(100) ,
Mar varchar2(100) ,
Apr varchar2(100) ,
May varchar2(100) ,
Jun varchar2(100) ,
Jul varchar2(100) ,
Aug varchar2(100) ,
Sep varchar2(100) ,
Oct varchar2(100) ,
Nov varchar2(100) ,
Dec varchar2(100),
year_name varchar2(100) )
/


This package will help to hold data temporarily.


CREATE OR REPLACE PACKAGE uldemo7_climate_data AS
    year_name  varchar2(100);
END uldemo7_climate_data;
/


This trigger will convert the formatted data in to our required column in my case I want to populate year_name column.


CREATE OR REPLACE TRIGGER uldemo7_climate_data_insert
  BEFORE INSERT ON climate_data
  FOR EACH ROW

  BEGIN
  IF substr(:new.Dec,1,3) ='Dec' THEN
     uldemo7_climate_data.year_name := substr(:new.Dec,5,4);   -- save value for later use
  ELSE
     :new.year_name := uldemo7_climate_data.year_name;   -- use last valid value
  END IF;

  END;
/


Following triggers is used to  delete extra/garbage  data form the table.


create or replace TRIGGER uldemo7_climate_data_aft_ins
  after INSERT ON climate_data

  BEGIN
 delete from climate_data where year_name is null or city_name like 'Jan%' or north is null;

  END;
  /
 

======================================

To load data using sqlloader


create load_data.ctl  with following contents in any folder  in my case I am using d:\dataloading.



------------------------------------------------------------
-- SQL-Loader Basic Control File
------------------------------------------------------------
options  ( skip=1 )
load data
  INFILE                'mydata.csv'          
truncate into table   scott.climate_data
fields terminated by ','  
trailing nullcols
  (
    city_Name ,
North ,
East ,
Jan ,
Feb ,
Mar ,
Apr ,
May ,
Jun ,
Jul ,
Aug ,
Sep ,
Oct ,
Nov ,
Dec ,
year_name
  )

 
  --------control file end ----------------------
 
in control file mydata.csv is c.v file that we want to load .
climate_data is table in which data would be loaded.
every time we will load data existing data in the table will be truncated.

Now place your mydata.csv in the same folder where control file is placed.i.e d:\dataloading

go to d:\dataloading using command prompt and execute following command.

  sqlldr scott/oracle@gisdb  control='load_data.ctl' log='result.log'

above command will upload data in results will be displayed.

now we can query climate_data to view loaded data.



 
 

Monday 9 January 2017

Convert matrix data in Excel /csv or text file into Oracle table in Raw format

We want to convert following matrix data into to raw format .

Copy this data in excel and save it in .csv format named mydata.csv.


Time Latitude Temperature Cover (%)Longitude(60-80)
60 60.1 60.2 60.3 60.4 60.5 60.6 60.7 60.8 60.9 61 61.1 61.2
00Z01AUG2015 23 23.8965 44.0527 48.6152 48.6152 62.5586 91.3066 97.3848 100 100 100 100 100 100
00Z01AUG2015 23.1 20.1816 20.5078 28.5332 28.5332 59.543 69.7168 87.7266 100 100 100 100 100 100
00Z01AUG2015 23.2 0 19.7207 25.5566 25.5566 39.8457 69.7168 91.502 91.502 91.502 99.9395 100 100 100
01Z01AUG2015 23.3 0 19.7207 25.5566 25.5566 39.8457 69.7168 91.502 91.502 91.502 99.9395 100 100 100
00Z01AUG2015 23.4 16.8008 19.7207 20.7754 20.7754 36.1016 53.9766 57.2246 63.9258 63.9258 75.6562 62.7617 80.5586 92.6621
00Z01AUG2015 24.9 9.33203 5.06641 10.8008 10.8008 11.75 12.709 13.0117 13.4883 13.4883 13.5254 20.5293 34.25 44.2461
00Z01AUG2015 25 13.293 6.91602 13.9023 13.9023 13.4043 56.5078 56.0312 14.5918 14.5918 65.084 74.0371 91.3945 76.4277
00Z01AUG2015 25.1 22.9375 14.3984 14.7734 14.7734 16.0293 56.9629 57.4316 57.4316 57.4316 70.4473 85.1523 85.8809 76.4277
00Z01AUG2015 25.2 62.002 57.2988 22.3496 22.3496 14.4746 57.9766 57.8535 35.9023 35.9023 70.4473 73.127 80.502 85.9414
00Z01AUG2015 25.3 62.002 57.2988 22.3496 22.3496 14.4746 57.9766 57.8535 35.9023 35.9023 70.4473 73.127 80.502 85.9414
00Z01AUG2015 25.4 61.2637 59.8574 9.69336 9.69336 14.4746 55.9375 58.998 20.9961 20.9961 30.2812 81.0508 83.0996 73.3652
00Z01AUG2015 25.5 61.2637 65.0254 65.0254 65.0254 82.6895 72.084 59.8789 34.9941 34.9941 39.6641 80.5527 75.7246 69.2969
00Z01AUG2015 25.6 62.041 86.4336 88.4277 88.4277 74.0996 60.6797 60.6797 74.2754 74.2754 94.7402 94.7402 75.2793 86.2031
00Z01AUG2015 25.7 83.4902 92.8145 86.5801 86.5801 82.9453 82.9453 83.2461 72.5547 72.5547 72.5547 88.8457 75.2793 74.9043
00Z01AUG2015 25.8 83.4902 92.8145 86.5801 86.5801 82.9453 82.9453 83.2461 72.5547 72.5547 72.5547 88.8457 75.2793 74.9043
00Z01AUG2015 25.9 59.7734 85.4707 92.1621 92.1621 83.2598 88.8789 83.2461 92.7461 92.7461 86.4648 68.2441 71.6484 92.416
00Z01AUG2015 26 92.0527 92.0527 83.252 83.252 90.3535 89.0508 93.7207 84.6875 84.6875 92.6348 56.6328 87.4727 72.209
00Z01AUG2015 26.1 58.6934 58.7188 66.1367 66.1367 62.8516 16.3477 60.5254 92.6309 92.6309 92.6309 63.4512 82.0742 75.3535
00Z01AUG2015 26.2 17.1348 17.0234 15.1914 15.1914 15.1914 15.0527 16.3008 16.5703 16.5703 13.375 63.4512 9.21094 92.0859
00Z01AUG2015 26.3 17.1348 17.0234 15.1914 15.1914 15.1914 15.0527 16.3008 16.5703 16.5703 13.375 63.4512 9.21094 92.0859



Now We are ready to convert the above data into the raw format using following script.

Execute following script on Oracle sql prompt or any Oracle client tool used to execute Oracle queries.

Following script uses External table to convert .csv files into oracle tables then
uses oracle commands to convert matrix data into Oracle raw table format.

Click here to see how to create and used external table to import data into Oracle.

set serveroutput on ;
declare
cursor tabcolumns is
select column_name, ' '||data_type||' ('||DATA_PRECISION||','||data_scale||')' data_types
from user_tab_columns
where table_name = 'LODADED_DATA'
and COLUMN_NAME  not in ('DATESTIME','LATITUDE')
order by column_id;

TOTAL_COLUMNS NUMBER:=0;
cursor matrix_tabcolumns is
select column_name
from user_tab_columns
where table_name = 'MATRIX_DATA'
and COLUMN_NAME  not in ('DATESTIME','LATITUDE')
AND COLUMN_ID<=TOTAL_COLUMNS
order by column_id;

columms varchar2(20000):='';
column_val varchar2(1000):='';
create_table varchar2(30000):='';
val_count number:=0;
TABLE_MISSING EXCEPTION;
PRAGMA EXCEPTION_INIT(TABLE_MISSING,-00942);
begin
    begin
        execute immediate 'ALTER TABLE EXT_TABLE LOCATION(''mydata.csv'')';
        execute immediate 'DROP TABLE LODADED_DATA';
        execute immediate 'CREATE TABLE LODADED_DATA  AS SELECT * FROM EXT_TABLE';
        exception WHEN TABLE_MISSING then
       execute immediate 'CREATE TABLE LODADED_DATA  AS SELECT * FROM EXT_TABLE';
    end;

  for rec_cols in tabcolumns  loop
  execute immediate 'select count('||rec_cols.column_name||') from LODADED_DATA ' into val_count;
    if val_count <> 0 then
       columms:=columms||','||rec_cols.column_name;
       execute immediate 'select '||'replace(''A_''||'||rec_cols.column_name||',''.'',''_'') '||' from LODADED_DATA where rownum=1' into column_val;
       create_table:=create_table||','||column_val||rec_cols.data_types;
       TOTAL_COLUMNS:=TOTAL_COLUMNS+1;
       -- dbms_output.put_line (rec_cols.column_name);
    end if;
  end loop;
  begin
    dbms_output.put_line ('Table created to load matrix data created');
    execute immediate 'drop table MATRIX_DATA';
    execute immediate 'create table MATRIX_DATA (DATESTIME varchar2(100),LATITUDE number(10,5)'||create_table|| ' )';
    exception WHEN TABLE_MISSING then
    execute immediate 'create table MATRIX_DATA (DATESTIME varchar2(100),LATITUDE number(10,5)'||create_table|| ' )';
  end;
  -- dbms_output.put_line (columms);
   execute immediate 'insert into MATRIX_DATA select DATESTIME,LATITUDE'||columms||' from LODADED_DATA where rowid <> (select rowid from LODADED_DATA where rownum=1)';
  commit;
 columms:='';
  for matrix_rec in matrix_tabcolumns loop
    columms:=columms||','||matrix_rec.column_name;
  end loop;
  execute immediate 'drop  table raw_data';
  execute immediate 'create table RAW_DATA as select DATESTIME,LATITUDE,REPLACE(REPLACE(LONGITUDE,''A_'',''''),''_'',''.'') LONGITUDE ,quantity from MATRIX_DATA  UNPIVOT (quantity FOR  LONGITUDE IN ('||substr(columms,2)||'))';
end;


-- select * from RAW_DATA;
-- drop table RAW_DATA;
--where longitude=60;

Restore Cold backup on different host.

In this article we are required to duplicate database using cold backup.

We have two servers named oracle1 and oracle2;

Source database path: D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB
Destination Database Path: C:\oracle\product\10.2.0\oradata\PDADB

We want to duplicate /clone the database  PDADB on oracle1 machine to Oracle2 machine.

For this first Install Oracle on the Oracle2 machine and create a database with same name ie PDADB.

Now  one Oracle2 machine stop oracle services and rename folder that contains database files;

In my case it is C:\oracle\product\10.2.0\oradata\PDADB.

So I renamed PDADB to PDADB_FRESH_BACKUP.

Now copy database folder from folder D:\ORACLE\PRODUCT\10.2.0\ORADATA on your Oracle1 machine to Oracle2 oradata  C:\oracle\product\10.2.0\oradata\ folder.

Start database in mount state.

sqlplus / as sysdba

startup mount;

Execute following command to create contolfile backup in text format, This will help us to verify files and their path on the Oracle1 server that needs to be changed on oracle2 server.


--- backup controlfile to trace;

alter database backup controlfile to trace as 'c:\PDAdb_controlfile.txt';


If your Oracle1 server database path and Oraocle2 server database path are different then execute following command to change path accordingly.

--------rename datafile location --------

alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\REDO01.LOG' to 'C:\oracle\product\10.2.0\oradata\PDADB\REDO01.LOG';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\REDO02.LOG' to 'C:\oracle\product\10.2.0\oradata\PDADB\REDO02.LOG';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\REDO03.LOG' to 'C:\oracle\product\10.2.0\oradata\PDADB\REDO03.LOG';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\SYSTEM01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\SYSTEM01.DBF';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\UNDOTBS01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\UNDOTBS01.DBF';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\SYSAUX01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\SYSAUX01.DBF';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\USERS01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\USERS01.DBF';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\EXAMPLE01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\EXAMPLE01.DBF';
alter database rename file'E:\ORACLEDATA\PDADB01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\e_oradata\PDADB01.DBF';
alter database rename file'E:\ORACLEDATA\PDAAOSDB01.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\e_oradata\PDAAOSDB01.DBF';
alter database rename file'D:\ORACLE\PRODUCT\10.2.0\ORADATA\PDADB\PDADB.DBF' to 'C:\oracle\product\10.2.0\oradata\PDADB\PDADB.DBF';


Execute following command to view the changed path
--- backup controlfile to trace;

alter database backup controlfile to trace as 'c:\PDAdb_controlfile.txt';


If all paths are ok now we can start database using following command;

Alter database open;

Duplicate process is complete.