Skip to content

Backing up and Restoring Database Snapshots

Guy Elsmore-Paddock edited this page Apr 25, 2019 · 1 revision

Outside of Docker, you may be accustomed to using Drush commands like sql-dump, sql-connect, and sql-cli to back-up and restore database snapshots through the CLI. When you start using Drush in a container, these commands are still available but the way you invoke them changes a bit.

Dumping a Database Snapshot

Use a command like the following, and run it from within your Drupal install:

docker run -v $(pwd):/app drush/drush sql-dump > my-snapshot.sql

This will create a file called my-snapshot.sql outside the container in the current working directory.

Restoring a Database Snapshot

Use a command like the following, and run it from within your Drupal install:

MYSQL_COMMAND=$(docker run -v $(pwd):/app drush/drush sql-connect)
docker run -v $(pwd):/app run --entrypoint "${MYSQL_COMMAND}" drush/drush < snapshot.sql

This command first invokes sql-connect to get the command needed to invoke the MySQL CLI client and connect it to the Drupal database. Then it executes that command inside the Drush container and pipes-in the database snapshot.