We use WordPress to power our company website. It gives us an excellent platform for managing and publishing our site content; we have been very happy with it, and would recommend it for other people looking to set up a straightforward site.

Managing backups, though, hasn’t been quite so simple. It is true that there are quite a few options available that cover different aspects of the backup:

  • It is easy to download site content in a WordPress export file, and this contains the writing that has gone into the site
  • Using external tools such as MySQLAdmin, exporting the underlying MySQL database is also easy enough
  • Various WordPress plugins are available to export different elements of the site

However, none of the options we could see appeared to quite meet our requirements: we wanted to be certain that every attachment, every tweak and adjustment to site content was kept safe – automatically, without intervention. The goal was to be able to reconstruct our site quickly, in the event of a serious hosting failure, including every aspect of the settings used to configure themes, plugins and so on.

Our solution

In the end, we wrote a small python script to do exactly what we needed, backing up:

1) The tables used by WordPress, as stored in MySQL
2) The WordPress directory containing software, themes, plugins, attachments and all other files as also backed up

The output is a zipped archive containing a complete snapshot, from which the site can be reconstructed or moved to a different platform. Some small changes to the config file may be required when moving, but we are now confident that everything we need is available, should disaster strike.

The script itself, when pointed to the root WordPress directory, parses the config file to find out all the necessary settings, and uses this to invoke mysqldump for the database backup. Because of this, the script dependencies are minimal. Putting it all together, we have a simple wrapper script that runs weekly, connects to the web server to run the script, and then copies the resulting archive across to our office server. We are then emailed a log of the results.

The wrapper script is nothing more than along the following lines, running the backup script and using rsync to copy the files back:

ssh [our_web_server] <<-EOF
~/bin/backup.py [site 1 root dir] -o ~/backup/site-1/
~/bin/backup.py [site 2 root dir] -o ~/backup/site-2/
EOF
rsync -avz --remove-source-files [our_web_server]:~/backup/ ~/backup/web_server/

In a spirit of openness, we have released this utility, which we have called wp-backup, on GitHub. Feel free to use it for your own purposes!