Skip to content

Instantly share code, notes, and snippets.

@maietta
Last active April 16, 2026 15:43
Show Gist options
  • Select an option

  • Save maietta/b190c8cc5899162de199ee191f4cc49e to your computer and use it in GitHub Desktop.

Select an option

Save maietta/b190c8cc5899162de199ee191f4cc49e to your computer and use it in GitHub Desktop.
WordPress & MariaDB Recovery: Restore gzipped tarball contents to Docker volumes and sync database via wp-config.php credentials.

WordPress Docker Volume Restoration Guide

This guide outlines the process of extracting a gzipped backup into a Docker volume and importing the corresponding SQL database.

1. Extract Files to Volume

Restores the wordpress/ folder from backup.tar.gz to the wordpress-12345 volume.

docker run --rm -v $(pwd):/backup -v wordpress-12345:/target busybox \
sh -c "tar -xzf /backup/backup.tar.gz -C /target --strip-components=1 wordpress/"

## 2. Sync Permissions
Ensures the www-data user (UID 33) owns the restored files.

docker run --rm -v wordpress-12345:/target busybox chown -R 33:33 /target

## 3. Import Database
Pipes a SQL dump into the database container using credentials found in wp-config.php.

cat database_dump.sql | docker exec -i db_container_name /usr/bin/mysql -u [USER] -p[PASS] [DB_NAME]
#!/bin/bash
# Configuration - Update these or pass as arguments
BACKUP_FILE="${1:-backup.tar.gz}"
VOLUME_NAME="${2:-wordpress-12345}"
DB_CONTAINER="${3:-db_container_name}"
echo "--- Starting WordPress Restore ---"
# 1. Restore Files
echo "[1/4] Extracting $BACKUP_FILE to $VOLUME_NAME..."
docker run --rm -v "$(pwd):/backup" -v "$VOLUME_NAME:/target" busybox \
sh -c "tar -xzf /backup/$BACKUP_FILE -C /target --strip-components=1 wordpress/"
# 2. Fix Permissions
echo "[2/4] Setting file permissions (www-data)..."
docker run --rm -v "$VOLUME_NAME:/target" busybox chown -R 33:33 /target
# 3. Extract Credentials from the volume's wp-config.php
echo "[3/4] Pulling credentials from wp-config.php..."
CREDS=$(docker run --rm -v "$VOLUME_NAME:/data" busybox sh -c "
DB_NAME=\$(grep 'DB_NAME' /data/wp-config.php | cut -d \"'\" -f 4)
DB_USER=\$(grep 'DB_USER' /data/wp-config.php | cut -d \"'\" -f 4)
DB_PASS=\$(grep 'DB_PASSWORD' /data/wp-config.php | cut -d \"'\" -f 4)
echo \"\$DB_NAME:\$DB_USER:\$DB_PASS\"
")
IFS=':' read -r DB_NAME DB_USER DB_PASS <<< "$CREDS"
echo " Target DB: $DB_NAME"
# 4. Import Database (if export.sql exists in current directory)
if [ -f "export.sql" ]; then
echo "[4/4] Importing database from export.sql..."
cat export.sql | docker exec -i "$DB_CONTAINER" /usr/bin/mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME"
echo "--- Restore Complete! ---"
else
echo "[!] export.sql not found. Skipping DB import."
echo "--- File Restore Complete! ---"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment