When I develop WordPress sites, I find that I end up repeating many identical tasks for each site. The phrase “repeating identical tasks” should (and does) set off alarms: This should be automated! To that end, I’m trying to learn some more command line tools for doing tasks that I currently use a GUI for.
One such task is migrating my development database to the staging/production server. Basically a mysqldump
from my local database that I can then import on another server. A key thing to watch out for with WordPress, though, is the base URL for your site. If you’re serving a site from localhost
while you develop it, you’ll need to change every occurrence of localhost
in your database to the base URL for the new server.
sed
is a command line tool for doing just that job. It runs a regular expression search on the input and outputs the replacement. Example:
$ echo "localhost" | sed "s/localhost/www.example.com/g" www.example.org
Pipe your mysqldump
through sed
to have an SQL file ready for your new server.
$ mysqldump -uusername -ppassword database_name | sed "s/localhost/www.example.com/g" > database.sql
This way I avoid having to open the SQL file in a text editor, doing a global search and replace, and re-saving.