Git Product home page Git Product logo

wordpress-backup's Introduction

wordpress-backup

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

Dockerhub badge

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 <backup-container-name> \
  --volumes-from=<your-wordpress-container> \
  --link=<your-mysql-container>:mysql \
  -d aveltens/wordpress-backup

You have to replace the placeholders:

  • <backup-container-name>: A name of your choice to identify the backup container
  • <your-wordpress-container>: The name of the WordPress container
  • <your-mysql-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 <backup-container-name> \
  -v </host/path/to/backups>:/backups \
  --volumes-from=<your-wordpress-container> \
  --link=<your-mysql-container>:mysql \
  -d aveltens/wordpress-backup
  • </host/path/to/backups>: 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-container-name> backup

<backup-container-name>: 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 <backup-container-name> restore <date>
  • <date>: 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 <backup-container-name> \
  --volumes-from=<your-wordpress-container> \
  --link=<your-mysql-container>: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="</path/to/wordpress>" \
  --file="</path/to/your/backups>/backup_0.tar.gz" "./"

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

mysqldump --add-drop-table \
  -u<wordpress-user> -p<wordpress-password> <wordpress-db> \
  bzip2 -c > </path/to/your/backups>/backup_0.sql.bz2`

You have to replace the placeholders in both commands:

  • </path/to/wordpress>: The root directory of your WordPress installation.
  • </path/to/your/backups>: The folder where you want to store the backup files.
  • <wordpress-db>: The name of the WordPress database.
  • <wordpress-user>: The database user that WordPress uses.
  • <wordpress-password>: The password of the WordPress database user.

Step 2:

Create a MySQL container:

docker run \
  --name wordpress-db \
  -e MYSQL_ROOT_PASSWORD=<root-password> \
  -e MYSQL_USER=wordpress \
  -e MYSQL_PASSWORD=<user-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=<user-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 <path/to/your/backups>:/backups \
--volumes-from=wordpress \
--link=wordpress-db:mysql \
-d aveltens/wordpress-backup

Replace <path/to/your/backups> 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 for a Docker Compose setup.

Source Code

The source code of wordpress-backup can be found at GitHub

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.

wordpress-backup's People

Contributors

8ear avatar angelo-v avatar bwnyasse avatar dependabot-preview[bot] avatar dybber avatar iddo avatar j3k0 avatar markahon avatar mathsen avatar ppetru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wordpress-backup's Issues

backup script is stopping before the end

Hello,
I have a problem using your wordpress backup.
When running the backup script it's stopping before the end, the db archive is not created :

root@myhost# docker exec -it backup-container bash
root@f586c32ad7e9:/# bash backup

The log is ending on :

...
-rwxrwxr-x www-data/www-data      238 2019-05-21 13:26 ./wp-admin/ms-options.php
-rwxrwxr-x www-data/www-data     6320 2019-05-21 13:26 ./wp-admin/plugin-install.php
-rwxrwxr-x www-data/www-data      369 2019-05-21 13:26 ./wp-blog-header.php

When running throw the Makefile that is on my host the script runs well till the end :
(extract of my Makefile)

backup:
	docker exec backup-container backup

Any idea ?
Thanks for your work !

Support password file

At the moment the mysql password is read from an env variable MYSQL_ENV_MYSQL_PASSWORD

I'm not using the MYSQL_ENV_MYSQL_PASSWORD or MYSQL_ENV_MYSQL_ROOT_PASSWORD env vars for setting passwords, instead I am using the alternative environment variables that end _FILE and poiint to a locally mounted secret file containing the password instead for better security.

This is described here (excerpt below): https://hub.docker.com/_/mysql/

Docker Secrets
As an alternative to passing sensitive information via environment variables, _FILE may be appended to the previously listed environment variables, causing the initialization script to load the values for those variables from files present in the container. In particular, this can be used to load passwords from Docker secrets stored in /run/secrets/<secret_name> files. For example:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag
Currently, this is only supported for MYSQL_ROOT_PASSWORD, MYSQL_ROOT_HOST, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD.

Would it be possible to add support for checking for these _FILE environment variables and if present, obtaining the the password from the file they point to instead - as this will then allow me to continue to use secrets, which I'd quite like to continue doing.

Use wp-cli to configure?

How about using wp-cli to configure the wordpress??? The user could implement his own "wp-cli.yml" to uninstall, define hostname and etc.

Especially in case of import external sites for dev using docker.

How about it?

Restoring order

I used the docker image to my wordpress stack.
When I restore I see:

โžœ  wordtest docker exec wordtest_backup_1 restore 20181004                      
deleting files from /var/www/html/                                              
restoring files from /backups/backup_20181004.tar.gz to /var/www/html           
restoring data from mysql dump file /backups/backup_20181004.sql.bz2            
mysql: [Warning] Using a password on the command line interface can be insecure.
Finished: SUCCESS                                                               

As far as I know, that's the wrong approach for backup/restore and the whole disaster recovery.

  • First you need to restore, make sure there is no error.
  • Then delete the current data.
  • Then move the restored data to the working directory.
    Not a big change and still simple.

Of course there is also other approach, but that's too mach for simple backup/restore:

  • Restore the files to new dir (with data suffix for example)
  • Rename original directory with .bak suffix
  • Make the workdir a symlink to restored dir

Latest ubuntu build don't ship cron

You need to:
RUN apt-get update &&
apt-get install mysql-client cron -y &&
apt-get clean &&
rm -rf /var/lib/apt/lists/* &&
mkdir /backups

small comment / note regarding backups into a docker container.

note that the official Docker wordpress and Mysql containers separate out their data directory (/var/www/html and /var/lib/mysql, I think). via the VOLUME command. Docker inspect the image to see where docker is saving your files.

So when you do a backup into a container, you're really backing up into the VOLUMEs that were created on your host, and not the actual containers.

ssl issues

I am having problems using this in conjuction with jwilder/nginx-proxy ..

What I am attempting to do is a site restore from a bitnami wordpress install.

First I have tested that this works just fine out of the box with SSL using vanilla wordpress from Docker.

The issue arises when I attempt to restore my Wordpress from a Bitnami stack wordpress.

The install appears fine, until I enable SSL on Nginx-proxy. Then, it goes into a redirect loop.

If I re-copy the vanilla wp-config.php into the migrated image, I get redirected to https://127.0.0.1/wordpress/wp-admin/.

I have tried tinkering with wp-config.php with no real luck - although I am unsure how to enable the PHP logs on the docker vanilla image.

alternative cleanup solution

On my server I have only limited space and thus I'd like to have an alternative cleanup solution to the 'older than x days' solution. The alternative should be tiered and keep backups based on different periods. Before creating a pull request I'd like to have some feedback on my suggested implementation. Maybe there is a better one from point of usability. What I had in mind:

A new cli argument like '-CLEANUP_SCHEDULE=1.1.1.4.4.30.30'

The resulting backups plan would look like:

x = latest backup
- = deleted
Day 01 x
Day 02 x 1+4+30
Day 03 x      1 1+4+30
Day 04 x      1      1 1+4+30
Day 05 x      1      1      1   4+30
Day 06 x    1+4      1      1      -   4+30
Day 07 x      1    1+4      1      -      -   4+30
Day 08 x      1      1    1+4      -      -      -   4+30
Day 09 x      1      1      1      4      -      -      -   4+30
Day 10 x    1+4      1      1      -      4      -      -      -   4+30
Day 11 x      1    1+4      1      -      -      4      -      -      -   4+30
Day 12 x      1      1    1+4      -      -      -      4      -      -      -   4+30
Day 13 x      1      1      1      4      -      -      -      4      -      -      -    30
Day 14 x    1+4      1      1      -      4      -      -      -      -      -      -      -    30

The code would look like following:

# read periods from cli argument
var periods = [{distance: 1, count: 1}, {distance: 4, count: 2}, {distance: 30, count: 2}]
# read backup dates from file system
var backups = [19990101, 19990102, 19990103, 19990104, ...]

# cache which backups to keep and which to remove
for each backup:
  flag_for_deletion(backup)

# find out which backups to keep
for each period:
  for i from 1 to period.count:
    # find the best suited backup, i.e. the oldest backup that is not older than i * period.distance
    var best = backups.filter(backup is not older than i * period.distance).find(is oldest)
    unflag_for_deletion(best)

# remove unneeded backups
for each backup:
  if flagged_for_deletion(backup)
    delete backup

The tiered backup schedule is borrowed from https://hub.docker.com/r/prodrigestivill/postgres-backup-local. The latter uses the cli options BACKUP_KEEP_DAYS, BACKUP_KEEP_WEEKS, BACKUP_KEEP_MONTHS. But that control creates some duplicate backups which is not very convenient for my very limited storage situation.

File in use when trying to restore from backup

Whenever I try to restore from backup I get this error

deleting files from /var/www/html/
rm: cannot remove '/var/www/html/wp-content': Device or resource busy```

I have tried stopping the Wordpress container and have gotten nothing. Any ideas why this would still be in use?

Step 2 in Migrate blog to Docker Container

Hi everyone.
I followed the tutorial and it worked. The problem is, I'm running and testing on Centos7 which is installed on Amazon EC2. After reboot instance and log in, i was not be able to restore back up.
It said;
Error response from daemon: Container wordpress-backup is not running
How should i do? If i run the wordpress-backup images, what would be the next step???

Cleanup solution

Hello,

This docker image is nice, a missing feature is that daily backups are never deleted: so this will just grow forever.

Did you happen to implement a solution to this, like a separate script to do some cleanup?

Alternatively, maybe an even cooler solution to this would be to implement incremental backup with tar. https://www.gnu.org/software/tar/manual/html_section/tar_40.html Just saying!

Thanks again for the great image ๐Ÿ‘

Container name reference in swarm mode

Hi there,

Thx for your work :)

I try to make it works in a docker-stack.yml, but my containers names in my stack are partialy randomly created :

winter_wp.1.pexv7h9ifzr582og0jmdjq8ns

The solution, to me, is to find the way to find the variable part of the container name.

Can you help me to use your image in a swarm mode ?

thx :)

environment variables not set

Hi,
thanks for your handy container!
I encountered one issue.

