Backup and migrate has some additional commands, but is there something "native" from drush to restore dumps created with drush?

Combining this with site aliases will be a killer feature, but it seems I can't find any doc.

Comments

moshe weitzman’s picture

Status: Active » Fixed

`drush sql-connect` < example.sql will import any SQL file such as one saved by drush sql-dump. Put your own path and filename where example.sql appears. Those backticks at start and end are important.

corbacho’s picture

That was fast. thx moshe.

jonhattan’s picture

This also works:

drush sql-cli < example.sql

Note this does not restore but "dumps into" (don't know if it's a correct expression). I mean, if you want a full restore you previously need to drop and recreate the database, or drop each table.

corbacho’s picture

Thanks jonhattan. It's true what you mention. I created finally an alias so I'm dropping the tables first with:

`drush sql-connect` -e "show tables" -s | xargs -I "@@" `drush sql-connect` -e "DROP TABLE @@"

(Snippet found here)

I tried first similar command with drush sql-cli or drush sqlq but it's much slower (4 secs Vs minutes)
Also could be used some trick to make DROP database_name, instead of going through all tables, but didn't know where to start.

I wonder if there is room for a new command restore,
drush restore dump.sql @site

that will prompt "Are you sure you want to override @site with this dump?"

Basically it's already done with sql-sync, only difference is that you don't need to go through the first step of making the first dump.

Anyway... it's quite easy already to restore it. I was only thinking out loud. Thanks again!

corbacho’s picture

My whole point was to make an easy and fast recover points when testing and creating patches for Drupal 7. It's easy to do it with files (cvs up -dPC), but not so straightforward with the db.

Similar to one of the functionalities of the Demo module, but faster, since it won't require php processing to restore the database.

moshe weitzman’s picture

in that case, use site-install or if that is too slow, write patches for the installer :)

jonhattan’s picture

I traditionally do use this in a local environment, with credentials in ~/.my.cnf

mysqldump db > db.sql
mysql -e 'drop database db; create database db';
mysql db < db.sql

For a drush command it should drop the tables instead of the database, because of permissions.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Mark Nielsen’s picture

jonhattan wrote:

This also works:

drush sql-cli < example.sql

Note this does not restore but "dumps into" (don't know if it's a correct expression). I mean, if you want a full restore you previously need to drop and recreate the database, or drop each table.

My understanding is that it's not necessary to drop the database or tables before restoring from a "dump" file, because the dump file contains instructions to drop the tables first and then recreate and populate them, for example:

DROP TABLE IF EXISTS `access`;
CREATE TABLE `access` (
...etc.

I've been just shoving dump files in on top of existing databases for a couple of months now, using "drush sql-cli < example.sql", and it seems to work OK...

loopduplicate’s picture

If you add some tables to your database by installing a module or something and then you restore a backup using drush sql-cli < example.sql, those new tables will be orphaned. Whether that creates a problem for you depends on your particular setup.

loopduplicate’s picture

with that said, I do this if I haven't added tables:

cd backup_migrate/manual/
latestfile=$(ls -t1 | head -n1)
drush bam-restore db manual "$latestfile"
jmsosso’s picture

Version: » 8.x-6.x-dev
Issue summary: View changes

As this page is the first result when you search in Google for "drush restore dump" I'm going to add a comment for the current drush version.

With drush 9 this can be easily done with:

drush sql:drop
drush sql:query --file=20200330_135446.sql.gz

Hope this help anyone that lands in this page in 2020 :)

azovsky’s picture

Thanks @jmsosso!
drush sql:drop and drush sql:query ... working good for Drush 9.x and Drupal 8.8.x!

arruk’s picture

Again since this is the first thing that comes up in google:
drush sql-drop -y
drush sqlc < sites/default/files/yourbackup.sql

aiphes’s picture

https://www.drush.org/latest/commands/sql_query/
Trying both way sql:query and sqlc and result is a non found file. :/
So, how to target the sql file when it's in the drupal root, same level as site folder ?

Drupal root    : /var/www/drupal-8
Drush version  : 9.7.1

Commands would be:
drush @lias sql:drop && drush @lias sql-query --file=../exports-bruts/file.mysql.gz or
drush @lias sql:drop && drush @lias sqlc < /exports-bruts/file.mysql.gz

jmsosso’s picture

@aiphes keep in mind that for drush the current directory is the Drupal root. So if the dump is in the Drupal root, no matter from which directory you run drush, the command will be drush sql-query --file=file.mysql.gz

If you what to use relative paths, like ../exports-bruts/file.mysql.gz that means that exports-bruts should be one level up from the Drupal root. If you are not sure, the better is to use full paths, like /home/user/project/drupal/html/xxx.sql :)

aiphes’s picture

@jmsosso
I done what I suggest and I get the same error:
dump is stored in : /var/www/drupal-8/exports-bruts/bdd.mysql

and when I run :

drush @lias sql:drop && drush @lias sql-query --file=bdd.mysql.gz

 Do you really want to drop all tables in the database d8bl? (yes/no) [yes]:
 >

 [error]  File(s) not found: bdd.mysql.gz
drush @lias sql:drop && drush @lias sql-query --file=/var/www/drupal-8/exports-bruts/bdd.mysql.gz

 Do you really want to drop all tables in the database d8bl? (yes/no) [yes]:
 >

So I need to use the full path to get this work.. randomly.

[error] File(s) not found: /var/www/drupal-8/exports-bruts/bdd.mysql.gz :/

Very weird thing.