add

Thursday, June 14, 2012

Shell Script for updating an Oracle table through a file (from a remote Server)


a simple Script for updating an Oracle table through a file (from a remote Server)
we will b using a shell script for all this
process is described step by step as
1. first Of all create a virtual directory and external table use this page http://eaziweb.blogspot.com/2012/03/oracle-create-external-table.html
2. get file from remote server using ncftpget
3. create Oracle Procedure for data insertions
4. Oracle Procedure uses  UTL_FILE utility for logging different file events like success and failure

Summary
Serrver : server IP like 192.168.168.0
directory Object Name : DIR_3352
directory Object Path : /home/xyz/3352
External File : xtern_3352.txt
Upload File Name Format : add_20120612.csv
Upload File Format : 03007236317,09-MAR-12,C2676M0749
External Table :
create table xtern_3352 ( mobile varchar2(100),dateCol varchar2(20),id varchar2(20))
organization external (
type oracle_loader
default directory DIR_3352
access parameters (
records delimited by newline
FIELDS TERMINATED BY ','
)
location ('xtern_3352.txt')
)
reject limit unlimited;
Procedure : TEST_PARAMETER


Shell Script :
######################################################################
##                                              USAGE                                                                         ##
##      call it as 3355.sh action-parameter                                                                          ##
##      eg. source 3355.sh add                                                                                          ##
##      current action parameters are                                                                                  ##
##      1.add                                                                                                                     ##
##      2.delete                                                                                                                  ##
######################################################################
#!/bin/sh
action=$1
day=1
date_var=`date -d "-$day days" +%Y-%m-%d`
#backup old files
mv -f /home/xyz/3352/xtern_3352.txt /home/xyz/3352/xtern_3352_`echo $date_var`.txt
#get the file from remote Server and put in /path/to/local/dir/ directory
ncftpget -u ftp-user-name -p paSSword rem.ote.Ser.ver.IP /path/to/local/dir/ /file`echo $date_var`_sub.csv
mv -f  /home/xyz/file`echo $date_var`_sub.csv  /home/xyz/new.txt
cat /home/xyz/new.txt | cut -d ' ' -f1> /home/xyz/xtern_3352.txt
source /file_validation_script.sh 3352/xtern_3352.txt
ORACLE_HOME=/Oracle/app/oracle/product/11.2.0/dbhome_1
export ORACLE_HOME
ORACLE_SID=orcl
export ORACLE_SID
/Oracle/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus /NOLOG << EOF

connect db_user/db_pwd
set serveroutput on
exec TEST_PARAMETER('`echo $action`');
EXIT;
echo "Now making file"
EOF

Oracle Procedure : TEST_PARAMETER

 ( action IN varchar2 )
  AS CURSOR C_CONTENT IS SELECT * from xtern_3352 ;
--============================================================
-- --
-- Initialization --
--===========================================================--
datecheck number;
v_message varchar2(100);
ERROR_mobile_FORMAT exception;
ERROR_mobile_VALUE exception;
ERROR_DATE_VALUE exception;
ERROR_DUPLICATE_mobile exception;
ERROR_NO_mobile_FOUND  exception;
v_file  UTL_FILE.FILE_TYPE;
s_file  UTL_FILE.FILE_TYPE;
cur_date varchar2(100);
file_Name varchar2(100);
success_File_Name varchar2(100);
var_query varchar2(100);
--=============================================================--
-- --
--                        Start of Logic -- --                                                                   --
--=============================================================--
BEGIN

    -------------------------------------------------------------------------------------------
    --                       File details               --
    -------------------------------------------------------------------------------------------
  SELECT  to_char(sysdate, 'DDMonYYYY_HH24MISS') into cur_date from dual;
  file_Name := '' ;
  success_File_Name := '' ;
  if ( action = 'add') then
    dbms_output.put_line('add Action');
    file_Name := 'add_error_'||cur_date||'.txt' ;
    success_File_Name := 'add_success_'||cur_date||'.txt' ;
    --var_query := 'insert into WHITE_LIST_auto values('||wl_DATA.mobile||','||wl_DATA.dateCol||','||wl_DATA.ID||')' ;
    var_query := 'insert into WHITE_LIST_auto values(wl_DATA.mobile,wl_DATA.dateCol,wl_DATA.ID)' ;
  else if ( action = 'delete') then
    dbms_output.put_line('delete Action');
    file_Name := 'delete_error_'||cur_date||'.txt' ;
    success_File_Name := 'delete_success_'||cur_date||'.txt' ;
    var_query := 'delete from WHITE_LIST_auto where mobile=wl_DATA.mobile' ;
  else
    dbms_output.put_line('What do u want to do ??? Please Select an action (add or delete)');
    return;
 
    dbms_output.put_line('else Action');
    --return 'Please Select an action (add or delete)';
  end if;
  end if;

  SELECT  to_char(sysdate, 'DDMonYYYY_HH24MISS') into cur_date from dual;
  --file_Name := 'error_'||cur_date||'.txt' ;
  dbms_output.put_line('error is '|| file_Name);
  v_file := UTL_FILE.FOPEN(location     => 'dir_3352',
                           filename     => file_Name,
                           open_mode    => 'w',
                           max_linesize => 32767);
  --success_File_Name := 'success_'||cur_date||'.txt' ;
  dbms_output.put_line('success_File_Name is '|| success_File_Name);
  s_file := UTL_FILE.FOPEN(location     => 'dir_3352',
                           filename     => success_File_Name,
                           open_mode    => 'w',
                           max_linesize => 32767);

