Changing Drupal database URLs programmatically from the command line

I had a need today to batch-change a lot of database connection strings in Drupal installations. You might need to do this if you have a lot of sites and are switching from webserver-and-database-server-on-the-same-box to Drupal with a separate database server. Here were some handy shell commands that I used.

Find all the settings.php files in this directory tree:

find /var/www/html -name settings.php -print

Show all the current database connection strings (make sure no one's looking over your shoulder!):

find . -name settings.php -exec grep -nH ^\$db_url {} \;

The grep finds lines beginning with $db_url. The -n switch prints out the line number of the match; the -H switch prints the filepath. Sample output:

./foo/site1/sites/default/settings.php:93:$db_url = 'mysql://myuser:secret@localhost/database1';
./foo/site2/sites/default/settings.php:93:$db_url = 'mysql://myotheruser:othersecret@localhost/database2';

Now, change the sites from pointing to localhost to pointing to database.example.com:

find /var/www/html -name settings.php -exec sed -i '80,95s#t@localhost#t@database.example.com#' {} \;

Translation to English: descend recursively into the /var/www/html directory. When you find a file named settings.php, look in lines 80-95 of the file. If you find a string that contains t@localhost, change it to t@database.example.com. Save the file, overwriting the file that is currently there (that's the -i switch to sed). The # characters are delimiters.

Note that I'm cheating here, because I happen to know that all passwords end with the letter t which makes my string matching easier.


Slides from Batch vs. Queue: an API Smackdown

Here are the slides and the two testing modules from my presentation at DrupalCon San Francisco 2010. The modules run on Drupal 7.

Slides from Batch vs Queue: an API Smackdown (5.8 MB PDF)

Package icon batchler.zip2.36 KB
Package icon queuer.zip2.97 KB
PDF icon Batch_and_Queue.pdf587.13 KB

Performance presentation, Des Moines Web Geeks

Matt Nuzum, the webmaster of ubuntu.com, invited me to speak at the Des Moines Web Geeks meeting last night. The venue, Impromptu Studios, was a bit hard to find. There were no signs for Impromptu Studios, but after lurking in dark alleyways for a while I eventually discovered what should have been obvious from the beginning: enter the furniture store with the "Closed" sign on the front and go up the stairs past the disused lavatory with a sign on the door saying "Beware of the Leopard."

The group was great and we talked for about an hour and half about bottlenecks and optimization. I even went home with a Django book!

Since this was a diverse group I tried to talk about things that everyone has to deal with (bottlenecks for storage, network, memory and CPU) rather than anything Drupal-specific.

As promised, last night's slides: Performance, Scalability on the Server Side (PDF, 1MB).

Drupal on Cover of Information Week

Look what's on the cover of the November 17, 2008 issue of Information Week! Drupal is mentioned several times throughout their series on open source.



Subscribe to RSS - Drupal