Categories

Backup & Restore WordPress Blog

wordpress-logoGoogle recently released a tool for converting between different blogs. I haven’t tried that tool yet, but I guess you’d have two blogs simultaneously online for converting. So the traditional way of backing up the blog data is still necessary to prevent accidentally lost of data or planned blog migration.

WordPress provided a convenient way to export/import the post data through a single XML file. However, it might be tricky to use if you are not lucky. I nearly lost my blog posts for several months with it. Fortunately, I’ve made another raw SQL dump of the database and managed to restore all but several posts. I’ll share my experience here hoping that similar things won’t happen to others.

The tool for exporting/importing is accessed through the Tools section in the admin page of WordPress 2.7 (I think it is at another place in some earlier version of WP. However, it is still not very hard to find.). By using the export utility, you’ll get an XML file containing all your posts, pages and comments etc. This is preferable to backing up the raw SQL (e.g. through phpMyAdmin) because this is in a portable format. So, while you might have problems to have the dumped SQL imported to and worked properly with a different version of WordPress, it is guaranteed that the XML format is compatible to all versions.

However, if you have many posts, the generated XML file might be big. And generating the XML file might even cause your PHP process to exceed the allowed memory limit. You’ll not get any error message if the memory limit exceeded. On the system of my original blog hosting, a backup file of more than 1.7 MB can not be generated because of memory limit. I backup my blog for several times in this way but when I finally try to restore the data, I found that at least posts of half a year was lost. After checking all my backup files, I found them all the same size. I opened the XML file and found none of them properly ended. It should be ended with

        </item>
    </channel>
</rss>

But in fact the XML file was unexpectedly ended in the middle without the final closing tags. I’m feeling weird. I searched the internet and found that many people were having problem importing the backup file but no problem exporting it.

Fortunately, I finally managed to recover most of my data by setting up a local WordPress (older version) and import the raw SQL dump. Then I tried again to export the XML file. The problem is still there. After checking the log files, I found that PHP exceeded its memory limit. I opened the php.ini (it is at /etc/php5/apache2/php.ini on a Ubuntu box) and change the memory_limit variable to 256M (originally 16M). Restarted apache and tried again — this time I got a proper XML dump (3.5 MB).

Similar problem might happen when you are trying to restore with the XML file. If your XML dump is too large, you might be unable to upload it to the server. Fortunately, you’ll get explicit error this time and the problem can also be fixed by modifying php.ini: just change the variable upload_max_filesize to some proper value that suits your needs.

WordPress can download and restore the attachments with the posts automatically. However, this feature requires both blogs to be online. We don’t need this since we can backup and restore the attachments with an easier way (introduced below).

Besides the database, you’ll also backup the files under the wp-content directory of your WordPress install. If you don’t want to backup the whole directory, you’ll archive at least the following sub-directories:

  • plugins: contains the plugins you’ve installed.
  • themes: contains the themes installed. It is recommended to backup at least the theme you are using if you’ve done some customization to it.
  • uploads: the uploaded files of all your posts go here (by default). So it is very import to backup this directory.

Restoring those files is very simple. Just copy them to your new WordPress install directory.

That’s all. Let’s make a summary. To backup and restore your WordPress blog:

  • It is recommended to use the built-in export/import tool.
  • Double-check the integrity of the exported XML file. Fix your server by changing memory_limit in php.ini if necessary.
  • Also backup files under the wp-content directory.
  • Modify upload_max_filesize in php.ini if your exported XML file is too large to be uploaded.

Finally, regularly backup important data is really a good habit. 🙂

4 comments to Backup & Restore WordPress Blog

  • 赞啊,最近正在迁移wordpress
    你的空间是哪家的?

  • @chunhao
    是 DreamHost 的空间。有不少朋友推荐,不过也有看到说不推荐的。不过架 Blog 之类的服务确实是非常方便的。

  • rhythm

    我在家里是电信的ADSL,访问我的blog非常得慢,但是访问你的blog时速度却不错。是因为cache的问题么?我用的是wp-cache。另外我用w3m访问自己的blog的时候速度很好,以至于我在家里都是用w3m来上blog……

  • @rhythm
    这就不知道了,我在 http://lifegoo.pluskid.org 上用了 WP Super Cache ,但是在这里并没有用什么 Cache 插件。用 w3m 上却很快的话,难道是你的 blog 上有什么重量级的 JS 代码?但是我上的时候好像也还好啊。

    ps: 之前用那个默认安装的 Hashcash 插件来防 spam 的,不过好像和我这个主题不兼容什么的,导致所有的评论都失败。现在用这个 Akismet ,好像把你的所有评论都当 spam 了…… -.-bb 反正现在刚刚安家,还是先不考虑 spam 的问题了。