| Locally mirror your WordPress blog |
|
| Wednesday, 15 July 2009 06:57 |
|
One of the things I like to do in my spare time is break software. Not intentionally, of course. It's just what happens when I start poking and prodding to see what makes something tick. That's not such a problem on my desktop where a complete reinstall can be done in under an hour, but when I break something on this blog, somebody is going to notice. To minimise the havoc caused by this addiction, I've set up a mirror of my blog on my local desktop machine, which I update every couple of days to the latest backup. Using this technique you can easily try out new things such as minimising bandwidth usage, trying out new themes or testing plugins without risking your blog's stability or annoying your readers. This tutorial will explain how it's done, but you'll need to use some elbow grease to tweak the procedure to suit your own setup. If you haven't already installed the LAMP stack, then you can check out this post to walk you through the procedure. It'll also explain how to move the document root to a more accessible location in your home folder. I'm going to continue this tutorial assuming that your document root is a folder called www in your home folder. My hosting provider uses CPanel for account management, so I'm going to have to write this post based on how my system is configured. Yours may be different, so I'll do my best to explain every step so that you can alter the process to suit your needs. CPanel offers a function to backup the entire site, creating a dated tar file. It also contains a second tar file that is a backup of the contents of the web site's home folder. After creating a backup, I FTP this folder to my local machine and place it in ~/workspace/blog/backups/. It is from here that I extract the archive into /tmp where I can work on it. Creating the databaseIf you don't have a wordpress database on your local MySQL server, then you'll need to execute the SQL statement "CREATE DATABASE wordpress", which can be done at the command line by logging into MySQL using the following (replace the db_username and db_password with the actual values). After the MySQL prompt appears, you can execute the SQL statement, press enter, then type "quit;" and enter again to return to the bash prompt. mysql --user=db_username --password=db_password Automating the processIn order to streamline the process, I wrote a bash script called "wp-extract" that automates restoring a backup. It will extract the latest backup file from the backups folder, restore the WordPress database into my local MySQL installation, replace the WordPress folder in Apache's document root with the contents of the backup and finally alter the backup's configuration so it is no longer pointing at the web site's url. The following is the script. #!/usr/bin/env bash You will need to tweak this script to suit your needs. I won't go into detail on what the script is doing because it's full of self explanatory comments, but I would like to explain the additional files that are referenced by the script. postcreate_dbThe first file is called is called "postcreate_db", and contains a block of SQL statements that will clear out the values in the WordPress database that reference the real blog. UPDATE wp_options SET option_value = 'http://localhost/blog.burlock.org' WHERE option_name IN ('home', 'siteurl');
wp-config.phpThe second file is a called "wp-config.php", and is merely a copy of the WordPress file located in the root Wordress folder. I've simply edited this file to suit the blog's new home, by changing the defines for the following values:
colors-fresh.cssThe third file, "colors-fresh.css", is a copy of the wordpress theme CSS file located in wp-admin/css/ that is used on the admin pages. I've made a minor modification to it to make the title bar green as a visual reminder that it's the local mirror that I'm looking at. All that happens is that the one on my local web server is overwritten by the copy I have in the files folder. carrington-blog.cssFinally, the fourth file is "carrington-blog.css", which is simply a copy of the Carrington Theme main CSS file. Like the third file, I've made a minor change to turn part of the window green so there's no mistakes when making changes. You'll need to find the equivalent file for your theme and do the necessary changes yourself. blog comments powered by Disqus |
