Archive for November, 2008

Backing Up is the Cool Thing to Do

November 30th, 2008
External Backup Drive

External Backup Drive

Backups saved my life yesterday. No, seriously. I upgraded an installation of Drupal, and forgot to copy the files for my Email client before I wrote over the root directory. After trying to check my mail (and getting an empty folder), I had about 30 seconds of complete terror, before I realized I had implemented an automated daily backup system about two months ago. Whew! I found a recent version in my backup files, and bingo, there were my mail client files. So, I’d like to talk a bit about backing up.

Every single person who uses a computer should create an automated backup system. Period. And let me give you a few guidelines: Burning CDs doesn’t cut it. Burning DVDs doesn’t cut it. Both take way too much time and energy, and you will quickly forget to keep up. You need to buy a spare hard drive, and dedicate it to backups. Hard drives are cheap. You can find some excellent options here.

Next, create a system to AUTOMATE the process. You don’t want to have to remember. If you have OSX 10.5, that means setting up Time Machine. I commend Apple on their push for automatic user backups. If you’re using XP, look here. If you’re using Vista, look here. If you’re using Linux. Look here and here.

The last link above is what I use to backup my server, although I use a slightly modified version. I have it set up to backup all my files every night. I keep 6 days of nightly backups at all times. Then, once a week, I do a weekly backup, of which I keep 4 at all times. Finally, on the first of every month, I do a monthly backup, of which I keep the last 12 at all times. The advantages of keeping many versions of your files are two-fold. First, your hard drive may crash slowly. In this case, it will start corrupting files before it outright dies. If you’re only keeping one copy of files on you backup drive, you could end up writing over your good files with corrupted ones. Keeping multiple, redundant versions of your data ensures there will always be healthy versions available. Second, your backup solution now serves as an archive, where you can recover files you may have lost or accidentally deleted (see first paragraph).

After reading this, you have no reason not to have an automated, dedicated backup solution. Please, I urge you, go set one up. The $100-$200 you will spend on an external hard drive is NOTHING compared to the value of your non-replaceable pictures, music, and files.

Book Review: Apache Cookbook

November 30th, 2008
Apache Cookbook

Apache Cookbook

I just finished reading Apache Cookbook, Ken Coar and Rich Bowen. I’d give it a solid five stars. I found it to be very readable, and not overly verbose or cryptic, a problem that can plague computer books. The book is quite the opposite, if I had one criticism, it would be that it could be a little thicker. It’s only 264 pages, and the font is plenty big, which makes for a fast read.

Having set up and run an apache server for a few months, I skipped over many of the early parts of the book. However, I did find lots of useful tips, tricks and tidbits, that I know will come in handy later. For example, I didn’t know you could make a custom log for any site, with any file, that contains almost any information. You simply specify a CustomLog directive for whatever host you want to monitor. I now set up several specific logs show me only the information I find useful.

Other useful directives are RewriteEngine and RewriteRule. You can do almost anything you want with the URLs from your site, including redirection, query string modification, folder modification, clean URLS and more. This all comes from the module mod_rewrite. There’s useful info on authentication and authorization, dynamic content, custom error pages, email notification, and much more.

Here’s a doozy: you can now use apache’s mod_proxy_balancer to balance a load between a cluster of computers! Who knew it was so easy! You can even specify with the ProxyPass directive whether to distribute by bandwith, or by requests!

A few other tidbits that I’m sure will be useful are the Apache bench utility for benchmarking site performance, and server-status which can monitor your server health and display it somewhere in your website.

All in all, I’d recommend the book, and I see it being a useful reference in the future

Get a Random Row from Database

November 29th, 2008

I had an app the other day that required a single random row to be displayed from the database. It’s an easy task, but I tripped on it for a second. Here was my impulse:

//Get the total number of rows from the MySQL database
$query = "SELECT COUNT(*) as total FROM table";
$result = mysql_query( $query);
$rows = mysql_fetch_assoc( $result );

//Let PHP choose a random row
//pick a number between 1 & the total
$row = rand(1, $rows['total'])

//Now get that row
$query = "SELECT * FROM table LIMIT($row, 1)";
$result = mysql_query( $query);

After typing that out, I realized that I had take the long way round the problem. Very similar to taking the Misty Mountain Path rather than the Mines of Moria. Here’s a better way:

