Dump A MySQL Database To YAML Using PHP

The Problem

I was recently working on a project that is built on WordPress / BuddyPress. I built up some content using the CMS features of WordPress in my development server and then I transfered the WordPress content to a beta server using the export/import functionality. That kind of stinks having to do that every time I want to push updates to my beta server. So I looked into using Doctrine to generate data fixtures which I could repeatedly run against the different database. Doctrine supports YAML files, which are extremely easy to edit by hand, so it seemed like a good idea.

So what’s the problem? Well, I didn’t currently have any of my data in YAML files. I didn’t want to had edit them or do some sort of copy and paste madness. So, I wrote a script in PHP to dump my data into a YAML file.

Continue Reading

Modify PHP CLI Include Path Dynamically

I was working with an application platform that I had just downloaded today. I just wanted to check it out and play with it a bit. In the application framework was an executable script that provided some command line utility. I fired up the script and received an error like “PHP Fatal error: require_once(): Failed opening required…”. Of course non of the files from the downloaded archived were in the include path for my system, but I didn’t want to just go adding it to my php.ini file just to play around with it short term. What to do.

Command Line Options

First I wanted to see if there was a command line option that I could pass to PHP to tell it what include_path to use for just this one execution. If you execute the following you’ll see the usage:
php --help
There is no include_path command line option, but there is the -d foo[=bar] option which allows us to modify any of the INI entries by key/value.

Continue Reading

Bash Script Init Style Status Message

Have you ever wondered how the OK and/or FAILED messages are printed out in the initialization of services on a Linux system during bootup? Well… I have. Anyway, I’ve always thought that it was really cool how the OK and FAILED messages printed out at the same column on the screen and in different colors. So of course I wanted to know how to do that so I could use it in my own scripts.

The Source

If you look inside some of your systm init scripts you’ll notice that many of them source the /etc/rc.d/init.d/functions script. This is actually where I found the code to produce neato status messages.

The Code

I could have just sourced that file (included) in my scripts and been fine, but I wanted my scripts to be self-contained so I took the liberty of pulling out the applicable code. Here it is below with comments:


So you can just drop the above at the top of your script and then call the appropriate functions where needed. For example you could do something like this:

Pretty simple, eh? Now what type of script might you need something like this in? Check out my previous article How To Create A Bash Install Script, which is a perfect example of the type of script in which you may want these status type messages.

Thanks for stopping by! I hope that you’ll leave any comments that you have below.

How To Create A Bash Install Script

Have you ever wanted to create an installer program on a Linux system, but didn’t want all the hassle of an actual install builder? I have. I recently had need of a simple, no frills installation that could:

  1. ask the user a few questions
  2. extract some binary files and
  3. do some stuff with those binary files.

So I’m going to show you how you can create such an installer with very little hassle. I can’t actually claim credit for this method though; I actually got the idea from Sun’s JDK installer for the Linux platform. You download a “.bin” file, change the file mode so that it is executable and then run it. It displays the end user license agreement, gets some feedback and then goes about installing Java for you. Well, if you open that .bin file up in a text editor (say vi) you’ll see that it’s nothing more than a shell script with a binary chunked onto the end. Thus my plans for world domination were born…

Bash Command Line Pro Tips

As someone that has used the Bash shell almost daily for over 15 years, Jason Cannon has accumulated several command line “tricks” that have saved him time and frustration. This guide is a collection of techniques that you can put to use right away to increase your efficiency at the command line. Start using these 10 tactics today to improve your effectiveness.

Click to download this resource for FREE.

The Script

The first part of this process is to create your install script. At the beginning of my script I put any environment variables and setup that I need for the script to run propertly.

For this exercise I chose a zip file as my binary distribution media. The ZIP_FILENAME variable holds the name of the file after extracting it from the binary installer. SCRIPT_LINES holds the number of lines in this script file. Actually it’s the number of lines plus one (more on this later). SUM1 and SUM2 are the two numbers that I got from running /usr/bin/sum on my zip file. We’ll use these numbers to verify the file after extracting it from the archive.

The next line uses the trap statement to execute some instructions in case the file exits. This just lets the script clean up after itself in the event that the user exits the file prematurely (ie using CTRL-C to close).

Now for the unpacking (or extracting) of our binary distribution media (a zip file in this case). Basically we’re using the tail command to do the job. The -n option indicates that our number argument is the number of lines that we want. Of course you see we’re using the $SCRIPT_LINES variable that we defined at the start of the script. We prepend the plus (+) operator in front of that number which tells the tail command that we want the last lines of the file starting at the number indicated. So we’ll get all of the file starting at the line just past our script, which will be the start of our zip file.

This little bit performs a checksum on the unpacked zip file and checks that against the values we set when building the installer.

The last little bit to this script does not really do that much. We’ve unpacked and verified our zip file so now we just unzip it. To finish up the script removes the zip file (we’ve already extracted it’s contents) and exits. Of course at this stage you could do any number of things that you wanted. You could compile some code if you wanted, prompt the user for feedback in order to build a configuration… whatever you need to do (that can be scripted).

Building The Installer

Ok, so we have our install script ready to go and we have a zip file (or binary distribution media). Now what? Building the final installer is actually a trivial matter from here. All we need to do is concatenate the script file (first) and the zip file into our final installer. We could also change the file mode so that it is executable.

Or if you want to get extremely fancy you can create a makefile! With this example a makefile is like hanging a photo on the wall with a jackhammer, but for more complex projects it may actually be warranted.

With this in a file called Makefile all you have to do is type make and it builds the installer for you.


Thank you so much for stopping by and reading. I sincerely hope that I’ve helped someone out there. To conclude here is a download that includes a) the install script b) the zip file I used and c) the Makefile to build the final installer. Enjoy! (3 KB)

Before you go… please feel to leave comments below. Especially if you have any suggestions as to how this method could be improved. Thanks!