Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/angelo-v/wordpress-backup

Simple Docker container that helps you backup and restore your WordPress blog.
https://github.com/angelo-v/wordpress-backup

Last synced: 6 days ago
JSON representation

Simple Docker container that helps you backup and restore your WordPress blog.

Awesome Lists containing this project

README

        

wordpress-backup
=========

wordpress-backup is a simple [Docker][1] container that helps you backup and restore your WordPress blog.

[![Dockerhub badge](http://dockeri.co/image/aveltens/wordpress-backup)](https://hub.docker.com/r/aveltens/wordpress-backup)

[1]: https://www.docker.com/

## Quick start

Precondition: Given you have a WordPress blog and the corresponding MySQL database running in Docker containers. If not, see section "Migrate your blog to Docker", to see how to move your existing blog into a Docker container within minutes.

*Step 1*: Create and run a backup container linked to your WordPress and MySQL containers

docker run \
--name backup-my-blog \
--volumes-from=your-wordpress-container \
--link=your-mysql-container:mysql \
-d aveltens/wordpress-backup

Replace the following values according to your system:

* `your-wordpress-container`: The name of the Docker container hosting your blog
* `your-mysql-container`: The name of the Docker container hosting your blogs MySQL database

*Step 2*: Backup your blog

docker exec backup-my-blog backup

Yep. That's all you need to create a complete backup of your blog HTML pages and database content. The backup is stored in the container, so you won't see any file on your host system for now, but we will come to this later.

*Step 3*: Restore the backup from a specific day

docker exec backup-my-blog restore 20141114

Replace 20141114 by the date, you actually made a backup.

All backups are timestamped with the date of the backup. So your blog can move back to any day in history on that you created a backup. The format of the timestamp is `yyyyMMdd` (4 digit year, 2 digit month, 2 digit day). But I am sure you noticed that already.

## Create and run the backup container

The Docker image is available on the public Docker hub under the name aveltens/wordpress-backup.

wordpress-backup is a separte container, performing backup and restore operations. The WordPress and MySQL containers of your blog are linked to wordpress-backup, but they are not modified in any way.

To run a backup container, you use the `docker run` command, linking your WordPress and MySQL containers:

docker run \
--name \
--volumes-from= \
--link=:mysql \
-d aveltens/wordpress-backup

You have to replace the placeholders:

* ``: A name of your choice to identify the backup container
* ``: The name of the WordPress container
* ``: The name of your MySQL container

You may also specify a volume to be able to access the backup files on the Docker host:

docker run \
--name \
-v :/backups \
--volumes-from= \
--link=:mysql \
-d aveltens/wordpress-backup

* ``: an absolute path on the system hosting the containers

After creating a backup you find the backup files on that path on your host system.

## Manual backup

To manually create a backup of your WordPress blog use `docker exec` to run the backup command:

docker exec backup

``: The name you chose when you created the container with `docker run`.

> Note that `docker exec` requires at leat Docker 1.3.

This will create two archive files under `/backups` in the container. If you mapped a volume you may see those files in the according directory on your host now. They should be named something like `backup_20141030.sql.bz2` and `backup_20141030.tar.gz`.

The number within the filenames is a date in the format `yyyyMMdd` (4 digit year, 2 digit month, 2 digit day). This means there can only be one backup per day. If you do multiple backups a day the files will be replaced by the latest backup.

> You do not have to backup manually. See section "Automatic backups".

## Restore

To restore a backup of your WordPress blog use `docker exec` to run the restore command:

docker exec restore

* ``: The timestamp of the backup to restore, in the format `yyyyMMdd`.

> Note that `docker exec` requires at leat Docker 1.3.

This will restore the database as well as the HTML content of your WordPress installation.

## Automatic backups

Per default wordpress-backup will automatically create a backup at 03:00 am every day. You can adjust that time by setting a cron expression to the variable BACKUP_TIME when creating the container. E.g. the following statement will create a container that does a backup at 2:00 am every day:

docker run \
--name \
--volumes-from= \
--link=:mysql \
-e "BACKUP_TIME=0 2 * * *" \
-d aveltens/wordpress-backup

## Automatic cleanup

Per default, wordpress-backup will never delete your backup files, so you can do it yourself, if and when you like.

If you want to delete old backups automatically, you can set the environment variable ```CLEANUP_OLDER_THAN``` to a number of days. In that case wordpress-backup will automatically delete backup older than that, before doing the next backup.

For example ```CLEANUP_OLDER_THAN=100``` will delete any backups, that are older than 100 days, as soon as the next (manual or automatic) backup is done.

*Be aware that the cleanup process does use the unix file last modified date as reference, and not the date in the file name. So a backup called backup_20110101.sql.bz2 that was last modified yesterday, will be only 1 day old!*

## Migrate your blog to Docker

If your WordPress blog is not yet running in a Docker container, you can migrate it with a few simple steps.

1. Manually back up your database and files
2. Create WordPress and MySQL containers
3. Restore your backups to those containers with the help of wordpress-backup

*Step 1:* Manually back up your database and files

Use the following command to back up your blog's HTML contents:

tar --create --gzip -vv \
--directory="" \
--file="/backup_0.tar.gz" "./"

...and this command to backup your blog's database:

mysqldump --add-drop-table \
-u -p \
bzip2 -c > /backup_0.sql.bz2`

You have to replace the placeholders in both commands:

* ``: The root directory of your WordPress installation.
* ``: The folder where you want to store the backup files.
* ``: The name of the WordPress database.
* ``: The database user that WordPress uses.
* ``: The password of the WordPress database user.

*Step 2:*

Create a MySQL container:

docker run \
--name wordpress-db \
-e MYSQL_ROOT_PASSWORD= \
-e MYSQL_USER=wordpress \
-e MYSQL_PASSWORD= \
-e MYSQL_DATABASE=wordpress \
-d mysql

Further explanation: https://registry.hub.docker.com/_/mysql/

Create a WordPress container:

docker run \
--name wordpress \
--link wordpress-db:mysql \
-e WORDPRESS_DB_USER=wordpress \
-e WORDPRESS_DB_PASSWORD= \
-e WORDPRESS_DB_NAME=wordpress \
-p 8080:80 \
-d wordpress

Further explanation: https://registry.hub.docker.com/_/wordpress/

You should have a fresh WordPress installation at `http://localhost:8080/` now. Do not touch it. We will restore your backup in the next step.

*Step 3:* Restore your backups to those containers with the help of wordpress-backup

Create a wordpress-backup container:

docker run \
--name wordpress-backup \
-v :/backups \
--volumes-from=wordpress \
--link=wordpress-db:mysql \
-d aveltens/wordpress-backup

> Replace with the actual path the backup files have been stored before.

...and finally restore your backup:

docker exec wordpress-backup restore 0

That's it! `http://localhost:8080/` should show your blog now.

## Docker Compose example

Take a look at [wordpress-backup-quickstart](https://github.com/angelo-v/wordpress-backup-quickstart) for a Docker Compose setup.

## Source Code

The source code of wordpress-backup can be found at [GitHub](https://github.com/angelo-v/wordpress-backup)

## Contribute

If you want to contribute I am happy to merge your pull request!
If you do so, please ensure

* that all automated tests pass (run `./test.sh`)
* the documentation is updated (`README.md`)

## Contact

Please contact me for any questions & feedback: [email protected]

## License

The MIT License (MIT)

Copyright (c) 2015, Angelo Veltens

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.