Wednesday, September 29, 2010

MySQL Dump cronjob on linux server

Creating mysql dump for production databases is very important. In case of any failure or malefic attack on DB, The only way to restore to previous state is by having proper mysql dump file. Follows an example mysql dump shell script with auto naming with dumping time stamp. Its important to create the shell scripts in unix mode, if you create on a PC, It'll add some unwanted character encoding, and it'll lead to some erroneous response on the script.

today=`date +%Y%m%d%H%M`
mysqldump -u user -ppassword mydb > /apps/mysql_dump/$backup
Its important to create the shell script carefully. No spaces, unless its required. in between -u and user name, there is a space, but no space between -p and password. As per the usage of system, we can decide on which interval the cronjob could be set. normally the format of a crontab entry is like the following.

* * * * * /apps/scripts/ >> /dev/null 2>&1
 Here the first 5 portions are the timing interval markers (Minute (0-59), Hour (2-24), Day of month (1-31), Month (1-12, Jan, Feb, etc), Day of week (0-6) 0 = Sunday, 1 = Monday etc or Sun, Mon etc). next to those is the script to run. The '>> /dev/null 2>&1' part means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Basically it runs the command without any output to a terminal.

If you want to run the script for some specific times only, lets say you want to run it on 4 pm and 8 pm, the timers looks like the following

0 16,20 * * *

if you want to run the script only in week days, the timers will be look like the following.

0 16,20 * * 1,2,3,4,5

If you run more than one script, better give them as seperate jobs.

Monday, September 20, 2010

building SOAP object in perl to call a web service