The environment variables needed e.g. for restoring do not get set in my setup (pretty much like the readme, but mariadb instead of mysql)

If I manually set (in my docker-compose.yml)

MYSQL_ENV_MYSQL_USER: <WP-USER>
MYSQL_ENV_MYSQL_PASSWORD: <USER-PW>
MYSQL_ENV_MYSQL_DATABASE: <WP-DB>
MYSQL_PORT_3306_TCP_PORT: 3306

everything works fine.

I could not find where these environments should be generated from, so I can only guess this is some docker magic that I'm no yet aware of.

Any hint?

[Question] Sync backup on an Object Storage DB

Hi,

First of all, thank you for your project.
I created a simple project where I customize a .env file and with docker-compose I create a WordPress website running on docker using MySQL, WordPress, Nginx-proxy (as a reverse proxy to access my websites on 80/443 port).
https://github.com/sasadangelo/webagency

Now I want to add a backup container. However, my understanding is that the backup is saved on a local volume I attach to the container. I am not an expert, but it would be helpful to have a system that saves these backups on S3 Object Storage, Dropbox, or other 3rdparty services.

I noticed there are several projects on the web, however, most of them simply sync the backup volumes with S3, for example. It would be better to have a container that keeps max N file in local (for example 5) and the rest on Object Storage. Moreover, it should have a sort of retention policy to say: keep only the latest N backup on S3.

Do you know if there is a project for that combined with your project meet these requirements?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.