Test a site backup locally

Download the site

Download the site and unzip it into public_html

Set up the docker-compose.yml

Download docker-compose.yml and adjust as needed or use the version below:

  • Set WordPress version
  • Specify any auth/key/salt values
  • Set the table prefix

docker-compose.yml

version: '3.4'

x-environment:
  wp_config: &wp-config
    image: "wordpress:6.5.5"
    environment: &wp-config-environment
      WORDPRESS_AUTH_KEY: null
      WORDPRESS_SECURE_AUTH_KEY: null
      WORDPRESS_LOGGED_IN_KEY: null
      WORDPRESS_NONCE_KEY: null
      WORDPRESS_AUTH_SALT: null
      WORDPRESS_SECURE_AUTH_SALT: null
      WORDPRESS_LOGGED_IN_SALT: null
      WORDPRESS_NONCE_SALT: null
      WORDPRESS_TABLE_PREFIX: "wp_"

  site:
    ## server/url info
    host: &site-host "http://localhost"
    port: &site-port 8000
    phpmyadmin_port: &site-phpmyadmin-port 8080

  services:
    db: &db-properties
      environment:
        MYSQL_PASSWORD: &mysql-password "password"
        MYSQL_USER: &mysql-user "wordpress"
        MYSQL_DATABASE: &mysql-database "wordpress"
        MYSQL_ROOT_PASSWORD: *mysql-password
        ## Environment variables: https://github.com/docker-library/wordpress/blob/master/wp-config-docker.php
      volumes:
        ## Choose one volume location for where to store the database
        - db_data:/var/lib/mysql  ## save in docker
    #        - ./db_data:/var/lib/mysql  ## save locally
    wordpress: &wordpress-properties
      volumes:
        - ./public_html:/var/www/html
        ## uncomment volumes if you want to work on the files locally
#        - ./wp-content/plugins:/var/www/html/wp-content/plugins
#        - ./wp-content/themes:/var/www/html/wp-content/themes
#        - ./wp-content/uploads:/var/www/html/wp-content/uploads

x-services-properties: &services-properties
  restart: "no"

services:
  db:
    <<: [*db-properties, *services-properties]
    image: mariadb:latest

  wordpress:
    <<: [*wordpress-properties, *services-properties, *wp-config]
    depends_on:
      - db
    environment:
      <<: *wp-config-environment
      WORDPRESS_DB_USER: *mysql-user
      WORDPRESS_DB_PASSWORD: *mysql-password
      WORDPRESS_DB_NAME: *mysql-database
      WORDPRESS_DB_HOST: &wordpress-db-host "db:3306"
      SITE_HOST: *site-host
      SITE_PORT: *site-port
      SITE_USER: *site-user
      SITE_PASSWORD: *site-password
      SITE_EMAIL: *site-email
      SITE_PLUGINS: *site-plugins
    ports:
      - target: 80
        published: *site-port

  phpmyadmin:
    <<: *services-properties
    image: phpmyadmin:latest
    profiles:
      - phpmyadmin
    depends_on:
      - db
    environment:
      PMA_USER: *mysql-user
      PMA_PASSWORD: *mysql-password
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: "1"
      UPLOAD_LIMIT: 100M
    ports:
      - target: 80
        published: *site-phpmyadmin-port

volumes:
  db_data:

## Start and install WordPress
#  $ docker-compose up -d && docker-compose up install

## fix local file permissions if needed
#  $ sudo chmod --recursive 0775 wp-content

## xDebug
##  Copy the WordPress core files to your local system for local path mapping
#  $ docker cp $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-):/var/www/html wordpress-core

##  Install xDebug, create the ini entries and restart apache/php
#  $ docker exec $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-) bash -c 'pecl install xdebug && echo -e "zend_extension=xdebug\n\n[xdebug]\nxdebug.mode=develop,debug\nxdebug.client_host='host.docker.internal'\nxdebug.discover_client_host=1\nxdebug.client_port=9003\nxdebug.start_with_request=trigger" > /usr/local/etc/php/conf.d/plugincritic-php-ext-xdebug.ini && apache2ctl restart'

Import the database

Change the path to the sql file in the docker container

# install mysql
docker exec $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-) bash -c "apt update && apt install --yes default-mysql-client && mysql --version"

# import the database
docker exec $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-) bash -c "mysql -u\$WORDPRESS_DB_USER -p\$WORDPRESS_DB_PASSWORD -hdb \$WORDPRESS_DB_NAME < /var/www/html/database.sql"

# update the host/domain for WordPress
docker exec $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-) bash -c "mysql -u\$WORDPRESS_DB_USER -p\$WORDPRESS_DB_PASSWORD -hdb \$WORDPRESS_DB_NAME -e \"UPDATE \${WORDPRESS_TABLE_PREFIX}options SET option_value='\$SITE_HOST:\$SITE_PORT' WHERE option_name IN ('siteurl', 'home')\""

Create a backup of the database

This will generate a backup of the database

docker exec $(docker inspect -f '{{.Name}}' $(docker-compose ps -q wordpress) | tr -d '\r' | cut -c2-) bash -c "mysqldump -u\$WORDPRESS_DB_USER -p\$WORDPRESS_DB_PASSWORD -hdb --events --routines --triggers \$WORDPRESS_DB_NAME" | gzip -9 -c > backup.sql