May 2012

Some fun unix commands

Cleaning up old Aegir backups

If you use the Backup Scheduler built into Aegir you've already figured out that it's weird to configure and can start to eat up a lot of disk space if you have an agressive backup scheme or a lot of Drupal instances. We had about 40 or so when I noticed that our disk space started to resemble a J curve:

At the max we were using about 400GB of drive space in the backup directory. This amount of disk usage isn't going to be sustainable when there is 400 instances running on this box. So I needed to delete the tar archives of the backups that are older than 10 days.

find /var/aegir/backups -type f -mtime +10 -delete

Find has some awesome option like -mtime where you can tell find to only return files that are 10 days old. You can also use the -delete option instead of using -exec rm -rf {} \;. I feel safer using the -delete instead of -exec. So this command runs in an aegir job every day and we don't fill up our /var/aegir/backup disk.

Running drush cron on lots of sites at once

I use MAMP for local development and I tend to use Drush a lot when building sites. I needed a way to run drupal cron on all of my development instances without having to create a Jenkins job for each. With find you can use the -type option to tell find to only find specific types of files. I'm using d to tell find to only return directories. There are plenty of other options for finding files, symlinks, sockets, etc. I'm also using $() to substitute the output of the find command into the for. You can use the $() or ```` (backticks) to do this.

#!/bin/bash

DRUSH=/usr/local/bin/drush
PROJECTS=$HOME/projects/dev

for d in $(find $PROJECTS -type d -depth 1)
do
  if [ -f $d/sites/default/settings.php ]; then
    $DRUSH -r $d cron
  fi
done

I have a Jenkins job that runs this script periodically.

Create files with simular filenames but different extensions

When you're building modules or themes for Drupal you need start out by creating your info, module, install, or whatever other files you need that all have be simularly named. You can use {} to do this with touch:

zach@brains /tmp » mkdir module_name
zach@brains /tmp » touch module_name/module_name.{info,module,install}
zach@brains /tmp » ls -alh module_name 
total 0
drwxr-xr-x   5 zach  wheel   170B May  3 17:34 .
drwxrwxrwt  15 root  wheel   510B May  3 17:33 ..
-rw-r--r--   1 zach  wheel     0B May  3 17:34 module_name.info
-rw-r--r--   1 zach  wheel     0B May  3 17:34 module_name.install
-rw-r--r--   1 zach  wheel     0B May  3 17:34 module_name.module

This works with a few other commands. It doesn't play well with cp, but that's okay. We have regular expressions for cp.

Installing postgres on OSX Lion with homebrew

1) Update homebrew and install it:

brew update
brew install postgresql

2) Create the postgres database

initdb /usr/local/var/postgres

3) Setup postgres to run on boot

cp /usr/local/Cellar/postgresql/9.1.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

That's it. You can now use createuser and createdb to setup your database.

Say hello to the OpenBlog project!

Yes! I've decided to put together a minimalistic Drupal 7 distro for blogging, OpenBlog! There are some awesome people working on it with me (shrop and thomas) and lots of great ideas going around.

There should be a release later this week after I implement a few more things.

Check out the OpenBlog project page and follow us on the twitter!