FOR wl_DATA in C_CONTENT
LOOP
  begin
    begin
    -------------------------------------------------------------------------------------------
    -- Error Validations      --
    -------------------------------------------------------------------------------------------
      if length(wl_DATA.mobile) <> 11 or substr(wl_DATA.mobile,0,2) <> '03' then
raise ERROR_mobile_FORMAT;
      else if  ( (LENGTH(TRIM(TRANSLATE(wl_DATA.mobile, '+0123456789',' '))) is not null)  ) then
raise ERROR_mobile_VALUE;
      else if (is_date(wl_DATA.dateCol, 'dd-mon-yy') = 0) then
raise ERROR_DATE_VALUE;
      else if (CHECK_WHITELIST(wl_DATA.mobile) = 1 and action = 'add') then
raise ERROR_DUPLICATE_mobile;
      else if (CHECK_WHITELIST(wl_DATA.mobile) = 0 and action = 'delete') then
raise ERROR_NO_mobile_FOUND;
      else
          -----------------------------------------------------------------------------
 -- Dumping Valid Data      --
 -----------------------------------------------------------------------------
 if ( action = 'add') then
   insert into WHITE_LIST_auto values(wl_DATA.mobile,wl_DATA.dateCol,wl_DATA.ID);
 else if ( action = 'delete') then
   delete from WHITE_LIST_auto where mobile=wl_DATA.mobile;
 else
   dbms_output.put_line('else Action');
   return;
   --return 'Please Select an action (add or delete)';
   end if;
   end if;
   
--dbms_output.put_line(var_query);
--execute immediate var_query ;
--insert into WHITE_LIST_auto values(wl_DATA.mobile,wl_DATA.dateCol,wl_DATA.ID);
UTL_FILE.PUT_LINE(s_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID);
      end if;
      end if;
      end if;
      end if;
      end if;
      exception
          -----------------------------------------------------------------------------
 -- Exception Handling      --
 -----------------------------------------------------------------------------
        when ERROR_mobile_FORMAT then
          v_message:='Mobile number Format is invalid';
 UTL_FILE.PUT_LINE(v_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID|| ',' ||v_message);
        when ERROR_mobile_VALUE then
          v_message:='Mobile number is invalid';
 UTL_FILE.PUT_LINE(v_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID|| ',' ||v_message);
when ERROR_DATE_VALUE then
          v_message:='date is invalid';
 UTL_FILE.PUT_LINE(v_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID|| ',' ||v_message);
        when ERROR_DUPLICATE_mobile then
          v_message:='Duplictae mobile';
 UTL_FILE.PUT_LINE(v_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID|| ',' ||v_message);
        when ERROR_NO_mobile_FOUND then
          v_message:='No mobile Found';
 UTL_FILE.PUT_LINE(v_file,wl_DATA.mobile|| ',' ||wl_DATA.dateCol|| ',' ||wl_DATA.ID|| ',' ||v_message);
    end;
  end;
  END LOOP;
  UTL_FILE.FCLOSE(v_file);        
  UTL_FILE.FCLOSE(s_file);        
commit;

END;
--=============================================================
-- --
-- End of Logic --
-- =============================================================

Monday, June 11, 2012

oracle procedure : number validation

if ( (LENGTH(TRIM(TRANSLATE(contentcode, ‘+0123456789′,’ ‘))) is not null) or (LENGTH(TRIM(TRANSLATE(otherfield, ‘+0123456789′,’ ‘))) is not null)) then
dbms_output.put_line(‘contentcode or otherfield is invalid’); 

Tuesday, May 29, 2012

Installing Nginx With PHP5 And MySQL Support On Ubuntu 12.04 (precise)

add relevant PPA in the source list.
 edit like
nano /etc/apt/sources.list

as i am using ubuntu 12.04 (precise ) so add following
deb http://ppa.launchpad.net/nginx/development/ubuntu precise main 
deb-src http://ppa.launchpad.net/nginx/development/ubuntu precise main 

 First, for good measure lets make sure our server is all up-to-date.
apt-get update

Installing MySQL

Then let’s begin by installing MySQL:
apt-get install mysql-server mysql-client
after entering the above command you will also be prompted for a MySQL “root” user password…

Installing PHP

Next up, lets install PHP5 and a few common extensions (here is a list if you are in need of other extensions):

apt-get install php5-cgi php5-cli php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl
As you may have noticed, we have installed php-cgi, that is because we will be running a FastCGI interface.
here are some articles online which recommend using lighttpd for its FastCGI interface, this is totally not needed. PHP has its own FastCGI interface which works perfectly well (thanks to Tomasz Sterna for a great article on FastCGI with Nginx)

At this point, we will be using a little bit of vim to do a bit of file editing, so here is a quick primer on using vim.
Lets create the following file:  
vim /etc/init.d/php-fastcgi

 This file will have the following content:
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=500

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL


 As Tomasz Sterna mentions, you will need to fiddle with the PHP_FCGI_CHILDREN and PHP_FCGI_MAX_REQUESTS variables depending on your server’s amount of memory and compute power. I am running a baseline 256 MB / 10 GB Rackspace Cloud Server so I use the following settings which seem to work very well (as seen above):

PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=500
Moving on … after you’ve created and saved the file we will make it executable and then start up the FastCGI service with the following commands:
chmod +x /etc/init.d/php-fastcgi
/etc/init.d/php-fastcgi start
We will want the service to auto start when we reboot our server, so we also do the following:
 update-rc.d php-fastcgi defaults

Installing Nginx

Installing Nginx is easy, use the following commands to install and then start up the Nginx server.
apt-get install nginx
/etc/init.d/nginx start


After installing Nginx, it will be automatically configured to start when we reboot our server (unlike the PHP FastCGI service we had to setup), so we are all set.

Testing Nginx and PHP

