I’ve spent a few hours over the past week restoring my old blog, which can now be reached at brbcoffee.com/archive/. This will be the final chapter in that saga.
My old blog is sort of old. It started out on MySpace, moved to Google’s BlogSpot platform, then moved to Tumblr for a while because I got some new authors on board. Finally we moved to a WordPress solution. From there on BRBcoffee has been on WordPress, but it’s moved around hosts like an alcoholic goes from bar to bar trying to get his fix. I’ve employed a bunch of plugins to do various things for me, from importing and exporting posts to providing language switching capabilities to advanced caching to image scaling to… you get the picture. This is all fine and dandy, but it does pose a few problems when you move around like I do.
- The image scaling edits your wp_posts database table, naturally. When you move, the scaled images don’t necessarily come with, resulting in a lot of broken links.
- The translation plugin just has it’s own tags in that same database table, and keeps two posts in one field. When the plugin isn’t there anymore, or isn’t compatible with your WordPress version, the tags become visible on the site, and you get two languages at once.
- Plugins are often developed by lone wolf devs, and they are never up to date when you check up on them years later.
So, we need to clean up the mess they’ve created, and we don’t install Yet Another Plugin™ to try to fix the problem. We need to dig into our database and fix things ourselves. There’s only one problem: Mysql is made for storing, finding, updating, removing, etc. It isn’t the tool for the job when you need to do complex things like searching for a pattern and replacing only the part of the string that matches that pattern. It can be done, I’m sure, but why would you do it when there are better tools for the job? Here’s how I do it:
- Export the table you need to edit with
mysqldump -u USER -p DATABASE TABLE > dumpfile.sql
- Do work on the dumpfile with specialized tools like sed, awk, or even vim when in a pinch.
- Empty the table before reimporting the modified dump with
mysql -u USER -p DATABASE -e "delete from TABLE;"
- Finally reimport the table with
mysql -u USER -p DATABASE < dumpfile.sql
Mysql also comes with a tool called mysqlimport, but I had a problem with it, so I just used the standard mysql command in the end. So what do those switches mean?
- -p means this user has a password, ask me for it before doing anything
- -u USER means use this username. Pretty obvious.
- -e means execute. Execute the following quoted code.
The only weird thing is that -u takes an argument, but -p doesn’t. It looks like DATABASE should be the argument to -p, but it isn’t. DATABASE is simply the database you want to do work on. In my case, it was called “archive,” while yours might be “blog,” “sales_records,” or even “naughty_movies.” That’s commandline switches for you. In my opinion, a single dash should be reserved for switches, double for arguments, like so:
-p [ask me for a password]
--user <name> [I'm logging in as name, how quaint]
Oh well, you get used to it. That’s how you should do many complex updates on a migrated database. This isn’t for when you want do do one small change on a live system. The 3rd step removes all records from the specified table. Your system will practically be offline until you’ve reimported the changed data.
If you wish this were simpler, you’re not alone. Behavior like this has been a feature request in mysql since 2007. Go there and click affects me, and maybe in another 7 years we’ll have it built in. Till then, use the method described above, and for heaven’s sake, don’t let the plugin rot get your blog too. If you can do it without a plugin, do it without a plugin.
I am a wizard