Automatic database backup

I use git extensively for version control. Its nice to use git since it backs up the code, but the database still remains unversioned. Well, thankfully, XAMPP has a script that does a database dump. So, I wrote a script to do the dump and then commit it to git.

Step 1

Create a directory in the home directory for the database dump

$ mkdir projectname-database $ cd projectname-database 

Step 2

Initialize git in the directory

$ git init 

Step 3

Do a database dump for the initial commit

$ /opt/lampp/bin/mysqldump -u root database-name > database.sql 

Step 4

Commit the database file

$ git add . $ git commit -am 'Initial database commit' 

Step 5

Now we’ll write a script to do the database dump and add commits. Open your favorite text editor and write the following script. Save the file as probably ‘database-project’ in your home folder

#!/bin/bash /opt/lampp/bin/mysqldump -u root database-name > /home/username/projectname-database/database.sql cd /home/username/projectname-database/ git commit -am 'Database dump at `date`' 

Save the script so that you can run it via a cron.

Step 6

Give the script executable permissions.

chmod a+x /home/username/database-project 

Step 7

Open crontab with the following command

$ crontab -e 

Step 8

Add the following line

00,30 * * * * /home/username/database-project 

This cron will run the script every 30 minutes.

Basically what happens here is the database dump will be taken every 30 minutes and the change will be committed to a git repository allowing you to keep track of the overwriting. I’ve found it quite useful and I hope you will too.

Baking on Ubuntu while using xampp

So, I’m a web developer working on Ubuntu. Since I don’t want to get into complications like pinning to have earlier versions of PHP (useful when developing on Drupal), I use XAMPP. Its a bit of a work to get php ready to bake while using XAMPP on Ubuntu. These instructions are probably generic to any bash shell, but tested and working on Ubuntu. Also note, I assume you install XAMPP in the expected directory at /opt/lampp.

  1. Add an alias for php in .bashrc. Open the ~/.bashrc file and add the following line:

     alias php='/opt/lampp/bin/php' 
  2. When running the bake script, run it as follows:

     php cake.php bake 

jQueryObject

CakePHP moved from Javascript helper to a Js helper and they decided to add support Prototype/Scriptaculous, Mootools/Mootools-more, and jQuery/jQuery UI.  Out of the 3, jQuery remains the one I like most so far. I’m not an expert in it (yet :p) but its pretty nifty to work with. Sometimes when working with one or more libraries, the jQueryObject which is $ by default tends to clash with other libraries. CakePHP of course has code to deal with this, only is not publicly mentioned how to do it. I spent around 5 hours to discover how, and here it goes drum roll

    $js->JqueryEngine->jQueryObject = '$j';     print $html->scriptBlock('var $j = jQuery.noConflict();',         array('inline' => false));

When you look at the code using firebug, you would now see the jQueryObject changed to $j. I’ve suggested a section to the book for this and I’ve also modified some of the documents for stuff that I found helpful.

pageTitle gotcha

When cake moved from 1.2 to 1.3, there were a few changes. While the manual is up-to-date, the default manual that’s shown tends to be 1.2 and there is a section in the 1.2 manual that talks about the migration. I got stuck with one of the gotchas today. I spent 1 hour trying to find out why the title I set in $pageTitle would not print with $title_for_layout until Phally pointed it out on IRC for me. To set the title from the controller, I’m supposed to this

    $this->set ('title*for*layout', 'My title');

Doh! Anyway, now I’ve changed my bookmark to the 1.3 manual instead of 1.2

For a full list of changes, take a look at the transition guide.

Cake is yummy!

Its a few weeks since I formally started my career as a web developer and I feel I’ve got lots to say. I started working on a project writing PHP code that mixed with the view, basically a bunch of php files with lots of PHP code and html. It was easy, but it wasn’t great fun. There was indeed a lot of copy and paste and find and replace, but it was hard work to get things done though it was easier to figure out how to do things.

When I started working on a new project, I convinced my boss that we should try moving to a framework and I chose CakePHP for some reason though there was CakePHP, Codeigniter, and Symfony to chose from. Something about CakePHP pulled me towards it. It took a fair bit of experimentation before I was able to convince myself that I could do it (yeah, this was after I convinced my boss).

After about a week into development, I can happily say that it is indeed awesome and yummy. I like the inbuilt helpers and components that helps do a lot of common tasks like access control, pagination, data validation, and others quite easily. But the best part, is baking code. It takes a lot of the boring part out of coding for a web application especially when your entire application is a CMS like system that is somewhat custom made to client requirements. Being the first project I’m doing in CakePHP, I think it might take around 2 weeks for this project, but I’m pretty sure, I can cut down this time to probably a week or less once I get a feel for it.

I’ve got stuck a fair number of times and I’m really glad that most of my google searches lead me back to the Manual and its mostly what I wanted at that time too. In some cases I got stuck and lost for hours at a time, and that was when IRC came to my rescue. There was always someone online who had a good idea about cake, particularly markstory and savant, always willing to help us newbies. I look forward to more adventures and perhaps more frequent posts.