Open Cloud Blog

Openstack and more

Backup of a WordPress Site into a Docker container

WordPress is a publishing software used by many users. This website (http://www.opencloudblog.com) is using WordPress. If somebody has a running website with useful content, it’s a good practice to backup the data. And it is a even better practice to VERIFY that the data in a backup can be read and reused.

To verify the data, an installation of a LAMP (Linux, Apache, Mysql, PHP) stack is needed. The docker container framework is a good tool to implement this LAMP stack. In order to create a copy of a running WordPress site in a Docker container the following steps are necessary:

  • Create a simple LAMP Docker container, which contains everything. Expose the apache server via port 8080 and ssh via port 22
  • Copy the data from the WordPress site (mysql database and the webserver content)
  • Create a new Docker container, copy the backup data to this container and expose the webserver via http://127.0.0.1:8080 on the local host

Create a LAMP container

The LAMP Docker container is the base for the WordPress installation. The solution shown here is based on the work of https://github.com/tutumcloud/tutum-docker-lamp . The Dockerfile is modified and a ssh daemon is added to the container (I like SSH…). The database is also stored in the LAMP container to reduce the number external dependencies. The Dockerfile for the LAMP stack is:

The other files needed in the docker directory are contained the following tar.gz file: Additional LAMP files

Now it’s time to build the LAMP Docker container using the command

Copy WordPress data

To copy the wordpress data requires two steps. The first step is to copy the directory containing the php wordpress code and the static content (e.g. the media files). The following code (works for Strato WordPress installations)

copies the static content to the local directory ./data/ .

Now the data of the mysql database is required. Your provider might offer a web/php backup tool, but I prefer a scripted solution, which works for Strato WordPress installations. Strato creates backups of the mysql database once per hour. The following script copies the last version of the backup to a local file:

Create the directory to for the WordPress container

Now you must create the directory for the WordPress docker container data. This directory contains the docker manifest and the static data.

Post process the WordPress data

After you have downloaded the WordPress data from your WordPress site, it is necessary to postprocess the data.

  • Create a tar gz file of the static content
  • Change the WordPress „site url“ in the mysql backup to 127.0.0.1:8080
  • Get the wp-config.php file from the static data and change the parameters for the mysql database

The compression of the static data is done by the following script:

Changing the WordPress site URL is done using the following script:

The used solution to do this is very dirty!!! I used this, because I did not find any simple solution to do this using WordPress. After running sed on the sql backup file, the database needs to by fixed, because string lengths might change (keyword php serialization). To do this, I use the python helper script sfix.pl from https://gist.github.com/astockwell/6489489:

Place sfix.py in the wp container directory.

Do not forget to save a copy of the wp-config.php file to the wp container directory and set the following values to:

The WordPress Docker Container

The wordpress docker container is build using the following Dockerfile:

The Dockerfile requires a helper script to import the database:

To build the image, you need to run

Check with the command docker images, that the image has been created:

Run the container

Now it’s time to run your copy of your wordpress site. Create a container using the command:

Connect to http://127.0.0.1:8080 to connect to the webserver and access the local copy of the wordpress site.Now you may run updates of wordpress, plugins, themes,… in the container.  If the versions of PHP, apache, mysql match the version of your original site, you can use the docker copy of your WordPress site as a test system.

You can login to the container using ssh to port 8022 (ssh -l root -p 8022 127.0.0.1)

You can create an image from your started container to create an archive of snapshots, holding e.g. monthly copies of the original content to review changes:

Updated: 16/11/2014 — 19:06
Open Cloud Blog © 2013-2015 Impressum Frontier Theme