Friday, 29 August 2014

grep search all file type but not in some file types

If you have GNU grep you can use the --exclude=GLOB option, like
grep -r --exclude='*.sql' pattern dir/

For example, below will search abc in all files apart from sql and svn-base file.
$ grep -riIn 'abc' --exclude=*{.sql,svn-base} .



Wednesday, 27 August 2014

How to restore postgres backup file

How
There are two tools to look at, depending on how you created the dump file.
Your first source of reference should be the man page pg_dump(1) as that is what creates the dump itself. It says:
Dumps can be output in script or archive file formats. Script dumps are plain-text files containing the SQL commands required to reconstruct the database to the state it was in at the time it was saved. To restore from such a script, feed it to psql(1). Script files can be used to reconstruct the database even on other machines and other architectures; with some modifications even on other SQL database products.
The alternative archive file formats must be used with pg_restore(1) to rebuild the database. They allow pg_restore to be selective about what is restored, or even to reorder the items prior to being restored. The archive file formats are designed to be portable across architectures.
So depends on the way it was dumped out. You can probably figure it out using the excellent file(1)command - if it mentions ASCII text and/or SQL, it should be restored with psql otherwise you should probably use pg_restore
Restoring is pretty easy:
psql -U <username> -d <dbname> -1 -f <filename>.sql
or
pg_restore -U <username> -d <dbname> -1 -f <filename>.dump
Check out their respective manpages - there's quite a few options that affect how the restore works. You may have to clean out your "live" databases or recreate them from template0 (as pointed out in a comment) before restoring, depending on how the dumps were generated.

Example
1> create a empty database for example my_test_db.
2> go to postgres bin folder, for examle

C:\Program Files (x86)\PostgreSQL\9.3\bin\

3>Then run this command

pg_restore.exe -h 127.0.0.1 -U postgres -p 5432 -d my_test_db "D:\tem\your_backfile.backup"

4> it will prompt for password, then put in password for use, in this case password for postgres.

Then it will run the store, you will see progress...


How to check tomcat version

  1. try parsing or executing the Tomcat_home/bin directory and look for a script named "version.sh" OR "version.bat" depending on your operating system.
  2. Execute the script ./version.sh
You would have something like:
Server version: Apache Tomcat/6.0.29
Server built:   July 19 2010 1458
Server number:  6.0.0.29
OS Name:        Windows 2003
OS Version:     5.2
Architecture:   x86
JVM Version:    1.6.0_30-b12
JVM Vendor:     Sun Microsystems Inc.

if there are no version.bat or version.sh then use a tool to unzipping JAR files (\tomcat\server\lib\catalina.jar) and look in the file , the version defined under "server.info="
org\apache\catalina\util\ServerInfo.properties. 
or
org\apache\catalina\util\lib\ServerInfo.properties. 

For example:
server.info=Apache Tomcat/6.0.29
server.number=6.0.0.29
server.built=July 19 2010 1458



Friday, 22 August 2014

When you suspect the deployed jar/class file is wrong (version): decompile it to java code and have a clear look !

Q:What program can I use to decompile a class file? Will I actually get Java code, or is it just JVM assembly code?
On Java performance questions on this site I often see responses from people who have "decompiled" the Java class file to see how the compiler optimizes certain things.
A:
  • The final release of JSR 176, defining the major features of J2SE 5.0 (Java SE 5), has been published on September 30, 2004.
  • The lastest Java version supported by JAD, the famous Java decompiler written by Mr. Pavel Kouznetsov, isJDK 1.3.
  • Most of the Java decompilers downloadable today from the Internet, such as “DJ Java Decompiler” or “Cavaj Java Decompiler”, are powered by JAD: they can not display Java 5 sources.
Java Decompiler (Yet another Fast Java decompiler) has:
  • Explicit support for decompiling and analyzing Java 5+ “.class” files.
  • A nice GUI:
screenshot
It works with compilers from JDK 1.1.8 up to JDK 1.7.0, and others (JikesJRockit, etc.).
It features an online live demo version (http://jd.benow.ca/#jd-online) that is actually fully functional! You can just drop a jar file on the page and see the decompiled source code without installing anything.

Postgresql convert/casting

There is no default value for a CAST:
A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:
CAST ( expression AS type )
expression::type
The CAST syntax conforms to SQL; the syntax with :: is historical PostgreSQL usage.

There is no room in the syntax for anything other than the expression to be casted and the desired target type.
However, you can do it by hand with a simple function:
create or replace function cast_to_int(text, integer) returns integer as $$
begin
    return cast($1 as integer);
exception
    when invalid_text_representation then
        return $2;
end;
$$ language plpgsql immutable;
Then you can say things like cast_to_int('pancakes', 0) and get 0.
PostgreSQL also lets you create your own casts so you could do things like this:
create or replace function cast_to_int(text) returns integer as $$
begin
    -- Note the double casting to avoid infinite recursion.
    return cast($1::varchar as integer);
exception
    when invalid_text_representation then
        return 0;
end;
$$ language plpgsql immutable;

create cast (text as integer) with function cast_to_int(text);
Then you could say
select cast('pancakes'::text as integer)
and get 0 or you could say
select cast(some_text_column as integer) from t
and get 0 for the some_text_column values that aren't valid integers. If you wanted to cast varchars using this auto-defaulting cast then you'd have to double cast:
select cast(some_varchar::text as integer) from t
Just because you can do this doesn't make it a good idea. I don't think replacing the standard text to integer cast is the best idea ever. The above approach also requires you to leave the standard varchar to integer cast alone, you could get around that if you wanted to do the whole conversion yourself rather than lazily punting to the built in casting.
NULL handling is left as an (easy) exercise for the reader.

===============================
Test
1>
select cast('1111.1' as decimal)
select cast('1111.1' as integer)
2>
select ('1111.1')::decimal
select ('1111')::integer

select ('1111.1')::integer //error here


3>check for null
SELECT NULLIF('1111', '')::integer
SELECT NULLIF('1111.1', '')::decimal

4>use cast within SQL
INSERT into test3 values(('888')::integer, '999');
INSERT into test3 values(CAST('888' as integer), '999');

Total visitors since Jan 2012

World Visitor Map