Wednesday, December 22, 2010

Monday, December 20, 2010

Installing and configure Apache Solr with MySQL


Apache Solr™ is our high performance enterprise search server, with XML/HTTP and JSON/Python/Ruby APIs, hit highlighting, faceted search, caching, replication, distributed search, database integration, web admin and search interfaces.

1. Install Java

yum install java


2. Download & Extract Solr

cd /usr/local/src
wget http://www.apache.org/dist//lucene/solr/1.4.1/apache-solr-1.4.1.tgz
tar -xzvf apache-solr-1.4.1.tgz


3. Download MySQL Java connector

wget http://mysql.spd.co.il/Downloads/Connector-J/mysql-connector-java-3.1.14.zip
unzip mysql-connector-java-3.1.14.zip
cd mysql-connector-java-3.1.14
cp mysql-connector-java-3.1.14-bin.jar /usr/local/src/apache-solr-1.4.1/example/lib/


4. Configure MySQL Database
Create a new xml file called data-import.xml , change the obvious variables to suit your DB. In this example, I am indexing a own DB table called users.

cd /usr/local/src/apache-solr-1.4.1/example/solr/conf
nano data-import.xml

put follow text

<dataconfig>
<datasource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.7/mydatabase" user="myuser" password="mypassword">
<document name="content">
<entity name="node" query="select user_id AS id, mobile AS mobile, user_status AS status FROM users">
<field column="id" name="id">
<field column="mobile" name="mobile">
<field column="status" name="status">
</field></field></field></entity>
</document>
</datasource>
</dataconfig>



5. Configure solr .
Edit file solrconfig.xml which is located in /usr/local/src/apache-solr-1.4.1/example/solr/conf directory. Add the following requestHandler entry if not already existing.

<requesthandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requesthandler>


6. Now we will configure solr’s schema by editing schema.xml in /usr/local/src/apache-solr-1.4.1/example/solr/conf directory. Delete old tag and add the following fields as required. The xml format is self explanatory.

<fields>

<field name="id" type="string" indexed="true" stored="true" default="NOW" required="true">
<field name="mobile" type="text" indexed="true" stored="true" required="true">
<field name="status" type="text" indexed="false" stored="true" required="false">

</field></field></field></fields>

<uniquekey>id</uniquekey>

<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultsearchfield>mobile</defaultsearchfield>



7. Start SOLR

java -jar /usr/local/src/apache-solr-1.4.1/example/start.jar



Performing full or delta indexing
If everything works correctly, you can get solr to fully index the configured tables by accessing the following command via your browser. http://:8983/solr/dataimport?command=full-import

You can check the status of the command by accessing http://:8983/solr/dataimport

If everything works correctly, you can now search for data from http://:8983/solr/admin/ and you should now have data results in XML format.


To do an incremental or delta indexing of data since the last full or delta, increment, issue the command http://:8983/solr/dataimport?command=delta-import

You can now access these xml results from your web application. There are client api’s available for RoR, php, java etc.

References
http://www.cabotsolutions.com/2009/05/using-solr-lucene-for-full-text-search-with-mysql-db/

http://www.ipros.nl/2008/12/15/using-solr-with-wordpress/

http://wiki.apache.org/solr/DataImportHandler#head-df246a3aed0bb38297f3449bc35a0bdf38a272b5

http://lucene.apache.org/solr/tutorial.html

Monday, October 4, 2010

Resize image and set background






$filename = '/path/to/source/file/1.jpg';

// Set a maximum height and width
$width = 98;
$height = 100;

// set background color
$color_R = 0;
$color_G = 0;
$color_B = 0;

$worg=$width;
$horg=$height;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$src = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($filename);
imagecopyresampled($src, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);


// Create image instances

$dest = imagecreatetruecolor($worg, $horg);
$mycolor = imagecolorallocate($dest, $color_R, $color_G, $color_B);
imagefill($dest, 0, 0, $mycolor);

if ($width==$worg) {$start_x=0; $start_y=round(($horg-$height)/2);}
if ($height==$horg) {$start_y=0; $start_x=round(($worg-$width)/2);}

//echo $width." ".$height." ".$worg." ".$horg."
";
//echo $start_x;
//die;

// Copy
imagecopy($dest, $src, $start_x, $start_y, 0,0,$width, $height);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($dest);

imagedestroy($dest);
imagedestroy($src);


Monday, July 5, 2010

Bash: Delete recursive duplicated folders

for i in `find /home/directory -type d`
do

cd $i
mydir=$(pwd|rev|awk -F \/ '{print $1}'|rev)
directory="$i/$mydir/$mydir"
dirtorm="$i/$mydir"
if [ -d "$directory" ]; then
echo "=======>$i"
rm -f -r $dirtorm
echo $directory
fi

done

Thursday, June 10, 2010

Bacula Backup - Building a File Daemon or Client

If you run the Director and the Storage daemon on one machine and you wish to back up another machine....

Install gcc & c++ compilers

yum install gcc gcc-c++


Download packages:


wget https://sourceforge.net/projects/bacula/files/bacula/5.0.2/bacula-5.0.2.tar.gz/download
tar -xzvf bacula-5.0.2.tar.gz

wget https://sourceforge.net/projects/bacula/files/depkgs/18Dec09/depkgs-18Dec09.tar.gz/download
tar -xzvf depkgs-18Dec09.tar.gz
mv depkgs bacula-5.0.2/



Compile it:

cd bacula-5.0.2/depkgs/
make
gmake mtx-install

cd ..



configure bacula


CFLAGS="-g -Wall" \
./configure \
--sbindir=$HOME/bacula/bin \
--sysconfdir=$HOME/bacula/bin \
--with-pid-dir=$HOME/bacula/bin/working \
--with-subsys-dir=$HOME/bacula/bin/working \
--enable-client-only \
--with-working-dir=$HOME/bacula/bin/working \
--with-dump-email=$USER



Install bacula

make install


How to start File daemon on start or add service

cd /root/bacula/bin

add content to file bacula

#! /bin/sh
# chkconfig: 2345 95 20
# description: Bacula Clien File Deamon
# processname: bacula



copy it to /etc/rc.d/init.d/

cp bacula /etc/rc.d/init.d/
chkconfig add bacula

Monday, April 26, 2010

PHP Retrieve string

Retrieve string from HTML or XML or any text.....

$string = "This is My text test Your text";
echo retrieve($string,"",""); // output: My text
echo retrieve($string,"","",2); // output: Your text


function retrieve($data, $from, $till, $freq){
if ($freq==0){$freq=1;}
//if (strpos($data, $from)==FALSE) return $data;
//if (strpos($data, $till)==FALSE) return $data;
while($freq){

$temp = substr($data, strpos($data,$from),(strlen($data)-strpos($data,$from)));
$freq--;
$data=substr($temp,strlen($from));
}
$temp = substr($temp,strlen($from),strpos($temp,$till)-strlen($from));
return $temp;
}

Friday, January 22, 2010

Embedded Linux From Scratch in 90 minutes

Goals
- Linux kernel configuring and compiling;
- Root file system creation;
- Busybox compiling and installation;
- Device file creation;
- System initialization scripts: virtual file system, networking;
- Setup of a simple HTTP interface to the system.

1. Download fast processor emulator using a portable dynamic translator.

wget http://download.savannah.gnu.org/releases/qemu/qemu-0.12.2.tar.gz
tar zxvf qemu-0.12.2.tar.gz
cd qemu-0.12.2.tar.gz


Then you configure QEMU and build it (usually no options are needed):

./configure


install zlib if you need (apt-get install zlib1g-dev)

make


Then type as root user:

make install




2. Getting the Kernel sources:
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.4.tar.gz

Start with a minimalistic kernel configuration
make allnoconfig


Adding settings specific to the embedded system
install ncurses if you need (apt-get install libncurses5-dev)
make menuconfig


Compiling:
make


You will get
arch/x86/boot/bzImage


3. Getting Busybox source
wget http://www.busybox.net/downloads/busybox-1.15.3.tar.gz


Configuring BusyBox
make menuconfig


choosing to build a statically, natively compiled executable.

Compiling busybox
make


Pre-installing busybox (in the _install/ subdirectory):
make install


4. Creating a root filesystem
Creating an empty file with a 1000K size:
dd if=/dev/zero of=rootfs.img bs=1k count=1000

Formating this file for the ext2 filesystem:
mkfs.ext2 -i 1024 -F rootfs.img

5. Populating the root filesystem
logged as root:
Creating a mount point:
mkdir /mnt/tmproot

Mounting the root filesystem image:
mount -o loop rootfs.img /mnt/tmproot

Copying the busybox file structure into the mounted image:
rsync -a busybox/_install/ /mnt/tmproot/
(or cp -ax busybox/_install/ /mnt/tmproot/)
chown -R root:rooot /mnt/tmproot/

Flushing the changes into the mounted filesystem image:
sync

6. Booting the Virtual System
Using the QEMU emulator as a bootloader
qemu -m 32 -hda rootfs.img -kernel linux-2.6.25.4/arch/x86/boot/bzImage -append "root=/dev/hda" -redir tcp:5555::80

click "Applications -> Internet -> Terminal Server Client"then fill in the Computer with "127.0.0.1:5900",and select VNC in the protocol, if VNC grayed, install vncviewer
apt-get install vncviewer

Friday, January 1, 2010

Network Traffic Generation Tool for Linux

Mausezahn is a free fast traffic generator written in C which allows you to send nearly every possible and impossible packet. It is mainly used to test VoIP or multicast networks but also for security audits to check whether your systems are hardened enough for specific attacks.

http://www.perihel.at/sec/mz/index.html