Backup your Wordpress installation with rsync
for wordpress by gillesklein
What this article is about
- Daily Incremental backups of your Wordpress blog files
- Daily report by e-mail on the status of the backup
What it does not cover
- This script does not make a backup of your Wordpress MYSQL server. In the near future I will write another script that does this automatically every night as well.
This blog is slowly starting to feel a little like home and after much polishing the number of hours invested in it is increasing. I have uploaded pictures, plug ins and upgraded my installation.
This is always where my paranoia sets making me wonder ‘what if’.. what if is of course any kind of disaster that would wipe out my work. Mad hackers, my hosting provider going belly up, or some country taking exception to my writing or most likely: a hard disk crash at my provider.
So today is backup day. And as every day should be backup day I am going to automate the whole thing.
I have an Ubuntu Linux workstation at home to which I would like to sync a copy of my Wordpress blog and Rsync is the tool I am going to use. It is a great piece of backup software designed to transfer only the changes made, not your whole site. So if you modified only one file, it will only resend that one file. And if you only changed a small thing in that one file, it will only send the actual changes.
For this to work you will need to have the following:
- SSH access to your hosting provider and of course they should run some kind of Unix/Linux
- A Linux computer to backup to. MacOX should work as well, but lacking a Mac I cannot vouch for this.
- rsync , under Ubuntu
apt-get install rsyncshould do the trick. - mailutils, under Ubuntu
apt-get install mailutils, optional but its nice to receive an e-mailed report of your backup. (Probably not required if you have configured sendmail/postfix etc)
Part 1 - Setting up shared keys
I am going to take a little detour here. Because I would like rsync to run at night it is best to make sure that it can login automatically at your hosting provider. The most secure way of doing this is by setting up shared encrypted keys on your computer and you hosting provider.
If you skip this part you can still run the script below — you just have to enter your password every time it runs.
If you have previously created an SSH keyset, you can skip Step #1. If you are not sure check if the following file exists under your home directory (~/.ssh/id_dsa).
Step #1
Open a terminal and enter the following: ssh-keygen -t dsa
Just press enter several times there is no need for a pass phrase.
martin@edinburgh:~$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/nis/martin/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nis/martin/.ssh/id_dsa.
Your public key has been saved in /home/nis/martin/.ssh/id_dsa.pub.
The key fingerprint is:
4e:9f:46:f9:d5:dc:53:40:c8:6a:25:3e:70:27:3e:03 martin@edinburgh
Step #2 - Copy the public key to your web host
Here it will ask you for your username & password.
scp ~/.ssh/id_dsa.pub youraccount@yourdomain:./id_dsa.pub
Step #3 - Login to the host server using SSH
Here it will ask you for your username & password.
ssh -v youraccount@yourdomain
Step #4 - Add the new public key to the keyring
mkdir .ssh
chmod 0700 .ssh
cd .ssh
touch authorized_keys2
chmod 600 authorized_keys2
cat ../id_dsa.pub >> authorized_keys2
rm ../id_dsa.pub
Step #5 - Log out & try out
If everything went smoothly you can now login to your hosting account without having to provide a password. Your computer and your hosting provider can now do this by exchanging cryptographic keys instead.
exit
ssh -v youraccount@yourdomain
Part 2 - Setting up the backup script
In this example I going to make a backup directory under my users home directory on my local Ubuntu machine. Of course this backup directory can reside anywhere else on your computer.
Step #1 - The script
mkdir ~/backup
mkdir ~/backup/wordpress
chmod 0700 ~/backup
chmod 0700 ~/backup/wordpress
Now use your favorite editor to create & modify the following script:
gedit ~/backup/run_wordpress_backup.sh
#!/bin/bash
# Your login details for your hosting account
USERNAME="yourusername"
DOMAIN="yourdomain.com"
# Your e-mail address for reporting errors
SYSOP=youremail@gmail.com
# The directory on your hosting account that contains wordpress, note the ""
REMOTEHOME="~/public_html/html"
# Directory on you local machine that stores the backup
# Note: no "" around the following
BACKUPTO=~/backup/wordpress
MailSuccess()
{
MSG="`hostname` reports wordpress backup complete!"
SUBJ="Backup Complete"
echo $MSG | mail $SYSOP -s"$SUBJ"
}
MailError()
{
MSG="`hostname` reports wordpress backup failed!"
SUBJ="Backup Failed"
echo $MSG | mail $SYSOP -s"$SUBJ"
}
# The actual sync command
rsync -e 'ssh -p 22' -v -r $USERNAME@$DOMAIN:$REMOTEHOME/* $BACKUPTO
if [ $? -gt 0 ]; then
MailError
exit 1
fi
MailSuccess
exit 0
Step #2 - Make the script executable
chmod +x ~/backup/run_wordpress_backup.sh
Step #3 - Test the script
~/backup/run_wordpress_backup.sh
On success the script will send you a brief e-mail.
If things don’t work out here you can add “-v -v” to the rsync command line to obtain more debugging information.
Part 3 - Finishing it up - and running this at night
If your home PC doesn’t sleep at night (and mine is always busy) you can set it up to run the backup automatically every night. For this you need to add a little crontab entry to your Ubuntu installation.
crontab -e
This will open an editor, in which you can add the following line:
01 04 * * * /home/yourhomedirectoy/backup/run_wordpress_backup.sh
Note that I didn’t use the “~/run_wordpress_backup.sh” notation here — its better to be specific when setting up a crontab entry.
Every night at 4:01am your computer will run the backup script and fresh in your Inbox every morning should be an e-mail telling you that things went swimmingly. If its missing your backup scheme just fell apart.
And of course don’t forget to regularly backup the backup directory to a saver medium!
Subscribe to this blog's RSS feed
Keywordlink Plugin

Neuschwanstein Castle by roblisameehan
In one of my many lives I am developing a travel website. While writing the content I quickly got tired of all the manual cross-linking. Each time I mention London in my articles I would like to add a link to the London introduction page.
It is fairly trivial to do this automatically — and it has been on my TODO list for quite a while. So when I came across someone else looking for a similar solution I cleaned up my code and put a little plug-in together that does just this.
You can find the Keyword Link plug-in here.
Have fun !
Subscribe to this blog's RSS feed
Embed images into your Wordpress Plugin
On the Wordpress forum there was some commotion regarding the GoogleSiteMap Generator Plugins use of base64 encoded content. This set off some stealth virus alerts - as a typical trick is to hide malicious code in these unreadable strings.
#region Embedded resources
if(isset($_GET["res"]) && !empty($_GET["res"])) {
$resources = array(
//PayPal
"{8C0BAD8C-77FA-4842-956E-CDEF7635F2C7}"
=>"R0lGODlhEAAQAMQQANbe5sjT3gAzZpGmvOTp7v///629zYSctK28zb"
.vI1ZKnvfH094SbtHaQrHaRrJ+xxf///wAAAAAAAAAAAAAAAAAAAAAAA"
. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAHoAxAALAAAAAA" .
. "QABAAQAVZICSOZDkGQqGuBTqUSMqqiTACwqIKgGkKwCDwVRIodkDVwjYSMFS"
. "NoCNQIgSugZ6vBMBetSYCj0UwCA6lgyy4YoqWASRAZSYNZNaA+VxM7B5bEA9C"
. boGGIyEAOw==" ); }
No virus here - Arne Brachhold, the plug-ins author, uses the base64 function to serve up images directly from the plug-ins source code. This is a neat little trick that I will gladly copy into future projects.
Why not just include a separate GIF/JPG file ? You can of course, however there is no standard way of determining your plug-ins home directory or url. So instead of creating a custom solution that works most of the time this is a nice trick that works all of the time.
The second trick is about how to serve the image on request and neatly catching a web browser just checking if its cache is still up-to-date.
if(array_key_exists($_GET["res"],$resources))
{
$key = $_GET["res"];
$content = base64_decode($resources[$key]);
$lastMod = filemtime(__FILE__);
$client = (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])?
$_SERVER['HTTP_IF_MODIFIED_SINCE']:false);
// Checking if the client is validating his cache and if it is current.
if (isset($client) && (strtotime($client) == $lastMod))
{
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastMod).' GMT', true, 304);
exit;
} else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastMod).' GMT', true, 200);
header('Content-Length: '.strlen($content));
header('Content-Type: image/gif');
echo $content;
exit;
}
}
The above code is outside of any function directly in the plug-in source so it is executed as Wordpress loads each plug-in into memory and before it starts calling any of the filter & action hooks.
http://blogmechanics.net/?res={8C0BAD8C-77FA-4842-956E-CDEF7635F2C7}
It inspects if the “res” parameter is passed to your blog in the url. If so it loads the images and finds the one that matches and serves it up and exits immediately.
The only drawback of catching a URL this early is that the images are always available. Also outside of the administration panel. Just click the link above, it works and will display the Paypal logo.
Subscribe to this blog's RSS feed
Tuning your blog for Google with some SEO magic
When a visitor comes across your website in Google what is the first thing they notice? And what is the second thing they notice?

The first thing to hit your eyes is the title. And in this example my blog is giving a boring, non-descriptive introduction to my site. It is just the title and nothing more. I can do better than this.
The second thing is the site description. Google used a random post from the frontpage to create a snippet and again it doesn’t actually catch your attention.
The title is easy to improve, but how about the description? If you though META tags where so 1995 it turns out that Google is actually looking for meta-tags these days to help it describe the pages it is indexing.
What is a meta-tag? A simple bit of HTML buried in the header of each web page.
<meta name="description" content="Blogmechanics is about practical blogging.
Cool plug-ins, software development and related are discussed." />
If for every post you need to write a different description this quickly starts to sound like a lot of work. This is where the All in One SEO Pack comes in.
Changing the title tag of your blogs front page is easy — just enter AiOSP setup screen and modify it.
But if you like more control you can also modify both the page title tag (normally the title of the post) manually and also its description using the AIOSP panel. You can find this panel at the bottom of the Wordpress Post editor.

If you do this for every new post it will not be too much additional work.
You can modify your older posts but for these AIOSP will auto-generate a small snippet.
Do you need to do this?
Of course not. But as it is, the All-In-One-SEO pack is one of the top 10 most installed plug-ins for Wordpress. So it does seem that everyone else is already doing this.
Subscribe to this blog's RSS feed
Speeding up Wordpress with WP Super Cache
A Really Fast Wheelie by DavidHT
Your moment of fame has arrived and with it the unwashed masses of the Internet flock to your site to gawk at your latest insights. As you have bought the promotion priced hosting with its retirement age web server it dies a horrible dead just seconds into the rush.
No budget priced hosting account will survive such an onslaught but there are many things that you can do to improve your visitors experience. Even when its not busy.
When a visitor looks at your blog Wordpress will go through a long start-up sequence and ramble through the database a couple of times. It needs to locate its plug-ins and allow each of them to add content. Despite all of that it can usually do this in a fraction of a second.
This is great if its a lone visitor looking at your blog but now imagine a hundred visitors doing that in the same second, or a thousand, or thousands. The web server quickly runs out of time and can no longer serve any pages.
The key is of course that 99% of the time Wordpress is going through exactly the same steps. If a user is not logged in your blog will look exactly the same to each visitor.
This is where a cache will help as it will store earlier generated pages and just keeps on resending them. Your web server can now send the same page hundreds of times faster. This is the approach taken by WP-Cache, the most popular way to speed up a blog.
But can we go even faster?
The quickest a web server can serve a page is if the page is loaded from disk and send directly to the visitor. It can take the file and send it on without needing to look at the contents.
For WP-Cache to run the web server still needs to load PHP. This is a massive interpreted language module that needs to initialize itself before giving control to Wordpress.
A fork of the WP-Cache project is WP Super Cache. It wraps around the WP-Cache and extends it. With some Apache configuration magic your webserver is now able to send plain text files directly from disk without ever having to load PHP.
Even better — it is also possible to pre-compress the files. So your visitors receive GZIP compressed web pages , cutting down on your bandwidth usage, while saving your webserver the work of having to compress the information in real-time.
Now you just have to find fame & fortune, you are all set for it.
Subscribe to this blog's RSS feed
Showing Code Snippets on your Blog
Code is Poetry by Ikhlasul Amal
A blog is a great place for displaying text but sometimes you would like to illustrate a little code and cutting and pasting it will just leave a mess. The problem is of course that a lot of the special characters (such as < and >) are removed or misinterpreted leaving the code all but unreadable.
A simple solution to this is the Code Markup plugin, which ensures that your code is shown as it should be. It is not a cure-all, but a great step in the right direction. No longer do you need to type every > or < by hand, simply cut and paste the code and let the plug-in preserve the formatting.
To show a code example, insert it between <pre> and <code> tags :
<pre> <code> Your code example... </pre> </code>
Below is a very simple C snippet, without the plug-in the #include statement would have been mangled.
#include <stdio.h>
main()
{
printf("Hello World");
}
Or an HTML example, normally all the tags would have been invisible, now they are properly escaped and shown in their full glory:
<html>
<head>
<title>The Title of Your Page</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Not bad! I am still looking for a plug-in that allows me to show fully color coded code examples but this is a good start.
And yes, the C code actually compiles. It has only been an embarrassing 5-6 years since I last touched a compiler. So much for all those lost hours of my youth tracing null pointer exceptions. But what could go possibly wrong with an innocent “Hello World” application?
Subscribe to this blog's RSS feed
A human readable sitemap

In an earlier post I touched on the sitemap.xml file, and how it is used by search engines to quickly detect any changes made to your website and to make sure that they do not miss a page when indexing your site.
An older but also tried approach is the human readable sitemap. Not only it is a great way for visitors to find obscure content on your site it also has benefits for search engine optimization (SEO).
Your sitemap page will list all the pages on your site, and because it is linked to from every page on your website (make sure you put in a link!) its distributes page rank neatly over your entire site.
Like the sitemap.xml file, it is entirely possible to automatically generate this file. A great plug-in that does just this is the Dagon Design Sitemap Generator plugin which generates sitemap pages on request.

After installing and configuring the plug-in, create an empty page in your blog and add the tag “<\!-- ddsitemapgen --\>” to it. (Remove the “\” slashes for it to work).
I decided to make my own sitemap a little more visitor friendly so I have added some text to the top of the page.
Notepade2 by harpreetsingh
Subscribe to this blog's RSS feed
Plugin: Google XML Sitemaps
In the good old days, web spiders employed by the search engines has to earn their keep. They had to crawl each page to find links to other pages and so slowly discover the world wide web. Its a good way of finding every page, but it is also very time consuming.
Chewing on their links these spiders thought: wouldn’t it be easier if every website just published a list of all its pages, nicely XML formatted ? And that is what happened, Google published a specification for a sitemap.xml file and asked web publishers to add one to their website to reduce the need to go over each site completely, time and time again.
Yahoo, ASK and MSN quickly adapted and now support XML encoded sitemaps as well.
When using Wordpress, one of the must-have plugins is the Google XML Sitemaps plugin. After installing the plugin will create and update a sitemap.xml for your entire blog after each new post ; and for good measure send a ping to Google/MSN/Ask/Yahoo to let them know you have updated your sitemap.
Installing it is straightforward and after it runs you can forget about it.
Now you have a sitemap.xml file, you can optionally tell Google the good news through the Google Webmasters tools site. If there is anything wrong with it, Google will tell you, but its highly unlikely.

Advanced
If you are writing your own plug ins, and especially if they generate dynamic content, you will like the Google XML Sitemap plugin’s ability to be extended. My Wordpress Theme Gallery plug in creates virtual pages for each theme added. These are invisible to Wordpress, so they do not get added to the sitemap. A few lines of code solved this problem:
function bm_gallery_sitemap()
{
$generatorObject = &GoogleSitemapGenerator::GetInstance();
if($generatorObject!=null)
{
// Get a listing of all the active themes in the database
$active_themes = bm_gallery_db_getallactive();
if ($active_themes)
foreach ($active_themes as $row)
$generatorObject->AddUrl(BM_GALLERY_URL . '?d=' .
$row['theme_id'],time(),"weekly",0.4);
}
}
/* If active, setup the sitemap hook */
if (BM_GALLERY_SITEMAP) add_action("sm_buildmap","bm_gallery_sitemap");
Spider Web by martinkreyness
Subscribe to this blog's RSS feed
Wordpress Theme Gallery Plugin
You can find many good collections of Wordpress themes, but how about building your own collection? You can offer your visitors a nice selection of quality themes to browse. I often come across many nice themes, but other than bookmarking them, I have no real way of sharing them. This plugin helps you create your own search able collection.
I have just finished putting together a simple but effective plug in that allows you to add a theme gallery to any Wordpress 2.3+ blog.
After installing it, simply past the URL of a theme you would like to add into the admin panel. The plugin will download the theme, decode and install it by reading the theme’s style.css file, and resizing the screenshot.png file. Then edit the description of the theme in the database before making it available on your website.
For a demonstration have a look at the brand new “Theme” gallery of this blog.
To download the plugin, have a look at the Blog Mechanics Theme Gallery plug-in page.

Subscribe to this blog's RSS feed
Speeding up WordPress / Manage Pages
In one of my projects I am trying to use Wordpress as a CMS. It is working out fine, except for one annoying detail. I have written well over 200 pages of content now (not posts), and in the management interface the “Manage>Pages” page is becoming unbearably slow. In my particular situation it can take well over 35 seconds before it loads.
Time for a little hacking to find out what causes this delay. It turns out that the code for nicely displaying the pages in nested order is not exactly optimized.
The culprit is (mostly) the function page_rows() in /wp-admin/functions/template.php for Wordpress 2.3.2
It loops over all pages for each page to find out the parent-child relationship and this quickly becomes a time waste.
The following quick fix does not solve the inefficiency (the code needs to be rewritten), but it does speed things up quite a bit. By moving the function setup_postdata() out of the search loop a lot of needless initialization is avoided. See the code sample below on how to apply the fix:
/wp-admin/functions/template.php at around line 167:

In my situation, the page loading time goes from 35 seconds down to now 4 seconds which is bearable, but not yet ideal. A better search routine should be able to completely reduce this wait.
Subscribe to this blog's RSS feed
