Friday, March 13, 2009

Command Line Upgrade Wordpress Databases

Oh yay, we're going to upgrade WordPress here at work! Now we have a couple hundred WordPress databases at work, so I don't want to have to manually load up a web browser and click the "WordPress Upgrade" button a hundred or so times. In technology, if you have to do the same process over and over, you should always look for a way to automate it, and that is what I've done here.

The PHP script is rather simple. If you look at it, it basically defines which database, db user, db password, db server, etc. and then it runs WordPress' wp_upgrade() function. That simple! Honestly, the hardest part (for me at least) was figuring which WordPress files I needed to include and what order they go in! (In the beginning, I traversed the order of my require statements. Doh!)



\<\?php //this gets hosed by blogger so i'm trying to "escape" it

if ($argc != 2) {
print "Usage: php $argv[0] [wpdb]\n";
exit;
}
$db = $argv[1];
define( 'ABSPATH', dirname(__FILE__) . '/' );
define('WP_ADMIN', true);
define('DB_NAME', $db);
define('DB_USER', 'wpuser'); // Your MySQL username
define('DB_PASSWORD', 'wppass'); // ...and password
define('DB_HOST', 'mysql.server.com'); // 99% chance you won't need to change this value
require('wp-load.php');
require('wp-admin/includes/upgrade.php');
print "Upgrading " . DB_NAME . "...\n";
wp_upgrade();
print "\n\nUpgrade for " . DB_NAME . " complete!\n";
?>


Now as you can see, I set up my PHP script to handle the database name as an argument. So now, all I need to do is write a bash wrapper script! I haven't done it yet, but it'll basically be similar to this:


for db in `cat listofdbs.txt`; do php ajc-wpupgrade.php $db; done;


And Bob's your uncle!

No comments: