Monday, August 25, 2008

Check MySQL connection from shell (bash) and backup database

et voilà. In case you should ever be in a situation to check your mysql connection from a shell script you can easily do so by:

#!/bin/bash
echo -n -e "Please enter the DB user: \t\t"
read DB_USER

echo -n -e "Please enter the DB password: \t\t"
read DB_PWD

DB_con_ok=$(mysql -u $DB_USER --password=$DB_PWD -e "show databases;"|grep "mysql")
if [[ $DB_con_ok != "mysql" ]]
then
echo
echo "The DB connection could not be established. Check you username and password and try again."
echo
exit;
fi
#whatever code is executed here, only is done so, IF the mysql credentials are valid


And above, if you need to back up your DB (e.g. in a cron), you can easily do so by

#!/bin/bash
#DEFINITIONS FIRST
DB_USER="mysqlUser"
DB_PWD="mysqlPwd"

VERSION=$(date +"%d-%m-%Y") #store the day, month and year the backup was performed

#CHANGE TO BACKUP DIRECTORY
cd /backup
#PERFORM THE MYSQL BACKUP COMMAND
mysqldump --routines --all-databases -u $DB_USER --password=$DB_PWD > db_$VERSION.mysql

The --routines command makes sure, you're backing up stored procedures as well.

OR if you only want to backup specific databases, just use

#!/bin/bash
#DEFINITIONS FIRST
DB_USER="mysqlUser"
DB_PWD="mysqlPwd"
DATABASES="database1 database2 database3" #define the databases that you want to backup (delimited by the ' ' whitespace character)
VERSION=$(date +"%d-%m-%Y") #store the day, month and year the backup was performed

#CHANGE TO BACKUP DIRECTORY
cd /backup
#PERFORM THE MYSQL BACKUP COMMAND

mysqldump --routines -u $DB_USER --password=$DB_PWD --databases $DATABASES > db_$VERSION.mysql

check out the MySQL manual for mysqldump as well. Hope this was useful to some of you :D

Cheers,
Charly

Labels: , ,

Thursday, August 7, 2008

PHP Performance Optimization

In every development cycle there comes the time, when you've got to optimize things. I really love the following article: guys & iris: you should all have a good read of http://c2.com/cgi/wiki?PrematureOptimization about this topic.

It clearly explains and lays out why it is the best to:
  1. Make it work.
  2. Make it right.
  3. Make it fast.
in exactly that order. To be even more precise it says:
  1. Make it work.
  2. Make it right
  3. Make everything work.
  4. Make everything right.
  5. Use the system and find performance bottlenecks.
  6. Use a profiler in those bottlenecks to determine what needs to be optimized.
  7. Make it fast. You maintained unit tests, right? Then you can refactor the code mercilessly in order to improve the performance.
So, I think we've hit that 2nd/4th point now. As you know from one of my last blogs, I massively like my MacBook. So of course, I'm not prepared to switch away for a simple performance analysis tool.

As you might be well aware, a typical combination to identify PHP bottlenecks is XDebug to log execution times and KCacheGrind to make them human-readable. Unfortunately, the installation process of KCacheGrind of Mac OS X is not that straight forward, although it's pretty straightforward :).

I've taken the following small tutorial from http://brent.izolo.com/blog/?p=4 and amended it where necessary.

  1. Install Fink. An excellent article on how to set up Fink on your Mac can be found in the O'Reilly macdevcenter. Check it out. Otherwise, you can just download the installable binaries for your version of Mac OS from the Fink homepage.
  2. install the X11 development package as hinted by this incredibly useful little tutorial:
    Installing X11 on OS X 10.4 Tiger. It's 5 years old, but still up to date.
  3. edit /sw/etc/fink.conf and add:
    unstable/main unstable/crypto
    to the Trees key. The line should now read like this:
    Trees: local/main stable/main stable/crypto unstable/main unstable/crypto
  4. Open the command-line and enter:
    fink selfupdate; fink index; fink scanpackages
  5. After the update and reindexing finished enter:
    sudo fink install kcachegrind
    in the command line
  6. Answer all the questions by the script and prepare for a looooooooooong configuration and installation time (many, many package dependencies)
To run KCacheGrind:
  1. start X11 (e.g. by using the Spotlight and searching for "X11")
  2. open the console and type:
    /sw/bin/kcachegrind
That's it (hopefully!). Enjoy.

Bests,
Charly

Labels: , , ,

Monday, July 7, 2008

PHP exec output

We encountered a random problem while trying to execute a program via the PHP exec command and trying to capture the output of the executed program; we always retrieved an empty String.

This is due to not using the default I/O buffer. You have to tell the shell which output buffer to use. This can be simply done by appending "2>&1" to the command.

exec($command, $output); ->
exec($command." 2>&1", $output);

et voilà, the output array variable should not be an empty string anymore.

Hope this helps.

Bests,
Charly

Thursday, July 3, 2008

Piins Contact API - The data is the users'

We just don't agree that platforms have the right to claim possession over user data in terms of completely restricting them from accessing/deleting/editing all their data in the most efficient way. CAVEAT: "most efficient" does definitely not refer to the way some platforms (we don't want to point with fingers here) define "efficient", i.e. only via their own platform.

They might do a nice job to convince themselves that they are the best, and market valuation might suggest that they are, but all the data there is still the users'. We'd even go that far to say, that not only the direct data, but also all the inferred one is the users', although you can probably argue about that, so we'll cover that in another posting :)

Anyways, as part of our movement to make all data as accessible to their respective users, being extremely aware of privacy and data protection standards, we've released our little contact-import and message sending API.

If you are interested contact us anytime, we're happy to discuss how you can help users to access all their data as easily as possible (well at least, as our little Piin-brains can do, if we piin down the time :). We want to speak to you first to assure, that you are not going to misuse the API in any malicious or harmful way.

The link for the API is for testing only. WE DO NOT STORE ANY OF YOUR CONTACTS, NEITHER DO WE SPAM THEM. We hate spam ourselves and if we had a little bit more time, we'd be working on a giant fight-spam-forever project! This is why we disabled message sending via the Outlook upload as well. You will still be able to see the imported contacts directly after import from your Outlook CSV file, but not be able to send them messages.

P.S.: If your are interested in an internationalization library for PHP, we've developed one for Smarty in PHP. A tutorial for this library can be found here. We're planning to release many more APIs for the open source community and the startup community, so stay tuned with our blog.

Labels: , , , , ,

Friday, June 27, 2008

Install Sphinx on Mac OSX Tiger (10.4)

Hello everyone (especially developers),

I know, I know - you should NEVER use a development environment that is different from your deployment one, but I just can't help myself. I just love my Mac and don't want to replace my nice little black MacBook by a clumsy other one, just because I've to run a customized version of Gentoo on it :) - neither do I want to mess with BootCamp...

That aside, I've struggled for a little bit getting Sphinx (the incredible indexing and search engine) to work on my machine, a MacBook with OSX Tiger (10.4) on it. Maybe some of you are struggling with the same problem, so I thought I put a short tutorial together that helped me compile and run it successfully.

So here we go:
  • download version 2.5 of XCode from the Apple page. Version 2.5 is for Tiger (10.4).
  • install the X11 development package as hinted by this incredibly useful little tutorial:
    Installing X11 on OS X 10.4 Tiger. It's 5 years old, but still up to date.
  • download and install MacPorts (if you haven't already): http://www.macports.org/
  • open console and enter sudo /opt/local/bin/port install mysql5-devel
  • wait, wait some more, wait, wait, get coffee and wait
  • Download Sphinx and untar it to any directory
  • in the console, change to the directory you just untar'd Sphinx to
  • enter in console: ./configure --with-mysql-libs=/opt/local/lib/mysql5/mysql/ --with-mysql-includes=/opt/local/include/mysql5/mysql/
  • enter in console: ./make
  • enter in console: sudo ./make install
That should have done the trick!

Enjoy.

Bests,
Charly

Labels: , ,

Tuesday, April 29, 2008

Piins - extended Beta

very, very soon (1st of May) Piins is entering an extended Beta version meaning that not only friends and family can see, live and breathe the spirit of Piins.

If you are interested in participating in this extended beta, send us a mail to invitations@piins.com with a funny, exciting or otherwise catchy reason why you should be on it and you'll get an invitation code. If you want, you can also get on our "most famous reasons why I want to be on Piins" list :)

Applications are open until 15th of May 2008.

Hope to see you soon on Piins.

Labels: , ,

Monday, April 7, 2008

Minibar honors Piins

Christian from Minibar allowed Piins do start the ‘web innovators in residence’ blog at the Minibar site. The first blog can be seen here on the Minibar site.

More blogs of Piins to appear there. So stay tuned :)

Labels: , ,