Wrapping Up: A Linux Script For The End Of The Day

Photo of Greg Harvey
Wed, 2010-01-27 11:47By greg

Here's another one of my little Linux admin scripts for all you Drupal developers out there. It's a Linux shell script requiring Drush, MySQL and Subversion, but could be easily modified to work with other databases and repositories and should work fine on a Mac, I think.

You'll need to modify it to your specific paths anyway, as this is set up for our standard working environment. It assumes the database dump name is always the same as the workspace project directory name (in our case this is true), the workspace is in the Linux user's home directory and each project contains a trunk/db directory for database snapshots and a trunk/www directory for the actual application.

Here's what it does:

  • Clears Drupal's cache
  • Deletes the old database dump
  • Dumps and bzip2s the database in to a pre-determined location in our workspace
  • Commits the new database dump
  • Updates the application (to catch any updates others might have done today)
  • Commits any changes to updated files/commits all added files

So when it's run, we have a database dump in synch with the code in version control. Always! I automated this because people (including myself) get lazy and can't be bothered to create the database snapshot with the code commit. This does it for them (and me) so it always happens. Yay!

Here's the code:

if [ $1 = "--help" ];
then
echo "
#########
# USAGE #
#########

Syntax for running this script is:
./end_of_day.sh projectdir databasehost databasename

projectdir - string, project directory in Eclipse workspace
databasehost - string, hostname or IP for database server (usually localhost)
databasename - string, name of database
." &
else
# TODO: we should really use drush to mark certain tables as excluded
echo "Clearing the Drupal cache to make the db dump smaller ..."
cd ~/workspace/$1/trunk/www
drush cache clear
echo "Updating application db directory at ~/workspace/$1/trunk/db ..."
svn up ~/workspace/$1/trunk/db
echo "Deleting old db dump from repository ..."
svn delete ~/workspace/$1/trunk/db/$1.sql.bz2
svn commit ~/workspace/$1/trunk/db/ -m "AUTO: deleting old database dump"
echo "Creating new db dump of $3 from $2 ..."
if [ $2 = 'localhost' ];
then
mysqldump -u root $3 | bzip2 > ~/workspace/$1/trunk/db/$1.sql.bz2
else
mysqldump -u root -h $2 $3 | bzip2 > ~/workspace/$1/trunk/db/$1.sql.bz2
fi
echo "Adding new db dump to repository ..."
svn add ~/workspace/$1/trunk/db/$1.sql.bz2
svn commit ~/workspace/$1/trunk/db/ -m "AUTO: adding new database dump"
echo "Updating application www directory at ~/workspace/$1/trunk/www ..."
svn up ~/workspace/$1/trunk/www
echo "Committing any added code changes to repository ..."
svn commit ~/workspace/$1/trunk/www/ -m "AUTO: committing code changes at end of day"
echo "Done!"
fi

Note the "TODO" task there.