TLDR;
- Export Database:
drush sql:dump --result-file="$(date +'%Y%m%dT%H%M%S') [Database] $(drush status --field=db-name).sql"- Export Filebase:
zip -r -0 "$(date +'%Y%m%dT%H%M%S') [Filebase] path$(drush status --field=root|sed 's%/%-%g').zip" .- Import Database:
drush sql:drop -y && drush sql:cli < example.sql
Drush Dump Database (sql:dump)
Export Drupal’s (default) database as SQL fiel using mysql dump or equivalanet command: (*here drush status --field=db-name is used to get the default database’s name
| |
Similarly I use the following command to zip all files in public_html folder (as filebase backup): (*here drush status --field=db-name is used to get the root folder’s path, and -0 is used to let zip command run without compression (fastest)
| |
Drush Import Database (sql:drop, sql:cli / sql:connect / sql:query)
Before importing from file to drupal’s database, drop all the existing tables:
| |
Then import from sql file via one of the following command:
| |
Drush Connect Database / Run Query (sql:cli, sql:connect, sql:query)
You can use drush sql:cli to open a SQL command-line interface using Drupal’s credentials
(*you also can use the drush sql:connect command to get a string to do the same thing)
| |
OR
| |
Similarly you can also use drush sql:query to execute a query against the database without going into the interactive session.
For instance, if you want to: delete all columns from the key_value_expire table for rows, where the collection value starts with "theme:", ends with ":http", and has any characters in between, you can do that via the following command:
| |
Reference
- Drush Official Documentation: