11.1. WordPress Backup

So far in the tutorial, we covered WordPress topics that assist us to install WordPress, add contents, optimize the performance and page ranking. All that effort we put to build a quality site may go in vein unless we have a good WordPress Backup and Restore strategy. In this chapter, we cover WordPress Backup topics - Backup, Restore, Migrate and Clone.

WordPress Backup

There are a number of plugins for WordPress backup. Out of them many offers essential features with an upgrade to paid version.

BackWPup Free Plugin is one of the best free plugin for WordPress backup. It comes with features to move the backup files to various cloud storages including Dropbox.

However, we use a command line scripts to backup codetab.org which is:

  • Simple to setup and use.

  • Backups the whole setup and contents.

  • Schedules automatic backup on OpenShift.

  • Push the compressed backup set to DropBox.

  • Restore the full site from the backup set.

  • Clone the site from the backup set.

Benefit of learning the backup at command line (CLI) is that it helps to understand structure and layout WordPress installation and this knowledge comes handy when we recover the site and to troubleshoot any other site related issues.

 
 

How data is stored in WordPress

Before we get into the WordPress backup, it is important to understand how WordPress stores the data.

WordPress installation consists of two parts - core WordPress installation and user added data. Core installation contains the PHP and setup files that powers the WordPress. We need not consider them for backup as we can always reinstall them from the source.

From the backup perspective, we concentrate on user added data that consists of themes, plugins, media library and finally actual posts and pages. Strictly speaking, there is no need to backup themes and plugins as we can reinstall them. However, for the sake of convenience, we include them in the backup so that we can restore the site less time.

WordPress stores the user data in two locations.

WordPress Backup - WordPress files and data locations

MySQL database: WordPress stores post and page contents in a MySQL database table. Plugin and theme configurations go into a database table, but WordPress stores their files in a directory. Likewise, it uploads the media files to a directory but stores the media metadata (details about the media) in a database table. WordPress settings too are saved in the database. WordPress saves its own settings in the database.

WordPress wp-content: Plugin, Theme, and Media files are stored in wp-content directory within WordPress installation directory. Majority of plugins store their settings in the database table, but some save in wp-content directory. Media and other uploaded files are saved in folder wp-content/uploads, plugins in wp-content/plugins and themes in wp-content/themes.

For a full backup of WordPress, we need to take backup of MySQL database, plugins, themes and uploads directories.

In this rest of the section, we explain the WordPress CLI backup. If you have an inclination towards CLI, then go for the CLI backup else use BackWPup Free Plugin to backup the site.

CLI

To complete tasks in this chapter, we need to access OpenShift Shell through RHC or PuTTY (for Windows users). OpenShift RHC Client Tools explains RHC and PuTTY setup. Before continuing, setup these tools and download and extract wordpress-tutorial-sample.zip to OpenShift server as explained in the Appendix.The backup folder in the wordpress-tutorial-sample.zip contains the backup scripts.

WordPress Backup Script

To backup WordPress, log in to OpenShift Shell.

 rhc ssh quickstart

In OpenShift Shell, change directory to app-root/data/backup, which holds the backup scripts extracted from wordpress-tutorial-sample.zip.

> cd app-root/data/backup

To create a backup, run the script file local-backup.sh.

> ./local-backup.sh

Script dumps the MySQL database to a SQL file. Then it archives the SQL file and three wp-content directories - plugins, themes and uploads - using tar (tape archive) command. In OpenShift, WordPress wp-content directories - plugins, themes and uploads - reside in app-root/data directory. Finally it compresses the tar file as a gzip file. We can list the backup file with:

> ls -lrt *.gz
WordPress Backup file listing.

Script names the backup gz file using WordPress application name and the backup date.

In the script, to backup MySQL database we use mysqldump command, which dumps the MySQL database as SQL statements.

backup/local-backup.sh

...

DB_NAME=$OPENSHIFT_APP_NAME
MYSQLDUMP_FILE="$OPENSHIFT_APP_NAME-$DATE.sql"

mysqldump -u $OPENSHIFT_MYSQL_DB_USERNAME --password=$OPENSHIFT_MYSQL_DB_PASSWORD     
          -S $OPENSHIFT_MYSQL_DB_SOCKET $DB_NAME > $BACKUP_DIR/$MYSQLDUMP_FILE

...
 
 

OpenShift environment variables are useful to pass Username, Password and Socket details to the mysqldump. For the application named quickstart, OpenShift sets OPENSHIFT_APP_NAME to quickstart and creates a database named quickstart. By using variable OPENSHIFT_APP_NAME, mysqldump dumps the database named quickstart.

In the next section, we expand the backup script to push the backup files to Dropbox.