DRUSH TO EXPORT/IMPORT A MYSQL DATABASE

So today, you want to learn how to export a Drupal database to a file quickly.

In the past, you would have logged in through cPanel and navigated to PHPMyAdmin, then manually selected a database export and you would have to choose where to save the file. Not to mention that all of the above steps listed need to be preceded by a Drupal cache dump (I don't like caching inside a database backup). Usually, this would have taken you about 5 minutes to complete, well folks, that is no longer the case.

Behold the power of Drush (once you have logged in and navigated to your drupal root directory).

Exporting the base

Drupal 6 and 7

drush cc
drush sql-dump > ~/my-sql-dump-file-name.sql

Drupal 8 and 9

drush cr
drush sql-dump > ~/my-sql-dump-file-name.sql

Those two commands clear all Drupal caches and then dump the sql database to a file in your home directory. awesome!

Download database (optional)

If the sql dump file is on a remote server, you can use this terminal command to download a copy of the sql file:

scp [email protected]:~/my-sql-dump-file-name.sql ~/Desktop/

Import database

You can use this command to import the sql dump into your Drupal database.

drush sql-drop
drush sql-cli < ~/my-sql-dump-file-name.sql

A non-drush approach

If you don't have the Drush configuration (which you should have configured if you want to be cool xD), you can check this article to backup your Drupal MySQL database from a terminal: Drupal - How to backup MySQL database

Have Any Project in Mind?

If you want to do something in Drupal maybe you can hire me.

Either for consulting, development or maintenance of Drupal websites.