mercoledì 9 aprile 2008

Oracle DB: Cold backup

A cold backup is a phisical backup and is done with the database in a shutdown state (not available to users).
The datafiles do not change during the copy so the database is in sync upon restore.

First of all, you need to shutdown your instance:

export TNS_ADMIN=/export/opt/oracle/8.1.7/network/admin
export ORACLE_SID=some_sid
export ORACLE_HOME=/export/opt/oracle/8.1.7

DT=`date +%Y.%m.%d`
PFILE=/export/opt/oracle/8.1.7/dbs/init$ORACLE_SID.ora

echo Backup for $DT
echo Selecting the files that must be backed up....

/export/opt/oracle/8.1.7/bin/sqlplus -S '/ as sysdba' [minor][minor]eofsql

set termout off
set pages 0
set lines 120
set feedback off
set trimspool on
spool files.2.backup
select name from v$datafile;
select name from v$controlfile;
select member from v$logfile;
select '$PFILE' from dual;
spool off
shutdown immediate
exit;
EOFSQL


then you should "tar" your datafiles, controlfiles and logfiles (so called "image copy"):

tar cfv - -I files.2.backup | gzip | rsh -l some_user some_host "cat - > /path/to/destdir/backup-$DT.tar.gz"


or you can direclty tar a folder; the following is an example on linux:

tar -zcvf ASRAC_20080409_00.tar.gz ASRAC /hera01/app/oracle/oradata/ASRAC

finally startup you instance:

/export/opt/oracle/8.1.7/bin/sqlplus -S '/ as sysdba' [minor][minor]eofsql
startup pfile=$PFILE
exit;
EOFSQL

giovedì 27 marzo 2008

LSOF: Who's doing what

Sometimes i got problems on identifing what kind of thing is doing a specified process.
First of all, you need to identify your process in "ps -efl" list.

To learn what program is listening on wich port:

netstat -tlnp

You can also identify processes using files or sockets:

fuser [FILENAME]

now check if you have LSOF utility on you machine.

locate lsof

probably you will find it under "/usr/sbin/lsof".
LSOF (LiSt Open Files) is a useful and powerful tool that will show you opened files.

When lsof is called without parameters, it will show all the files opened by any processes.

lsof | nl

Other examples on who is using the apache executable file, /etc/passwd, what files are opened on device /dev/hda6 or who's accessing /dev/cdrom:

lsof `which apache2`
lsof /etc/passwd
lsof /dev/hda6
lsof /dev/cdrom


What process IDs are using the apache binary, and only the PID?

lsof -t `which apache2`


what files are opened by processes whose names starts by "k" (klogd, kswapd...) and bash?

lsof -c k
lsof -c bash

what files are opened by init?

lsof -c init


what files are opened by processes whose names starts by "courier", but exclude those whose owner is the user "mack"?

lsof -c courier -u ^mack


processes opened by user apache and user mack:

lsof -u apache,mack

Show what files are using the process whose PID is 30297:

lsof +p 30297

Search for all opened instances of directory /tmp and all the files and directories it contains:

lsof +D /tmp

List all opened internet sockets and sockets related to port 80:

lsof -i
lsof -i :80

List all opened Internet and UNIX domain files:

lsof -i -U

Show us what process(es) has an UDP connection opened to or from the host www.akadia.com at port 123 (ntp):

lsof -iUDP@www.akadia.com:123




(about LSOF: http://www.akadia.com/services/lsof_intro.html)

mercoledì 26 marzo 2008

Ldapsearch and LDIF files in OID

The ldapsearch utility tool comes with every common LDAP library and allows a command-line user to run queries against LDAP directories.

Using Oracle OID ldap you can find the utility under "$OID_HOME/bin/ldapsearch" and the following is the syntax:

ldapsearch -h oid_hostname
-D "binddn"
-w password
[-Y "proxy_dn"]
[-p ldap_port]
[-V ldap_version]
-b "basedn"
{-s base|one|sub}
{"filter_string" [attributes]|-f input_file}
[-A]
[-a never|always|search|find]
[-F separator]
[-S] [-R] [-i 1|0] [-t] [-u] [-L|-X] [-B] [-M] [-v] [-n]
[-l time_limit]
[-z size_limit]
[-O ref_hop_limit]
[-U SSL_auth_mode {-W wallet_location -P wallet_password}]
[-d debug_level]
[-E character_set]


(details here: http://download.oracle.com/docs/cd/B14099_19/idmanage.1012/b15883/syntax_datamngmnt013.htm)


Here some example on using this utility.


[Performing a simple subtree search]

ldapsearch -p 389 -h myhost -b "c=US" -s sub -v "cn=John*"


[The following example retrieves only the distinguished name along with the surname (sn) and description (description) attribute values]

ldapsearch -p 389 -h myhost -b "c=US" -s sub -v "cn=Person*" dn sn description


[search for all_groups starting on a given DN]

ldapsearch \
-h hostname \
-p 3060 \
-D cn=adminuser \
-w password \
-b 'cn=Groups,dc=organizazion,dc=com' \
-s sub objectclass=orclgroup 'cn=*'


[search for users in a given DN]


ldapsearch \
-h hostname \
-p 3060 \
-D cn=adminuser \
-w password \
-b 'cn=GRP_ITA_TPM_VIW,cn=portal.id_install,cn=groups,dc=organization,dc=com' \
-s sub objectclass=orclgroup \
uniquemember



You can generate an LDIF file from an ldapsearch.
The LDAP Data Interchange Format (LDIF) is a standard plain text data interchange format for representing LDAP directory content and update requests.
For example, i can search for all groups with name starting with FBK,DOC,GRP,ORG a generate a file:


ldapsearch \
-h hostname \
-p 3060 \
-D cn=adminuser \
-w password \
-b 'cn=portal.id_install,cn=Groups,dc=organization,dc=com' \
-s sub \
"(&(objectclass=orclgroup)(|(cn=FBK*)(cn=DOC*)(cn=GRP*)(cn=ORG*)))" \
dn > all_grp.ldif

Optionally i can clear the file and consider only the rows i need:

cat all_grp.ldif |grep cn > t1.txt


and then import the LDIF file on a target LDAP:


ldapadd \
-h targethost \
-p 13060 \
-D cn=adminuser \
-w password \
-f ./t1.ldif

Case statement in SQL and PL/SQL

The following as examples using SIMPLE and SEARCHED CASE statement in pl/sql.
Simple CASE:

text := case n
when 1 then one
when 2 then two

when 3 then three
else other
end case;


Searched CASE:


text := case
when n = 1 then one

when n = 2 then two
when n = 3 then three
when ( n > 3 and n <>
else other
end;


Exception handling:

...
case
when p = 1 then Action1;
when r = 2 then Action2;
when q > 1 then Action3;
end case;
exception
when case_not_found
...



In SQL, you can also have SIMPLE and SEARCHED case.

SELECT last_name, commission_pct,
(CASE commission_pct
WHEN 0.1 THEN ‘Low’
WHEN 0.15 THEN ‘Average’
WHEN 0.2 THEN ‘High’
ELSE ‘N/A’
END ) Commission
FROM employees ORDER BY last_name;



SELECT last_name, job_id, salary,
(CASE
WHEN job_id LIKE 'SA_MAN' AND salary <>
WHEN job_id LIKE 'SA_MAN' AND salary >= 12000 THEN '15%'
WHEN job_id LIKE 'IT_PROG' AND salary <>
WHEN job_id LIKE 'IT_PROG' AND salary >= 9000 THEN '12%'
ELSE 'NOT APPLICABLE'
END ) Raise
FROM employees;

venerdì 7 marzo 2008

Disable Ctrl-N

Here is a simple way to disable CTRL-N in Javascript.



document.onkeydown = function(){
if ((event.keyCode == 78) && (event.ctrlKey)){
//alert ("No new window")
event.cancelBubble = true;
event.returnValue = false;
event.keyCode = false;
return false;
}
}

No right click

Here is a simple way to disable right-click in Javascript.



var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")

mercoledì 5 marzo 2008

JSON

JSON (JavaScript Object Notation) is a format used for transmitting structured data over a network connection in a process called serialization. Its main application is in Ajax web application programming, where it serves as an alternative to the traditional use of XML.


JSON's basic types are
  • Number (integer, real, or floating point)
  • String (double-quoted Unicode with backslash escapement)
  • Boolean (true and false)
  • Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
  • Object (collection of key/value pairs, comma-separated and enclosed in curly brackets)
  • null


The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, contains an object representing the person's address, and contains a list of phone numbers (an array).


{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567"
]
}



The following Javascript code shows how the client can use an XMLHttpRequest to request an object in JSON format from the server. (The server-side programming is omitted; it has to be set up to respond to requests at url with a JSON-formatted string.)


var the_object;
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 ) {
if ( http_request.status == 200 ) {
the_object = eval( "(" + http_request.responseText + ")" );
} else {
alert( "There was a problem with the URL." );
}
http_request = null;
}
};





More info at http://www.json.org