Export and import Drupal database with drush command

Drush is the ultimate tool and companion for Drupal and it does a great job in situations where we want to import or export our Drupal database.

Export/Backup Drupal database with Drush

The best way to export the Drupal database is to first rebuild/clear the cache first and then export DB with sql-dump command. Clearing the cache first will empty the cache tables in the database and significantly decrease the database size.

Export Drupal 8 database

  1. The first step is to rebuild your cache before database export
    drush cr
  2. Now run the sql-dump command with the destination argument to export the Drupal 8 database to the desired location. This will dump the database into a gzip file without cache table content.
    drush sql-dump --gzip --result-file
    
    drush sql-dump --gzip --result-file=/path/to/file.sql

Export database in Drupal 6 or 7

  1. For Drupal 6 and 7 also, you need to clear the cache first to empty the cache from the database.
    drush cc
    
  2. Now run the sql-dump command to export the Drupal 6 and 7 databases. The export will be a gzip file without cache table content.
    drush sql-dump --gzip --result-file
    
    drush sql-dump --gzip --result-file=/path/to/file.sql

Import Drupal database with Drush

The below command can be used to import an SQL dump file into the Drupal database. Make sure to drop the database first and then run the import command which will import the data to Drupal's current database.

drush sql-drop -y
drush sql-cli < ~/path/to/file.sql

 

Learn Drupal development