📍 Overview
This guide explains how to create a complete backup of a WordPress site running in Docker, covering both the database and uploaded media (including featured images).
⚙️ Docker Environment Summary
My Docker setup includes:
MariaDB container as the database service
WordPress container with files stored in the
wp_data
Docker volumeA custom backup script to automate full backups
📦 Backup Script
Below is the script used to perform the full backup:
#!/bin/bash
# === Settings ===
BACKUP_DIR="/mnt/usb/service_backup/wordpress_backup"
DATE=$(date +'%Y-%m-%d_%H-%M')
DB_CONTAINER="wordpress-db-1"
DB_NAME="wordpress"
DB_USER="wordpress"
DB_PASS="wordpress"
DB_BACKUP="$BACKUP_DIR/db_backup_$DATE.sql"
WP_BACKUP="$BACKUP_DIR/wp_data_$DATE.tar.gz"
WP_VOLUME="wp_data"
LOG_FILE="$BACKUP_DIR/backup_log_$DATE.txt"
# === Create backup directory if not exists ===
mkdir -p $BACKUP_DIR
# === Logging ===
exec > >(tee -a "$LOG_FILE") 2>&1
echo "🔄 Starting WordPress backup at $DATE..."
# === Check mount ===
if [ ! -d "$BACKUP_DIR" ]; then
echo "❌ Backup directory $BACKUP_DIR not found. Is the SSD mounted?"
exit 1
fi
# === Dump database ===
echo "📦 Dumping MySQL database..."
docker exec $DB_CONTAINER sh -c "exec mysqldump -u$DB_USER -p$DB_PASS $DB_NAME" > $DB_BACKUP
# === Backup wp_data volume ===
echo "🗃️ Backing up wp_data volume..."
docker run --rm -v $WP_VOLUME:/volume -v $BACKUP_DIR:/backup alpine tar czf /backup/wp_data_$DATE.tar.gz -C /volume .
# === Optional: Cleanup old backups (7+ days) ===
find "$BACKUP_DIR" -type f -mtime +7 -name "*.tar.gz" -delete
find "$BACKUP_DIR" -type f -mtime +7 -name "*.sql" -delete
echo "✅ Backup complete: $DATE"
🔍 What Gets Backed Up?
The script captures:
MySQL Database: All posts, pages, users, and metadata
WordPress Files (
wp_data
volume):Plugins, themes, core files
All media uploads (e.g., featured images, PDFs, video, etc.)
Customizations or configuration files inside
/var/www/html
📂 Generated Files
Located in your backup directory (/mnt/usb/service_backup/wordpress_backup
):
db_backup_YYYY-MM-DD_HH-MM.sql
→ MySQL dumpwp_data_YYYY-MM-DD_HH-MM.tar.gz
→ Full archive of WordPress files (includes media like featured images)backup_log_YYYY-MM-DD_HH-MM.txt
→ Execution log
🚀 How to Run
./wordpressbackup.sh
Ensure it’s executable:
chmod +x wordpressbackup.sh
⏳ Schedule Automatic Backups
To run every day at 2am:
crontab -e
Add:
0 2 * * * /path/to/wordpressbackup.sh
🚧 Optional: Restore Guide
To restore:
Extract the tar archive back into the Docker volume
Import the
.sql
file into the running MySQL container
Let me know if you’d like this included as a section.