At this point we can see that Nginx is working by typing the server’s IP address into a web browser (http://[IP_ADDRESS]/). You should get a “Welcome to nginx!” message.
Now lets test PHP, we will create a generic phpinfo.php file with the following:

echo "" > /var/www/nginx-default/phpinfo.php

/var/www/nginx-default/ is the Nginx server default root directory…
If you use your browser to go to  
http://[IP_ADDRESS]/phpinfo.php 
,you will notice that it doesn’t work … before this will work, we have to enable FastCGI in the Nginx config file. Open up the following file:

vim /etc/nginx/sites-available/default
in the server {
change #root /usr/share/nginx/www; 

to root /var/www;
 
 
Find the following lines (scroll to line 47):
       
#location ~ \.php$ {
    #fastcgi_pass   127.0.0.1:9000;
    #fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #includefastcgi_params;
#}
 and change them to (removing the # character from each line, changing line 50 and adding a space between include and fastcgi_params on line 51):
location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default/$fastcgi_script_name;
    include fastcgi_params;
}
Now lets restart Nginx so our config changes will take effect:
/etc/init.d/nginx restart
 Now use your web browser to go to http://[IP_ADDRESS]/phpinfo.php, you should see a PHP info page.

Installing phpMyAdmin + phpMyAdmin Vhost Configuration 

apt-get install phpmyadmin
You will see the following questions:
Web server to reconfigure automatically: <-- select none (because only apache2 and lighttpd are available as options)
Configure database for phpmyadmin with dbconfig-common? <-- No
You can now find phpMyAdmin in the /usr/share/phpmyadmin/ directory. Now we must configure our vhost so that nginx can find phpMyAdmin in that directory.
Open /etc/nginx/sites-available/
nano /etc/nginx/sites-available/
 and add the following part to the server {} container:

server {
[...]
        location /phpmyadmin {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+\.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       fastcgi_pass 127.0.0.1:9000;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include /etc/nginx/fastcgi_params;
               }
               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }
[...]
}
Reload nginx:
/etc/init.d/nginx reload

That's it! You can now go to
http://127.0.0.1/phpmyadmin/
or http://localhost/phpmyadmin
 some useful links for further help regarding LEMP (linux,enginx,mysql,php)
  1. http://suckup.de/linux/ubuntu/nginx-php5-fpm-auf-debianubuntu/
  2. http://www.rackspace.com/knowledge_center/article/installing-nginx-and-php-fpm-running-on-unix-file-sockets
  3. http://www.howtoforge.com/running-phpmyadmin-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04






linux file permission simplest rule of thumb


Chmod 777 –R /var/www/html/webdir
Permission level
1.       r (read)       =  4
2.       w (write)     =  2
3.       x (execute)  =  1

For example : 
chmod 753 abc.txt thus means
assign (4+2+1)(4+1)(2+1) to abc.txt

=>
assign (rwx)(rx)(wx) to abc.txt
assign (owner)(group)(other) to abc.txt
Another example

chmod 572 dump.txt thus means
assign (4+1)(4+2+1)(2) to 
dump .txt
=>
assign (rw)(rwx)(w) to  dump .txt

Monday, May 28, 2012

Sending Email With Attachments From Unix / Linux Command

If you need to send an email with a text file (or binary file) as attachment using shell script or command prompt in Unix or Linux; try mutt - a terminal-based e-mail client for Unix-like systems.

mutt -s "Test mail" -a /tmp/shahzeb.tar.gz shahzeb143@gmail.com < /tmp/mailmessage.txt

Where,
  • shahzeb143@gmail.com   - is the recipient.
  • /tmp/mailmessage.txt - is the main body of the e-mail (read message from the file "mailmessage.txt").
  • -a /tmp/shahzeb.tar.gz - is an attachment.
  • -s "Test mail" - is a subject line of email Message.

If MUTT is not available???

If mutt is not installed, use the apt-get or yum or up2date commands as follows (you must login as a root user). Debian / Ubuntu Linux user type the following command to install mutt client:
# apt-get install mutt
OR
# yum install mutt 

Some useful resources 


Tuesday, May 22, 2012

php soap client Uncaught SoapFault exception


I was receiving the following error while running a php soap client on my live server
googled but of no use
[Tue May 22 12:09:33 2012] [error] [client 39.47.135.86] PHP Fatal error:  Uncaught SoapFault exception: [HTTP] Error Fetching http headers in /var/www/html/abc/xyz.php:7\nStack trace:\n
#0 [internal function]: SoapClient->__doRequest('
#1 /var/www/html/abc/xyz.php(7): SoapClient->__soapCall('auth_method', Array)\n
#2 /var/www/html/abc/def.php(12): soapclient('user', 'pass')\n
#3 {main}\n  thrown in /var/www/html/abc/xyz.php on line 7, referer: http://192.168....../abc/xyz.php?msgs=%3Cdiv%20class=%27eRR%27%3EPlease%20log%20in%20to%20visit%20this%20page%3C/div%3E


Solution: There was a little bug missing ";" in my soap server - I think some one changed the file.
check the remote file /Server there may b some error in Soap Server


Friday, May 4, 2012

nginx

some helpful URLs for a starter to install and configure nginx with php and phpmyadmin on ubuntu
1. http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-debian-squeeze
2. http://www.howtoforge.com/installing-nginx-with-php5-and-mysql-support-on-debian-squeeze
3. http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-11.10
4.http://www.howtoforge.com/running-phpmyadmin-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04
5. http://rubyist-journal.com/2010/02/28/howto-nginx-php5-mysql-phpmyadmin-ubuntu-shortest-setup/

detailed step by step process can be found at
http://eaziweb.blogspot.com/2012/05/installing-nginx-with-php5-and-mysql.html