//Get a random row from the database
$query = "SELECT * FROM table ORDER BY RAND() LIMIT 1";
$result = mysql_query( $query );

Much better. Gimley the dwarf would be proud

Restore Firefox Tab

November 29th, 2008

Have you ever been browsing with Firefox and accidentally closed a tab that you didn’t mean to? It’s happened to me more than I’d like to admit. The next time it does, Here’s the keyboard shortcut to recover it:

Control + Shift + T ( or Apple + Shift + T)

Easy huh? You’ll like it. I guarantee it.

Mac OS X vs Gnome (Ubuntu) Spaces

November 29th, 2008

Apple introduced a fabulous feature in OSX 10.5 called spaces. It basically gives you 4 monitors (actually between 2 and 8), so you can have more windows open, and switch between them as necessary. Linux has been doing this for a decade, and I’m happy to finally see OS X pick the feature up. This is preferable to minimizing windows for several reasons. First, you don’t have to waste time trying to figure out which minimized window (which all look the same at 50 pixels) is the one you want. I know Firefox is open is space 2 (or upper-right), and I can quickly move there with the same keystroke every time. Second, you don’t need to leave the keyboard to switch views. I can be coding in Dreamweaver, shift to view some example code in Firefox, and switch back to Dreamweaver and continue typing away, no time lost. Basically spaces = productivity.

I see three problems with the implementation of spaces in 10.5. First, each space has a default application that comes in focus when you switch to that space. If I open up Filemaker in space 1, then open Dreamweaver, Filemaker will be the default application, and will always be in focus when switching between spaces. Not very effective if I’m currently coding in Dreamweaver. Second, opening a new window of an already open application will always send you back to the original space. For example, if now I try to open a Filemaker database in space 3, it will automatically shift me back to space 1. Most annoying. Third, you can set up 4 workspaces as either a row (1×4), or a square (2×2). This is great, except that the keyboard shortcuts don’t recognize the difference. If I’m in the upper-right space in a square setup, and press the “move right” key, it sends me down to the lower left space. For those of us visual people, this is an obvious problem.

Fortunately, OSX 10.5.2 gives you a setting to disable the automatic space-shifting in the control panel. I was happy to see this in the update. Now, if I want to open Firefox in two different spaces, I can do that. Apple needs to work on the other two issues though. If anyone knows ways to change this behavior, please let me know.

Ubuntu (more accurately, the Gnome desktop), Implements this feature with a bit more success. First, it doesn’t suffer from three problems mentioned earlier. I can open windows in any space. It will remember what window I was using, and bring me back when I switch, and pressing “up” and “right” will always bring me to the upper right space, regardless of where I’m working. There’s a couple other touches I like as well. It moves faster; if I press keys in quick succession, it will always catch them, whereas with OSX it will often miss one (such as the upper-right example). Ubuntu also shows your panels moving with the space, which makes more sense visually to me.

Basically, I’m happy to Apple finally use this feature, but it needs a little polishing before I’ll prefer it over Gnome’s more intuitive (and more established) version.

Setting MP3 Bitrate in Rhythmbox

November 28th, 2008

Rhythmbox is the music player that comes on the default Ubuntu installation. Anyone that has ever used iTunes, Windows Media Player or any other similar program will find it very intuitive. You do need to download an extension to add MP3 capability, but this is easy to do.

What frustrated me for months has been the lack of options for ripping music from CDs. Although it has a great selection of formats (WAV, flac, ogg, mp3) it doesn’t give you any options for setting the mp3 bitrate. At last I found a way, and thought I would pass it on:

Open “Preferences” from the “Edit” menu
Select the “Edit” button to the right of “Preferred Format”
Select “CD Quality, MP3” from the menu and hit “Edit”
Under the “Gstreamer pipeline” field you will find the following:

audio/x-raw-int,rate=44100,channels=2 ! lame name=enc mode=0 vbr-quality=6 ! id3v2mux

To remove the default, remove the vbr-quality=6 statement, and replace it with vbr=0 bitrate=256. This will change it from variable to constant bit rate, and set it to 256 kb/s. You can set it to whatever bit rate you prefer, I like 256. You line should now look like the following:

audio/x-raw-int,rate=44100,channels=2 ! lame name=enc mode=0 vbr=0 bitrate=256 ! id3v2mux

Close the window, and Viola! You’ll now rip CDs at a higher quality