Got a new web project? Request a quote

Copy a remote database or backup with drush

I use drush a lot during development. A common task is to restore the database or copy the database from the stg server to my local machine.
A lot of people use Backup and migrate to do this which works fine but is just too many steps for me. With drush, this can be as long as a single command:

drush sql-sync @site-dev @self --sanitize

This command will copy the db from the dev server to the local machine.
The --sanitize switch will sanitize the email addresses and the passwords of the users. This way, you will be sure that there will no email accidentally sent from the devel machine.
Be carefull, not to switch the order of @site-dev and @self or you will copy the local db to the dev server.
It is usually good to do a:

drush sql-drop

before running the sql-sync command. This will drop the local db and it will likely save you some time troubleshooting.

Another method copying the remote db is to use the sql-dump command:

drush @site-dev sql-dump --gzip --result-file=site-dev.sql

You can then decompress the file and import it:

gunzip site-dev.sql.gz;`drush sql-connect` < site-dev.sql

Did you like this post? Drop me a line in the comments below