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:
|
|
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:
|
|
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:
- Stop and remove your existing containers along with their volumes by running:
|
|
- Restore the WordPress and MariaDB volumes from your backup archive. Assuming you have a backup file named
backup.tar.gz
in the./backups
directory:
|
|
- Bring up your Docker Compose stack again:
|
|
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.