How to Backup and Restore WordPress With Docker Compose

In this guide, we will walk you through the process of creating scripts to backup and restore a WordPress website running in Docker Compose. These scripts will help you safeguard your WordPress data and quickly restore it if needed.

Backup WordPress Volumes

Step 1: Create a Backup Script

First, you need to create a backup script. This script will use the futurice/docker-volume-backup Docker image to back up the volumes associated with your WordPress and MariaDB containers.

Add the following code to your docker-compose.yml file under the services section:

1
2
3
4
5
6
7
8
9
backup:
  image: futurice/docker-volume-backup
  environment:
    BACKUP_CRON_EXPRESSION: "#"  # Set your desired backup schedule
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock:ro
    - wordpress:/backup/wordpress:ro
    - mariadb:/backup/mariadb:ro
    - ./backups:/archive

Replace # in BACKUP_CRON_EXPRESSION with your desired backup schedule in cron format (e.g., "0 2 * * *" for daily backups at 2:00 AM).

Step 2: Run the Backup Script

After adding the backup service to your docker-compose.yml file, save the file and run the following command in your terminal:

1
docker-compose exec backup ./backup.sh

This command will execute the backup script and create backup archives in the ./backups directory with timestamped filenames.

Restore WordPress Volumes

Step 1: Restore the Volumes

To restore your WordPress volumes from a backup, follow these steps:

  1. Stop and remove your existing containers along with their volumes by running:
1
docker-compose down --volumes
  1. Restore the WordPress and MariaDB volumes from your backup archive. Assuming you have a backup file named backup.tar.gz in the ./backups directory:
1
2
docker-compose run --no-deps -v ./backups:/backups web bash -c "cd /var/www/html && tar --strip 2 -zvxf /backups/backup.tar.gz backup/wordpress"
docker-compose run --no-deps -v ./backups:/backups db bash -c "cd /var/lib/mysql && tar --strip 2 -zvxf /backups/backup.tar.gz backup/mariadb"
  1. Bring up your Docker Compose stack again:
1
docker-compose up -d

Your WordPress site should now be restored from the backup.

Conclusion

By following these steps and using the provided backup and restore scripts, you can easily backup and restore your WordPress website running in Docker Compose. This ensures that your website data is safe and can be quickly recovered in case of any issues.

0%