Follows an example perl script snippet structure to build SOAP object to call a web service

  1. Use the required library

    use SOAP::Lite +trace => 'debug';
  2. Define the SOAP object

    my $soap = SOAP::Lite
                       -> uri('http://localhost:1325/WSImpl/')
                       -> on_action( sub { return '""' } )
                       -> proxy('http://localhost:1325/WSImpl/MaterialService.asmx')
                       -> on_fault(sub { my($soap, $res) = @_;
                 die ref $res ? $res->faultdetail : $soap->transport->status, "\n";
  3. Define the SOAP method

    my $method = SOAP::Data->name('UpdateMaterialRequest')
                ->attr({xmlns => ''});
  4. Build the SOAP query data

    my $query = SOAP::Data->value(
        SOAP::Data->name(ServiceTags =>
          SOAP::Data->name(UserName => ''),
          SOAP::Data->name(Pwd => ''))),
          SOAP::Data->name(Details =>
             SOAP::Data->name(DetailsName => Name )
  5. Make the Call

    $result = $soap->call($method => $query);
  6. Read the resultant data

    my $statusmes = $result->valueof('//GetResponse/Status');

Friday, June 18, 2010

Perl: Read Text Files and Filter Data

Perl is the language for text manipulation. Its designed in a way, to handle the data in whatever the format, and process out the data into the desired format. Lets see some text processing examples from an external text file.

Step One:
Open a text file for reading,
 open (FILE, 'data.txt');
 while () {
   print $_;
 close (FILE);

Monday, June 7, 2010

Round the float

There are 3 types of rounding a float to integer is applicable in any language. we programmers will come across such situation in every project. the three types are,

  1. round to next bigger integer, with not considering the decimal value.
  2. round to the next smaller integer, with not considering the decimal value.
  3. round the float according to the value of decimal value, the real round of float number.
The rounding methods depend on which scenario we need the round function.

Follows the examples of php rounding methods.
for the case 1, rounding to the next big integer, we use ceil() function
Example : 
$rounded_value = ceil($floor_value);
if rounded the following values, the results are like shown
2 = ceil(1.3)
2 = ceil(1.5)
2 = ceil(1.7)

for the case 2, round to the next small integer, we could use floor()
$rounded_value = floor($floor_value);
if rounded the following values, the results are like shown
1 = floor(1.3)
1 = floor(1.5)
1 = floor(1.7)

for the case 3, round as per the decimal value, we could use round()
$rounded_value = round($floor_value);
if rounded the following values, the results are like shown
1 = round(1.3)
2 = round(1.5)
2 = round(1.7)

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems   

Tuesday, May 18, 2010

posting code snippets to blogger

When you try to post any code snippet, which related to web applications, If you directly include them into the post with blockquotes, you could never say whether its going to display as it is. because the browser transforms the html code as per the tags. So, its important to post the code with the correct formats. There is a cool site, which converts code snippets to cope with posting on a blog page.

The interface is like the following.

you have to paste your code into 'Enter Markup' text box, and click process, then the site will return the HTML safe code in the second text box, also you can see the preview of the code.

This is the site name :

Blogger: Beyond the Basics: Customize and promote your blog with original templates, analytics, advertising, and SEO (From Technologies to Solutions)   Google Blogger For Dummies

Then you can cut and paste the code which returned on the second box to the required place with block quotes. So the code will be displayed correctly. There are also some advanced methods and styles to post the code snippet to the blogger also available, but for this method, you might not need to do any further change on code.

Monday, May 17, 2010

lightbox Image viewer for blogger

Its normally very hard with blogger pages when we click on an image. It opens the image on the browser.
When i roam around to find any alternative to show the pictures on a separate shadow pop-up, I found some cool script which uses scriptaculous and prototype. Follows the steps.

  1. Login to your blogger Layout
  2. Click on Edit HTML
  3. Find the </head> tag and replace it with the following

<!--Light Box Code Starts-->
#lightbox{    position: absolute;    left: 0; width: 100%; z-index: 100; text-align: center; line-height: 0;}
#lightbox img{ width: auto; height: auto;}
#lightbox a img{ border: none; }
#outerImageContainer{ position: relative; background-color: #fff; width: 250px; height: 250px; margin: 0 auto; }
#imageContainer{ padding: 10px; }
#loading{ position: absolute; top: 40%; left: 0%; height: 25%; width: 100%; text-align: center; line-height: 0; }
#hoverNav{ position: absolute; top: 0; left: 0; height: 100%; width: 100%; z-index: 10; }
#imageContainer>#hoverNav{ left: 0;}
#hoverNav a{ outline: none;}
#prevLink, #nextLink{ width: 49%; height: 100%; background-image: url(data:image/gif;base64,AAAA); /* Trick IE into showing hover */ display: block; }
#prevLink { left: 0; float: left;}
#nextLink { right: 0; float: right;}
#prevLink:hover, #prevLink:visited:hover { background: url( left 15% no-repeat; }
#nextLink:hover, #nextLink:visited:hover { background: url( right 15% no-repeat; }
#imageDataContainer{ font: 10px Verdana, Helvetica, sans-serif; background-color: #fff; margin: 0 auto; line-height: 1.4em; overflow: auto; width: 100%    ; }
#imageData{    padding:0 10px; color: #666; }
#imageData #imageDetails{ width: 70%; float: left; text-align: left; }
#imageData #caption{ font-weight: bold;    }
#imageData #numberDisplay{ display: block; clear: left; padding-bottom: 1.0em;    }        
#imageData #bottomNavClose{ width: 66px; float: right;  padding-bottom: 0.7em; outline: none;}
#overlay{ position: absolute; top: 0; left: 0; z-index: 90; width: 100%; height: 500px; background-color: #000; }
round-color: #000; }
lute; top: 0; left: 0; z-index: 90; width: 100%; height: 500px; background-color: #000; }
round-color: #000; }
<script src='' type='text/javascript'/>
<script src='' type='text/javascript'/>
<script src='' type='text/javascript'/>
<!--Light Box Code Ends-->

Now you have enabled javascript based lightbox.

Google Blogger For Dummies    Creating Unique Websites With Blogger    ProBlogger: Secrets for Blogging Your Way to a Six-Figure Income

To enable this to the images you upload, do the following
  1. Insert Image
  2. Switch the editor to 'Edit HTML'
There is an anchor tag for each of the picture you uploaded.
Insert the following code if you want to open the image as separate one ( there is no next, prev)
rel = "lightbox" title = "Caption of your Image"
Insert the following code, if your image is part of an album (set of images)
 rel = "lightbox[album_name]" title = "Caption of your Image"
Sometimes, the image is too bigger for showing on the window. To handle that, Check the 'href' value of the anchor. There you can find something like '/s1600' (if you found '/s1600-h', remove '-h'). s1600 means the x-large or original size of the image. so you can replace it with '/s640', which is suitable within the monitor.

  • Single Image

  • Album Images